Linux 如何查看线程在哪个 CPU 内核中运行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8032372/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 07:04:30  来源:igfitidea点击:

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

linuxlinux-kernelscheduled-tasksschedulingscheduler

提问by flypen

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]/stat 和 /proc/[pid]/schedstat 都很有用。但是我怎样才能得到一个线程正在运行的 CPU 核心数呢?如果线程处于睡眠状态,我如何知道它在再次调度后将运行哪个核心?

BTW, is there a way to dump the process(thread) list of running and sleeping tasks for each CPU core?

顺便说一句,有没有办法为每个 CPU 内核转储运行和睡眠任务的进程(线程)列表?

采纳答案by David Schwartz

The answer below is no longer accurate as of 2014

截至 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/<tid>/status。如果线程正在运行,第三个字段将是一个“R”。最后一个字段的第六个将是线程当前正在运行的核心,或者它上次运行(或迁移到)的核心(如果当前未运行)。

31466 (bc) S31348 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 30 0 0 0 0

31466(BC)š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 30 0 0 0 0

Not currently running. Last ran on core 3.

当前未运行。最后在核心 3 上运行。

31466 (bc) R31348 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 20 0 0 0 0

31466(BC)- [R31348 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 20 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_statfunction in fs/proc/array.cor Documentation/filesystems/stat.txt.

要了解其余字段的含义,请查看 Linux 内核源代码——特别是或 中的do_task_stat函数。fs/proc/array.cDocumentation/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 opencall on the file in proc and when that call returned.

请注意,所有这些信息在您获得时可能已经过时。在您open对 proc 中的文件进行调用和该调用返回之间的某个时间点确实如此。

回答by maltanar

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}

top -H -p {PROC_ID}

then pressing fto go into field selection, jto enable the CPU core column, and Enterto display.

然后按f进入字段选择,j启用 CPU core 列,并Enter显示。

回答by Dmitry Vyal

You can also use ps, something like this:

你也可以使用ps,像这样:

ps -mo pid,tid,%cpu,psr -p `pgrep BINARY-NAME`

回答by Monir

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):

线程不需要绑定一个特定的核心(如果您没有固定它)。因此,要查看您可以使用的核心的连续切换(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\`

回答by Tarak nath Sinha

This can be done with topcommand. The default topcommand output does not show these details. To view this detail you will have to press fkey while on top command interface and then press j(press Enterkey 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(按Enter后按键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 Pcolumn 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 是否仅在该特定核心上运行

topfnavigation screen ( a live system example ) :

topf导航屏幕(实时系统示例)

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

回答by SergeyA

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 正在运行线程(或最后一个运行)的方法:

  1. 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.
  2. Use ps. Either give it -Fswitch, or use output modifiers and add code PSR.
  3. Use top with Last Used Cpu column (hitting fgets you to column selection)
  4. Use htop with PROCESSOR column (hitting F2gets you to setup screen)
  1. 直接阅读/proc/<pid>/task/<tid>/stat。在这样做之前,请确保格式没有随着最新的内核而改变。文档并不总是最新的,但至少您可以尝试https://www.kernel.org/doc/Documentation/filesystems/proc.txt。在撰写本文时,它将是从末尾算起的第 14 个值。
  2. 使用ps. 要么给它-F开关,要么使用输出修饰符并添加代码PSR
  3. 将 top 与 Last Used Cpu 列一起使用(点击可f让您选择列)
  4. 将 htop 与 PROCESSOR 列一起使用(点击F2让您进入设置屏幕)

回答by devOgopan

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 #