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]/stat and /proc/[pid]/schedstat are all useful. But how can I get the CPU core number that a thread is running in? If a thread is in sleep state, how can I know which core it will run after it is scheduled again?
在Linux中,假设一个线程的pid是[pid],从目录/proc/[pid]可以得到许多有用的信息。例如,这些proc文件、/proc/[pid]/status、/proc/[pid]/ [pid]/stat /schedstat都是有用的。但是,如何获得线程正在运行的CPU核心编号呢?如果线程处于休眠状态,我如何知道它将在重新调度之后运行哪个核心?
BTW, is there a way to dump the process(thread) list of running and sleeping tasks for each CPU core?
顺便说一下,是否有一种方法可以将进程(线程)列表转储到每个CPU核心的运行和睡眠任务中?
6 个解决方案
#1
29
The answer below is no longer accurate as of 2014
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. The third field will be an 'R' if the thread is running. 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.
要获得您想要的信息,请查看/proc/ pid>/task/
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
3 . (b) (c) (c) (c) (c) (c) (c) (c) (c) (b) (c) (c) (b) (c) (c)
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内核源代码——特别是fs/proc/array中的do_task_stat函数。/文件系统/ stat.txt c或文档。
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中对文件进行公开调用时,以及当该调用返回时,它是正确的。
#2
32
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}
最高- 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核心列,并输入显示。
#3
7
You can also use ps
, something like this:
你也可以用ps,像这样:
ps -mo pid,tid,%cpu,psr -p `pgrep BINARY-NAME`
#4
7
The threads are not necessary to bound one particular Core (if you did not pin it). 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\`
#5
5
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命令来完成。默认的top命令输出没有显示这些细节。要查看这个细节,你必须在命令界面上按下f键,然后按j(按下j键后按回车键),现在输出将显示一个进程的详细信息,以及处理器的运行情况。示例输出如下所示。
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
输出中的P列显示当前正在执行进程的处理器核数。监视这几分钟将使您了解pid是在中间切换处理器内核。您还可以验证您的pid是否在特定的核心上运行。
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 <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
#6
0
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运行线程(或者是最后一个运行)的方法:
- Directly read
/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. As of this writing, it will be the 14th value from the end. - 直接读取/proc/< pid > / / < tid > /统计任务。在这样做之前,确保格式不会随着最新的内核而改变。文档并不总是最新的,但至少可以尝试https://www.kernel.org/doc/Documentation/filesystems/proc.txt。在这篇文章中,它将是最后的第14个值。
- Use
ps
. Either give it-F
switch, or use output modifiers and add codePSR
. - 使用ps。要么给它-F开关,要么使用输出修饰符,并添加代码PSR。
- Use top with Last Used Cpu column (hitting
f
gets you to column selection) - 使用顶部和最后一个使用的Cpu列(点击f使您选择列选择)
- Use htop with PROCESSOR column (hitting
F2
gets you to setup screen) - 使用htop和处理器列(点击F2,可以设置屏幕)
#1
29
The answer below is no longer accurate as of 2014
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. The third field will be an 'R' if the thread is running. 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.
要获得您想要的信息,请查看/proc/ pid>/task/
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
3 . (b) (c) (c) (c) (c) (c) (c) (c) (c) (b) (c) (c) (b) (c) (c)
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内核源代码——特别是fs/proc/array中的do_task_stat函数。/文件系统/ stat.txt c或文档。
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中对文件进行公开调用时,以及当该调用返回时,它是正确的。
#2
32
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}
最高- 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核心列,并输入显示。
#3
7
You can also use ps
, something like this:
你也可以用ps,像这样:
ps -mo pid,tid,%cpu,psr -p `pgrep BINARY-NAME`
#4
7
The threads are not necessary to bound one particular Core (if you did not pin it). 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\`
#5
5
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命令来完成。默认的top命令输出没有显示这些细节。要查看这个细节,你必须在命令界面上按下f键,然后按j(按下j键后按回车键),现在输出将显示一个进程的详细信息,以及处理器的运行情况。示例输出如下所示。
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
输出中的P列显示当前正在执行进程的处理器核数。监视这几分钟将使您了解pid是在中间切换处理器内核。您还可以验证您的pid是否在特定的核心上运行。
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 <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
#6
0
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运行线程(或者是最后一个运行)的方法:
- Directly read
/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. As of this writing, it will be the 14th value from the end. - 直接读取/proc/< pid > / / < tid > /统计任务。在这样做之前,确保格式不会随着最新的内核而改变。文档并不总是最新的,但至少可以尝试https://www.kernel.org/doc/Documentation/filesystems/proc.txt。在这篇文章中,它将是最后的第14个值。
- Use
ps
. Either give it-F
switch, or use output modifiers and add codePSR
. - 使用ps。要么给它-F开关,要么使用输出修饰符,并添加代码PSR。
- Use top with Last Used Cpu column (hitting
f
gets you to column selection) - 使用顶部和最后一个使用的Cpu列(点击f使您选择列选择)
- Use htop with PROCESSOR column (hitting
F2
gets you to setup screen) - 使用htop和处理器列(点击F2,可以设置屏幕)