6

I am working on a ARM cortex M4 evaluation board, its a bare metal application without any operating system running on it.

Now I want to measure CPU usage of a given process/algorithm , what would be the best way to do so?

Should i implement an operating system to measure the CPU usage that have the functionality for such demand?

pradipta
  • 1,718
  • 2
  • 13
  • 24
Fluffy
  • 337
  • 1
  • 5
  • 16
  • Do you need to measure the the CPU usage on the fly? – junix Jul 14 '13 at 16:40
  • Why do you want CPU usage? On such a controller you usually want real-time behavior, i.e. certain tasks must finish fast enough within a given time frame. – starblue Jul 15 '13 at 06:40

3 Answers3

7

The question almost answers itself. What is your bare metal application doing when it is not in that process/algorithm? Measure one or the other or both. If you have a bare metal application that is not completely consuming the cpu in this algorithm, then you already have an operating system to the extent that you are managing this application/function's time. You can use a number of methods from a simple counter in a loop relative to a timer to see how many counts per loop when the algorithm is getting time slices vs not. You can simply time the algorithm itself, etc.

I assume when you say CPU you mean the whole system as your performance is heavily dependent both on your code and what it is talking to. If running from flash on a cortex-m4 depending on the clock rate you may be burning processor cycles just waiting for instructions or data (and can very easily get the wrong notion of processor performance for an algorithm when it isnt the algorithm burning clocks). The caches mask/manipulate that performance and can easily greatly affect the performance if you are not careful and aware of what they are doing. Being a C++ question your compiler plays a large role in performance as well as your code of course, can very easily make the code run several times faster or slower with minimal changes to the command line or code.

If the algorithm is part of an isr then the processor goes to sleep otherwise, you can use the gpio pin and scope techinique to get a feel for the running vs sleeping ratio.

old_timer
  • 69,149
  • 8
  • 89
  • 168
4

Implementing an OS to measure idle time of a CPU seems a bit overengineered for me. From my knowledge the Cortex-M4 includes a Debug unit (DWT) that allows you to snapshot a cycle counter. But the easiest thing would be to hook a pin to an oscilloscope and toggle it on enter and on exit of your algorithm.

junix
  • 3,161
  • 13
  • 27
  • But what would taking snapshot of register help me ? as far as i understand the only thing that i will get is the number of cycles since the beginning of my process . – Fluffy Jul 14 '13 at 16:50
  • And i am already use this cycle counter to measuring times of my process , how can i use it to measure cpu usage ? – Fluffy Jul 14 '13 at 16:51
  • You get the number of cycles the CPU did since it's start. Taking a snapshot on entry and on exit allows you to calculate the delta of cycles passed and hence get the number of cycles your algorithm used. Do you have a thing like an "idle" task? – junix Jul 14 '13 at 17:00
  • and by taking this cycles multiplying it by my cycle time in nanoseconds,i will get the runtime,but not the cpu usage, am i correct? – Fluffy Jul 14 '13 at 17:03
  • Yes, and that allows you to calculate the runtime of your algorithm and compare it with your measurement period or the ratio between the cycles passed until you reenter your algorithm and the number of cycles your algorithm used. – junix Jul 14 '13 at 17:07
  • Actually, run time and CPU usage almost the same on bare hardware. If you not theoretical engineer (I mean for example your program read from memory, and there are no data in cache, and other instructions depend on this memory reading, so CPU core hang and wait IP block that works with memory to transfer data to cache, yes it is possible to say that we not used CPU core here, but who care about this). So you need just deal with interrupts, and subscract their time from global counter, and your runtime will be almost the same as time spend by CPU. – fghj Jul 14 '13 at 17:25
  • @user1034749 That really depends what else you do with the CPU. For example if you go to power down mode, only cycle count give you a hint about CPU usage. – junix Jul 14 '13 at 17:30
  • Yes, I didn't think about sleep mode. But in bare metal if he wnat go to sleep, he must do it explicity, and I think Topic Starter talk about CPU usage of funciton/process for optmization purpuses, it is rather strange sleeping at the middle of CPU intensive calculation. – fghj Jul 14 '13 at 17:40
1

First, of all implementing an operating system will not be practical or even possible for the purpose of only measuring the performance.So one possible approach is to keep a count variable which will record the number of tick occurred till that duration. And increment that variable in a interrupt of the Timer.

Manish
  • 513
  • 1
  • 8
  • 23