105

Is there any way to run a C++ program slower by changing any OS parameters in Linux? In this way I would like to simulate what will happen if that particular program happens to run on a real slower machine.

In other words, a faster machine should behave as a slower machine to that particular program.

12
  • 5
    You could use 'nice' to give it a really low priority - not a definitive thing, but may help!
    – John3136
    Jan 17, 2013 at 2:29
  • 7
    run it inside a vm with another program that spins the processor.
    – thang
    Jan 17, 2013 at 2:30
  • 12
    nice is a command that lowers the scheduling priority. Vary ancient, relatively crude. You could look at scheduling priorities, but if the machine is not busy doing other things, you program will still run fast. So, it probably won't help sufficiently. Jan 17, 2013 at 2:31
  • 9
    You have to keep in mind that computers have a lot of running processes any time, as such making just your app run slower won't be particularly helpful. If you want to see how your app performs on crappy hardware then you should get said crappy hardware or temporarily change BIOS settings of your hardware to make it crappy. Some BIOSes will let you disable extra cores and clock down the CPU. Jan 17, 2013 at 2:34
  • 24
    Unpress the "Turbo" button!
    – SoftDev
    Jan 22, 2013 at 21:11

8 Answers 8

147
  • Lower the priority using nice (and/or renice). You can also do it programmatically using nice() system call. This will not slow down the execution speed per se, but will make Linux scheduler allocate less (and possibly shorter) execution time frames, preempt more often, etc. See Process Scheduling (Chapter 10) of Understanding the Linux Kernel for more details on scheduling.
  • You may want to increase the timer interrupt frequency to put more load on the kernel, which will in turn slow everything down. This requires a kernel rebuild.
  • You can use CPU Frequency Scaling mechanism (requires kernel module) and control (slow down, speed up) the CPU using the cpufreq-set command.
  • Another possibility is to call sched_yield(), which will yield quantum to other processes, in performance critical parts of your program (requires code change).
  • You can hook common functions like malloc(), free(), clock_gettime() etc. using LD_PRELOAD, and do some silly stuff like burn a few million CPU cycles with rep; hop;, insert memory barriers etc. This will slow down the program for sure. (See this answer for an example of how to do some of this stuff).
  • As @Bill mentioned, you can always run Linux in a virtualization software which allows you to limit the amount of allocated CPU resources, memory, etc.
  • If you really want your program to be slow, run it under Valgrind (may also help you find some problems in your application like memory leaks, bad memory references, etc).
  • Some slowness can be achieved by recompiling your binary with disabled optimizations (i.e. -O0 and enable assertions (i.e. -DDEBUG).
  • You can always buy an old PC or a cheap netbook (like One Laptop Per Child, and don't forget to donate it to a child once you are done testing) with a slow CPU and run your program.
10
  • 15
    +1: varied set of suggestions, including basic requirements for each
    – lxop
    Jan 17, 2013 at 3:14
  • 4
    Enabling debugging symbols (-ggdb3) does not slow down the execution of the binary. It merely makes it larger.
    – caf
    Jan 17, 2013 at 3:33
  • 11
    +1 esp., for "... buy an old PC or a cheap netbook ..., and don't forget to donate it to a child once you are done testing"
    – Kris
    Jan 17, 2013 at 9:29
  • 3
    Could you edit your answer to show how to simulate different types of 'slow' ? There is a difference between slow I/O, slow CPU, slow memory, memory thrashing, etc
    – parasietje
    Jan 17, 2013 at 10:53
  • 3
    +1 for Vlad. Probably cpufreq is easy to do, if you have a CPU and linux kernel with support. This should have instruction level granularity. This is probably the best generic answer without purchasing new hardware; it doesn't simulate a slower network, disk, video, etc which also can cause races. Jan 17, 2013 at 15:25
37

QEMU is a CPU emulator for Linux. Debian has packages for it (I imagine most distros will). You can run a program in an emulator and most of them should support slowing things down. For instance, Miroslav Novak has patches to slow down QEMU.

Alternatively, you could cross compile to another CPU-linux (arm-none-gnueabi-linux, etc) and then have QEMU translate that code to run.

The nice suggestion is simple and may work if you combine it with another process which will consume cpu.

nice -19 test &
while [ 1 ] ; do sha1sum /boot/vmlinuz*; done;

You did not say if you need graphics, file and/or network I/O? Do you know something about the class of error you are looking for? Is it a race condition, or does the code just perform poorly at a customer site?

Edit: You can also use signals like STOP and CONT to start and stop your program. A debugger can also do this. The issue is that the code runs a full speed and then gets stopped. Most solutions with the Linux scheduler will have this issue. There was some sort of thread analyzer from Intel afair. I see Vtune Release Notes. This is Vtune, but I was pretty sure there is another tool to analyze thread races. See: Intel Thread Checker, which can check for some thread race conditions. But we don't know if the app is multi-threaded?

1
  • 1
    and bochs is older and slower cpu emulator (only x86).
    – osgx
    Apr 22, 2013 at 12:10
23

Use cpulimit:

Cpulimit is a tool which limits the CPU usage of a process (expressed in percentage, not in CPU time). It is useful to control batch jobs, when you don't want them to eat too many CPU cycles. The goal is prevent a process from running for more than a specified time ratio. It does not change the nice value or other scheduling priority settings, but the real CPU usage. Also, it is able to adapt itself to the overall system load, dynamically and quickly.

The control of the used cpu amount is done sending SIGSTOP and SIGCONT POSIX signals to processes.

All the children processes and threads of the specified process will share the same percent of CPU.

It's in the Ubuntu repos. Just

apt-get install cpulimit

Here're some examples on how to use it on an already-running program:

Limit the process 'bigloop' by executable name to 40% CPU:

cpulimit --exe bigloop --limit 40
cpulimit --exe /usr/local/bin/bigloop --limit 40 

Limit a process by PID to 55% CPU:

cpulimit --pid 2960 --limit 55
3
  • I haven't tested cpulimit yet, but it seems like the best answer across similar questions on SE sites. Is there any notable difference between a programm running with limitations imposed by cpulimit and a program running on slower hardware? My goal is to test an app to make sure the app is responsive enough on slower machines (and to tune the graphics down for the slower machines).
    – trusktr
    Apr 14, 2020 at 22:53
  • @trusktr That depends heavily on what that program is actually doing. Offhand with older hardware I can think of disk speeds and available memory (RAM) also affecting performance, and from the part about graphics, GPU as well. There may be more. If CPU is actually the bottleneck, cpulimit is probably still worth trying though. (This answer is 7 years old, and back then such performance hits weren't anywhere near the top of my mind)
    – Izkata
    Apr 15, 2020 at 16:08
  • Interesting. You're right, I didn't consider HDD or GPU. I suppose testing with actual slower hardware is the best way to do it, but at the moment I only have a powerful workstation although I'd like to publish even for lower-end phones (JS+WebGL app).
    – trusktr
    Apr 15, 2020 at 19:29
13
  1. Get an old computer
  2. VPS hosting packages tend to run slowly, have lots of interruptions, and wildly varying latencies. The cheaper you go the worse the hardware will be. Unlike truly old hardware, there is a good chance they will contain instruction sets (SSE4) that are not usually found on old hardware. Neverthless, if you want a system that walks slowly and shutters often, a cheap VPS host will be the quickest start.
3

If you just want to simulate your program to analyze its behavior on really slow machine, you can try making your whole program to run as a thread of some other main program.

In this manner you can prioritize same code with different priorities in few threads at once and collect data of your analysis. I have used this in game development for frame-processing analysis.

2

Use sleep or wait inside of your code. Its not the brightest way to do but acceptable in all kind of computer with different speeds.

2

The simplest possible way to do it would be to wrap your main runable code in a while loop with a sleep at the end of it.

For example:

void main()
{
    while 1
    {
        // Logic
        // ...
        usleep(microseconds_to_sleep)
    }
}

As people will mention, this isn't the most accurate way, since your logic code will still run at normal speed but with delays between runs. Also, it assumes that your logic code is something that runs in a loop.

But it is both simple and configurable.

1

You can increase load on CPUs via launching tools like stress or stress-ng

Not the answer you're looking for? Browse other questions tagged or ask your own question.