clock_gettime function doesn't scale to real time.. and changing CNTFRQ_EL0 doesn't make any change..(arm64)

Siddh Raman Pant code at siddh.me
Fri Nov 4 15:51:22 EDT 2022


On Fri, 04 Nov 2022 14:34:15 +0530, Chan Kim wrote:
> Hello linux experts and newbies,
> 
> I have ported linux on our arm64 fpga board. Both 5.10.0 and 5.15.xx works
> ok with minimal config.
> 
> I have run a simple application and timed the processing time using
> clock_gettime function.
> 
> It felt like it took almost 2.3 seconds but the program say it took only
> 0.36 seconds.

Try a simple command line loop to see if the problem is with system or your
program: while $(sleep 1); do echo "Hi"; done;

Also, while of no concern here, note that kernel also has a real-time config
(CONFIG_PREEMPT_RT).

> Here is how I did it in the application.
> 
> Int main() {
>       struct timespec start, stop;
> 	float exec_time_sec, exec_time_nsec;
> 
>       //check start time
> 	if (clock_gettime(CLOCK_REALTIME, &start) == -1 ) {
> 	     perror ("clock_gettime");
> 	     exit(EXIT_FAILURE);
> 	}
> 
>       Do something... (calculate fibonacci value for 1 ~ 30)
> 
>       //check end time
> 	if (clock_gettime(CLOCK_REALTIME, &stop) == -1 ) {
> 		perror ("clock_gettime");
> 		exit(EXIT_FAILURE);
> 	}
> 
> 	//Normalize to mili second
> 	exec_time_sec = (float)(stop.tv_sec - start.tv_sec);
> 	exec_time_nsec = (float)((double)(stop.tv_nsec - start.tv_nsec)/(double)BILLION);
> 	printf("Execution time : %f sec\n", exec_time_sec + exec_time_nsec);
> 
>       return 0;
> 
> }

Adding to what Linus said in the other reply, CLOCK_REALTIME is not guaranteed
to be monotonic. For calculation of intervals, you should instead use
CLOCK_MONOTONIC or CLOCK_MONOTONIC_RAW.

Also, try just having Fibonacci calculation in main, and then use the `time`
command to achieve what you want.

> I used u-boot program for loading linux kernel and the u-boot program sets
> the CNTFRQ_EL0 register with 5000000.
> 
> (which is 5MHz, I heard the system clock runs at 5MHz in the board).
> 
> The description of the register in armv8 arch manual says :
> > This register is provided so that software can discover the frequency of the
> > system counter. It must be programmed with this value as part of system
> > initialization. The value of the register is not interpreted by hardware.
> 
> I tried setting the CNTFRQ_EL0 with 20Mhz, expecting the execution to be
> displayed 4 times shorter but it is the same!

Because as the description says, this register is not interpreted by the
hardware. That means this won't affect hardware in any way, and thus won't
increase frequency. That's why your execution time remains the same.

The value stored here is for use by software to know the clock frequency of
the machine, so it can do its time related calculations appropriately.

What you did is equivalent of trying to hoodwink the system!

> I couldn't find how linux uses clock_gettime.

If you mean how clock_gettime function is defined, see it here:
https://elixir.bootlin.com/linux/latest/source/kernel/time/posix-clock.c#L259

Otherwise: man clock_gettime

> How can I solve this problem?
> Any advice will be deeply appreciated.

Try making the changes and let us know. This is new for me too!

> Thank you!
> Chan Kim

Please use plain text email instead of HTML, that's the kernel mailing list
etiquette. It is also superior once you get the hang of it! :)

Thanks,
Siddh



More information about the Kernelnewbies mailing list