4

What stream does the perf command use!? I've been trying to capture it with

(perf stat -x, -ecache-misses ./a.out>/dev/null) 2> results

following https://stackoverflow.com/q/13232889/50305, but to no avail. Why can I not capture the input... it's like letting some fish get away!!

Dervin Thunk
  • 3,429
  • 4
  • 23
  • 21

2 Answers2

7

Older versions of perf ~2.6.x

I'm using perf version: 2.6.35.14-106.

Capture all the output

I don't have the -x switch on my Fedora 14 system so I'm not sure if that's your actual problem or not. I'll investigate on a newer Ubuntu 12.10 system later on but this worked for me:

$ (perf stat -ecache-misses ls ) > stat.log 2>&1
$
$ more stat.log 
maccheck.txt
sample.txt
stat.log

 Performance counter stats for 'ls':

              13209  cache-misses            

        0.018231264  seconds time elapsed

I only want perf's output

You could try this, the output from ls will get redirected to /dev/null. The output form perf (both STDERR and STDOUT) goes to the file, stat.log.

$ (perf stat -ecache-misses ls > /dev/null ) > stat.log 2>&1
[saml@grinchy 89576]$ more stat.log 

 Performance counter stats for 'ls':

              12949  cache-misses            

        0.022831281  seconds time elapsed

Newer versions of perf 3.x+

I'm using perf version: 3.5.7

Capturing only perf's output

With the newer versions of perf there are dedicated options for controlling where messages get sent. You have the choice of either sending them to a file via the -o|--output option. Simply give either of those switches a filename to capture the output.

-o file, --output file
    Print the output into the designated file.

The alternative is to redirect the output to a alternate file descriptor, 3, for example. All you need to do is direct this alternate file handle prior to streaming to it.

--log-fd
    Log output to fd, instead of stderr. Complementary to --output, and 
    mutually exclusive with it. --append may be used here. Examples: 
       3>results perf stat --log-fd 3  — $cmd
       -or-
       3>>results perf stat --log-fd 3 --append — $cmd

So if we wanted to collect the perf output for the ls command you could use this command:

$ 3>results.log perf stat --log-fd 3 ls > /dev/null
$ 
$ more results.log

 Performance counter stats for 'ls':

          2.498964 task-clock                #    0.806 CPUs utilized          
                 0 context-switches          #    0.000 K/sec                  
                 0 CPU-migrations            #    0.000 K/sec                  
               258 page-faults               #    0.103 M/sec                  
           880,752 cycles                    #    0.352 GHz                    
           597,809 stalled-cycles-frontend   #   67.87% frontend cycles idle   
           652,087 stalled-cycles-backend    #   74.04% backend  cycles idle   
         1,261,424 instructions              #    1.43  insns per cycle        
                                             #    0.52  stalled cycles per insn [55.31%]
     <not counted> branches                
     <not counted> branch-misses           

       0.003102139 seconds time elapsed

If you use the --append version then the contents of multiple commands will be appended to the same log file, results.log in our case.

Installing perf

Installation is pretty trivial:

Fedora

$ yum install perf

Ubuntu/Debian

$ apt-get install linux-tool-common linux-tools

References

slm
  • 363,520
  • 117
  • 767
  • 871
  • nope, still not redirecting to file. I want to redirect perf's output to file, not my command. – Dervin Thunk Sep 05 '13 at 22:20
  • @DervinThunk - you only want perfs output, correct? – slm Sep 05 '13 at 22:21
  • That is correct. But I think I've found it... there's an --append option in perf – Dervin Thunk Sep 05 '13 at 22:23
  • @DervinThunk - see updates. What version/OS are you on? I don't have that either. – slm Sep 05 '13 at 22:24
  • Linux 3.2.0-52-generic #78-Ubuntu SMP Fri Jul 26 16:21:44 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux – Dervin Thunk Sep 05 '13 at 22:30
  • I'm going crazy. Still not redirecting, even trying your command verbatim. – Dervin Thunk Sep 05 '13 at 22:34
  • @DervinThunk - saw you figured it out. I also found it once I got into my Ubuntu box. Seems the `perf` command has gotten a lot more options since my version of Fedora 8-). I still updated my answer with more details and examples, hopefully it will help others. – slm Sep 06 '13 at 01:41
  • 1
    There is no -o option in 'perf version 3.10.0-1062.1.1.el7.x86_64.debug' in CentOS7. – karatedog Sep 18 '19 at 12:52
1

This is what finally worked for me:

3>results perf stat -x, -ecache-misses --log-fd 3 --append -- ./a.out

as per man perf-stat, the log-fd flag.

Dervin Thunk
  • 3,429
  • 4
  • 23
  • 21