【问题标题】:What does native_write_msr in kernel do?内核中的 native_write_msr 是做什么的?
【发布时间】:2020-02-19 00:59:52
【问题描述】:

我有一个 python 脚本,一开始有时很慢。前几天我在上面运行了perf top,我只能看到:

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

   100.00%  [kernel]       [k] native_write_msr

谷歌搜索函数名称对我没有多大帮助。

【问题讨论】:

  • MSR 写入是内核编程硬件性能计数器的方式。这似乎很奇怪;我想知道/proc/sys/kernel/perf_event_paranoid 的限制性设置是否可以解释这一点。 (0 允许在开发机器上使用最有用的东西。)kernel.yama.ptrace_scope = 0 也可能是相关的?你输入了什么确切的命令,你在什么样的系统上? perf stat 是否正常工作,显示指令和周期的计数?

标签: kernel perf


【解决方案1】:

native_write_msr 是 x86/x86_64 特定的内核函数,允许内核代码执行 MSR 写入: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.txthttps://www.kernel.org/doc/html/v4.17/trace/events-msr.html 指令激活 msr 跟踪。

native_write_msr 是内核 wrmsrwrmsl 函数的内部实现,这些函数有许多可能的调用:https://elixir.bootlin.com/linux/v4.8/ident/wrmsrhttps://elixir.bootlin.com/linux/v4.8/ident/wrmsrl,正如彼得在评论中所说,其中一些是用于编程性能计数器(来自 @ 987654331@目录)。

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

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 会显示统计信息,但会遗漏一些仅显示十六进制地址的符号

$ 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 script 将为您提供执行代码的时间线,列出来自perf record 的每个样本(-F 20000 的样本是在大约 20 kHz 下采集的)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-20
    • 1970-01-01
    • 2019-07-21
    • 2021-05-30
    • 1970-01-01
    • 2021-01-13
    相关资源
    最近更新 更多