5

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

2
  • I don't see a -x option on stat. What version of perf are you using? Commented Sep 5, 2013 at 22:06
  • @slm: perf version 3.2.48 Commented Sep 5, 2013 at 22:21

2 Answers 2

8

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

8
  • nope, still not redirecting to file. I want to redirect perf's output to file, not my command. Commented Sep 5, 2013 at 22:20
  • @DervinThunk - you only want perfs output, correct? Commented Sep 5, 2013 at 22:21
  • That is correct. But I think I've found it... there's an --append option in perf Commented Sep 5, 2013 at 22:23
  • @DervinThunk - see updates. What version/OS are you on? I don't have that either. Commented Sep 5, 2013 at 22:24
  • 1
    There is no -o option in 'perf version 3.10.0-1062.1.1.el7.x86_64.debug' in CentOS7. Commented Sep 18, 2019 at 12:52
2

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.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.