How can I see which CPU core a thread is running in?

前端 未结 7 1249
悲&欢浪女
悲&欢浪女 2020-12-07 14:09

In Linux, supposing a thread\'s pid is [pid], from the directory /proc/[pid] we can get many useful information. For example, these proc files, /proc/[pid]/status,/proc/[pid

7条回答
  •  臣服心动
    2020-12-07 14:23

    This can be done with top command. The default top command output does not show these details. To view this detail you will have to press f key while on top command interface and then press j(press Enter key after you pressed j). Now the output will show you details regarding a process and which processor its running. A sample output is shown below.

    top - 04:24:03 up 96 days, 13:41,  1 user,  load average: 0.11, 0.14, 0.15
    Tasks: 173 total,   1 running, 172 sleeping,   0 stopped,   0 zombie
    Cpu(s):  7.1%us,  0.2%sy,  0.0%ni, 88.4%id,  0.1%wa,  0.0%hi,  0.0%si,  4.2%st
    Mem:   1011048k total,   950984k used,    60064k free,     9320k buffers
    Swap:   524284k total,   113160k used,   411124k free,    96420k cached
    
      PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  P COMMAND
    12426 nginx     20   0  345m  47m  29m S 77.6  4.8  40:24.92 7 php-fpm
     6685 mysql     20   0 3633m  34m 2932 S  4.3  3.5  63:12.91 4 mysqld
    19014 root      20   0 15084 1188  856 R  1.3  0.1   0:01.20 4 top
        9 root      20   0     0    0    0 S  1.0  0.0 129:42.53 1 rcu_sched
     6349 memcache  20   0  355m  12m  224 S  0.3  1.2   9:34.82 6 memcached
        1 root      20   0 19404  212   36 S  0.0  0.0   0:20.64 3 init
        2 root      20   0     0    0    0 S  0.0  0.0   0:30.02 4 kthreadd
        3 root      20   0     0    0    0 S  0.0  0.0   0:12.45 0 ksoftirqd/0
    

    The P column in the output shows the processor core number where the process is currently being executed. Monitoring this for a few minutes will make you understand that a pid is switching processor cores in between. You can also verify whether your pid for which you have set affinity is running on that particular core only

    top f navigation screen ( a live system example ) :

    Fields Management for window 1:Def, whose current sort field is forest view
       Navigate with Up/Dn, Right selects for move then  or Left commits,
       'd' or  toggles display, 's' sets sort.  Use 'q' or  to end!
    
    * PID     = Process Id
    * USER    = Effective User Name
    * PR      = Priority
    * NI      = Nice Value
    * VIRT    = Virtual Image (KiB)
    * RES     = Resident Size (KiB)
    * SHR     = Shared Memory (KiB)
    * S       = Process Status
    * %CPU    = CPU Usage
    * %MEM    = Memory Usage (RES)
    * TIME+   = CPU Time, hundredths
    * COMMAND = Command Name/Line
      PPID    = Parent Process pid
      UID     = Effective User Id
      RUID    = Real User Id
      RUSER   = Real User Name
      SUID    = Saved User Id
      SUSER   = Saved User Name
      GID     = Group Id
      GROUP   = Group Name
      PGRP    = Process Group Id
      TTY     = Controlling Tty
      TPGID   = Tty Process Grp Id
      SID     = Session Id
      nTH     = Number of Threads
    * P       = Last Used Cpu (SMP)
      TIME    = CPU Time
      SWAP    = Swapped Size (KiB)
      CODE    = Code Size (KiB)
      DATA    = Data+Stack (KiB)
      nMaj    = Major Page Faults
      nMin    = Minor Page Faults
      nDRT    = Dirty Pages Count
      WCHAN   = Sleeping in Function
      Flags   = Task Flags 
      CGROUPS = Control Groups
      SUPGIDS = Supp Groups IDs
      SUPGRPS = Supp Groups Names
      TGID    = Thread Group Id
      ENVIRON = Environment vars
      vMj     = Major Faults delta
      vMn     = Minor Faults delta
      USED    = Res+Swap Size (KiB)
      nsIPC   = IPC namespace Inode
      nsMNT   = MNT namespace Inode
      nsNET   = NET namespace Inode
      nsPID   = PID namespace Inode
      nsUSER  = USER namespace Inode
      nsUTS   = UTS namespace Inode
    

提交回复
热议问题