4

I want to print CPU number on which the current process or function is executing similar to ftrace like this:

 TASK-PID   CPU#      TIMESTAMP  FUNCTION
    | |       |          |         |
<idle>-0     [002]  23636.756054: ttwu_do_activate.constprop.89 <-try_to_wake_up
<idle>-0     [002]  23636.756054: activate_task <-ttwu_do_activate.constprop.89
<idle>-0     [002]  23636.756055: enqueue_task <-activate_task

How do I get that value? I suppose its there in some function of start_kernel function. Can we print its value? I am using linux-4.1 kernel.

Ivid
  • 203
  • 3
  • 10

1 Answers1

4

For printing current cpu in kernel, the cpu field of task_struct can be used. Note that the kernel configuration CONFIG_THREAD_INFO_IN_TASK should be enabled. This will work for 4.9 kernel.

printk("My current cpu is %d\n", current->cpu);

smp_processor_id() also can be used if cpu field is not available.

Jeyaram
  • 9,158
  • 7
  • 41
  • 63
  • 1
    I want to print CPU number very early during kernel initialization , so I have tried to use `smp_processor_id()` after `boot_cpu_init()` function where processor gets activated.But I get some garbage symbol and kernel hangs. Architecture is `armv7`. I tried like `char str = smp_processor_id() + '0';`. Then `printch(str);` I am using printch to enable early printing. – Ivid Jan 26 '17 at 17:34
  • At very early would it be different to 0? – 0andriy Jan 26 '17 at 20:06
  • 1
    ``pr_info("Booting Linux on physical CPU 0x%lx\n", (unsigned long)mpidr);`` in ``smp_setup_processor_id()`` should help on arm for boot cpu. Do you see this message? – Jeyaram Jan 27 '17 at 09:24