如何知道我的代码正在哪个物理处理器和哪个物理核心上运行

发布于 2024-11-08 05:04:43 字数 66 浏览 0 评论 0原文

在 C 程序中,我如何知道我的代码正在哪个物理处理器和内核上运行? 我正在使用 Linux 和 gcc 4.4.3。

How can I know in a C program, on which physical processor and core my code is running?
I'm using Linux and gcc 4.4.3.

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

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

发布评论

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

评论(5

萌逼全场 2024-11-15 05:04:43

sched_getcpu()

调用返回虚拟 CPU 编号。虚拟 CPU 到真实 CPU 信息的映射位于 /proc/cpuinfo 中。

如果您的系统支持 VDSO,则 sched_getcpu() 相对较快。

CPU编号也可以使用CPUID指令获取,但它比sched_getcpu()慢。

sched_getcpu()

call returns virtual CPU number. Mapping of virtual CPU to real CPU info is in /proc/cpuinfo.

If your system supports VDSO, then sched_getcpu() is relatively fast.

CPU number can be also obtained using CPUID instruction, but it is slower than sched_getcpu().

雨巷深深 2024-11-15 05:04:43

请参阅 http://en.wikipedia.org/wiki/CPUID#Accessing_the_id_from_other_languages

你想要什么是 APIC ID...基本上:

    cpuid
    shr     ebx, 24

See http://en.wikipedia.org/wiki/CPUID#Accessing_the_id_from_other_languages

What you want is the APIC ID... basically:

    cpuid
    shr     ebx, 24
浪荡不羁 2024-11-15 05:04:43

您可以根据http://www./proc//stat 文件系统检查。 kernel.org/doc/Documentation/filesystems/proc.txt,您应该只检查task_cpu标志。

作为没有正确类型和错误检查的示例:

struct pstat
{
  int  pid;       //process id
  char tcomm[256];//filename of the executable
  char state[2];  //state (R is running, S is sleeping, D is sleeping in an
                  //uninterruptible wait, Z is zombie, T is traced or stopped)
  int ppid;//          process id of the parent process
  int pgrp;//          pgrp of the process
  int sid;//           session id
  int tty_nr;//        tty the process uses
  int tty_pgrp;//      pgrp of the tty
  int flags;//         task flags
  int min_flt;//       number of minor faults
  int cmin_flt;//      number of minor faults with child's
  int maj_flt;//       number of major faults
  int cmaj_flt;//      number of major faults with child's
  int utime;//         user mode jiffies
  int stime;//         kernel mode jiffies
  int cutime;//        user mode jiffies with child's
  int cstime;//        kernel mode jiffies with child's
  int priority;//      priority level
  int nice;//          nice level
  int num_threads;//   number of threads
  int it_real_value;//  (obsolete, always 0)
  int start_time;//    time the process started after system boot
  int vsize;//         virtual memory size
  int rss;//           resident set memory size
  int rsslim;//        current limit in bytes on the rss
  int start_code;//    address above which program text can run
  int end_code;//      address below which program text can run
  int start_stack;//   address of the start of the stack
  int esp;//           current value of ESP
  int eip;//           current value of EIP
  int pending;//       bitmap of pending signals
  int blocked;//       bitmap of blocked signals
  int sigign;//        bitmap of ignored signals
  int sigcatch;//      bitmap of catched signals
  int wchan;//         address where process went to sleep
  int i0;//             (place holder)
  int i1;//             (place holder)
  int exit_signal;//   signal to send to parent thread on exit
  int task_cpu;//      which CPU the task is scheduled on
  int rt_priority;//   realtime priority
  int policy;//        scheduling policy (man sched_setscheduler)
  int blkio_ticks;//   time spent waiting for block IO
  int gtime;//         guest time of the task in jiffies
  int cgtime;//        guest time of the task children in jiffies
} p ;

int main()
{
    char name[256];
    char state[8];
    FILE* f = fopen("/proc/self/stat", "r");

    fscanf(f,  "%d%s%s%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d",
           &p.pid, &p.tcomm, &p.state, &p.ppid, &p.pgrp, &p.sid, &p.tty_nr, &p.tty_pgrp, &p.flags,
           &p.min_flt, &p.cmin_flt, &p.maj_flt, &p.cmaj_flt, &p.utime, &p.stime,  &p.cutime, &p.cstime,
           &p.priority, &p.nice, &p.num_threads, &p.it_real_value, &p.start_time,  &p.vsize, &p.rss,
           &p.rsslim, &p.start_code, &p.end_code, &p.start_stack, &p.esp, &p.eip,  &p.pending, &p.blocked,
           &p.sigign, &p.sigcatch, &p.wchan, &p.i0, &p.i1, &p.exit_signal,  &p.task_cpu, &p.rt_priority, &p.policy,
           &p.blkio_ticks, &p.gtime, &p.cgtime);

     printf("CPU %d\n", p.task_cpu);
 return 0;  
 }

You may check /proc//stat file system, according to http://www.kernel.org/doc/Documentation/filesystems/proc.txt, you should just check task_cpu flag.

As example without correct types and error checking:

struct pstat
{
  int  pid;       //process id
  char tcomm[256];//filename of the executable
  char state[2];  //state (R is running, S is sleeping, D is sleeping in an
                  //uninterruptible wait, Z is zombie, T is traced or stopped)
  int ppid;//          process id of the parent process
  int pgrp;//          pgrp of the process
  int sid;//           session id
  int tty_nr;//        tty the process uses
  int tty_pgrp;//      pgrp of the tty
  int flags;//         task flags
  int min_flt;//       number of minor faults
  int cmin_flt;//      number of minor faults with child's
  int maj_flt;//       number of major faults
  int cmaj_flt;//      number of major faults with child's
  int utime;//         user mode jiffies
  int stime;//         kernel mode jiffies
  int cutime;//        user mode jiffies with child's
  int cstime;//        kernel mode jiffies with child's
  int priority;//      priority level
  int nice;//          nice level
  int num_threads;//   number of threads
  int it_real_value;//  (obsolete, always 0)
  int start_time;//    time the process started after system boot
  int vsize;//         virtual memory size
  int rss;//           resident set memory size
  int rsslim;//        current limit in bytes on the rss
  int start_code;//    address above which program text can run
  int end_code;//      address below which program text can run
  int start_stack;//   address of the start of the stack
  int esp;//           current value of ESP
  int eip;//           current value of EIP
  int pending;//       bitmap of pending signals
  int blocked;//       bitmap of blocked signals
  int sigign;//        bitmap of ignored signals
  int sigcatch;//      bitmap of catched signals
  int wchan;//         address where process went to sleep
  int i0;//             (place holder)
  int i1;//             (place holder)
  int exit_signal;//   signal to send to parent thread on exit
  int task_cpu;//      which CPU the task is scheduled on
  int rt_priority;//   realtime priority
  int policy;//        scheduling policy (man sched_setscheduler)
  int blkio_ticks;//   time spent waiting for block IO
  int gtime;//         guest time of the task in jiffies
  int cgtime;//        guest time of the task children in jiffies
} p ;

int main()
{
    char name[256];
    char state[8];
    FILE* f = fopen("/proc/self/stat", "r");

    fscanf(f,  "%d%s%s%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d",
           &p.pid, &p.tcomm, &p.state, &p.ppid, &p.pgrp, &p.sid, &p.tty_nr, &p.tty_pgrp, &p.flags,
           &p.min_flt, &p.cmin_flt, &p.maj_flt, &p.cmaj_flt, &p.utime, &p.stime,  &p.cutime, &p.cstime,
           &p.priority, &p.nice, &p.num_threads, &p.it_real_value, &p.start_time,  &p.vsize, &p.rss,
           &p.rsslim, &p.start_code, &p.end_code, &p.start_stack, &p.esp, &p.eip,  &p.pending, &p.blocked,
           &p.sigign, &p.sigcatch, &p.wchan, &p.i0, &p.i1, &p.exit_signal,  &p.task_cpu, &p.rt_priority, &p.policy,
           &p.blkio_ticks, &p.gtime, &p.cgtime);

     printf("CPU %d\n", p.task_cpu);
 return 0;  
 }
始于初秋 2024-11-15 05:04:43

您可以使用 GCC 获取进程与处理器的关联性。 CPU 关联 API 可能会帮助您。您是否正在尝试使用此信息来确保您的流程或此类高优先级任务不会被中断?

You can get the affinity of the process to a processor using GCC. The CPU affinity APIs might help you out. Are you trying to use this information to ensure your process is not interrupted or such high priority task?

欲拥i 2024-11-15 05:04:43

总的来说,很难以有意义的方式找到这一点。您的线程在其生命周期中通常会在许多不同的处理器上运行。您可以调用一个函数来询问您所在的处理器,并在该函数执行时获得上下文切换。函数应该返回什么?

By and large it's hard to find this out in a meaningful way. Your thread will often run on many different processors during its life. You could call a function to ask which processor you are on and get a context switch whilst the function is executing. What should the function return?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文