如何查看线程正在哪个 CPU 核心上运行?
在Linux中,假设一个线程的pid是[pid],从目录/proc/[pid]中我们可以得到很多有用的信息。例如,这些 proc 文件 /proc/[pid]/status、/proc/[pid]/stat 和 /proc/[pid]/schedstat 都很有用。但是如何获取线程正在运行的CPU核心数呢?如果一个线程处于睡眠状态,我如何知道它再次被调度后将运行哪个核心?
顺便说一句,有没有办法转储每个 CPU 核心的正在运行和休眠任务的进程(线程)列表?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
“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.
自 2014 年起,下面的答案不再准确,
任务不会在任何特定核心中休眠。并且调度程序不会提前知道它将在哪个核心上运行线程,因为这将取决于这些核心的未来使用情况。
要获取所需的信息,请查看 /proc//task//status。如果线程正在运行,第三个字段将为“R”。最后一个字段中的第六个将是线程当前运行的核心,或者如果当前未运行,则为最后运行(或迁移到)的核心。
当前未运行。最后在核心 3 上运行。
当前在核心 2 上运行。
要了解其余字段的含义,请查看 Linux 内核源代码 - 特别是
fs/proc/array.c
或 do_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.
Not currently running. Last ran on core 3.
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 infs/proc/array.c
orDocumentation/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.您还可以使用
ps
,如下所示:You can also use
ps
, something like this:线程不需要绑定一个特定的核心(如果您没有固定它)。因此,要查看核心的连续切换,您可以使用(Dmitry 的修改答案):
例如:
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):
For example:
这可以通过
top
命令来完成。默认的top
命令输出不显示这些详细信息。要查看此详细信息,您必须在顶部命令界面上按 f 键,然后按 j(按按j后Enter键)。现在,输出将向您显示有关进程及其运行的处理器的详细信息。下面显示了示例输出。输出中的
P
列显示当前正在执行进程的处理器核心编号。监视几分钟将使您了解 pid 正在之间切换处理器内核。您还可以验证您设置了关联性的 pid 是否仅在该特定核心上运行top
f 导航屏幕 (实时系统示例):This can be done with
top
command. The defaulttop
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.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 onlytop
f navigation screen ( a live system example ) :接受的答案不准确。以下是在查询时找出哪个 CPU 正在运行该线程(或者是最后一个运行的线程)的方法:
/proc//task//stat< /代码>。执行此操作之前,请确保格式未随最新内核而更改。文档并不总是最新的,但至少您可以尝试 https://www .kernel.org/doc/Documentation/filesystems/proc.txt。截至撰写本文时,这将是倒数第 14 个值。
ps
。要么给它-F
开关,要么使用输出修饰符并添加代码PSR
。f
即可进入列选择)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:
/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.ps
. Either give it-F
switch, or use output modifiers and add codePSR
.f
gets you to column selection)F2
gets you to setup screen)