Skip to content

Commit d7e7a45

Browse files
Stephane Eranianacmel
authored andcommitted
perf stat: Add per processor socket count aggregation
This patch adds per-processor socket count aggregation for system-wide mode measurements. This is a useful mode to detect imbalance between sockets. To enable this mode, use --aggr-socket in addition to -a. (system-wide). The output includes the socket number and the number of online processors on that socket. This is useful to gauge the amount of aggregation. # ./perf stat -I 1000 -a --aggr-socket -e cycles sleep 2 # time socket cpus counts events 1.000097680 S0 4 5,788,785 cycles 2.000379943 S0 4 27,361,546 cycles 2.001167808 S0 4 818,275 cycles Signed-off-by: Stephane Eranian <eranian@google.com> Cc: Andi Kleen <ak@linux.intel.com> Cc: Ingo Molnar <mingo@elte.hu> Cc: Jiri Olsa <jolsa@redhat.com> Cc: Namhyung Kim <namhyung.kim@lge.com> Cc: Peter Zijlstra <peterz@infradead.org> Link: http://lkml.kernel.org/r/1360161962-9675-3-git-send-email-eranian@google.com [ committer note: Added missing man page entry based on above comments ] Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent 5ac59a8 commit d7e7a45

2 files changed

Lines changed: 123 additions & 12 deletions

File tree

‎tools/perf/Documentation/perf-stat.txt‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,16 @@ perf stat --repeat 10 --null --sync --pre 'make -s O=defconfig-build/clean' -- m
116116

117117
-I msecs::
118118
--interval-print msecs::
119-
print count deltas every N milliseconds (minimum: 100ms)
119+
Print count deltas every N milliseconds (minimum: 100ms)
120120
example: perf stat -I 1000 -e cycles -a sleep 5
121121

122+
--aggr-socket::
123+
Aggregate counts per processor socket for system-wide mode measurements. This
124+
is a useful mode to detect imbalance between sockets. To enable this mode,
125+
use --aggr-socket in addition to -a. (system-wide). The output includes the
126+
socket number and the number of online processors on that socket. This is
127+
useful to gauge the amount of aggregation.
128+
122129
EXAMPLES
123130
--------
124131

‎tools/perf/builtin-stat.c‎

Lines changed: 115 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
static void print_stat(int argc, const char **argv);
6969
static void print_counter_aggr(struct perf_evsel *counter, char *prefix);
7070
static void print_counter(struct perf_evsel *counter, char *prefix);
71+
static void print_aggr_socket(char *prefix);
7172

7273
static struct perf_evlist *evsel_list;
7374

@@ -79,6 +80,7 @@ static int run_count = 1;
7980
static bool no_inherit = false;
8081
static bool scale = true;
8182
static bool no_aggr = false;
83+
static bool aggr_socket = false;
8284
static pid_t child_pid = -1;
8385
static bool null_run = false;
8486
static int detailed_run = 0;
@@ -93,6 +95,7 @@ static const char *post_cmd = NULL;
9395
static bool sync_run = false;
9496
static unsigned int interval = 0;
9597
static struct timespec ref_time;
98+
static struct cpu_map *sock_map;
9699

97100
static volatile int done = 0;
98101

@@ -312,7 +315,9 @@ static void print_interval(void)
312315
sprintf(prefix, "%6lu.%09lu%s", rs.tv_sec, rs.tv_nsec, csv_sep);
313316

314317
if (num_print_interval == 0 && !csv_output) {
315-
if (no_aggr)
318+
if (aggr_socket)
319+
fprintf(output, "# time socket cpus counts events\n");
320+
else if (no_aggr)
316321
fprintf(output, "# time CPU counts events\n");
317322
else
318323
fprintf(output, "# time counts events\n");
@@ -321,7 +326,9 @@ static void print_interval(void)
321326
if (++num_print_interval == 25)
322327
num_print_interval = 0;
323328

324-
if (no_aggr) {
329+
if (aggr_socket)
330+
print_aggr_socket(prefix);
331+
else if (no_aggr) {
325332
list_for_each_entry(counter, &evsel_list->entries, node)
326333
print_counter(counter, prefix);
327334
} else {
@@ -349,6 +356,12 @@ static int __run_perf_stat(int argc __maybe_unused, const char **argv)
349356
ts.tv_nsec = 0;
350357
}
351358

359+
if (aggr_socket
360+
&& cpu_map__build_socket_map(evsel_list->cpus, &sock_map)) {
361+
perror("cannot build socket map");
362+
return -1;
363+
}
364+
352365
if (forks && (pipe(child_ready_pipe) < 0 || pipe(go_pipe) < 0)) {
353366
perror("failed to create pipes");
354367
return -1;
@@ -529,13 +542,21 @@ static void print_noise(struct perf_evsel *evsel, double avg)
529542
print_noise_pct(stddev_stats(&ps->res_stats[0]), avg);
530543
}
531544

532-
static void nsec_printout(int cpu, struct perf_evsel *evsel, double avg)
545+
static void nsec_printout(int cpu, int nr, struct perf_evsel *evsel, double avg)
533546
{
534547
double msecs = avg / 1e6;
535548
char cpustr[16] = { '\0', };
536549
const char *fmt = csv_output ? "%s%.6f%s%s" : "%s%18.6f%s%-25s";
537550

538-
if (no_aggr)
551+
if (aggr_socket)
552+
sprintf(cpustr, "S%*d%s%*d%s",
553+
csv_output ? 0 : -5,
554+
cpu,
555+
csv_sep,
556+
csv_output ? 0 : 4,
557+
nr,
558+
csv_sep);
559+
else if (no_aggr)
539560
sprintf(cpustr, "CPU%*d%s",
540561
csv_output ? 0 : -4,
541562
perf_evsel__cpus(evsel)->map[cpu], csv_sep);
@@ -734,7 +755,7 @@ static void print_ll_cache_misses(int cpu,
734755
fprintf(output, " of all LL-cache hits ");
735756
}
736757

737-
static void abs_printout(int cpu, struct perf_evsel *evsel, double avg)
758+
static void abs_printout(int cpu, int nr, struct perf_evsel *evsel, double avg)
738759
{
739760
double total, ratio = 0.0;
740761
char cpustr[16] = { '\0', };
@@ -747,7 +768,15 @@ static void abs_printout(int cpu, struct perf_evsel *evsel, double avg)
747768
else
748769
fmt = "%s%18.0f%s%-25s";
749770

750-
if (no_aggr)
771+
if (aggr_socket)
772+
sprintf(cpustr, "S%*d%s%*d%s",
773+
csv_output ? 0 : -5,
774+
cpu,
775+
csv_sep,
776+
csv_output ? 0 : 4,
777+
nr,
778+
csv_sep);
779+
else if (no_aggr)
751780
sprintf(cpustr, "CPU%*d%s",
752781
csv_output ? 0 : -4,
753782
perf_evsel__cpus(evsel)->map[cpu], csv_sep);
@@ -853,6 +882,70 @@ static void abs_printout(int cpu, struct perf_evsel *evsel, double avg)
853882
}
854883
}
855884

885+
static void print_aggr_socket(char *prefix)
886+
{
887+
struct perf_evsel *counter;
888+
u64 ena, run, val;
889+
int cpu, s, s2, sock, nr;
890+
891+
if (!sock_map)
892+
return;
893+
894+
for (s = 0; s < sock_map->nr; s++) {
895+
sock = cpu_map__socket(sock_map, s);
896+
list_for_each_entry(counter, &evsel_list->entries, node) {
897+
val = ena = run = 0;
898+
nr = 0;
899+
for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
900+
s2 = cpu_map__get_socket(evsel_list->cpus, cpu);
901+
if (s2 != sock)
902+
continue;
903+
val += counter->counts->cpu[cpu].val;
904+
ena += counter->counts->cpu[cpu].ena;
905+
run += counter->counts->cpu[cpu].run;
906+
nr++;
907+
}
908+
if (prefix)
909+
fprintf(output, "%s", prefix);
910+
911+
if (run == 0 || ena == 0) {
912+
fprintf(output, "S%*d%s%*d%s%*s%s%*s",
913+
csv_output ? 0 : -5,
914+
s,
915+
csv_sep,
916+
csv_output ? 0 : 4,
917+
nr,
918+
csv_sep,
919+
csv_output ? 0 : 18,
920+
counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
921+
csv_sep,
922+
csv_output ? 0 : -24,
923+
perf_evsel__name(counter));
924+
if (counter->cgrp)
925+
fprintf(output, "%s%s",
926+
csv_sep, counter->cgrp->name);
927+
928+
fputc('\n', output);
929+
continue;
930+
}
931+
932+
if (nsec_counter(counter))
933+
nsec_printout(sock, nr, counter, val);
934+
else
935+
abs_printout(sock, nr, counter, val);
936+
937+
if (!csv_output) {
938+
print_noise(counter, 1.0);
939+
940+
if (run != ena)
941+
fprintf(output, " (%.2f%%)",
942+
100.0 * run / ena);
943+
}
944+
fputc('\n', output);
945+
}
946+
}
947+
}
948+
856949
/*
857950
* Print out the results of a single counter:
858951
* aggregated counts in system-wide mode
@@ -882,9 +975,9 @@ static void print_counter_aggr(struct perf_evsel *counter, char *prefix)
882975
}
883976

884977
if (nsec_counter(counter))
885-
nsec_printout(-1, counter, avg);
978+
nsec_printout(-1, 0, counter, avg);
886979
else
887-
abs_printout(-1, counter, avg);
980+
abs_printout(-1, 0, counter, avg);
888981

889982
print_noise(counter, avg);
890983

@@ -940,9 +1033,9 @@ static void print_counter(struct perf_evsel *counter, char *prefix)
9401033
}
9411034

9421035
if (nsec_counter(counter))
943-
nsec_printout(cpu, counter, val);
1036+
nsec_printout(cpu, 0, counter, val);
9441037
else
945-
abs_printout(cpu, counter, val);
1038+
abs_printout(cpu, 0, counter, val);
9461039

9471040
if (!csv_output) {
9481041
print_noise(counter, 1.0);
@@ -980,7 +1073,9 @@ static void print_stat(int argc, const char **argv)
9801073
fprintf(output, ":\n\n");
9811074
}
9821075

983-
if (no_aggr) {
1076+
if (aggr_socket)
1077+
print_aggr_socket(NULL);
1078+
else if (no_aggr) {
9841079
list_for_each_entry(counter, &evsel_list->entries, node)
9851080
print_counter(counter, NULL);
9861081
} else {
@@ -1228,6 +1323,7 @@ int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused)
12281323
"command to run after to the measured command"),
12291324
OPT_UINTEGER('I', "interval-print", &interval,
12301325
"print counts at regular interval in ms (>= 100)"),
1326+
OPT_BOOLEAN(0, "aggr-socket", &aggr_socket, "aggregate counts per processor socket"),
12311327
OPT_END()
12321328
};
12331329
const char * const stat_usage[] = {
@@ -1314,6 +1410,14 @@ int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused)
13141410
usage_with_options(stat_usage, options);
13151411
}
13161412

1413+
if (aggr_socket) {
1414+
if (!perf_target__has_cpu(&target)) {
1415+
fprintf(stderr, "--aggr-socket only available in system-wide mode (-a)\n");
1416+
usage_with_options(stat_usage, options);
1417+
}
1418+
no_aggr = true;
1419+
}
1420+
13171421
if (add_default_attributes())
13181422
goto out;
13191423

0 commit comments

Comments
 (0)