[英]How can I see which CPU core a thread is running in?
In Linux, supposing a thread's pid is [pid], from the directory /proc/[pid] we can get many useful information. 在Linux中,假设线程的pid是[pid],从目录/ proc / [pid]我们可以获得许多有用的信息。 For example, these proc files, /proc/[pid]/status,/proc/[pid]/stat and /proc/[pid]/schedstat are all useful. 例如,这些proc文件,/ proc / [pid] / status,/ proc / [pid] / stat和/ proc / [pid] / schedstat都很有用。 But how can I get the CPU core number that a thread is running in? 但是,如何获取运行线程的CPU核心编号? If a thread is in sleep state, how can I know which core it will run after it is scheduled again? 如果一个线程处于睡眠状态,我怎么知道它再次被调度后会运行哪个核心?
BTW, is there a way to dump the process(thread) list of running and sleeping tasks for each CPU core? BTW,有没有办法为每个CPU核心转储运行和休眠任务的进程(线程)列表?
The "top" command may help towards this, it does not have CPU-grouped list of threads but rather you can see the list of threads (probably for a single process) and which CPU cores the threads are running on by “top”命令可能对此有所帮助,它没有CPU分组的线程列表,而是您可以看到线程列表(可能是针对单个进程)以及线程运行的CPU内核
top -H -p {PROC_ID}
then pressing f to go into field selection, j to enable the CPU core column, and Enter to display. 然后按f进入字段选择,按j启用CPU核心列,然后按Enter键显示。
Tasks don't sleep in any particular core. 任务不会在任何特定核心中休眠。 And the scheduler won't know ahead of time which core it will run a thread on because that will depend on future usage of those cores. 并且调度程序将不会提前知道它将运行线程的核心,因为这将取决于这些核心的未来使用。
To get the information you want, look in /proc/<pid>/task/<tid>/status. 要获取所需信息,请查看/ proc / <pid> / task / <tid> / status。 The third field will be an 'R' if the thread is running. 如果线程正在运行,第三个字段将是'R'。 The sixth from the last field will be the core the thread is currently running on, or the core it last ran on (or was migrated to) if it's not currently running. 最后一个字段中的第六个字段将是当前运行的线程的核心,或者它最后运行的核心(或迁移到的核心),如果它当前没有运行。
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 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
Not currently running. 目前尚未运行。 Last ran on core 3. 最后跑到核心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 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
Currently running on core 2. 目前正在核心2上运行。
To see what the rest of the fields mean, have a look at the Linux kernel source -- specifically the do_task_stat
function in fs/proc/array.c
or Documentation/filesystems/stat.txt
. 要查看该字段的其余部分的意思是,看看Linux内核源代码-特别是do_task_stat
在功能fs/proc/array.c
或Documentation/filesystems/stat.txt
。
Note that all of this information may be obsolete by the time you get it. 请注意,所有这些信息在您获取时可能已过时。 It was true at some point between when you made the open
call on the file in proc and when that call returned. 在proc中对文件进行open
调用和返回该调用之间的某个时刻确实如此。
The threads are not necessary to bound one particular Core (if you did not pin it). 线程不必绑定一个特定的Core(如果你没有固定它)。 Therefore to see the continuous switching of the core you can use (a modified answer of Dmitry): 因此,要查看核心的连续切换,您可以使用(德米特里的修改后的答案):
watch -tdn0.5 ps -mo pid,tid,%cpu,psr -p \`pgrep BINARY-NAME\`
For example: 例如:
watch -tdn0.5 ps -mo pid,tid,%cpu,psr -p \`pgrep firefox\`
您也可以使用ps
,如下所示:
ps -mo pid,tid,%cpu,psr -p `pgrep BINARY-NAME`
This can be done with top
command. 这可以使用top
命令完成。 The default top
command output does not show these details. 默认的top
命令输出不显示这些详细信息。 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 ). 要查看这个细节你必须按F键的同时在上面的命令界面,然后按J(按Enter键按下你在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. 输出中的P
列显示当前正在执行进程的处理器核心编号。 Monitoring this for a few minutes will make you understand that a pid is switching processor cores in between. 监视这几分钟将使您了解pid正在切换处理器核心。 You can also verify whether your pid for which you have set affinity is running on that particular core only 您还可以验证您已设置亲缘关系的pid是否仅在该特定核心上运行
top
f navigation screen ( a live system example ) : top
f导航屏幕(实时系统示例) : 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
Accepted answer is not accurate. 接受的答案不准确。 Here are the ways to find out which CPU is running the thread (or was the last one to run) at the moment of inquiry: 以下是在查询时找出运行该线程的CPU(或者是最后一个运行)的方法:
/proc/<pid>/task/<tid>/stat
. 直接读取/proc/<pid>/task/<tid>/stat
。 Before doing so, make sure format didn't change with latest kernel. 在此之前,请确保格式没有随最新内核而改变。 Documentation is not always up to date, but at least you can try https://www.kernel.org/doc/Documentation/filesystems/proc.txt . 文档并不总是最新的,但至少可以尝试https://www.kernel.org/doc/Documentation/filesystems/proc.txt 。 As of this writing, it will be the 14th value from the end. 在撰写本文时,它将是最终的第14个值。 ps
. 使用ps
。 Either give it -F
switch, or use output modifiers and add code PSR
. 给它-F
开关,或者使用输出修饰符并添加代码PSR
。 f
gets you to column selection) 使用top with Last Used Cpu列(点击f
可以进入列选择) F2
gets you to setup screen) 使用带有PROCESSOR列的htop(点击F2
可以进入设置界面) 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 #
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.