program tip

프로세스에 대한 개별 CPU 코어 사용량을 어떻게 측정합니까?

radiobox 2020. 8. 11. 08:15
반응형

프로세스에 대한 개별 CPU 코어 사용량을 어떻게 측정합니까?


코어별로 특정 프로세스 CPU 사용량을 측정하는 방법이 있습니까?

top 이 코어별로 전체 시스템의 CPU 사용량을 측정하는 데 유용하다는 것을 알고 있으며 작업 세트 는 프로세스가 실행될 수있는 CPU 코어에 대한 정보를 제공 할 수 있습니다.

그러나 CPU 코어별로 특정 프로세스의 CPU 사용량을 어떻게 측정합니까?


여전히 top 에서 할 수 있습니다 . top 이 실행되는 동안 키보드에서 '1'을 누르면 코어 당 CPU 사용량이 표시됩니다.

특정 프로세스가 특정 사용자 계정으로 실행되도록하여 표시되는 프로세스를 제한하고 유형 'u'를 사용하여 해당 사용자로 제한합니다.


당신이 사용할 수있는:

 mpstat -P ALL 1

각 코어가 얼마나 많이 사용되는지 보여주고 매초마다 자동으로 업데이트됩니다. 출력은 다음과 같습니다 (쿼드 코어 프로세서에서).

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

이 명령은 원래 질문에 대답하지 않습니다. 즉, 특정 프로세스에 대한 CPU 코어 사용량을 표시하지 않습니다.


사용할 수 있습니다 ps.
예 : 듀얼 코어 CPU에서 두 개의 사용중인 스레드가있는 파이썬 프로세스 :

$ 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은 스레드가 현재 할당 된 CPU ID입니다.)

스레드가 동일한 CPU 코어에서 실행되고 있음을 알 수 있습니다 (GIL 때문에).

자이 썬에서 동일한 파이썬 스크립트를 실행하면 스크립트가 두 코어를 모두 활용하고 있음을 알 수 있습니다 (그리고 거의 유휴 상태 인 다른 많은 서비스 또는 스레드가 있음).

$ 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

출력을 처리하고 각 CPU 코어의 총 CPU를 계산할 수 있습니다.

안타깝게도이 접근 방식은 100 % 신뢰할 수없는 것 같습니다. 때로는 첫 번째 경우 두 작업 스레드가 각 CPU 코어로 분리 된 것으로보고되거나 후자의 경우 두 스레드가 켜져있는 것으로보고됩니다. 같은 코어 ..


htop 개별 코어 사용에 대한 멋진 개요를 제공합니다.


ps솔루션은 내가 필요했던 거의 무엇을 원래의 질문을 요청 정확히 무엇을 던져 일부 bash는 함께 : 코어 당 특정 프로세스의 사용보고

이것은 멀티 스레드 프로세스 의 코어 당 사용량도 보여줍니다 .

다음과 같이 사용하십시오. 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 stats are based on process lifetime, not the last X seconds, so you'll need to restart your process to reset the counter.


dstat -C 0,1,2,3 

Will also give you the CPU usage of first 4 cores. Of course, if you have 32 cores then this command gets a little bit longer but useful if you only interested in few cores.

For example, if you only interested in core 3 and 7 then you could do

dstat -C 3,7

I thought perf stat is what you need.

It shows a specific usage of a process when you specify a --cpu=list option. Here is an example of monitoring cpu usage of building a project using perf stat --cpu=0-7 --no-aggr -- make all -j command. The output is:

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 right most column is the usage of the CPU. If you don't specify the --no-aggr option, the result will aggregated together. The --pid=pid option will help if you want to monitor a running process.

Try -a --per-core or -a perf-socket too, which will present more classified information.

More about usage of perf stat can be seen in this tutorial: perf cpu statistic, also perf help stat will help on the meaning of the options.


I had just this problem and I found a similar answer here.

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

Although this might not work if you want to run multiple top's with different configurations.

So via what I consider a work around you can write to different config files / use different config files by doing one of the following...

1) Rename the binary

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

Now .top2rc is going to be written to your $HOME

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

HOME=./
top

Now .toprc is going to be written to the current folder.

Via use of other peoples comments to add the various usage accounting in top you can create a batch output for that information and latter coalesces the information via a script. Maybe not quite as simple as you script but I found top to provide me ALL processes so that later I can recap and capture a state during a long run that I might have missed otherwise (unexplained sudden CPU usage due to stray processes)

참고URL : https://stackoverflow.com/questions/3342889/how-do-i-measure-separate-cpu-core-usage-for-a-process

반응형