Presumably there is a library or simple asm blob that can get me the number of the current CPU that I am executing on.
相关问题
- Is shmid returned by shmget() unique across proces
- how to get running process information in java?
- Error building gcc 4.8.3 from source: libstdc++.so
- Why should we check WIFEXITED after wait in order
- Null-terminated string, opening file for reading
You need to do something like:
Now you can CPUID again whenever you need to and lookup which core you're on.
But I'd query why you need to do this; normally you want to take control via sched_setaffinity rather than finding out which core you're on (and even that's a pretty rare thing to want/need). (That's why I don't know the crucial detail of what to pull out of CPUID exactly, sorry!)
Update: Just learned about sched_getcpu from litb's response here. Much better! (my Debian/etch libc is too old to have it though).
Use
sched_getcpu
to determine the CPU on which the calling thread is running. Seeman getcpu
(the system call) andman sched_getcpu
(a library wrapper). However, note what it says:I don't know of anything to get your current core id. With kernel level task/process migration, you wouldn't be guaranteed that it would remain constant for any length of time, unless you were running in some form of real-time mode.
If you want to be on a specific core, you can put use that
sched_setaffinity()
function or thetaskset
command to launch your program. I believe that these need elevated permissions to work, though. In your program, you could then runsched_getaffinity()
to see the mask that was set earlier and use that as a best guess at the core on which you are executing.