Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

It would be very useful for me to run specific programs in a Linux environment with a fixed (lower) CPU clock speed (say, runat 400mhz ./my-program --argument-of="my program" for getting that clock speed).

I need it to generate delays between each assembly instruction of my program, and it wouldn't kill me if the delay isn't exact (say, if it depends on the OS scheduler, as it almost sure will).

The goal is to make algorithms efficiency very explicit when programming not-that-heavy applications at school (having a 3GHz CPU makes it difficult to notice slow algorithms that run for little time and could be really optimized). Also, it could be useful for spotting race conditions in real time, maybe.

Do you know of any such tool? Is there any kind-of-interpreter that I could hack to achieve this? Can gdb help me in a relatively easy way? I think it's the closest thing to controlling a Linux program execution I know.

Running the hole system in a CPU capped VM could be another option, given it provides me that illusion - a lower speed CPU - but or would be best to run single programs.

share|improve this question
    
Incomplete answer but you may look into manipulating cpu.shares via cgroups. Basically configure the cgroup then run the program in the constained cgroup via cgexec. –  Joel Davis Oct 17 '13 at 0:08
    
@JoelDavis I don't think you can slow down a process via cgroups. You can control how big a slice of CPU time it receives, but if it's the only active process, it'll get 100% of the CPU time. –  Gilles Oct 17 '13 at 0:21
    
Related: unix.stackexchange.com/questions/73/… –  slm Oct 17 '13 at 1:39
add comment

5 Answers

I think the application you're looking for is called cpulimit. This isn't a command that's normally included with a system so you'll have to install it either via your distros package manager (Ubuntu/Debian) or from source.

Ubuntu/Debian

$ apt-cache search cpulimit
cpulimit - tool for limiting the CPU usage of a process

So installation would go like this:

$ sudo apt-get install cpulimit

From source

Download it like so:

$ wget https://github.com/opsengine/cpulimit/archive/master.zip \
       -O cpulimit-master.zip

Unzip & build:

$ unzip cpulimit-master.zip ; cd cpulimit-master ; make

Usage

You can install it with a make install, but for now we'll just use it from the directory where we built it:

$ pwd
/home/saml/tst/96387/cpulimit-master
$ src/cpulimit -e firefox -l 30

This will limit the process called firefox to 30% of the available CPU usage.

You can also apply it to running processes after they've been invoked:

$ pgrep firefox
1234

$ src/cpulimit -p 1234 -l 30

You can see the rest of the usage of cpulimit:

$ src/cpulimit 
Error: You must specify a cpu limit percentage
Usage: cpulimit [OPTIONS...] TARGET
   OPTIONS
      -l, --limit=N          percentage of cpu allowed from 0 to 400 (required)
      -v, --verbose          show control statistics
      -z, --lazy             exit if there is no target process, or if it dies
      -i, --ignore-children  don't limit children processes
      -h, --help             display this help and exit
   TARGET must be exactly one of these:
      -p, --pid=N            pid of the process (implies -z)
      -e, --exe=FILE         name of the executable program file or path name
      COMMAND [ARGS]         run this command and limit it (implies -z)

Report bugs to <marlonx80@hotmail.com>.

Also there is a good tutorial on its usage here on cyberciti.biz, titled: Linux Limit CPU Usage Per Process.

NOTE: Remember that it's limiting CPU usage as a percentage so if you have a quad core box (4 cpus) then you'll need to limit it from 0% - 400%.

share|improve this answer
add comment

Another option is to use a slow hardware - some kind of inexpensive ARM-based board like Raspbery Pi or (slightly more powerful) Compulab Trimslice should do. They've got a limited amount of memory, slow CPU yet can run a full Linux system - Fedora, Debian, and a few other distros have an ARM version.

share|improve this answer
add comment

The goal is to make algorithms efficiency very explicit when programming not-that-heavy applications

The normative way to do this is to repeat the process a large number of times. If you have two different algorithms that perform the same (not-that-heavy) task X and you want to benchmark them, you time them repeating task X a thousand or a million times. This may require that you write a simple (high level) program to drive the benchmark; as long as the same driver program runs all the tests, you will get a realistic comparison of how fast one is in relation to the other.

share|improve this answer
    
While it's true that benchmarking is the official way to go - this is academic. I'm editing my question for clarifying this. Thanks for your answer. –  mgarciaisaia Oct 17 '13 at 1:14
add comment

You may be able to set your processor's clock, but globally, not for a specific program. Processors don't work this way. Processors intended for power-limited environments (especially laptops and other mobile devices) tend to support multiple speeds.

You could in principle arrange to change the processor speed (if supported) when your process is scheduled, and reset the processor speed when another process is scheduled. I'm not aware of any software supporting that, it would probably require a fairly hefty modification to the kernel.

You could run your program inside a slow virtual machine, such as Qemu (without KVM, so you get pure software emulation) or Bochs.

Running at a lower CPU speed isn't likely to expose more race conditions.

If your program is multithreaded, you can force it to use a single processor or a specific set of processors with taskset, e.g. taskset 3 myprogram to run only on CPUs #0 and #1. Forcing the program to run on a single CPU is likely to exhibit fewer race conditions.

For your use case, rather than run your program slower, run it several time. For example, if you intend your program to have a certain speed on devices that are about 10 times slower than your computer, test it by running your program 10 times in a row. Running the program multiple times increases the likelihood of running into race conditions.

share|improve this answer
    
Take a look at the man page for taskset. You can also tell it to run on specific cpus using the -c switch. taskset -c 0,2,4-11 myprogram. –  slm Oct 17 '13 at 1:37
add comment

You can use cpufreq-info to look up all CPU clock speeds that is supported by your cpu. And you can use cpufreq-set to select a cpu clock speed.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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