如何查看线程在哪个 CPU 内核中运行? [英] How can I see which CPU core a thread is running in?

查看:72
本文介绍了如何查看线程在哪个 CPU 内核中运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Linux中,假设一个线程的pid是[pid],从目录/proc/[pid]我们可以得到很多有用的信息.例如,这些 proc 文件,/proc/[pid]/status、/proc/[pid]/stat 和/proc/[pid]/schedstat 都是有用的.但是我怎样才能得到一个线程正在运行的 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?

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

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

推荐答案

截至 2014 年以下答案不再准确

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

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.

要获取您想要的信息,请查看/proc//task//status.如果线程正在运行,第三个字段将是一个R".最后一个字段中的第六个将是线程当前正在运行的核心,或者它上次运行(或迁移到)的核心(如果当前未运行).

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)的取值 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 173 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

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

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 172 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

目前在核心 2 上运行.

Currently running on core 2.

要了解其余字段的含义,请查看 Linux 内核源代码——特别是 fs/proc/array.c 中的 do_task_stat 函数或Documentation/filesystems/stat.txt.

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.

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

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.

这篇关于如何查看线程在哪个 CPU 内核中运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆