Search code examples
linuxlinux-kernellinux-device-driverftrace

Printing CPU number similar to ftrace


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.


Solution

  • 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.