5

I have a complex application that executes in a number of phases. I would like to profile only one of the phases.

The C++ application runs on Linux, x86-64.

This program takes several minutes to run. If I use perf to profile the whole thing, the resulting data set is too large for perf report to process. However, at this point I am interested only in profiling the execution of one phase of the program that takes maybe 1/3 of the total time. Perhaps this data set will be easier for perf to report on.

Ideally, I'd like something along the lines of "send yourself SIGUSR1 to start profiling, and SIGUSR2 to stop it". At that point I can easily delineate the execution phase that I want profile information for.

I can always write my own (albeit basic) profiler using SIGPROF, but is there a way I can do this with existing tools such as perf?

d3jones
  • 751
  • 3
  • 5
  • 2
    Possible duplicate of [perf stat for part of program](https://stackoverflow.com/questions/26267588/perf-stat-for-part-of-program) – UKMonkey Jun 25 '19 at 12:40
  • I think the answer there needs a fiddle so that the process you want to perf broadcasts when you should start/stop perfing; but I think fundamentally it's as good as you're going to get – UKMonkey Jun 25 '19 at 12:43
  • It is feasable using directly system calls: http://man7.org/linux/man-pages/man2/perf_event_open.2.html – Oliv Jun 25 '19 at 16:40

1 Answers1

0

A possible way to do this is to attach perf to an existing process.

So, start your program, check out its pid. Then start profiling with the -p <pid> option, when it's appropriate. And use CTRL-C or SIGINT to stop the profiling. But this trick works only if you don't need to start/stop profiling a lot of times, as the data append functionality has been removed from perf long time ago.

Or maybe you can just decrease sampling frequency with -F, so the resulting data becomes more tractable.

geza
  • 28,403
  • 6
  • 61
  • 135