Skip to content

Commit d4f63a4

Browse files
olsajiriacmel
authored andcommitted
perf stat: Introduce print_counters function
Centralize counters print code into single print_counters function. Signed-off-by: Jiri Olsa <[email protected]> Cc: Adrian Hunter <[email protected]> Cc: Andi Kleen <[email protected]> Cc: David Ahern <[email protected]> Cc: Namhyung Kim <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Stephane Eranian <[email protected]> Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent 5835e22 commit d4f63a4

File tree

1 file changed

+64
-63
lines changed

1 file changed

+64
-63
lines changed

tools/perf/builtin-stat.c

Lines changed: 64 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,7 @@
6767
#define CNTR_NOT_SUPPORTED "<not supported>"
6868
#define CNTR_NOT_COUNTED "<not counted>"
6969

70-
static void print_stat(int argc, const char **argv);
71-
static void print_counter_aggr(struct perf_evsel *counter, char *prefix);
72-
static void print_counter(struct perf_evsel *counter, char *prefix);
73-
static void print_aggr(char *prefix);
70+
static void print_counters(struct timespec *ts, int argc, const char **argv);
7471

7572
/* Default events used for perf stat -T */
7673
static const char *transaction_attrs = {
@@ -365,53 +362,14 @@ static void read_counters(bool close)
365362

366363
static void process_interval(void)
367364
{
368-
static int num_print_interval;
369-
struct perf_evsel *counter;
370365
struct timespec ts, rs;
371-
char prefix[64];
372366

373367
read_counters(false);
374368

375369
clock_gettime(CLOCK_MONOTONIC, &ts);
376370
diff_timespec(&rs, &ts, &ref_time);
377-
sprintf(prefix, "%6lu.%09lu%s", rs.tv_sec, rs.tv_nsec, csv_sep);
378-
379-
if (num_print_interval == 0 && !csv_output) {
380-
switch (aggr_mode) {
381-
case AGGR_SOCKET:
382-
fprintf(output, "# time socket cpus counts %*s events\n", unit_width, "unit");
383-
break;
384-
case AGGR_CORE:
385-
fprintf(output, "# time core cpus counts %*s events\n", unit_width, "unit");
386-
break;
387-
case AGGR_NONE:
388-
fprintf(output, "# time CPU counts %*s events\n", unit_width, "unit");
389-
break;
390-
case AGGR_GLOBAL:
391-
default:
392-
fprintf(output, "# time counts %*s events\n", unit_width, "unit");
393-
}
394-
}
395-
396-
if (++num_print_interval == 25)
397-
num_print_interval = 0;
398371

399-
switch (aggr_mode) {
400-
case AGGR_CORE:
401-
case AGGR_SOCKET:
402-
print_aggr(prefix);
403-
break;
404-
case AGGR_NONE:
405-
evlist__for_each(evsel_list, counter)
406-
print_counter(counter, prefix);
407-
break;
408-
case AGGR_GLOBAL:
409-
default:
410-
evlist__for_each(evsel_list, counter)
411-
print_counter_aggr(counter, prefix);
412-
}
413-
414-
fflush(output);
372+
print_counters(&rs, 0, NULL);
415373
}
416374

417375
static void handle_initial_delay(void)
@@ -901,9 +859,35 @@ static void print_counter(struct perf_evsel *counter, char *prefix)
901859
}
902860
}
903861

904-
static void print_stat(int argc, const char **argv)
862+
static void print_interval(char *prefix, struct timespec *ts)
863+
{
864+
static int num_print_interval;
865+
866+
sprintf(prefix, "%6lu.%09lu%s", ts->tv_sec, ts->tv_nsec, csv_sep);
867+
868+
if (num_print_interval == 0 && !csv_output) {
869+
switch (aggr_mode) {
870+
case AGGR_SOCKET:
871+
fprintf(output, "# time socket cpus counts %*s events\n", unit_width, "unit");
872+
break;
873+
case AGGR_CORE:
874+
fprintf(output, "# time core cpus counts %*s events\n", unit_width, "unit");
875+
break;
876+
case AGGR_NONE:
877+
fprintf(output, "# time CPU counts %*s events\n", unit_width, "unit");
878+
break;
879+
case AGGR_GLOBAL:
880+
default:
881+
fprintf(output, "# time counts %*s events\n", unit_width, "unit");
882+
}
883+
}
884+
885+
if (++num_print_interval == 25)
886+
num_print_interval = 0;
887+
}
888+
889+
static void print_header(int argc, const char **argv)
905890
{
906-
struct perf_evsel *counter;
907891
int i;
908892

909893
fflush(stdout);
@@ -929,36 +913,53 @@ static void print_stat(int argc, const char **argv)
929913
fprintf(output, " (%d runs)", run_count);
930914
fprintf(output, ":\n\n");
931915
}
916+
}
917+
918+
static void print_footer(void)
919+
{
920+
if (!null_run)
921+
fprintf(output, "\n");
922+
fprintf(output, " %17.9f seconds time elapsed",
923+
avg_stats(&walltime_nsecs_stats)/1e9);
924+
if (run_count > 1) {
925+
fprintf(output, " ");
926+
print_noise_pct(stddev_stats(&walltime_nsecs_stats),
927+
avg_stats(&walltime_nsecs_stats));
928+
}
929+
fprintf(output, "\n\n");
930+
}
931+
932+
static void print_counters(struct timespec *ts, int argc, const char **argv)
933+
{
934+
struct perf_evsel *counter;
935+
char buf[64], *prefix = NULL;
936+
937+
if (interval)
938+
print_interval(prefix = buf, ts);
939+
else
940+
print_header(argc, argv);
932941

933942
switch (aggr_mode) {
934943
case AGGR_CORE:
935944
case AGGR_SOCKET:
936-
print_aggr(NULL);
945+
print_aggr(prefix);
937946
break;
938947
case AGGR_GLOBAL:
939948
evlist__for_each(evsel_list, counter)
940-
print_counter_aggr(counter, NULL);
949+
print_counter_aggr(counter, prefix);
941950
break;
942951
case AGGR_NONE:
943952
evlist__for_each(evsel_list, counter)
944-
print_counter(counter, NULL);
953+
print_counter(counter, prefix);
945954
break;
946955
default:
947956
break;
948957
}
949958

950-
if (!csv_output) {
951-
if (!null_run)
952-
fprintf(output, "\n");
953-
fprintf(output, " %17.9f seconds time elapsed",
954-
avg_stats(&walltime_nsecs_stats)/1e9);
955-
if (run_count > 1) {
956-
fprintf(output, " ");
957-
print_noise_pct(stddev_stats(&walltime_nsecs_stats),
958-
avg_stats(&walltime_nsecs_stats));
959-
}
960-
fprintf(output, "\n\n");
961-
}
959+
if (!interval && !csv_output)
960+
print_footer();
961+
962+
fflush(output);
962963
}
963964

964965
static volatile int signr = -1;
@@ -1407,13 +1408,13 @@ int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused)
14071408

14081409
status = run_perf_stat(argc, argv);
14091410
if (forever && status != -1) {
1410-
print_stat(argc, argv);
1411+
print_counters(NULL, argc, argv);
14111412
perf_stat__reset_stats();
14121413
}
14131414
}
14141415

14151416
if (!forever && status != -1 && !interval)
1416-
print_stat(argc, argv);
1417+
print_counters(NULL, argc, argv);
14171418

14181419
perf_evlist__free_stats(evsel_list);
14191420
out:

0 commit comments

Comments
 (0)