2

perf can be used to analyze the performance of a program.

Say you want to test the performance of a program prgrm, with input from a file, you can run:

perf stat prgrm < inp.dat

Now when you wish to repeat the experiment multiple times, you can use the --repeat option, but:

perf stat --repeat 50 prgrm < inp.dat

doesn't work. I understand the reason: the inp.dat is "piped" through perf and in the second run, the first run has already "consumed" the entire file so the input fed to stdin has reached its end.

Is it however possible, to repeatedly run the experiment by each time reading from inp.dat from the beginning?

1 Answer 1

2

If your program doesn't have a way to take an input filename, then you probably want to use the shell inside of each perf stat iteration. I'd just drop the whole thing you want into a little shell script:

test-prgrm.sh:

#!/bin/sh
prgrm < inp.data

then run with:

perf stat --repeat 50 ./test-prgrm.sh

But you can do it even without the intermediate shell script file using sh -c:

perf stat --repeat 50 sh -c 'prgrm < inp.data'

perf by default keeps the counters on for any children of the process it invokes. You may get some samples within the shell execution, but presumably it's minor overhead compared to running of your program.

1
  • Yes, that works. Only problem is perhaps the small overhead of sh that will play a part in the statistics. But I agree it is only a fraction. May 31, 2015 at 2:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .