Brennan,

Thank you for telling me the details about CPU time measurement. I did not know that CPU would not be used when during sleep on Linux.

I googled it later and now my understanding is that when doing sleep CPU is not busy but idle state and that's why CPU time measurement would be close to 0. As for NuttX, they don't use CPU time actually. they use elapsed time since boot. That's why I got a value like 1.010000 sec NOT 0 sec.

I changed "System timer tick period (microseconds)" from 10000 to 1000 then I got 1.001000 sec in result which is more exact than before.
I'll try Tickless OS configuration if I need later.

Thank you very much.

Yuta Ide

On 2021/04/30 2:41, Brennan Ashton wrote:
On Thu, Apr 29, 2021, 10:07 AM yuta <yutr...@gmail.com> wrote:

Brennan,
Thank you for your advice.

I checked links you shared and
https://www.gnu.org/software/libc/manual/html_node/CPU-Time.html
I got elapsed time. Thank you.

However I'm wondering why the elapsed time I got was not exactly the same
I expected to get.
I tried below.

// ***** start
clock_t start, end;
double cpu_time_used;

start = clock();
usleep(1000000); // 1sec
end = clock();

cpu_time_used = ((double)(end - start)) / CLOCKS_PER_SEC;

printf("CPU time used: %f sec\n", cpu_time_used);
// ***** end

What I got is ...
"CPU time used: 1.010000 sec" (I expected to get 1.000000 sec)

Do you have any idea?

Two things are going on here.
1. So if you did this on Linux you should get almost 0 because you used the
CPU time measurement and so the thread is suspended and not utilizing the
CPU. This is actually what I was talking about being not compliant about
NuttX, it is giving you the delta in time since boot.
2. You are paying for a context switch and if you are not using the
tickless OS configuration you also are at tick resolution which is normally
10ms which is what we see here.

If you want the wall clock duration you should use elapsed time, even
though on NuttX it may be the same. We likely will fix this in the future
to be more standards compliant.

https://www.gnu.org/software/libc/manual/html_node/Calculating-Elapsed-Time.html

Hope that helps.

--Brennan

Reply via email to