I am looking for a Linux kernel API that returns the core number currently executing the task. So I want to know in my code on which particular core is being executed.
Asked
Active
Viewed 1,694 times
0
-
I found answer at https://stackoverflow.com/questions/7315907/how-to-find-physical-and-logical-core-number-in-a-kernel-module/7316216 – Mark Sep 13 '17 at 15:44
-
Possible duplicate of [How to find physical and logical core number in a kernel module?](https://stackoverflow.com/questions/7315907/how-to-find-physical-and-logical-core-number-in-a-kernel-module) – Tsyvarev Sep 13 '17 at 19:20
-
judging by the answers, it seems unclear you are talking about user context or kernel context. – Ezequiel Garcia Sep 14 '17 at 18:01
2 Answers
3
typically when you are locking a process to a core you use get_cpu
this is to prevent preemption so that your process doesn't suddenly move to another CPU. If you know that you're not going to be preempted you can use smp_processor_id
to get the CPU id.
#include/asm/smp.h
static int my_cpu() {
return smp_processor_id();
}
NOTES
CPU ids are between 0 and NR_CPUS and they are not necessarily continuous

Community
- 1
- 1

Ahmed Masud
- 21,655
- 3
- 33
- 58
0
Already answered:
how can I get the processor ID of the current process in C in Linux?
See man page:
http://man7.org/linux/man-pages/man2/getcpu.2.html
tldr:
#include <linux/getcpu.h>
int getcpu(unsigned *cpu, unsigned *node, struct getcpu_cache *tcache);

Blunt Jackson
- 616
- 4
- 17