繁体   English   中英

perf 记录的默认行为是什么?

[英]What is the default behavior of perf record?

我很清楚perf总是记录一个或多个事件,并且采样可以是基于计数器或基于时间的。 但是当没有给出-e-F开关时, perf record的默认行为是什么? perf-record的手册页没有告诉您它在这种情况下的作用。

默认事件是cycles ,可以通过在perf record之后运行perf script来查看。 在那里,您还可以看到默认的采样行为是基于时间的,因为周期数不是恒定的。 默认频率为 4000 Hz,可以在源代码中看到并通过将文件大小或样本数与指定了-F 4000的记录进行比较来检查。

perf wiki说该速率为 1000 Hz,但对于 3.4 之后的内核,这不再是正确的。

性能perf record中的默认事件选择是在用户空间性能工具中完成的,该工具通常作为 linux kernel 的一部分分发。 With make perf-src-tar-gz from linux kernel source dir we can make tar gz for quick rebuild or download such tar from https://mirrors.edge.kernel.org/pub/linux/kernel/tools/perf . linux kernel 源代码也有几个在线“LXR”交叉参考查看器,可以像 grep 一样使用来了解性能内部结构。

性能记录有 function 到 select 默认事件列表(evlist): __perf_evlist__add_default of tools/perf/util/evlist.Z4A8A08F09D37B73795364903B 文件

int __perf_evlist__add_default(struct evlist *evlist, bool precise)
{
    struct evsel *evsel = perf_evsel__new_cycles(precise);
    evlist__add(evlist, evsel);
    return 0;
}

如果从选项解析的事件为零,则从 perf 记录实现调用: tools/perf/builtin-record.c: int cmd_record()

rec->evlist->core.nr_entries == 0 &&
    __perf_evlist__add_default(rec->evlist, !record.opts.no_samples)

并且perf_evsel__new_cycles将询问硬件事件周期(PERF_TYPE_HARDWARE + PERF_COUNT_HW_CPU_CYCLES),可选 kernel 采样和最大精确度检查 man perf-list 中的修饰符,它是使用 PEBS 或 IBS 的 EIP 采样防滑解决方法):

struct evsel *perf_evsel__new_cycles(bool precise)
{
    struct perf_event_attr attr = {
        .type   = PERF_TYPE_HARDWARE,
        .config = PERF_COUNT_HW_CPU_CYCLES,
        .exclude_kernel = !perf_event_can_profile_kernel(),
    };
    struct evsel *evsel;

    /*
     * Now let the usual logic to set up the perf_event_attr defaults
     * to kick in when we return and before perf_evsel__open() is called.
     */
    evsel = evsel__new(&attr);
    evsel->precise_max = true;

    /* use asprintf() because free(evsel) assumes name is allocated */
    if (asprintf(&evsel->name, "cycles%s%s%.*s",
             (attr.precise_ip || attr.exclude_kernel) ? ":" : "",
             attr.exclude_kernel ? "u" : "",
             attr.precise_ip ? attr.precise_ip + 1 : 0, "ppp") < 0)
    return evsel;
}

如果 perf_event_open 失败(无法访问硬件周期采样,例如在没有虚拟化 PMU 的虚拟化环境中),则在tools/perf/builtin-record.c: int record__open()中故障回复到软件 cpu 时钟采样perf_evsel__fallback()工具/perf/util/evsel.c

bool perf_evsel__fallback(struct evsel *evsel, int err,
              char *msg, size_t msgsize)
{
    if ((err == ENOENT || err == ENXIO || err == ENODEV) &&
        evsel->core.attr.type   == PERF_TYPE_HARDWARE &&
        evsel->core.attr.config == PERF_COUNT_HW_CPU_CYCLES) {
        /*
         * If it's cycles then fall back to hrtimer based
         * cpu-clock-tick sw counter, which is always available even if
         * no PMU support.
         */
        scnprintf(msg, msgsize, "%s", "The cycles event is not supported, trying to fall back to cpu-clock-ticks");

        evsel->core.attr.type   = PERF_TYPE_SOFTWARE;
        evsel->core.attr.config = PERF_COUNT_SW_CPU_CLOCK;

        return true;
    } ...
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM