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

前端 未结 8 1719
故里飘歌
故里飘歌 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: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  [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.

提交回复
热议问题