What stream does perf use

consoleio-redirectionoutputperf

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!!

Best Answer

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

Related Question