我如何知道在C程序中,我的代码运行在哪个物理处理器和核心上? 我正在使用Linux和gcc 4.4.3。
答案 0 :(得分:9)
调用返回虚拟CPU编号。虚拟CPU到实际CPU信息的映射在/ proc / cpuinfo。
如果您的系统支持VDSO,则sched_getcpu()
相对较快。
也可以使用CPUID
指令获取CPU编号,但速度比sched_getcpu()
慢。
答案 1 :(得分:2)
请参阅http://en.wikipedia.org/wiki/CPUID#Accessing_the_id_from_other_languages
你想要的是APIC ID ...基本上:
cpuid
shr ebx, 24
答案 2 :(得分:0)
您可以使用GCC获得进程与处理器的亲和力。 CPU affinity API可能会帮助您。您是否尝试使用此信息来确保您的流程不会中断或执行如此高优先级的任务?
答案 3 :(得分:0)
您可以检查/ proc // stat文件系统,根据http://www.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;
}
答案 4 :(得分:-1)
总的来说,很难以有意义的方式找到它。您的线程通常会在其生命周期中运行在许多不同的处理器上您可以调用一个函数来询问您所在的处理器,并在函数执行时获取上下文切换。该函数应该返回什么?