How to measure if a program was run in parallel over multiple cores in Linux?

2.9k views Asked by At

I want to know if my program was run in parallel over multiple cores. I can get the perf tool to report how many cores were used in the computation, but not if they were used at the same time (in parallel).

How can this be done?

2

There are 2 answers

2
optimist On BEST ANSWER

You can try using the command top in another terminal while the program is running. It will show the usage of all the cores on your machine.

0
thkala On

A few possible solutions:

  • Use htop on another terminal as your program is being executed. htop shows the load on each CPU separately, so on an otherwise idle system you'd be able to tell if more than one core is involved in executing your program.

    It is also able to show each thread separately, and the overall CPU usage of a program is aggregated, which means that parallel programs will often show CPU usage percentages over 100%.

  • Execute your program using the time command or shell builtin. For example, under bash on my system:

    $ dd if=/dev/zero bs=1M count=100 2>/dev/null | time -p xz -T0 > dev/null
    real 0.85
    user 2.74
    sys 0.14
    

    It is obvious that the total CPU time (user+sys) is significantly higher than the elapsed wall-clock time (real). That indicates the parallel use of multiple cores. Keep in mind, however, that a program that is either inefficient or I/O-bound could have a low overall CPU usage despite using multiple cores at the same time.

  • Use top and monitor the CPU usage percentage. This method is even less specific than time and has the same weakness regarding parallel programs that do not make full use of the available processing power.