According to the gperftools documentation, the profiler can be started using any of the following methods:
- Setting the
CPUPROFILE
environment variable to the filename the profile information will be saved to - Doing the above, and also setting
CPUPROFILESIGNAL
and sending the appropriate signal to start or stop the sampling. - Calling
ProfilerStart(filename)
andProfileStop()
directly from your code
All three methods require that libprofiler.so
be linked as well.
When I tried this, the third method worked, but when I merely set CPUPROFILE
, no profiling information was generated.
Does not work:
$ cat foo.c
#include <stdio.h>
int main(void) {
printf("Hello, world!\n");
}
$ gcc foo.c -std=c99 -lprofiler -g && CPUPROFILE=foo.prof ./a.out
Hello, world!
$ ls foo.prof
ls: cannot access foo.prof: No such file or directory
Does work:
$ cat bar.c
#include <stdio.h>
#include <gperftools/profiler.h>
int main(void) {
ProfilerStart("bogus_filename");
printf("Hello, world!\n");
ProfilerStop();
}
$ gcc -std=c99 bar.c -lprofiler -g && CPUPROFILE=foo.prof ./a.out
Hello, world!
PROFILE: interrupts/evictions/bytes = 0/0/64
$ ls foo.prof
foo.prof
$ ls bogus_filename
ls: cannot access bogus_filename: No such file or directory
$ ./a.out
Hello, world!
PROFILE: interrupts/evictions/bytes = 0/0/64
$ ls bogus_filename
bogus_filename
Note that CPUPROFILE
is being read, since its value overrides the filename passed to ProfileStart()
if set.