Skip to content

perf 工具 ​

perf 工具的组件名称为 perf, 相关内核选项为

ini
# General setup
# -> Kernel Performance Events And Counters
# -> Kernel performance events and counters
CONFIG_PERF_EVENTS=y

# Kernel hacking
# -> Tracers
# -> Enable kprobes-based dynamic events
CONFIG_KPROBE_EVENTS=y

# Kernel hacking
# -> Tracers
# -> Enable uprobes-based dynamic events
CONFIG_UPROBE_EVENTS=y

检查工具为

bash
#!/bin/bash

source ./kernel_config_utils.sh
source ./net_utils.sh

perf_check() {
    is_error=false
    if ! kernel_config_check CONFIG_PERF_EVENTS; then
        is_error=true
    fi
    if [ "$(kernel_version_cmp "$KERNEL_VERSION" "v4.11")" -ge "0" ]; then
        if ! kernel_config_check CONFIG_KPROBE_EVENTS; then
            is_error=true
        fi
        if ! kernel_config_check CONFIG_UPROBE_EVENTS; then
            is_error=true
        fi
    else
        if ! kernel_config_check CONFIG_KPROBE_EVENT; then
            is_error=true
        fi
        if ! kernel_config_check CONFIG_UPROBE_EVENT; then
            is_error=true
        fi
    fi
    if [ "$is_error" == "true" ]; then
        return 1
    fi

    return 0
}

如果 /proc/kcore 文件不存在, perf 相关命令可能无法查看内核符号汇编显示以下内容:

Note that annotation using /proc/kcore requires CAP_SYS_RAWIO capability

在 Linux v4.11 版本后, 内核选项 CONFIG_{U,K}PROBE_EVENT 重命名为 CONFIG_{U,K}PROBE_EVENTS, 相关提交为 6b0b7551428e4caae1e2c023a529465a9a9ae2d4. 内核选项 UPROBE_EVENT, UPROBE_EVENTS 依赖于 ARCH_SUPPORTS_UPROBES, ARCH_SUPPORTS_UPROBES 开始设置的 Linux 版本和提交如下表所示:

CPU 架构版本提交号
aarch64v4.109842ceae9fa8deae141533d52a6ead7666962c09
armv7lv3.15c7edc9e326d53ca5ef9bed82de0740c6b107d55b
loongarch64v6.519bc6cb6409289106d38f9ad1b2ecf73980df6b5
x86_64v3.52b144498350860b6ee9dc57ff27a93ad488de5dc