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

前端 未结 8 1499
故里飘歌
故里飘歌 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:19

    You can use:

     mpstat -P ALL 1
    

    It shows how much each core is busy and it updates automatically each second. The output would 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 doesn't answer original question though i.e. it does not show CPU core usage for a specific process.

    0 讨论(0)
  • 2021-01-29 19:23

    The ps solution was nearly what I needed and with some bash thrown in does exactly what the original question asked for: to see per-core usage of specific processes

    This shows per-core usage of multi-threaded processes too.

    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 stats are based on process lifetime, not the last X seconds, so you'll need to restart your process to reset the counter.

    0 讨论(0)
提交回复
热议问题