2

我想用 SystemTap 分析内核模块的缓存行为(#cache 引用、#cache 未命中等)。在线有一个示例脚本,它显示了如何使用 SystemTap 来读取性能事件和计数器,包括与缓存相关的事件和计数器: https ://sourceware.org/systemtap/examples/profiling/perf.stp

此示例脚本默认适用于进程:

probe perf.hw.cache_references.process("/usr/bin/find").counter("find_insns") {} 

我用我的内核模块的名称替换了process关键字module和可执行文件的路径:

probe perf.hw.cache_references.module(MODULE_NAME).counter("find_insns") {} 

我很确定我的模块有调试信息,但是运行我得到的脚本:

语义错误:解析探测点时:perf.stp:14:7 处的标识符 'perf' 来源:probe perf.hw.instructions.module(MODULE_NAME).counter("find_insns") {}

有什么想法可能是错的吗?

编辑:

好的,我意识到性能计数器只能绑定到进程而不是模块(此处解释:https ://sourceware.org/systemtap/man/stapprobes.3stap.html )。因此我将其改回:

probe perf.hw.cache_references.process(PATH_TO_BINARY).counter("find_insns") {} 

现在,正如示例脚本所暗示的,我有:

probe module(MODULE_NAME).function(FUNC_NAME) {
#save counter values on entrance
...
}

但现在运行它,我得到:

语义错误:性能计数器“find_insns”未定义语义错误:解析探测点时:perf.stp:26:7 处的标识符“模块”源:探测模块(MODULE_NAME).function(FUNC_NAME)

编辑2:

所以这是我的完整脚本:

#! /usr/bin/env stap

# Usage: stap perf.stp <path-to-binary> <module-name> <function-name>

global cycles_per_insn
global branch_per_insn
global cacheref_per_insn
global insns
global cycles
global branches
global cacherefs
global insn
global cachemisses
global miss_per_insn

probe perf.hw.instructions.process(@1).counter("find_insns") {} 
probe perf.hw.cpu_cycles.process(@1).counter("find_cycles") {} 
probe perf.hw.branch_instructions.process(@1).counter("find_branches") {} 
probe perf.hw.cache_references.process(@1).counter("find_cache_refs") {} 
probe perf.hw.cache_misses.process(@1).counter("find_cache_misses") {}


probe module(@2).function(@3)
{
 insn["find_insns"] = @perf("find_insns")
 insns <<< (insn["find_insns"])
 insn["find_cycles"] = @perf("find_cycles")
 cycles <<< insn["find_cycles"]
 insn["find_branches"] = @perf("find_branches")
 branches <<< insn["find_branches"]
 insn["find_cache_refs"] = @perf("find_cache_refs")
 cacherefs <<< insn["find_cache_refs"]
 insn["find_cache_misses"] = @perf("find_cache_misses")
 cachemisses <<< insn["find_cache_misses"]
}


probe module(@2).function(@3).return 
{
    dividend = (@perf("find_cycles") - insn["find_cycles"])
    divisor =  (@perf("find_insns") - insn["find_insns"])
    q = dividend / divisor
    if (q > 0)
    cycles_per_insn <<< q

    dividend = (@perf("find_branches") - insn["find_branches"])
    q = dividend / divisor
    if (q > 0)
    branch_per_insn <<< q

    dividend = (@perf("find_cycles") - insn["find_cycles"])
    q = dividend / divisor
    if (q > 0)
    cacheref_per_insn <<< q

    dividend = (@perf("find_cache_misses") - insn["find_cache_misses"])
    q = dividend / divisor
    if (q > 0)
        miss_per_insn <<< q
}

probe end
{
 if (@count(cycles_per_insn)) {
   printf ("Cycles per Insn\n\n")
   print (@hist_log(cycles_per_insn))
 }
 if (@count(branch_per_insn)) {
   printf ("\nBranches per Insn\n\n")
   print (@hist_log(branch_per_insn))
 }
 if (@count(cacheref_per_insn)) {
   printf ("Cache Refs per Insn\n\n")
   print (@hist_log(cacheref_per_insn))
 }
 if (@count(miss_per_insn)) {
   printf ("Cache Misses per Insn\n\n")
   print (@hist_log(miss_per_insn))
 }
}
4

1 回答 1

1

Systemtap 无法读取内核探测的硬件性能值,因为 linux 没有提供合适的(例如,原子的)内部 API 来安全地从所有上下文中读取这些值。perf...process 探针之所以起作用,只是因为该上下文不是原子的:systemtap 探针处理程序可以安全地阻塞。

我无法回答您关于您上次尝试的两个(?)脚本的详细问题,因为它们不完整。

于 2015-07-22T23:15:20.050 回答