Mark
Mark

Reputation: 6464

kernel API to return now running cpu number

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.

Upvotes: 0

Views: 2016

Answers (2)

Ahmed Masud
Ahmed Masud

Reputation: 22374

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

Upvotes: 3

Blunt Jackson
Blunt Jackson

Reputation: 664

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);

Upvotes: 0

Related Questions