如何查看线程正在哪个 CPU 核心上运行?

发布于 2024-12-14 04:27:14 字数 245 浏览 1 评论 0 原文

在Linux中,假设一个线程的pid是[pid],从目录/proc/[pid]中我们可以得到很多有用的信息。例如,这些 proc 文件 /proc/[pid]/status、/proc/[pid]/stat 和 /proc/[pid]/schedstat 都很有用。但是如何获取线程正在运行的CPU核心数呢?如果一个线程处于睡眠状态,我如何知道它再次被调度后将运行哪个核心?

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

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?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

分开我的手 2024-12-21 04:27:14

“top”命令可能对此有所帮助,它没有按 CPU 分组的线程列表,但您可以通过

top 查看线程列表(可能是单个进程)以及线程在哪些 CPU 核心上运行-H -p {PROC_ID}

然后按 f 进入字段选择,j 进入启用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 -H -p {PROC_ID}

then pressing f to go into field selection, j to enable the CPU core column, and Enter to display.

若水微香 2024-12-21 04:27:14

自 2014 年起,下面的答案不再准确,

任务不会在任何特定核心中休眠。并且调度程序不会提前知道它将在哪个核心上运行线程,因为这将取决于这些核心的未来使用情况。

要获取所需的信息,请查看 /proc//task//status。如果线程正在运行,第三个字段将为“R”。最后一个字段中的第六个将是线程当前运行的核心,或者如果当前未运行,则为最后运行(或迁移到)的核心。

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 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 内核源代码 - 特别是 fs/proc/array.cdo_task_stat 函数="http://www.kernel.org/doc/Documentation/filesystems/proc.txt" rel="noreferrer">Documentation/filesystems/stat.txt

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

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.

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.

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.

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.

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.

高跟鞋的旋律 2024-12-21 04:27:14

您还可以使用 ps,如下所示:

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

You can also use ps, something like this:

ps -mo pid,tid,%cpu,psr -p `pgrep BINARY-NAME`
以往的大感动 2024-12-21 04:27:14

线程不需要绑定一个特定的核心(如果您没有固定它)。因此,要查看核心的连续切换,您可以使用(Dmitry 的修改答案):

watch -tdn0.5 ps -mo pid,tid,%cpu,psr -p \`pgrep BINARY-NAME\`

例如:

watch -tdn0.5 ps -mo pid,tid,%cpu,psr -p \`pgrep firefox\`

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\`
凉城凉梦凉人心 2024-12-21 04:27:14

这可以通过 top 命令来完成。默认的 top 命令输出不显示这些详细信息。要查看此详细信息,您必须在顶部命令界面上按 f 键,然后按 j(按按jEnter键)。现在,输出将向您显示有关进程及其运行的处理器的详细信息。下面显示了示例输出。

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

输出中的 P 列显示当前正在执行进程的处理器核心编号。监视几分钟将使您了解 pid 正在之间切换处理器内核。您还可以验证您设置了关联性的 pid 是否仅在该特定核心上运行

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

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 <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
一场春暖 2024-12-21 04:27:14

接受的答案不准确。以下是在查询时找出哪个 CPU 正在运行该线程(或者是最后一个运行的线程)的方法:

  1. 直接读取 /proc//task//stat< /代码>。执行此操作之前,请确保格式未随最新内核而更改。文档并不总是最新的,但至少您可以尝试 https://www .kernel.org/doc/Documentation/filesystems/proc.txt。截至撰写本文时,这将是倒数第 14 个值。
  2. 使用ps。要么给它 -F 开关,要么使用输出修饰符并添加代码 PSR
  3. 将 top 与“上次使用的 Cpu”列一起使用(按 f 即可进入列选择)
  4. 将 htop 与“处理器”列一起使用(按 F2 即可进入设置屏幕)

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:

  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 -F switch, or use output modifiers and add code PSR.
  3. Use top with Last Used Cpu column (hitting f gets you to column selection)
  4. Use htop with PROCESSOR column (hitting F2 gets you to setup screen)
浅忆 2024-12-21 04:27:14
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 #
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 #
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文