How can I see which CPU core a thread is running in?
在Linux中,假设线程的pid是[pid],从目录/ proc / [pid]我们可以获得许多有用的信息。 例如,这些proc文件,/ proc / [pid] / status,/ proc / [pid] / stat和/ proc / [pid] / schedstat都很有用。 但是,如何获取运行线程的CPU核心编号? 如果一个线程处于睡眠状态,我怎么知道它再次被调度后会运行哪个核心?
BTW,有没有办法为每个CPU核心转储运行和休眠任务的进程(线程)列表?
"top"命令可能对此有所帮助,它没有CPU分组的线程列表,而是您可以看到线程列表(可能是针对单个进程)以及线程运行的CPU内核
然后按f进入字段选择,j启用CPU核心列,并显示Enter。
截至2014年,以下答案不再准确
任务不会在任何特定核心中休眠。并且调度程序将不会提前知道它将运行线程的核心,因为这将取决于这些核心的未来使用。
要获取所需信息,请查看/ proc /
31466 (bc) S 31348 31466 31348 34819 31466 4202496 2557 0 0 0 5006 16 0 0 20 0 1 0 10196934 121827328 1091 18446744073709551615 4194304 4271839 140737264235072 140737264232056 217976807456 0 0 0 137912326 18446744071581662243 0 0 17 3 0 0 0 0 0
目前尚未运行。最后跑到核心3。
31466 (bc) R 31348 31466 31348 34819 31466 4202496 2557 0 0 0 3818 12 0 0 20 0 1 0 10196934 121827328 1091 18446744073709551615 4194304 4271839 140737264235072 140737264231824 4235516 0 0 0 2 0 0 0 17 2 0 0 0 0 0
目前正在核心2上运行。
要查看其余字段的含义,请查看Linux内核源代码 - 特别是
请注意,所有这些信息在您获取时可能已过时。在proc中对文件进行
线程不必绑定一个特定的Core(如果你没有固定它)。因此,要查看核心的连续切换,您可以使用(德米特里的修改后的答案):
1 | watch -tdn0.5 ps -mo pid,tid,%cpu,psr -p \`pgrep BINARY-NAME\` |
例如:
1 | watch -tdn0.5 ps -mo pid,tid,%cpu,psr -p \`pgrep firefox\` |
您也可以使用
1 | ps -mo pid,tid,%cpu,psr -p `pgrep BINARY-NAME` |
这可以使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 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 |
输出中的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | Fields Management for window 1:Def, whose current sort field is forest view Navigate with Up/Dn, Right selects for move then <Enter> or Left commits, 'd' or <Space> toggles display, 's' sets sort. Use 'q' or <Esc> 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 <sched.h> 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 |
接受的答案不准确。以下是在查询时找出运行该线程的CPU(或者是最后一个运行)的方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | To see the threads of a process : ps -T -p PID To see the thread run info ps -mo pid,tid,%cpu,psr -p PID Example : /tmp # ps -T -p 3725 PID SPID TTY TIME CMD 3725 3725 ? 00:00:00 Apps 3725 3732 ? 00:00:10 t9xz1d920 3725 3738 ? 00:00:00 XTimer 3725 3739 ? 00:00:05 Japps 3725 4017 ? 00:00:00 QTask 3725 4024 ? 00:00:00 Kapps 3725 4025 ? 00:00:17 PTimer 3725 4026 ? 00:01:17 PTask 3725 4027 ? 00:00:00 RTask 3725 4028 ? 00:00:00 Recv 3725 4029 ? 00:00:00 QTimer 3725 4033 ? 00:00:01 STask 3725 4034 ? 00:00:02 XTask 3725 4035 ? 00:00:01 QTimer 3725 4036 ? 00:00:00 RTimer 3725 4145 ? 00:00:00 t9xz1d920 3725 4147 ? 00:00:02 t9xz1d920 3725 4148 ? 00:00:00 t9xz1d920 3725 4149 ? 00:00:00 t9xz1d920 3725 4150 ? 00:00:00 t9xz1d920 3725 4865 ? 00:00:02 STimer /tmp # /tmp # /tmp # ps -mo pid,tid,%cpu,psr -p 3725 PID TID %CPU PSR 3725 - 1.1 - - 3725 0.0 2 - 3732 0.1 0 - 3738 0.0 0 - 3739 0.0 0 - 4017 0.0 6 - 4024 0.0 3 - 4025 0.1 0 - 4026 0.7 0 - 4027 0.0 3 - 4028 0.0 7 - 4029 0.0 0 - 4033 0.0 4 - 4034 0.0 1 - 4035 0.0 0 - 4036 0.0 2 - 4145 0.0 2 - 4147 0.0 0 - 4148 0.0 5 - 4149 0.0 2 - 4150 0.0 7 - 4865 0.0 0 /tmp # |