Skip to content

Commit 1f16c57

Browse files
Peter Zijlstraacmel
authored andcommitted
perf stat: Add --pre and --post command
In order to measure kernel builds, one has to do some pre/post cleanup work in order to do the repeat build. So provide --pre and --post command hooks to allow doing just that. perf stat --repeat 10 --null --sync --pre 'make -s O=defconfig-build/clean' \ -- make -s -j64 O=defconfig-build/ bzImage Signed-off-by: Peter Zijlstra <[email protected]> Acked-by: Ingo Molnar <[email protected]> Cc: Stephane Eranian <[email protected]> Link: http://lkml.kernel.org/r/1350992414.13456.5.camel@twins [ committer note: Added respective entries in Documentation/perf-stat.txt ] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent 54a3cf5 commit 1f16c57

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

tools/perf/Documentation/perf-stat.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,11 @@ with it. --append may be used here. Examples:
108108
3>results perf stat --log-fd 3 -- $cmd
109109
3>>results perf stat --log-fd 3 --append -- $cmd
110110

111+
--pre::
112+
--post::
113+
Pre and post measurement hooks, e.g.:
114+
115+
perf stat --repeat 10 --null --sync --pre 'make -s O=defconfig-build/clean' -- make -s -j64 O=defconfig-build/ bzImage
111116

112117

113118
EXAMPLES

tools/perf/builtin-stat.c

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
#include "util/thread.h"
5858
#include "util/thread_map.h"
5959

60+
#include <stdlib.h>
6061
#include <sys/prctl.h>
6162
#include <locale.h>
6263

@@ -83,6 +84,9 @@ static const char *csv_sep = NULL;
8384
static bool csv_output = false;
8485
static bool group = false;
8586
static FILE *output = NULL;
87+
static const char *pre_cmd = NULL;
88+
static const char *post_cmd = NULL;
89+
static bool sync_run = false;
8690

8791
static volatile int done = 0;
8892

@@ -265,7 +269,7 @@ static int read_counter(struct perf_evsel *counter)
265269
return 0;
266270
}
267271

268-
static int run_perf_stat(int argc __maybe_unused, const char **argv)
272+
static int __run_perf_stat(int argc __maybe_unused, const char **argv)
269273
{
270274
unsigned long long t0, t1;
271275
struct perf_evsel *counter, *first;
@@ -405,6 +409,32 @@ static int run_perf_stat(int argc __maybe_unused, const char **argv)
405409
return WEXITSTATUS(status);
406410
}
407411

412+
static int run_perf_stat(int argc __maybe_unused, const char **argv)
413+
{
414+
int ret;
415+
416+
if (pre_cmd) {
417+
ret = system(pre_cmd);
418+
if (ret)
419+
return ret;
420+
}
421+
422+
if (sync_run)
423+
sync();
424+
425+
ret = __run_perf_stat(argc, argv);
426+
if (ret)
427+
return ret;
428+
429+
if (post_cmd) {
430+
ret = system(post_cmd);
431+
if (ret)
432+
return ret;
433+
}
434+
435+
return ret;
436+
}
437+
408438
static void print_noise_pct(double total, double avg)
409439
{
410440
double pct = rel_stddev_stats(total, avg);
@@ -1069,8 +1099,7 @@ static int add_default_attributes(void)
10691099

10701100
int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused)
10711101
{
1072-
bool append_file = false,
1073-
sync_run = false;
1102+
bool append_file = false;
10741103
int output_fd = 0;
10751104
const char *output_name = NULL;
10761105
const struct option options[] = {
@@ -1114,6 +1143,10 @@ int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused)
11141143
OPT_BOOLEAN(0, "append", &append_file, "append to the output file"),
11151144
OPT_INTEGER(0, "log-fd", &output_fd,
11161145
"log output to fd, instead of stderr"),
1146+
OPT_STRING(0, "pre", &pre_cmd, "command",
1147+
"command to run prior to the measured command"),
1148+
OPT_STRING(0, "post", &post_cmd, "command",
1149+
"command to run after to the measured command"),
11171150
OPT_END()
11181151
};
11191152
const char * const stat_usage[] = {
@@ -1238,9 +1271,6 @@ int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused)
12381271
fprintf(output, "[ perf stat: executing run #%d ... ]\n",
12391272
run_idx + 1);
12401273

1241-
if (sync_run)
1242-
sync();
1243-
12441274
status = run_perf_stat(argc, argv);
12451275
}
12461276

0 commit comments

Comments
 (0)