Linux 查看cpu占用率

top和htop只会显示所有CPU核心的总体使用情况。如果要查看每个CPU核心的使用情况,可以按下数字键"1"

top
1

显示

Tasks:  60 total,   1 running,  59 sleeping,   0 stopped,   0 zombie
%Cpu0  :  1.0 us,  0.0 sy,  0.0 ni, 99.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu1  :  3.3 us,  1.0 sy,  0.0 ni, 95.7 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu2  :  0.0 us,  0.7 sy,  0.0 ni, 99.3 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu3  :  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu4  :  0.0 us,  0.7 sy,  0.0 ni, 99.3 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu5  :  0.0 us,  0.3 sy,  0.0 ni, 99.7 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu6  :  1.7 us,  0.3 sy,  0.0 ni, 98.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu7  :  2.3 us,  2.0 sy,  0.0 ni, 95.7 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu8  :  1.7 us,  0.3 sy,  0.0 ni, 98.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu9  :  0.3 us,  0.7 sy,  0.0 ni, 99.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu10 :  2.3 us,  0.3 sy,  0.0 ni, 97.3 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu11 :  1.0 us,  0.0 sy,  0.0 ni, 99.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu12 :  1.0 us,  0.0 sy,  0.0 ni, 99.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu13 :  0.7 us,  0.3 sy,  0.0 ni, 99.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu14 :  1.0 us,  1.0 sy,  0.0 ni, 98.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu15 :  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
MiB Mem :   7808.3 total,   2648.8 free,   3748.4 used,   1411.0 buff/cache
MiB Swap:   2048.0 total,   2048.0 free,      0.0 used.   3802.3 avail Mem

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
 3755 huang     20   0 1083904 222756  41112 S   9.7   2.8  28:56.35 node
   97 huang     20   0 1017184 156312  36788 S   8.3   2.0  28:51.41 node
   37 huang     20   0  661072  64648  32420 S   0.7   0.8   2:57.11 node
 4608 huang     20   0  589240  47828  29800 S   0.3   0.6   0:03.47 node
    1 root      20   0    2328   1712   1604 S   0.0   0.0   0:00.01 init(Ubuntu-20.
    4 root      20   0    2384     68     68 S   0.0   0.0   0:03.33 init
   14 root      20   0    2336    108      0 S   0.0   0.0   0:00.00 SessionLeader
   15 root      20   0    2352    116      0 S   0.0   0.0   0:00.00 Rela

未指定cpu内核,进程将会随机取cpu内核

%Cpu0  :  1.3 us,  0.0 sy,  0.0 ni, 97.7 id,  0.3 wa,  0.0 hi,  0.7 si,  0.0 st
%Cpu1  :100.0 us,  0.0 sy,  0.0 ni,  0.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu2  :100.0 us,  0.0 sy,  0.0 ni,  0.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu3  :  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu4  :  0.7 us,  1.0 sy,  0.0 ni, 98.3 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu5  :  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu6  :100.0 us,  0.0 sy,  0.0 ni,  0.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu7  :  1.0 us,  0.0 sy,  0.0 ni, 99.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu8  :  0.0 us,  1.0 sy,  0.0 ni, 99.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu9  :  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu10 :  0.3 us,  0.7 sy,  0.0 ni, 99.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu11 :  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu12 :  1.7 us,  1.0 sy,  0.0 ni, 97.4 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu13 :  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu14 :  0.3 us,  0.0 sy,  0.0 ni, 99.7 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu15 :  1.0 us,  0.3 sy,  0.0 ni, 98.7 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
MiB Mem :   7808.3 total,   2585.9 free,   3754.1 used,   1468.4 buff/cache
MiB Swap:   2048.0 total,   2048.0 free,      0.0 used.   3796.3 avail Mem

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
 3593 huang     20   0    5888   1516   1364 R  99.7   0.0   0:30.98 a.out
 3618 huang     20   0    5888   1524   1368 R  99.7   0.0   0:27.53 a.out
 3643 huang     20   0    5888   1516   1360 R  99.7   0.0   0:24.09 a.out
   97 huang     20   0 1017184 156044  36788 S   5.0   2.0  31:14.68 node
 3755 huang     20   0 1083904 222992  41112 S   1.3   2.8  31:13.00 node
 3843 huang     20   0 1795568 204216  14584 S   0.3   2.6   3:05.45 cpptools
 3874 huang     20   0  970192 196920  32812 S   0.3   2.5   0:10.73 node

指定cpu内核运行程序,命令taskset -c 11 ./a.out

Tasks:  64 total,   2 running,  62 sleeping,   0 stopped,   0 zombie
%Cpu0  :  0.3 us,  0.3 sy,  0.0 ni, 94.9 id,  0.0 wa,  0.0 hi,  4.4 si,  0.0 st
%Cpu1  :  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu2  :  0.7 us,  0.0 sy,  0.0 ni, 99.3 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu3  :  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu4  :  0.0 us,  0.7 sy,  0.0 ni, 99.3 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu5  :  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu6  :  0.0 us,  0.3 sy,  0.0 ni, 99.7 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu7  :  0.7 us,  0.3 sy,  0.0 ni, 99.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu8  :  0.3 us,  0.0 sy,  0.0 ni, 99.7 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu9  :  0.7 us,  0.0 sy,  0.0 ni, 99.3 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu10 :  0.0 us,  0.3 sy,  0.0 ni, 99.7 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu11 :100.0 us,  0.0 sy,  0.0 ni,  0.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu12 :  0.0 us,  0.3 sy,  0.0 ni, 99.7 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu13 :  0.0 us,  0.7 sy,  0.0 ni, 99.3 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu14 :  0.0 us,  0.3 sy,  0.0 ni, 99.7 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu15 :  0.7 us,  1.7 sy,  0.0 ni, 97.7 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
MiB Mem :   7808.3 total,   2591.5 free,   3748.2 used,   1468.6 buff/cache
MiB Swap:   2048.0 total,   2048.0 free,      0.0 used.   3802.2 avail Mem

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
 4743 huang     20   0    5888   1512   1360 R 100.0   0.0   0:06.13 a.out
   97 huang     20   0 1017184 156044  36788 S   6.3   2.0  31:25.16 node
 3755 huang     20   0 1083904 222888  41112 S   1.0   2.8  31:24.84 node
 2769 huang     20   0 5836480  24688   9632 S   0.3   0.3   0:11.64 cpptools-srv
 2979 huang     20   0   10996   3760   3116 R   0.3   0.0   0:00.20 top
    1 root      20   0    2328   1712   1604 S   0.0   0.0   0:00.01 init(Ubuntu-20.
    4 root      20   0    2384     68     68 S   0.0   0.0   0:03.36 init
   14 root      20   0    2336    108      0 S   0.0   0.0   0:00.00 SessionLeade

如果想要将一个已经在运行的进程绑定到指定的CPU核心上,可以使用taskset命令的-p选项

taskset -p 0x1 <pid>
taskset -p 0x5F5E100 4743

在这个例子中,-p 0x1表示将进程亲和于第1个CPU核心上。
这个16进制要转成二进制,为1时候,才为指定核可用

c语言指定本程序的cpu内核

#include 
#include 
#include 
#include "unistd.h"

int main()
{
    cpu_set_t mask;
    pid_t pid = getpid();
    CPU_ZERO(&mask);
    CPU_SET(3, &mask); // 指定第几个核运行,本程序
    if(sched_setaffinity(pid, sizeof(mask), &mask) == -1)
    {
        perror("sched_setaffinity() failed");
        exit(EXIT_FAILURE);
    }
    while (1);
    return 0;
}

top查看到,本程序运行在cpu3上

你可能感兴趣的:(linux)