How to measure individual CPU usage for a process?

Is there a way to measure the specific CPU usage of a processor across cores?

I know top is good for measuring all CPU usage of a processor by cores and taskset can provide information about which processor core is allowed to start a process.

But how can I measure the specific process "CPU usage by processor cores?"

+80
performance linux multicore
Jul 27 '10 at 11:00
source share
8 answers

You can still do it on top

Type 1 - shows each cpu

Limit the processes indicated by the fact that this particular process runs under a specific user account and use "u" to restrict this user

+88
Oct 11 '10 at 21:45
source share

You can use:

mpstat -P ALL 1 

Shows how many cells are occupied and it is updated automatically every second. The result will be something like this (on a quad-core processor):

 10:54:41 PM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %idle 10:54:42 PM all 8.20 0.12 0.75 0.00 0.00 0.00 0.00 0.00 90.93 10:54:42 PM 0 24.00 0.00 2.00 0.00 0.00 0.00 0.00 0.00 74.00 10:54:42 PM 1 22.00 0.00 2.00 0.00 0.00 0.00 0.00 0.00 76.00 10:54:42 PM 2 2.02 1.01 0.00 0.00 0.00 0.00 0.00 0.00 96.97 10:54:42 PM 3 2.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 98.00 10:54:42 PM 4 14.15 0.00 1.89 0.00 0.00 0.00 0.00 0.00 83.96 10:54:42 PM 5 1.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 99.00 10:54:42 PM 6 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 100.00 10:54:42 PM 7 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 100.00 

This command does not answer the original question, although it does not show the use of the CPU core for a specific process.

+62
Jul 16 2018-11-11T00:
source share

you can use ps .
for example with a python process with two busy threads on a dual core processor:

 $ ps -p 29492 -L -o pid,tid,psr,pcpu PID TID PSR %CPU 29492 29492 1 0.0 29492 29493 1 48.7 29492 29494 1 51.9 

(PSR is the identifier of the CPU to which the thread is currently assigned)

you see that threads are running on the same processor core (due to GIL)

works with the same python script in jython, we see that the script uses both cores (and there are many other services or any threads that are almost idle):

 $ ps -p 28671 -L -o pid,tid,psr,pcpu PID TID PSR %CPU 28671 28671 1 0.0 28671 28672 0 4.4 28671 28673 0 0.6 28671 28674 0 0.5 28671 28675 0 2.3 28671 28676 0 0.0 28671 28677 1 0.0 28671 28678 1 0.0 28671 28679 0 4.6 28671 28680 0 4.4 28671 28681 1 0.0 28671 28682 1 0.0 28671 28721 1 0.0 28671 28729 0 88.6 28671 28730 1 88.5 

You can process the output and calculate a common processor for each CPU core.

Unfortunately, this approach does not seem to be 100% reliable, sometimes I see that in the first case, two worker threads are reported to be shared on each CPU core, or in the latter case, two threads are reported to be on the same core.

+30
Oct 11 2018-10-10
source share

The ps solution was almost what I needed, and with some bash that was done, it does exactly what the original question asked: see the use of the kernel in specific kernels

This shows the use of the kernel for multi - threaded processes.

Use like: cpustat `pgrep processname`` pgrep otherprocessname` ...

 #!/bin/bash pids=() while [ $# != 0 ]; do pids=("${pids[@]}" "$1") shift done if [ -z "${pids[0]}" ]; then echo "Usage: $0 <pid1> [pid2] ..." exit 1 fi for pid in "${pids[@]}"; do if [ ! -e /proc/$pid ]; then echo "Error: pid $pid doesn't exist" exit 1 fi done while [ true ]; do echo -e "\033[H\033[J" for pid in "${pids[@]}"; do ps -p $pid -L -o pid,tid,psr,pcpu,comm= done sleep 1 done 

Note. These statistics are based on the lifetime of the process, not the last X seconds, so you need to restart the process to reset the counter.

+3
Apr 26 '13 at 1:45
source share
 dstat -C 0,1,2,3 

Also gives you the CPU usage of the first 4 cores. Of course, if you have 32 cores, this command gets a little longer, but is useful if you are only interested in a few cores.

For example, if you are only interested in core 3 and 7, you can do

 dstat -C 3,7 
+1
Aug 25 '16 at 5:00
source share

htop gives a good overview of using a separate kernel

+1
05 Oct '17 at 9:37 on
source share

I had exactly this problem, and I found a similar answer here .

The method is to set top way you want, and then press W (capital W). This saves the current top layout in the configuration file in $ HOME / .toprc

Although this may not work if you want to run multiple top with different configurations.

So, thanks to what I consider my work, you can write to different configuration files / use different configuration files by doing one of the following:

1) Rename the binary

  ln -s /usr/bin/top top2 ./top2 

Now .top2rc will be written to your $ HOME

2) Set $ ​​HOME to some alternative path, since it will write its configuration file to the $ HOME / .binary-name.rc file

 HOME=./ top 

Now .toprc will be written to the current folder.

Using other people's comments to add various usage accounts at the top, you can create batch output for this information, and the latter combines the information through a script. It may not be as easy as you script, but I found a peak to provide me with ALL processes, so that later I could repeat and fix the state for a long time, which I might miss otherwise (inexplicable sudden CPU usage due to wandering processes)

0
Oct 26 '15 at 12:56
source share

I thought perf stat is what you need.

Shows the specific use of the process when you specify the --cpu=list option. The following is an example of monitoring CPU usage to create a project using the command perf stat --cpu=0-7 --no-aggr -- make all -j . Exit:

 CPU0 119254.719293 task-clock (msec) # 1.000 CPUs utilized (100.00%) CPU1 119254.724776 task-clock (msec) # 1.000 CPUs utilized (100.00%) CPU2 119254.724179 task-clock (msec) # 1.000 CPUs utilized (100.00%) CPU3 119254.720833 task-clock (msec) # 1.000 CPUs utilized (100.00%) CPU4 119254.714109 task-clock (msec) # 1.000 CPUs utilized (100.00%) CPU5 119254.727721 task-clock (msec) # 1.000 CPUs utilized (100.00%) CPU6 119254.723447 task-clock (msec) # 1.000 CPUs utilized (100.00%) CPU7 119254.722418 task-clock (msec) # 1.000 CPUs utilized (100.00%) CPU0 8,108 context-switches # 0.068 K/sec (100.00%) CPU1 26,494 context-switches (100.00%) CPU2 10,193 context-switches (100.00%) CPU3 12,298 context-switches (100.00%) CPU4 16,179 context-switches (100.00%) CPU5 57,389 context-switches (100.00%) CPU6 8,485 context-switches (100.00%) CPU7 10,845 context-switches (100.00%) CPU0 167 cpu-migrations # 0.001 K/sec (100.00%) CPU1 80 cpu-migrations (100.00%) CPU2 165 cpu-migrations (100.00%) CPU3 139 cpu-migrations (100.00%) CPU4 136 cpu-migrations (100.00%) CPU5 175 cpu-migrations (100.00%) CPU6 256 cpu-migrations (100.00%) CPU7 195 cpu-migrations (100.00%) 

The left column is the specific CPU index, and the rightmost column is the CPU usage. If you do not specify the --no-aggr , the result will be merged together. The option --pid=pid will help if you want to control the running process.

Try -a --per-core or -a perf-socket too to provide more sensitive information.

You can learn more about using perf stat in this lesson: statistics on the Persian processor , perf help stat will help in terms of meaning.

0
Mar 15 '16 at 7:15
source share



All Articles