Hello,
I wish to measure the CPU utlization for a cortex processor running an RTOS (keil RTX). Without an RTOS, i can measure the idle time to come to some conclusion. But with RTOS, can someone guide me how to measure CPU utlization?
Thanks & Best Regards Vivek
vivek,
Sorry I meant software variable acting just as counter.
I use a software integer variable that I just increment in the idle loop for a known period, this period can be set by a timer interrupt. Say once a second - although the period doesn't matter much, as long as its relatively constant and the software variable counter doesn't overflow. Each period the software variable counter is reset to 0;
If you measure the number that the software variable counter reaches when no tasks are running. This is the equivalent to 100% unused processor time during this period - lets call it max_counts
Now once tasks are running the processor will spend less time in the idle loop and so the software variable counter will reach a lower number for the same timer period. Lets call this current_counts.
So unused processor % during this period is given by
unused_cpu_percentage=(current_counts*100)/max_counts
and used processor % during this period is given by
used_cpu_percentage=100-unused_cpu_percentage
Hope that helps,
Stuart
This is the internal way of doing the same thing I suggested.
For the external way - toggling a port pin - it is possible to connect the port pin to a counter input on another processor - such as a eval board - and have it present the CPU load on a display. A very quick hack with a spare Keil eval board and a blinky/hello world app is enough to get a CPU load meter.
Thanks for your replies, Stuart and Per.
I measured the idle time in a while(1) loop for a period of 10 ms.
Then i included RTOS and TCP IP stack from keil. I included the variable counter in the idle task's infinite loop. I observe that the variable hasnt been incremented at all.
What could be the reasons? Does my TCP IP stack utilize the entire CPU time waiting for events? In this case how do i measure CPU load?
Thanks in advance, Vivek
See my task above:
the principle is correct, but there is problem with what Per suggested: RTX's idle task is only invoked if there are no really to run tasks, yielding a constant 100% processor utilization...
what it means is that at no time RTX cannot chose a task to run. there is always one available, thus the idle task is not executed thus your code does not work, for the reasons I have specified over a week ago =:0
Yes Tamir. I was re-reading your comment twice. How do i proceed with this? Any idea?
If you have included the example way of implementing the RTX and TCP stack then it is 100% ready all the time. So Tamir is right, it would never get to the idle task. I have a os_dly_wait in the main thread of my TCP task so that it gives up some time.
this measurement is actually meaningless in RTX. in Linux systems, the scheduler has window of a predetermined size in which tasks are executed; if there is time left in it, the idle task is run. but RTX does not work like that. what you can do it use the __weak function void rt_post_taskswitch(U32 new_task) to log the timestamp at which a context switch has occured (use a free running hardware timer for that). it is called when a context switch happens. maybe you can compute how much of its time slice each task has consumed every period of something similar.
Thanks Stuart.
I was really blind. I found os_tsk_pass() in the routine. I changed it to os_dly_wait(). Now iam getting some meaningful numbers. Thanks again
Hello Tamir,
Why do you think, this approach wont work in RTX?
Even if RTX is preemptive and doesn't have a fixed window size to execute tasks, I believe, then also this approach could give a measure of CPU idle time. Isnt it?
If i use context switching to save a timer value, then also i need to check for idle task's id etc...and then its just a matter of using a time difference right away instead of using variables.
Am I missing something?
Thanks & Best Regards, Vivek
Vivek,
the fact that the idle task is not executed does not mean the processor is 100% loaded. proof? you can probably add a task that needs to wait for a predetermined period before waking up (even quite often) and it will probably never miss its deadline.
"the fact that the idle task is not executed does not mean the processor is 100% loaded. proof? you can probably add a task that needs to wait for a predetermined period before waking up (even quite often) and it will probably never miss its deadline."
Is that proof?
You can always add a task and have it run without it missing it's deadline so long as the priorities are suitable. (i.e., higher priority than others that are ready to run.)
Those lower priority tasks will still be consuming CPU cycles if they're ready to run.
ok, it's not "proof" - I just wanted to demonstrate that one cannot conclude anything based on idle task.