Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Valued Contributor III
1,343 Views

times() always returns 1 in NIOS II

Hi, 

 

I am trying to measure the process execution time using the following code... 

 

# include <stdlib.h># include "sys/alt_stdio.h"# include <sys/alt_alarm.h># include "sys/times.h"# include "alt_types.h"# include "system.h"# include <unistd.h> int main() { char buffer; clock_t T1, T2; //########### T1 = times(NULL); //my statements here T2 = times(NULL); //########### gcvt(((double)T2-(double)T1) / alt_ticks_per_second(), 10, buffer); alt_putstr(buffer); return 0; }  

 

I have an Interval Timer existing and connected in my NIOS II processor and floating point custom instructions, also I have sys_clk_timer set in the BSP editor, but this code outputs NaN. 

If I just output T2 and T1, I always only get 1 respectively. 

 

I tried to figure out the error for hours, but cant just cant think of why it doesnt work... Hope you guys have an idea. 

 

Thx in advance. 

 

Jimmy
0 Kudos
8 Replies
Highlighted
Valued Contributor III
23 Views

Dunno, the altera timer block is horrendously over-complicated and needs lots of clock cycoles to do anything at all (not least because it is a 16bit slave!). 

I used a simple 32bit up-counter connected to an avalon slave for performance cycle counting (one avalon cycle to read), the same slave also has a millisecond up-counter we use for actual timers. 

I was then able to determine/verify the actual cycle counts for most instructions - although signaltap did show one or two things I hadn't noticed!
0 Kudos
Highlighted
Valued Contributor III
23 Views

 

--- Quote Start ---  

 

I have an Interval Timer existing and connected in my NIOS II processor and floating point custom instructions, also I have sys_clk_timer set in the BSP editor, but this code outputs NaN. 

If I just output T2 and T1, I always only get 1 respectively. 

 

--- Quote End ---  

 

 

I think you're getting NaN because your expression evaluates to 0/0 which means that alt_ticks_per_second() is returning '0', as well as times() returning '1'. 

 

So.... it sounds like you've got a problem with either your Qsys system not hooked up right or with your BSP. 

 

Is it something simple like your BSP compile out of date or you're using the wrong copy of it, etc. etc. etc. ? 

Your generated system.h should have macros for ALT_SYS_CLK that points to a timer which has a corresponding "_TICKS_PER_SEC" macro associated with it with a non-zero value in it. 

 

The alt_nticks_per_second() returning zero should be pretty easy to step through and figure out why. The code is in your BSP HAL/inc/sys/alt_alarm.h, see how alt_sysclk_init() is being called, if at all. 

 

As a sanity check, your code runs fine on my BSP + HW.
0 Kudos
Highlighted
Valued Contributor III
23 Views

Hi, 

 

thanks for your answers. In the end it was because the sys_clk_timer set to None in the BSP, now it is working. 

 

But on yet another different but related issue - the execution time of my statement is too short, even with very high amount of repeated executions using a for loop the execution time is less than one system clock tick, so that the measured result is not very useful. Does anyone know how to measure the execution time of a very fast statement? 

 

Thanks.
0 Kudos
Highlighted
Valued Contributor III
23 Views

You want the performance counter timer, which has granularity in units of the clock you feed it. 

 

http://www.altera.com/literature/an/an391.pdf
0 Kudos
Highlighted
Valued Contributor III
23 Views

 

--- Quote Start ---  

Hi, 

 

thanks for your answers. In the end it was because the sys_clk_timer set to None in the BSP, now it is working. 

 

Thanks. 

--- Quote End ---  

 

 

How did you change the sys_clk_timer value to make it work? In my BSP editor it has only the None value. I guess I have to change it in system.h. Which value must I give to it? 

 

Thanks!
0 Kudos
Highlighted
Valued Contributor III
23 Views

 

--- Quote Start ---  

How did you change the sys_clk_timer value to make it work? In my BSP editor it has only the None value. I guess I have to change it in system.h. Which value must I give to it? 

 

Thanks! 

--- Quote End ---  

 

 

Does your Qsys system contain one or more "Interval Timer" peripherals? It sounds like it doesn't. The BSP editor will present you with a choice of all the timers in your system that are correctly connected to your processor. Changing it in system.h is not going to work.
0 Kudos
Highlighted
Valued Contributor III
23 Views

Ok. I added an Interval Timer to my system, I attached it to the sys_clk_timer in the BSP editor, but now my program doesn't seem to work. 

 

printf("LOADING...\n\n"); start = times(NULL); //Lots of work here stop = times(NULL); t_total = ((double)stop-(double)start) / alt_ticks_per_second(); printf("Start = %d\n", start); printf("Stop = %d\n", stop); printf("Total time = %f\n", t_total); 

 

Before adding the Interval timer I got 0s on Start and Stop. Now the NIOS console doesn't show anything, not even the LOADING print at the top. 

Please, I need some help to measure the execution time of my code! Thanks!
0 Kudos
Highlighted
Valued Contributor III
23 Views

Sorry, you'll need to debug that yourself. For example, try single stepping through your program to see where the problem is. If nothing else, you ought to be able to examine the "t_total" variable and see it become non-zero value.

0 Kudos