1

I'm trying to understand how to compute the CPU utilisation for audio and video use cases.

In real time audio applications, this is what I typically do: if an application takes 4ms to process 28ms of audio data, I say that the CPU utilisation is 14.28% (4/28).

How should this be done for applications like resize/crop? let's say I'm resizing an image from 162*122 to 128*128 size image at 1FPS, and it takes 11ms.. What would be the CPU utilisation?

tandem
  • 2,040
  • 4
  • 25
  • 52
  • you compute how much time is required in total. Previously your application took 4ms to process 28ms of data (4 / 28 = 14.28%), now with preprocessing how much time do this 28ms of data take to process? let's say that preprocessing adds 10ms to the time. So you have (4ms + 10ms) / 28 = 50% CPU – Sembei Norimaki Feb 06 '23 at 14:50
  • For audio, it's clear for me. How would I do it for video/image preprocessing? – tandem Feb 06 '23 at 14:54
  • the same way. how much time does it take to process divided by how much video time is processed – Sembei Norimaki Feb 06 '23 at 15:01
  • so, are you saying that if i'm working with 1FPS, my cpu utilisation in the above case is 11%? – tandem Feb 06 '23 at 15:02
  • according to your cpu utilization formula (which you defined as time taken to process / real media time) yes. but I don't know were did you take that formula for CPU utilization. – Sembei Norimaki Feb 06 '23 at 15:03
  • if we do forget my formula, how would you compute CPU utilisation? – tandem Feb 06 '23 at 15:04

1 Answers1

0

CPU utilization is quite complicated, and strongly depends on stuff like:

  • The CPU itself
  • The algorithms utilized for the task
  • Other tasks running alongside the CPU

CPU utilization is also strongly related to the process scheduling of your PC, hence the operating system used, so most operating systems will expose some kind of API for CPU utilization diagnostics, but such API is highly platform-dependent.

But how does CPU utilization calculations work anyway?

The most simple way in which CPU utilization is calculated is taking a (for example) 1 second period, in which you observe how long the CPU has been idling (not executing any processes), and divide that by the time interval you selected. For example, if the CPU did useful calculations for 10 milliseconds, and you were observing for 500ms, this would mean that the CPU utilization is 2%.

Answering your question / TL; DR

You can apply this principle in your program. For the case you provided (processing video), this could be done in more or less the same way: you calculate how long it takes to calculate one frame, and divide that by the length of a frame (1 / FPS). Of course, this could be done for a longer period of time, to get a more accurate reading, in the following way: you track how much time it takes to process, for example, 2 seconds of video, and divide that by 2. Then, you'll have your CPU utilization.

NOTE: if you aren't able to process the frame in time, for example, your video is 10FPS (0.1ms), and processing one frame takes 0.5ms, then your CPU utilization will be seemingly 500%, but obviously you can't utilize more than 100% of your CPU, so you should just cap the CPU utilization at 100%.

TopchetoEU
  • 697
  • 5
  • 9
  • I think I understand. So, the answer to my question based on your method is: if I'm resizing one image (so one FPS) and it takes 11ms, then my CPU utilisation will be 9.09% (1/11), is that correct? – tandem Feb 06 '23 at 15:25
  • @tandem pretty much yes. if this is the answer you were looking for, please upvote it and mark it as the answer – TopchetoEU Feb 06 '23 at 15:26
  • that does't seem right. it should be 11/1000 = 1.1%.. otherwise we are having ms in the numerator, and seconds in the denominator. – tandem Feb 06 '23 at 15:32
  • @tandem not quite, excuse my mistake. you should divide 11ms (0.011 seconds) by 1 second, which equals, as you said, 1.1% – TopchetoEU Feb 06 '23 at 15:36