打印与ftrace相似的CPU数量

时间:2022-06-27 16:52:25

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

我想打印当前进程或函数执行的CPU编号,类似于ftrace:

 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.

如何得到这个值?假设它在start_kernel函数的某个函数中。我们可以打印它的值吗?我正在使用linux-4.1内核。

1 个解决方案

#1


3  

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.

对于在内核中打印当前cpu,可以使用task_struct的cpu字段。注意,内核配置CONFIG_THREAD_INFO_IN_TASK应该启用。这将适用于4.9内核。

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

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

如果cpu字段不可用,也可以使用smp_processor_id()。

#1


3  

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.

对于在内核中打印当前cpu,可以使用task_struct的cpu字段。注意,内核配置CONFIG_THREAD_INFO_IN_TASK应该启用。这将适用于4.9内核。

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

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

如果cpu字段不可用,也可以使用smp_processor_id()。