内核中的native_write_msr有什么作用? [英] What does native_write_msr in kernel do?

查看:423
本文介绍了内核中的native_write_msr有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个python脚本,开始时有时会很慢.前几天我在它上面跑了 perf top ,我所能看到的是:

I have a python script that is sometimes slow at the beginning. I ran perf top on it the other day and all I could see was:

   PerfTop:       2 irqs/sec  kernel:100.0%  exact:  0.0% [4000Hz cycles],  (target_pid: 1234)
-------------------------------------------------------------------------------------------------

   100.00%  [kernel]       [k] native_write_msr

搜索功能名称并没有太大帮助.

Googling the function name didn't help me much.

推荐答案

native_write_msr 是特定于x86/x86_64的内核功能,该功能允许内核代码执行MSR写入:

native_write_msr is x86/x86_64 specific kernel function which allows kernel code to do MSR writes: https://elixir.bootlin.com/linux/v4.8/source/arch/x86/include/asm/msr.h#L118

static inline void native_write_msr(unsigned int msr,
                      unsigned low, unsigned high)
{
  asm volatile("1: wrmsr\n"
           "2:\n"
           : : "c" (msr), "a"(low), "d" (high) : "memory");
  if (msr_tracepoint_active(__tracepoint_write_msr))
      do_trace_write_msr(msr, ((u64)high << 32 | low), 0);
}

如果要获取实际的MSR索引和值,可以尝试使用 https://www.kernel.org/doc/Documentation/trace/events-msr.txt

If you want to get actual MSR indexes and values you may try to activate msr tracing with https://www.kernel.org/doc/Documentation/trace/events-msr.txt or https://www.kernel.org/doc/html/v4.17/trace/events-msr.html instructions.

native_write_msr 是内核的 wrmsr wrmsl 函数的内部实现,它们具有许多可能的调用: https://elixir.bootlin.com/linux/v4.8/ident/wrmsrl 和其中的一些,正如Peter在评论中所说的那样,是用于编程性能计数器的(来自 arch/x86/events 目录).

native_write_msr is internal implementation of kernel's wrmsr and wrmsl functions which have many possible invocations: https://elixir.bootlin.com/linux/v4.8/ident/wrmsr and https://elixir.bootlin.com/linux/v4.8/ident/wrmsrl and some of them, as Peter says in comment are for programming performance counters (from arch/x86/events directory).

要对短脚本进行概要分析,请不要使用 perf top 工具(它用于长时间运行的进程和整个系统的概要分析),但是请尝试 perf记录python3 ./您的脚本.py 将配置文件记录到 perf.data 文件和 perf报告 perf报告中>report.txt 来解码 perf.data 文件.这是一个非常简短的脚本,所以我将采样频率更改为更高的值,并且我也没有配置内核(:u 后缀)

For profiling of short scripts don't use perf top tool (it is for long-running processes and whole system profiling), but try perf record python3 ./your-script.py to record profile into perf.data file and perf report or perf report > report.txt to decode perf.data file. This is very short script so I changed sample frequency to higher value, and also I did not profile kernel (:u suffix)

echo 2 | sudo tee  /proc/sys/kernel/perf_event_paranoid
$ perf record -e cycles:u -F 20000 python3 -c 'print(1)' 
1
[ perf record: Woken up 1 times to write data ]

性能报告将为您显示统计信息,但会丢失一些仅显示十六进制地址的符号

perf report will show you statistics, but it will miss some symbols showing only hex addresses

$ perf report            # interactive TUI
$ perf report|head -n 20|tail
#
     9.75%  python3  python3.6          [.] _PyEval_EvalFrameDefault
     2.77%  python3  python3.6          [.] 0x000000000049b284
     1.95%  python3  libc-2.27.so       [.] __strlen_avx2
     1.86%  python3  python3.6          [.] PyObject_GetAttr
     1.80%  python3  python3.6          [.] PyDict_SetDefault
     1.61%  python3  python3.6          [.] PyUnicode_New
     1.55%  python3  libc-2.27.so       [.] _int_malloc
     1.52%  python3  python3.6          [.] _PyDict_LoadGlobal
     1.41%  python3  python3.6          [.] _PyObject_GenericSetAttrWithDict

perf脚本将为您提供执行代码的时间轴,列出 perf记录中的每个样本(使用 -F 20000 样本在大约20 kHz).

perf script will give you timeline of code executed, listing every sample from the perf record (With -F 20000 samples are taken at around 20 kHz).

这篇关于内核中的native_write_msr有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆