Profiling sleep times with perf

c, linux, perf, profiling

Solution

There is a error message from your second perf command from https://perf.wiki.kernel.org/index.php/Tutorial#Profiling_sleep_times - `perf inject -s`

$ sudo perf inject -v -s -i ~/perf.data.raw -o ~/perf.data
build id event received for [kernel.kallsyms]: d62870685909222126e7070d2bafdf029f7ed3b6
failed to write feature 2

failed to write feature 2 doesn't look too user-friendly...

... but it was added to perf to made errors more user-friendly: http://lwn.net/Articles/460520/ "perf: make perf.data more self-descriptive (v5)" by Stephane Eranian , 22 Sep 2011:

+static int do_write_feat(int fd, struct perf_header *h, int type,  ....
+           pr_debug("failed to write feature %d\n", type);

All features are listed here http://lxr.free-electrons.com/source/tools/perf/util/header.h#L13

 15         HEADER_TRACING_DATA     = 1,
 16         HEADER_BUILD_ID,

So, it sounds like perf inject was not able to write information about build ids (error from function `write_build_id()` from util/header.c) if I'm not wrong. There are two cases which can lead to error: unsuccessful call to `perf_session__read_build_ids()` or failing in writing buildid table `dsos__write_buildid_table` (this is not our case because there is no "failed to write buildid table" error message; check `write_build_id`)

You may check, do you have all buildids needed for the session. Also it may be useful to clear your buildid cache (`rm -rf ~/.debug`), and check that you have up-to-date vmlinux with debugging info or kallsyms enabled in your kernel.

UPDATE: in comments Pavel says that his pref record had no any `sched:sched_stat_sleep` events written to perf.data:

`sudo perf record -e sched:sched_stat_sleep -e sched:sched_switch -e sched:sched_process_exit -g -o ~/perf.data.raw ./a.out`

As he explains in his answer, his default debian kernel have `CONFIG_SCHEDSTATS` option disabled with vendor's patch. The redhat did the same thing with the option in release kernels since 3.11, and this is explained in Redhat Bug 1013225 (Josh Boyer 2013-10-28, comment 4):

We switched to enabling that only on debug builds a while ago. It seems that was turned off entirely with the final 3.11.0 build and has remained off since. Internal testing shows the option has a non-trivial performance impact for context switches.

We can turn this on in debug kernels again, but I'm not sure it's worthwhile.

Josh Poimboeuf 2013-11-04 in comment 8 says that performance impact is detectable:

In my tests I did a lot of context switches under various CPU loads. I saw a ~5-10% drop in average context switch speed when CONFIG_SCHEDSTATS was enabled. ...The performance hit only seemed to happen on post-CFS kernels (>= 2.6.23). The previous O(1) scheduler didn't seem to have this issue.

Fedora disabled CONFIG_SCHEDSTAT in non-debug kernels at 12 July 2013 "[kernel] Disable LATENCYTOP/SCHEDSTATS in non-debug builds." by Dave Jones. First kernel with disabled option: 3.11.0-0.rc0.git6.4.

In order to use any `perf` software tracepoint event with name like `sched:sched_stat_*` (`sched:sched_stat_wait`, `sched:sched_stat_sleep`, `sched:sched_stat_iowait`) we must recompile kernel with `CONFIG_SCHEDSTATS` option enabled and replace default Debian, RedHat or Fedora kernels which have no this option.

Thank you, Pavel Davydov.

Problem

I was looking for a way to find out where my program spends time. I read the perf tutorial and tried to profile sleep times as it is described there. I wrote the simplest possible program to profile: ``` #include <unistd.h> int main() { sleep(10); return 0; } ``` then I executed it with perf: ``` $ sudo perf record -e sched:sched_stat_sleep -e sched:sched_switch -e sched:sched_process_exit -g -o ~/perf.data.raw ./a.out [ perf record: Woken up 1 times to write data ] [ perf record: Captured and wrote 0.013 MB /home/pablo/perf.data.raw (~578 samples) ] $ sudo perf inject -v -s -i ~/perf.data.raw -o ~/perf.data build id event received for [kernel.kallsyms]: d62870685909222126e7070d2bafdf029f7ed3b6 failed to write feature 2 $ sudo perf report --stdio --show-total-period -i ~/perf.data Error: The /home/pablo/perf.data file has no samples! ``` Does anybody know how to avoid these errors? What do they mean? `failed to write feature 2` doesn't look too user-friendly... Update: ``` $ uname -a Linux debian 3.12-1-amd64 #1 SMP Debian 3.12.9-1 (2014-02-01) x86_64 GNU/Linux ```

Original source