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
A cheap trick. Let the idle task busy-loop constantly toggling a pin. Check what frequency it manages to toggle the pin. Use a counter to check the number of toggles you get during normal operation. If you get 10% toggles then you spend about 10% of the time in the idle task. With an intelligent scope/logic analyzer, you could also pick up the longest interval without any toggling - that could potentially indicate starvation of your lowest-priority non-idle task.
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...
I meant: "ready to run tasks", of course.
Yes, if no work tasks are running (they don't exist, or they are waiting for some event), the idle task (potentially just a normal low-priority user task) will run and will toggle an output pin at full speed.
When tasks or interrupts are serviced, the toggling task will not get any CPU time, so the toggling will stop.
No toggling means that the processor don't have any spare time for such a low-priority task.
The number of toggles/second correlates to the number of loops/second this idle task performs, and hence informs about spare processor time. No pin toggles - no spare time.
Ok. I get the point. Thanks for the discussion, Per and Tamir.
wow. you guys are so smart.
I do something similar in that I have a counter in the idle task. I Let this run for a set period when I know no tasks are running. Then when tasks are running the difference in this counter can be used to calculate/estimate the cpu used.
It is surprising how useful this can be, and its quite simple with no external hardware.
Stuart.
I learn much from the discussion. But I still don't really understand the meaning. What's the meaning of the "CPU"? Is it the computer CPU? I haven't used the Keil RTX? I had used SComAssistant.exe V2.2 to transmit the data from the IC to computer. I used the software of Delphi7 to program a UART transmission software which could control the LEDs on a board which had the IC of RS232,AT89S52 and so on. I think that if the computer is good enough. We don't need to care about the CPU utilization. My comprehension is limited. The above is my mind.
In this case, the CPU is the microprocessor in the embedded system, i.e. the microcontroller.
For more advanced and/or critical systems, it is vital that the processor is always fast enough to handle all critical events in real time, i.e. with a minimum of delay and as fast as new events arrives. As soon as events are lost or gets enqueued, the device will no longer uphold real-time performance.
But a system normally also have low-priority tasks. Things that is allowed to be delayed while critical events gets serviced. But even these low-priority tasks must finally get serviced even if it may be ok if they are delayed for several minutes or more. Being able to measure idle time for a processor can be used to estimate the probabilities that low-priority tasks gets starved.
Hello stuart,
Do you mean using timer as a counter or just a software variable?
If it is a software variable, Is there a way to correlate between the counter to the actual time taken?
My intention is to arrive at a CPU utlization %.
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:
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