Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: Limiting CPU usage intentionally

Tags:

c++

cpu-usage

At my company, we often test the performance of our USB and FireWire devices under CPU strain.

There is a test code we run that loads the CPU, and it is often used in really simple informal tests to see what happens to our device's performance.

I took a look at the code for this, and its a simple loop that increments a counter and does a calculation based on the new value, storing this result in another variable.

Running a single instance will use 1/X of the CPU, where X is the number of cores.

So, for instance, if we're on a 8-core PC and we want to see how our device runs under 50% CPU usage, we can open four instances of this at once, and so forth...

I'm wondering:

  1. What decides how much of the CPU gets used up? does it just run everything as fast as it can on a single thread in a single threaded application?

  2. Is there a way to voluntarily limit the maximum CPU usage your program can use? I can think of some "sloppy" ways (add sleep commands or something), but is there a way to limit to say, some specified percent of available CPU or something?

like image 464
8bitcartridge Avatar asked Apr 30 '11 03:04

8bitcartridge


People also ask

Can you limit your CPU usage?

Change the CPU Affinity Another method to curb the CPU usage of a program is by adjusting the CPU Affinity. It is simply restricting the processes to use fewer CPU cores of your system. By changing CPU affinity, you can free up CPU cores which will become available to other processes.

How can CPU usage exceeds 100?

Multiple instances of a service running on one server or in a multi-core environment can produce CPU usage percentages well above 100%. If you upgrade from a dual processor to a quad processor under the same architecture, you should see roughly the same CPU numbers for the same loads and applications.


2 Answers

CPU quotas on Windows 7 and on Linux.

Also on QNX (i.e. Blackberry Tablet OS) and LynuxWorks

In case of broken links, the articles are named:

  • Windows -- "CPU rate limits in Windows Server 2008 R2 and Windows 7"
  • Linux -- "CPU Usage Limiter for Linux"
  • QNX -- "Adaptive Partitioning"
  • LynuxWorks - "Partitioning Operating Systems" and "ARINC 653"
like image 91
Ben Voigt Avatar answered Oct 04 '22 14:10

Ben Voigt


  1. The OS usually decides how to schedule processes and on which CPUs they should run. It basically keeps a ready queue for processes which are ready to run (not marked for termination and not blocked waiting for some I/O, event etc.). Whenever a process used up its timeslice or blocks it basically frees a processing core and the OS selects another process to run. Now if you have a process which is always ready to run and never blocks then this process essentially runs whenever it can thus pushing the utilization of a processing unit to a 100%. Of course this is a bit simplified description (there are things like process priorities for example).
  2. There is usually no generic way to achieve this. The OS you are using might offer some mechanism to do this (some kind of CPU quota). You could try and measure how much time has passed vs. how much cpu time your process used up and then put your process to sleep for certain periods to achieve an approximation of desired CPU utilization.
like image 44
ChrisWue Avatar answered Oct 04 '22 14:10

ChrisWue