Skip to contentSkip to conversation

perf stat: Support metrics with perf stat --per-thread

PATCH In review Jin Yao 2 participants 6 messages Oct 12, 2017 perf events

lore
Share & tools
lore

Jin Yao

author
#0 lore raw

PATCH 1/1 perf stat: Support metrics with perf stat --per-thread

perf stat --per-thread doesn't support outputting metrics, like IPC.

We should support this because it would allow easily to collect metrics for different threads in applications.

1. Current output, for example:

root@skl:/tmp# perf stat --per-thread -p 21623
^C
 Performance counter stats for process id '21623':
          vmstat-21623              0.517479      task-clock (msec)         #    0.000 CPUs utilized
          vmstat-21623                     1      context-switches
          vmstat-21623                     0      cpu-migrations
          vmstat-21623                     0      page-faults
          vmstat-21623               461,306      cycles
          vmstat-21623               630,724      instructions
          vmstat-21623               136,265      branches
          vmstat-21623                 2,520      branch-misses
       1.444020756 seconds time elapsed
root@skl:/tmp# perf stat --per-thread --metrics ipc -p 21623
^C
 Performance counter stats for process id '21623':
          vmstat-21623               631,185      inst_retired.any
          vmstat-21623               605,893      cpu_clk_unhalted.thread
       1.415679293 seconds time elapsed

2. With this patch, the result would be:

root@skl:/tmp# perf stat --per-thread -p 21623
^C
 Performance counter stats for process id '21623':
          vmstat-21623              0.533759      task-clock (msec)         #    0.000 CPUs utilized
          vmstat-21623                     1      context-switches          #    0.002 M/sec
          vmstat-21623                     0      cpu-migrations            #    0.000 K/sec
          vmstat-21623                     0      page-faults               #    0.000 K/sec
          vmstat-21623               473,896      cycles                    #    0.888 GHz
          vmstat-21623               631,072      instructions              #    1.33  insn per cycle
          vmstat-21623               136,307      branches                  #  255.372 M/sec
          vmstat-21623                 2,524      branch-misses             #    1.85% of all branches
       1.544862861 seconds time elapsed
root@skl:/tmp# perf stat --per-thread --metrics ipc -p 21623
^C
 Performance counter stats for process id '21623':
          vmstat-21623             1,259,104      inst_retired.any          #      1.2 IPC
          vmstat-21623             1,056,756      cpu_clk_unhalted.thread
       2.040954502 seconds time elapsed
---
 tools/perf/util/stat.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
tools/perf/util/stat.c +23 -0
index 35e9848..1164e68 100644
@@ -314,6 +314,26 @@ static int process_counter_maps(struct perf_stat_config *config,
 	return 0;
 }
 
+static int process_aggr_thread_counter(struct perf_evsel *counter)
+{
+	int nthreads = thread_map__nr(counter->threads);
+	int ncpus = cpu_map__nr(counter->cpus);
+	int cpu, thread;
+	u64 tmp;
+
+	for (thread = 0; thread < nthreads; thread++) {
+		u64 val = 0;
+
+		for (cpu = 0; cpu < ncpus; cpu++)
+			val += perf_counts(counter->counts, cpu, thread)->val;
+
+		tmp = val * counter->scale;
+		perf_stat__update_shadow_stats(counter, &tmp, 0);
+	}
+
+	return 0;
+}
+
 int perf_stat_process_counter(struct perf_stat_config *config,
 			      struct perf_evsel *counter)
 {
@@ -342,6 +362,9 @@ int perf_stat_process_counter(struct perf_stat_config *config,
 	if (ret)
 		return ret;
 
+	if (config->aggr_mode == AGGR_THREAD)
+		return process_aggr_thread_counter(counter);
+
 	if (config->aggr_mode != AGGR_GLOBAL)
 		return 0;
 
Signature · 1 line
2.7.4
#1 lore raw

On Thu, Oct 12, 2017 at 05:05:00PM +0800, Jin Yao wrote:

SNIP

Quoted 49 lines
---
 tools/perf/util/stat.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
tools/perf/util/stat.c +23 -0
index 35e9848..1164e68 100644
@@ -314,6 +314,26 @@ static int process_counter_maps(struct perf_stat_config *config,
 	return 0;
 }
 
+static int process_aggr_thread_counter(struct perf_evsel *counter)
+{
+	int nthreads = thread_map__nr(counter->threads);
+	int ncpus = cpu_map__nr(counter->cpus);
+	int cpu, thread;
+	u64 tmp;
+
+	for (thread = 0; thread < nthreads; thread++) {
+		u64 val = 0;
+
+		for (cpu = 0; cpu < ncpus; cpu++)
+			val += perf_counts(counter->counts, cpu, thread)->val;
+
+		tmp = val * counter->scale;
+		perf_stat__update_shadow_stats(counter, &tmp, 0);
+	}
+
+	return 0;
+}
+
 int perf_stat_process_counter(struct perf_stat_config *config,
 			      struct perf_evsel *counter)
 {
@@ -342,6 +362,9 @@ int perf_stat_process_counter(struct perf_stat_config *config,
 	if (ret)
 		return ret;
 
+	if (config->aggr_mode == AGGR_THREAD)
+		return process_aggr_thread_counter(counter);
+
 	if (config->aggr_mode != AGGR_GLOBAL)
 		return 0;
 
Signature · 1 line
2.7.4

I wonder if patch below would be enough for this

jirka

---

tools/perf/util/stat.c +4 -0
index 35e9848734d6..56a0a36d0653 100644
@@ -259,6 +259,7 @@ process_counter_values(struct perf_stat_config *config, struct perf_evsel *evsel
 	struct perf_counts_values *aggr = &evsel->counts->aggr;
 	static struct perf_counts_values zero;
 	bool skip = false;
+	u64 val;
 
 	if (check_per_pkg(evsel, count, cpu, &skip)) {
 		pr_err("failed to read per-pkg counter\n");
@@ -270,6 +271,9 @@ process_counter_values(struct perf_stat_config *config, struct perf_evsel *evsel
 
 	switch (config->aggr_mode) {
 	case AGGR_THREAD:
+		val = count->val * evsel->scale;
+		perf_stat__update_shadow_stats(evsel, &val, 0);
+		break;
 	case AGGR_CORE:
 	case AGGR_SOCKET:
 	case AGGR_NONE:
author
#2 lore raw
Quoted 6 lines

I wonder if patch below would be enough for this

jirka

Hi Jiri,

I think below patch and next patch (in your another mail) is OK, thanks!

Do I need to upgrade my patch to v2 according to your modifications?

Or you will post your patches? Both OK for me. :)

Thanks
Jin Yao

Quoted 24 lines

---

tools/perf/util/stat.c +4 -0
index 35e9848734d6..56a0a36d0653 100644
@@ -259,6 +259,7 @@ process_counter_values(struct perf_stat_config *config, struct perf_evsel *evsel
  	struct perf_counts_values *aggr = &evsel->counts->aggr;
  	static struct perf_counts_values zero;
  	bool skip = false;
+	u64 val;
 
  	if (check_per_pkg(evsel, count, cpu, &skip)) {
  		pr_err("failed to read per-pkg counter\n");
@@ -270,6 +271,9 @@ process_counter_values(struct perf_stat_config *config, struct perf_evsel *evsel
 
  	switch (config->aggr_mode) {
  	case AGGR_THREAD:
+		val = count->val * evsel->scale;
+		perf_stat__update_shadow_stats(evsel, &val, 0);
+		break;
  	case AGGR_CORE:
  	case AGGR_SOCKET:
  	case AGGR_NONE:
#3 lore raw
Quoted 7 lines · Yao
Quoted 6 lines

I wonder if patch below would be enough for this

jirka

Quoted 7 lines

Hi Jiri,

I think below patch and next patch (in your another mail) is OK, thanks!

Do I need to upgrade my patch to v2 according to your modifications?

Or you will post your patches? Both OK for me. :)

I can post it later this week with my other fixes

jirka

author
#4 lore raw
Quoted 12 lines · Jiri Olsa
Quoted 11 lines · Yao
Quoted 6 lines

I wonder if patch below would be enough for this

jirka

Hi Jiri,

I think below patch and next patch (in your another mail) is OK, thanks!

Quoted 8 lines
Quoted 3 lines

Do I need to upgrade my patch to v2 according to your modifications?

Or you will post your patches? Both OK for me. :)

I can post it later this week with my other fixes

jirka

Thanks!

#5 lore raw
In reply to Jin Yao

On Thu, Oct 12, 2017 at 05:05:00PM +0800, Jin Yao wrote:

SNIP

Quoted 17 lines
---
 tools/perf/util/stat.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
tools/perf/util/stat.c +5 -0
index 35e9848..1164e68 100644
@@ -314,6 +314,26 @@ static int process_counter_maps(struct perf_stat_config *config,
 	return 0;
 }
 
+static int process_aggr_thread_counter(struct perf_evsel *counter)
+{
+	int nthreads = thread_map__nr(counter->threads);
+	int ncpus = cpu_map__nr(counter->cpus);
+	int cpu, thread;

tools/perf/util/stat.c static int process_counter_maps(struct perf_stat_config *config, · line 331

Quoted 10 lines
tools/perf/util/stat.c +10 -0
+	u64 tmp;
+
+	for (thread = 0; thread < nthreads; thread++) {
+		u64 val = 0;
+
+		for (cpu = 0; cpu < ncpus; cpu++)
+			val += perf_counts(counter->counts, cpu, thread)->val;
+
+		tmp = val * counter->scale;
+		perf_stat__update_shadow_stats(counter, &tmp, 0);

also maybe we should move scale mult. into perf_stat__update_shadow_stats, something like below (on top of my earlier patch)

not much tested so far.. but looks ok ;-)

jirka

---

tools/perf/builtin-stat.c +1 -2
index 9f2f07c0237d..4e109e6ba341 100644
@@ -1267,8 +1267,7 @@ static void aggr_update_shadow(void)
 					continue;
 				val += perf_counts(counter->counts, cpu, 0)->val;
 			}
-			val = val * counter->scale;
-			perf_stat__update_shadow_stats(counter, &val,
+			perf_stat__update_shadow_stats(counter, val,
 						       first_shadow_cpu(counter, id));
 		}
 	}
tools/perf/util/stat-shadow.c +25 -23

83 lines not on this page · raw message

tools/perf/util/stat.c +4 -9
index 6f94f3a2282e..c1cf42d04826 100644
@@ -259,7 +259,6 @@ process_counter_values(struct perf_stat_config *config, struct perf_evsel *evsel
 	struct perf_counts_values *aggr = &evsel->counts->aggr;
 	static struct perf_counts_values zero;
 	bool skip = false;
-	u64 val;
 
 	if (check_per_pkg(evsel, count, cpu, &skip)) {
 		pr_err("failed to read per-pkg counter\n");
@@ -278,11 +277,9 @@ process_counter_values(struct perf_stat_config *config, struct perf_evsel *evsel
 			perf_evsel__compute_deltas(evsel, cpu, thread, count);
 		perf_counts_values__scale(count, config->scale, NULL);
 		if (config->aggr_mode == AGGR_NONE)
-			perf_stat__update_shadow_stats(evsel, count->values, cpu);
-		if (config->aggr_mode == AGGR_THREAD) {
-			val = count->val * evsel->scale;
-			perf_stat__update_shadow_stats(evsel, &val, 0);
-		}
+			perf_stat__update_shadow_stats(evsel, count->val, cpu);
+		if (config->aggr_mode == AGGR_THREAD)
+			perf_stat__update_shadow_stats(evsel, count->val, 0);
 		break;
 	case AGGR_GLOBAL:
 		aggr->val += count->val;
@@ -325,7 +322,6 @@ int perf_stat_process_counter(struct perf_stat_config *config,
 	struct perf_counts_values *aggr = &counter->counts->aggr;
 	struct perf_stat_evsel *ps = counter->priv;
 	u64 *count = counter->counts->aggr.values;
-	u64 val;
 	int i, ret;
 
 	aggr->val = aggr->ena = aggr->run = 0;
@@ -365,8 +361,7 @@ int perf_stat_process_counter(struct perf_stat_config *config,
 	/*
 	 * Save the full runtime - to allow normalization during printout:
 	 */
-	val = counter->scale * *count;
-	perf_stat__update_shadow_stats(counter, &val, 0);
+	perf_stat__update_shadow_stats(counter, *count, 0);
 
 	return 0;
 }
tools/perf/util/stat.h +1 -1
index 47915df346fb..490b78aa7230 100644
@@ -82,7 +82,7 @@ typedef void (*new_line_t )(void *ctx);
 
 void perf_stat__init_shadow_stats(void);
 void perf_stat__reset_shadow_stats(void);
-void perf_stat__update_shadow_stats(struct perf_evsel *counter, u64 *count,
+void perf_stat__update_shadow_stats(struct perf_evsel *counter, u64 count,
 				    int cpu);
 struct perf_stat_output_ctx {
 	void *ctx;