How do I measure separate CPU core usage for a process?

前端 未结 8 1718
故里飘歌
故里飘歌 2021-01-29 18:45

Is there any way to measure a specific process CPU usage by cores?

I know top is good for measuring the whole system\'s CPU usage by cores and taskset can provide inform

8条回答
  •  生来不讨喜
    2021-01-29 19:02

    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.

提交回复
热议问题