C ++: intentionally restricting CPU usage

At my company, we often check the performance of our USB and FireWire devices under processor load.

There is a test code that we run that loads the CPU, and is often used in really simple unofficial tests to find out what happens with the performance of our device.

I looked at the code for this and its simple loop, which increments the counter and calculates based on the new value, storing this result in another variable.

Starting one instance will use 1 / X CPU, where X is the number of cores.

So, for example, if we are on an 8-core PC and want to see how our device works with 50% CPU usage, we can immediately open four instances of this and so on ...

I am wondering:

  • What decides how much of the processor is depleted? does it just start everything as fast as it can in a single thread in a single-threaded application?

  • Is there a way to voluntarily limit the maximum CPU usage that your program can use? I can think of some โ€œsloppyโ€ ways (add sleep commands or something else), but is there a way to limit, for example, a certain percentage of available processor or something like that?

+6
source share
4 answers

CPU quotas on Windows 7 and Linux .

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

In the case of broken links, articles are called:

  • Windows - "CPU Speed โ€‹โ€‹Limitations in Windows Server 2008 R2 and Windows 7"
  • Linux - "Limiting CPU Usage for Linux"
  • QNX - Adaptive Sharing
  • LynuxWorks - "Separation of operating systems" and "ARINC 653"
+6
source
  • Typically, the OS decides how to plan processes and on which processors they should run. It basically stores a ready-made queue for processes that are ready to start (not marked for completion and not blocked waiting for some I / O operations, events, etc.). Whenever a process uses its time or blocks it, it basically frees up the processing core, and the OS chooses another process to start. Now, if you have a process that is always ready to start and never blocks, this process basically starts whenever it can thus increase processor utilization by up to 100%. Of course, this is a slightly simplified description (for example, things like process priorities).
  • There is usually no general way to do this. The OS you use may offer some mechanism for this (some kind of processor quota). You could try to measure how much time has passed versus how much processor time you spent on the process, and then lay the process in certain periods of time in order to achieve an approximation of the desired processor usage.
+1
source

You really answered your questions!

A key feature of code that burns a lot of CPUs is that it never does anything that blocks (for example, waiting for network or file I / O) and never voluntarily gives its time fragment (for example, sleep (), etc. .) ,.

Another trick is that the code should do something that the compiler cannot optimize. Thus, most likely, your write code on the CPU outputs something based on the calculation of the cycle at the end, or simply compiles without optimization, so the optimizer is not tempted to delete the unnecessary cycle. Since you are trying to load the processor, there is no point optimizing anyway.

As you suggested, the single-threaded code corresponding to this description will saturate the CPU core, if the OS has more such processes than it has kernels to start them, then it will cyclically plan them, and the use of each of them will form part 100%.

0
source

The problem is not how much time the processor spends in standby mode, but how much time it takes to run your code. Who cares if it is idle or does low priority work, if latency is low?

Your problem is fundamentally the result of using a synthetic test, presumably in an attempt to get reproducible results. But synthetic tests tend to produce meaningless results, so reproducibility is controversial.

Look at your error database, find current customer complaints, and use actual software and test equipment to reproduce a situation that actually made someone unhappy. Designing a performance test in parallel with stringent and significant performance requirements.

0
source

Source: https://habr.com/ru/post/886993/


All Articles