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 索引和值,您可以尝试使用以下命令激活 msr 跟踪https://www.kernel.org/doc/Documentation/trace/events-msr.txt or https://www.kernel.org/doc/html/v4.17/trace/events-msr.html指示。
native_write_msr
是内核的内部实现wrmsr
and wrmsl
有许多可能调用的函数:https://elixir.bootlin.com/linux/v4.8/ident/wrmsr and https://elixir.bootlin.com/linux/v4.8/ident/wrmsrl正如彼得在评论中所说,其中一些用于编程性能计数器(来自arch/x86/events
目录)。
对于短脚本的分析,不要使用perf top
工具(它用于长时间运行的进程和整个系统分析),但尝试perf record python3 ./your-script.py
将配置文件记录到perf.data
文件和perf report
or perf report > report.txt
解码perf.data
文件。这是非常短的脚本,因此我将采样频率更改为更高的值,而且我也没有分析内核(: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
会显示统计信息,但会丢失一些仅显示十六进制地址的符号
$ 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
(With -F 20000
采样频率约为 20 kHz)。