Skip to contentSkip to conversation

perf, tools: Refactor and support interval and CSV metrics

PATCH In review Andi Kleen 2 participants 13 messages Jul 30, 2015 – Aug 8, 2015 perf events

lore
Share & tools
lore

Andi Kleen

author
#0 lore raw

Currently perf stat does not support printing computed metrics for interval (-I xxx) or CSV (-x,) mode. For example IPC or TSX metrics over time are quite useful to know.

This patch implements them. The main obstacle was that the
metrics printing was all open coded all over the metrics computation code. The second patch refactors the metrics printing to work through call backs that can be more easily changed. This also cleans up the metrics printing significantly. The indentation is now handled through printf, no more need to manually count spaces.

Then based on that it implements metrics printing for CSV and interval mode.

Example output:

% perf stat -I1000 -a sleep 1

#          time              counts unit events                    metric                              multiplex
     1.001301370       12020.049593      task-clock (msec)                                             (100.00%)
     1.001301370              3,952      context-switches          #    0.329 K/sec                    (100.00%)
     1.001301370                 69      cpu-migrations            #    0.006 K/sec                    (100.00%)
     1.001301370                 76      page-faults               #    0.006 K/sec                  
     1.001301370        386,582,789      cycles                    #    0.032 GHz                      (100.00%)
     1.001301370        716,441,544      stalled-cycles-frontend   #  185.33% frontend cycles idle     (100.00%)
     1.001301370    <not supported>      stalled-cycles-backend   
     1.001301370        101,751,678      instructions              #    0.26  insn per cycle         
     1.001301370                                                   #    7.04  stalled cycles per insn  (100.00%)
     1.001301370         20,914,692      branches                  #    1.740 M/sec                    (100.00%)
     1.001301370          1,943,630      branch-misses             #    9.29% of all branches        

CSV mode

% perf stat -x, -I1000 -a sleep 1

     1.000852081,12016.143006,,task-clock
     1.000852081,4457,,context-switches,12015168277,100.00,0.371,K/sec
     1.000852081,50,,cpu-migrations,12014024424,100.00,0.004,K/sec
     1.000852081,76,,page-faults,12013076716,100.00,0.006,K/sec
     1.000852081,515854373,,cycles,12011235336,100.00,0.043,GHz
     1.000852081,1030742150,,stalled-cycles-frontend,12010984057,100.00,199.81,frontend cycles idle
     1.000852081,<not supported>,,stalled-cycles-backend,0,100.00
     1.000852081,116782495,,instructions,12011130729,100.00,0.23,insn per cycle
     1.000852081,,,,12011130729,100.00,8.83,stalled cycles per insn
     1.000852081,23748237,,branches,12010745125,100.00,1.976,M/sec
     1.000852081,1976560,,branch-misses,12010501884,100.00,8.32,of all branches

Available in
git://git.kernel.org/pub/scm/linux/kernel/git/ak/linux-misc perf/stat-metrics

Note: for some of the --per-*/-A modes metrics are not printed correctly. That was already the case before, so I didn't change it. I think some of it may be related to Jiri's earlier stat changes.

author
#1 lore raw

PATCH 1/4 perf, tools: Do not include escape sequences in color_vfprintf return

From: Andi Kleen <[email protected]>

color_vprintf was including the length of the invisible escape sequences in its return argument. Don't include them to make the return value usable for indentation calculations.

---
 tools/perf/util/color.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
tools/perf/util/color.c +3 -3
index 55355b3..f3d1d6d 100644
@@ -83,12 +83,12 @@ static int __color_vfprintf(FILE *fp, const char *color, const char *fmt,
 	}
 
 	if (perf_use_color_default && *color)
-		r += fprintf(fp, "%s", color);
+		fprintf(fp, "%s", color);
 	r += vfprintf(fp, fmt, args);
 	if (perf_use_color_default && *color)
-		r += fprintf(fp, "%s", PERF_COLOR_RESET);
+		fprintf(fp, "%s", PERF_COLOR_RESET);
 	if (trail)
-		r += fprintf(fp, "%s", trail);
+		fprintf(fp, "%s", trail);
 	return r;
 }
 
Signature · 1 line
2.4.3
#2 lore raw

PATCH 1/1 perf tools: Remove color_fprintf_ln function

Quoted 16 lines · Andi Kleen

From: Andi Kleen <[email protected]>

color_vprintf was including the length of the invisible escape sequences in its return argument. Don't include them to make the return value usable for indentation calculations.

---
 tools/perf/util/color.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
tools/perf/util/color.c +0 -0
index 55355b3..f3d1d6d 100644

tools/perf/util/color.c static int __color_vfprintf(FILE *fp, const char *color, const char *fmt, · line 86

Quoted 5 lines
tools/perf/util/color.c +1 -1
@@ -83,12 +83,12 @@ static int __color_vfprintf(FILE *fp, const char *color, const char *fmt,
 	}
 
 	if (perf_use_color_default && *color)
-		r += fprintf(fp, "%s", color);
+		fprintf(fp, "%s", color);

could you please put in a comment saying we dont include colors in the return length..

tools/perf/util/color.c static int __color_vfprintf(FILE *fp, const char *color, const char *fmt, · line 91

Quoted 7 lines
tools/perf/util/color.c +2 -2
 	r += vfprintf(fp, fmt, args);
 	if (perf_use_color_default && *color)
-		r += fprintf(fp, "%s", PERF_COLOR_RESET);
+		fprintf(fp, "%s", PERF_COLOR_RESET);
 	if (trail)
-		r += fprintf(fp, "%s", trail);
+		fprintf(fp, "%s", trail);

trail's not color, right?

anyway seems like it's always '\n' through color_fprintf_ln, which is not used at all, removing.. ;-)

jirka

---
It's not used anymore.

---
 tools/perf/util/color.c | 16 ++--------------
 tools/perf/util/color.h |  1 -
 2 files changed, 2 insertions(+), 15 deletions(-)
tools/perf/util/color.c +2 -14
index 55355b3d4f85..ff051d24a0bb 100644
@@ -68,7 +68,7 @@ static int __color_vsnprintf(char *bf, size_t size, const char *color,
 }
 
 static int __color_vfprintf(FILE *fp, const char *color, const char *fmt,
-		va_list args, const char *trail)
+		va_list args)
 {
 	int r = 0;
 
@@ -87,8 +87,6 @@ static int __color_vfprintf(FILE *fp, const char *color, const char *fmt,
 	r += vfprintf(fp, fmt, args);
 	if (perf_use_color_default && *color)
 		r += fprintf(fp, "%s", PERF_COLOR_RESET);
-	if (trail)
-		r += fprintf(fp, "%s", trail);
 	return r;
 }
 
@@ -100,7 +98,7 @@ int color_vsnprintf(char *bf, size_t size, const char *color,
 
 int color_vfprintf(FILE *fp, const char *color, const char *fmt, va_list args)
 {
-	return __color_vfprintf(fp, color, fmt, args, NULL);
+	return __color_vfprintf(fp, color, fmt, args);
 }
 
 int color_snprintf(char *bf, size_t size, const char *color,
@@ -126,16 +124,6 @@ int color_fprintf(FILE *fp, const char *color, const char *fmt, ...)
 	return r;
 }
 
-int color_fprintf_ln(FILE *fp, const char *color, const char *fmt, ...)
-{
-	va_list args;
-	int r;
-	va_start(args, fmt);
-	r = __color_vfprintf(fp, color, fmt, args, "\n");
-	va_end(args);
-	return r;
-}
-
 /*
  * This function splits the buffer by newlines and colors the lines individually.
  *
tools/perf/util/color.h +0 -1
index 38146f922c54..a93997f16dec 100644
@@ -35,7 +35,6 @@ int color_vsnprintf(char *bf, size_t size, const char *color,
 int color_vfprintf(FILE *fp, const char *color, const char *fmt, va_list args);
 int color_fprintf(FILE *fp, const char *color, const char *fmt, ...);
 int color_snprintf(char *bf, size_t size, const char *color, const char *fmt, ...);
-int color_fprintf_ln(FILE *fp, const char *color, const char *fmt, ...);
 int color_fwrite_lines(FILE *fp, const char *color, size_t count, const char *buf);
 int value_color_snprintf(char *bf, size_t size, const char *fmt, double value);
 int percent_color_snprintf(char *bf, size_t size, const char *fmt, ...);
Signature · 1 line
2.4.3
author
#3 lore raw

PATCH 2/4 perf, tools, stat: Abstract stat metrics printing

In reply to Andi Kleen

From: Andi Kleen <[email protected]>

Abstract the printing of shadow metrics. Instead of every metric calling fprintf directly and taking care of indentation, use two call backs: one to print metrics and another to start a new line.

This will allow adding metrics to CSV mode and also using them for other purposes.

The computation of padding is now done in the central callback, instead of every metric doing it manually. This makes it easier to add new metrics.

Right now there is no (intentional) behavior change, just refactoring.

---
 tools/perf/builtin-stat.c     |  98 +++++++++++++++++++--------
 tools/perf/util/stat-shadow.c | 154 ++++++++++++++++++++++--------------------
 tools/perf/util/stat.h        |  10 ++-
 3 files changed, 160 insertions(+), 102 deletions(-)
tools/perf/builtin-stat.c +71 -27
index d99d850..e6386f1 100644
@@ -617,7 +617,49 @@ static void aggr_printout(struct perf_evsel *evsel, int id, int nr)
 	}
 }
 
-static void nsec_printout(int id, int nr, struct perf_evsel *evsel, double avg)
+struct outstate {
+	FILE *fh;
+};
+
+#define BASE_INDENT 41
+#define AGGR_INDENT  8
+#define METRIC_LEN  35
+#define NA_INDENT   16
+
+static void new_line_no_aggr_std(void *ctx)
+{
+	struct outstate *os = ctx;
+	fprintf(os->fh, "\n%*s", BASE_INDENT + NA_INDENT, "");
+}
+
+static void new_line_std(void *ctx)
+{
+	struct outstate *os = ctx;
+	fprintf(os->fh, "\n%-*s", BASE_INDENT + AGGR_INDENT, "");
+}
+
+static void print_metric_std(void *ctx, const char *color, const char *fmt,
+			     const char *unit, double val)
+{
+	struct outstate *os = ctx;
+	FILE *out = os->fh;
+	int n;
+
+	if (unit == NULL) {
+		fprintf(out, "%-*s", METRIC_LEN, "");
+		return;
+	}
+
+	n = fprintf(out, " # ");
+	if (color)
+		n += color_fprintf(out, color, fmt, val);
+	else
+		n += fprintf(out, fmt, val);
+	fprintf(out, " %-*s", METRIC_LEN - n - 1, unit);
+}
+
+static void nsec_printout(int id, int nr, struct perf_evsel *evsel, double avg,
+			  void *ctx, print_metric_t print_metric)
 {
 	double msecs = avg / 1e6;
 	const char *fmt_v, *fmt_n;
@@ -647,13 +689,16 @@ static void nsec_printout(int id, int nr, struct perf_evsel *evsel, double avg)
 		return;
 
 	if (perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK))
-		fprintf(output, " # %8.3f CPUs utilized          ",
-			avg / avg_stats(&walltime_nsecs_stats));
+		print_metric(ctx, NULL, "%8.3f", "CPUs utilized",
+			     avg / avg_stats(&walltime_nsecs_stats));
 	else
-		fprintf(output, "                                   ");
+		print_metric(ctx, NULL, NULL, NULL, 0);
 }
 
-static void abs_printout(int id, int nr, struct perf_evsel *evsel, double avg)
+static void abs_printout(int id, int nr, struct perf_evsel *evsel, double avg,
+			 void *ctx,
+			 print_metric_t print_metric,
+			 void (*new_line)(void *))
 {
 	double sc =  evsel->scale;
 	const char *fmt;
@@ -688,7 +733,23 @@ static void abs_printout(int id, int nr, struct perf_evsel *evsel, double avg)
 	if (csv_output || interval)
 		return;
 
-	perf_stat__print_shadow_stats(output, evsel, avg, cpu, aggr_mode);
+	perf_stat__print_shadow_stats(evsel, avg, cpu,
+				      print_metric,
+				      new_line,
+				      ctx);
+}
+
+static void printout(int id, int nr, struct perf_evsel *counter, double uval)
+{
+	struct outstate os = { .fh = output };
+
+	if (nsec_counter(counter))
+		nsec_printout(id, nr, counter, uval, &os, print_metric_std);
+	else
+		abs_printout(id, nr, counter, uval, &os,
+			     print_metric_std,
+			     aggr_mode == AGGR_NONE ?
+			     new_line_no_aggr_std : new_line_std);
 }
 
 static void print_aggr(char *prefix)
@@ -744,12 +805,7 @@ static void print_aggr(char *prefix)
 				continue;
 			}
 			uval = val * counter->scale;
-
-			if (nsec_counter(counter))
-				nsec_printout(id, nr, counter, uval);
-			else
-				abs_printout(id, nr, counter, uval);
-
+			printout(id, nr, counter, uval);
 			if (!csv_output)
 				print_noise(counter, 1.0);
 
@@ -779,11 +835,7 @@ static void print_aggr_thread(struct perf_evsel *counter, char *prefix)
 			fprintf(output, "%s", prefix);
 
 		uval = val * counter->scale;
-
-		if (nsec_counter(counter))
-			nsec_printout(thread, 0, counter, uval);
-		else
-			abs_printout(thread, 0, counter, uval);
+		printout(thread, 0, counter, uval);
 
 		if (!csv_output)
 			print_noise(counter, 1.0);
@@ -832,11 +884,7 @@ static void print_counter_aggr(struct perf_evsel *counter, char *prefix)
 	}
 
 	uval = avg * counter->scale;
-
-	if (nsec_counter(counter))
-		nsec_printout(-1, 0, counter, uval);
-	else
-		abs_printout(-1, 0, counter, uval);
+	printout(-1, 0, counter, uval);
 
 	print_noise(counter, avg);
 
@@ -888,11 +936,7 @@ static void print_counter(struct perf_evsel *counter, char *prefix)
 		}
 
 		uval = val * counter->scale;
-
-		if (nsec_counter(counter))
-			nsec_printout(cpu, 0, counter, uval);
-		else
-			abs_printout(cpu, 0, counter, uval);
+		printout(cpu, 0, counter, uval);
 
 		if (!csv_output)
 			print_noise(counter, 1.0);
tools/perf/util/stat-shadow.c +81 -73

324 lines not on this page · raw message

tools/perf/util/stat.h +8 -2
index 1cfbe0a..152322d 100644
@@ -83,11 +83,17 @@ void perf_stat_evsel_id_init(struct perf_evsel *evsel);
 
 extern struct stats walltime_nsecs_stats;
 
+typedef void (*print_metric_t)(void *ctx, const char *color, const char *unit,
+			       const char *fmt, double val);
+
 void perf_stat__reset_shadow_stats(void);
 void perf_stat__update_shadow_stats(struct perf_evsel *counter, u64 *count,
 				    int cpu);
-void perf_stat__print_shadow_stats(FILE *out, struct perf_evsel *evsel,
-				   double avg, int cpu, enum aggr_mode aggr);
+void perf_stat__print_shadow_stats(struct perf_evsel *evsel,
+				   double avg, int cpu,
+				   print_metric_t print_metric,
+				   void (*new_line)(void *ctx),
+				   void *ctx);
 
 struct perf_counts *perf_counts__new(int ncpus, int nthreads);
 void perf_counts__delete(struct perf_counts *counts);
Signature · 1 line
2.4.3
#4 lore raw

Re: [PATCH 2/4] perf, tools, stat: Abstract stat metrics printing

On Wed, Jul 29, 2015 at 05:21:38PM -0700, Andi Kleen wrote:

SNIP

Quoted 7 lines
tools/perf/builtin-stat.c +2 -0
+static void nsec_printout(int id, int nr, struct perf_evsel *evsel, double avg,
+			  void *ctx, print_metric_t print_metric)
 {
 	double msecs = avg / 1e6;
 	const char *fmt_v, *fmt_n;
@@ -647,13 +689,16 @@ static void nsec_printout(int id, int nr, struct perf_evsel *evsel, double avg)
 		return;

tools/perf/builtin-stat.c static void nsec_printout(int id, int nr, struct perf_evsel *evsel, double avg) · line 696

Quoted 10 lines
tools/perf/builtin-stat.c +3 -3
 
 	if (perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK))
-		fprintf(output, " # %8.3f CPUs utilized          ",
-			avg / avg_stats(&walltime_nsecs_stats));
+		print_metric(ctx, NULL, "%8.3f", "CPUs utilized",
+			     avg / avg_stats(&walltime_nsecs_stats));
 	else
-		fprintf(output, "                                   ");
+		print_metric(ctx, NULL, NULL, NULL, 0);
 }

hum, this should go to perf_stat__print_shadow_stats, could you please move it there before this patch?

thanks,
jirka

#5 lore raw

Re: [PATCH 2/4] perf, tools, stat: Abstract stat metrics printing

In reply to Andi Kleen
Quoted 28 lines · Andi Kleen

From: Andi Kleen <[email protected]>

Abstract the printing of shadow metrics. Instead of every metric calling fprintf directly and taking care of indentation, use two call backs: one to print metrics and another to start a new line.

This will allow adding metrics to CSV mode and also using them for other purposes.

The computation of padding is now done in the central callback, instead of every metric doing it manually. This makes it easier to add new metrics.

Right now there is no (intentional) behavior change, just refactoring.

---
 tools/perf/builtin-stat.c     |  98 +++++++++++++++++++--------
 tools/perf/util/stat-shadow.c | 154 ++++++++++++++++++++++--------------------
 tools/perf/util/stat.h        |  10 ++-
 3 files changed, 160 insertions(+), 102 deletions(-)
tools/perf/builtin-stat.c +0 -0
index d99d850..e6386f1 100644

tools/perf/builtin-stat.c static void aggr_printout(struct perf_evsel *evsel, int id, int nr) · line 622

Quoted 7 lines
tools/perf/builtin-stat.c +3 -1
@@ -617,7 +617,49 @@ static void aggr_printout(struct perf_evsel *evsel, int id, int nr)
 	}
 }
 
-static void nsec_printout(int id, int nr, struct perf_evsel *evsel, double avg)
+struct outstate {
+	FILE *fh;
+};

because we already need to make the print_metric callback global, would it be better to make this struct global, having all the needed callbacks defined within? something like:

typedef void (*perf_stat_output_metric_t)(void *ctx, const char *color, const char *unit,

					  const char *fmt, double val);

typedef void (*perf_stat_output_newln_t)(void *ctx);

struct perf_stat_output_ctx {
	FILE			  *output
	perf_stat_output_metric_t  metric;
	perf_stat_output_newln_t   nl;
};

not sure about the naming, but IMO should have some perf_sta_.. global form

it'd also ease the new arguments count from 3 to 1

jirka

author
#6 lore raw

Re: [PATCH 2/4] perf, tools, stat: Abstract stat metrics printing

Quoted 4 lines

because we already need to make the print_metric callback global, would it be better to make this struct global, having all the needed callbacks defined within? something like:

It's actually not global, but static.

I skipped this change. After some other changes there is only a single function call with these arguments left, so it's not an issue to pass it around.

-Andi

#7 lore raw

Re: [PATCH 2/4] perf, tools, stat: Abstract stat metrics printing

Quoted 10 lines · Andi Kleen
Quoted 4 lines

because we already need to make the print_metric callback global, would it be better to make this struct global, having all the needed callbacks defined within? something like:

It's actually not global, but static.

I skipped this change. After some other changes there is only a single function call with these arguments left, so it's not an issue to pass it around.

hum, I dont understand.. I can se the same code in the new version:

+typedef void (*print_metric_t)(void *ctx, const char *color, const char *unit,
+ const char *fmt, double val);
+
void perf_stat__reset_shadow_stats(void);
void perf_stat__update_shadow_stats(struct perf_evsel *counter, u64 *count,

                                    int cpu);
-void perf_stat__print_shadow_stats(FILE *out, struct perf_evsel *evsel,
-                                  double avg, int cpu, enum aggr_mode aggr);
+void perf_stat__print_shadow_stats(struct perf_evsel *evsel,
+                                  double avg, int cpu,
+                                  print_metric_t print_metric,
+                                  void (*new_line)(void *ctx),
+                                  void *ctx);

both:

print_metric_t print_metric
void (*new_line)(void *ctx)

are still there.. what single function call did you mean?

jirka

author
#8 lore raw

PATCH 3/4 perf, tools, stat: Add support for metrics in interval mode

In reply to Andi Kleen

From: Andi Kleen <[email protected]>

Now that we can modify the metrics printout functions easily, it's straight forward to support metric printing for interval mode. All that is needed is to print the time stamp on every new line. Pass the prefix into the context and print it out.

---
 tools/perf/builtin-stat.c | 62 ++++++++++++++++++++++++++++++-----------------
 1 file changed, 40 insertions(+), 22 deletions(-)
tools/perf/builtin-stat.c +40 -22
index e6386f1..372e719 100644
@@ -619,6 +619,9 @@ static void aggr_printout(struct perf_evsel *evsel, int id, int nr)
 
 struct outstate {
 	FILE *fh;
+	const char *prefix;
+	int nfields;
+	u64 run, ena;
 };
 
 #define BASE_INDENT 41
@@ -629,13 +632,13 @@ struct outstate {
 static void new_line_no_aggr_std(void *ctx)
 {
 	struct outstate *os = ctx;
-	fprintf(os->fh, "\n%*s", BASE_INDENT + NA_INDENT, "");
+	fprintf(os->fh, "\n%s%-*s", os->prefix, BASE_INDENT + NA_INDENT, "");
 }
 
 static void new_line_std(void *ctx)
 {
 	struct outstate *os = ctx;
-	fprintf(os->fh, "\n%-*s", BASE_INDENT + AGGR_INDENT, "");
+	fprintf(os->fh, "\n%s%-*s", os->prefix, BASE_INDENT + AGGR_INDENT, "");
 }
 
 static void print_metric_std(void *ctx, const char *color, const char *fmt,
@@ -662,6 +665,7 @@ static void nsec_printout(int id, int nr, struct perf_evsel *evsel, double avg,
 			  void *ctx, print_metric_t print_metric)
 {
 	double msecs = avg / 1e6;
+	double ratio;
 	const char *fmt_v, *fmt_n;
 	char name[25];
 
@@ -685,12 +689,13 @@ static void nsec_printout(int id, int nr, struct perf_evsel *evsel, double avg,
 	if (evsel->cgrp)
 		fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
 
-	if (csv_output || interval)
+	if (csv_output)
 		return;
 
-	if (perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK))
+	ratio = avg_stats(&walltime_nsecs_stats);
+	if (perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK) && ratio)
 		print_metric(ctx, NULL, "%8.3f", "CPUs utilized",
-			     avg / avg_stats(&walltime_nsecs_stats));
+			     avg / ratio);
 	else
 		print_metric(ctx, NULL, NULL, NULL, 0);
 }
@@ -730,7 +735,7 @@ static void abs_printout(int id, int nr, struct perf_evsel *evsel, double avg,
 	if (evsel->cgrp)
 		fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
 
-	if (csv_output || interval)
+	if (csv_output)
 		return;
 
 	perf_stat__print_shadow_stats(evsel, avg, cpu,
@@ -739,17 +744,25 @@ static void abs_printout(int id, int nr, struct perf_evsel *evsel, double avg,
 				      ctx);
 }
 
-static void printout(int id, int nr, struct perf_evsel *counter, double uval)
+static void printout(int id, int nr, struct perf_evsel *counter, double uval,
+		     char *prefix)
 {
-	struct outstate os = { .fh = output };
+	struct outstate os = {
+		.fh = output,
+		.prefix = prefix ? prefix : ""
+	};
+	print_metric_t pm = print_metric_std;
+	void (*nl)(void *);
+
+	if (aggr_mode == AGGR_NONE)
+		nl = new_line_no_aggr_std;
+	else
+		nl = new_line_std;
 
 	if (nsec_counter(counter))
-		nsec_printout(id, nr, counter, uval, &os, print_metric_std);
+		nsec_printout(id, nr, counter, uval, &os, pm);
 	else
-		abs_printout(id, nr, counter, uval, &os,
-			     print_metric_std,
-			     aggr_mode == AGGR_NONE ?
-			     new_line_no_aggr_std : new_line_std);
+		abs_printout(id, nr, counter, uval, &os, pm, nl);
 }
 
 static void print_aggr(char *prefix)
@@ -805,7 +818,7 @@ static void print_aggr(char *prefix)
 				continue;
 			}
 			uval = val * counter->scale;
-			printout(id, nr, counter, uval);
+			printout(id, nr, counter, uval, prefix);
 			if (!csv_output)
 				print_noise(counter, 1.0);
 
@@ -835,7 +848,7 @@ static void print_aggr_thread(struct perf_evsel *counter, char *prefix)
 			fprintf(output, "%s", prefix);
 
 		uval = val * counter->scale;
-		printout(thread, 0, counter, uval);
+		printout(thread, 0, counter, uval, prefix);
 
 		if (!csv_output)
 			print_noise(counter, 1.0);
@@ -884,7 +897,7 @@ static void print_counter_aggr(struct perf_evsel *counter, char *prefix)
 	}
 
 	uval = avg * counter->scale;
-	printout(-1, 0, counter, uval);
+	printout(-1, 0, counter, uval, prefix);
 
 	print_noise(counter, avg);
 
@@ -936,7 +949,7 @@ static void print_counter(struct perf_evsel *counter, char *prefix)
 		}
 
 		uval = val * counter->scale;
-		printout(cpu, 0, counter, uval);
+		printout(cpu, 0, counter, uval, prefix);
 
 		if (!csv_output)
 			print_noise(counter, 1.0);
@@ -955,20 +968,25 @@ static void print_interval(char *prefix, struct timespec *ts)
 	if (num_print_interval == 0 && !csv_output) {
 		switch (aggr_mode) {
 		case AGGR_SOCKET:
-			fprintf(output, "#           time socket cpus             counts %*s events\n", unit_width, "unit");
+			fprintf(output, "#	   time  socket  cpus	         counts %*s events		       metric			           multiplex\n",
+					unit_width, "unit");
 			break;
 		case AGGR_CORE:
-			fprintf(output, "#           time core         cpus             counts %*s events\n", unit_width, "unit");
+			fprintf(output, "#	   time  core            cpus          counts %*s events                    metric                              multiplex\n",
+					unit_width, "unit");
 			break;
 		case AGGR_NONE:
-			fprintf(output, "#           time CPU                counts %*s events\n", unit_width, "unit");
+			fprintf(output, "#	   time  CPU		     counts %*s events                    metric                              multiplex\n",
+					unit_width, "unit");
 			break;
 		case AGGR_THREAD:
-			fprintf(output, "#           time             comm-pid                  counts %*s events\n", unit_width, "unit");
+			fprintf(output, "#         time	            comm-pid                 counts   %*s events                    metric\n",
+					unit_width, "unit");
 			break;
 		case AGGR_GLOBAL:
 		default:
-			fprintf(output, "#           time             counts %*s events\n", unit_width, "unit");
+			fprintf(output, "#         time	             counts %*s events                     metric                              multiplex\n",
+					unit_width, "unit");
 		}
 	}
 
Signature · 1 line
2.4.3
#9 lore raw

Re: [PATCH 3/4] perf, tools, stat: Add support for metrics in interval mode

On Wed, Jul 29, 2015 at 05:21:39PM -0700, Andi Kleen wrote:

SNIP

Quoted 15 lines
tools/perf/builtin-stat.c +6 -3
 		case AGGR_SOCKET:
-			fprintf(output, "#           time socket cpus             counts %*s events\n", unit_width, "unit");
+			fprintf(output, "#	   time  socket  cpus	         counts %*s events		       metric			           multiplex\n",
+					unit_width, "unit");
 			break;
 		case AGGR_CORE:
-			fprintf(output, "#           time core         cpus             counts %*s events\n", unit_width, "unit");
+			fprintf(output, "#	   time  core            cpus          counts %*s events                    metric                              multiplex\n",
+					unit_width, "unit");
 			break;
 		case AGGR_NONE:
-			fprintf(output, "#           time CPU                counts %*s events\n", unit_width, "unit");
+			fprintf(output, "#	   time  CPU		     counts %*s events                    metric                              multiplex\n",
+					unit_width, "unit");
 			break;

tools/perf/builtin-stat.c static void print_interval(char *prefix, struct timespec *ts) · line 989

Quoted 10 lines
tools/perf/builtin-stat.c +4 -2
 		case AGGR_THREAD:
-			fprintf(output, "#           time             comm-pid                  counts %*s events\n", unit_width, "unit");
+			fprintf(output, "#         time	            comm-pid                 counts   %*s events                    metric\n",
+					unit_width, "unit");
 			break;
 		case AGGR_GLOBAL:
 		default:
-			fprintf(output, "#           time             counts %*s events\n", unit_width, "unit");
+			fprintf(output, "#         time	             counts %*s events                     metric                              multiplex\n",
+					unit_width, "unit");

can't see why those header display is adjusted

jirka

author
#10 lore raw

PATCH 4/4 perf, tools, stat: Implement CSV metrics output

In reply to Andi Kleen

From: Andi Kleen <[email protected]>

Now support CSV output for metrics. With the new output callbacks this is relatively straight forward by creating new callbacks.

The new line callback needs to know the number of fields to skip them correctly

To avoid reordering the existing CSV fields, I had to move the noise printing into the lower level print metrics call back, so that noise can be printed before metrics.

This actually cleans up the callers because it avoids a lot of duplicated code.

---
 tools/perf/builtin-stat.c | 98 +++++++++++++++++++++++++++++++++++------------
 1 file changed, 73 insertions(+), 25 deletions(-)
tools/perf/builtin-stat.c +73 -25
index 372e719..5dae6aa 100644
@@ -661,6 +661,49 @@ static void print_metric_std(void *ctx, const char *color, const char *fmt,
 	fprintf(out, " %-*s", METRIC_LEN - n - 1, unit);
 }
 
+static void new_line_csv(void *ctx)
+{
+	struct outstate *os = ctx;
+	int i;
+
+	fputc('\n', os->fh);
+	if (os->prefix)
+		fprintf(os->fh, "%s%s", os->prefix, csv_sep);
+	for (i = 0; i < os->nfields; i++)
+		fputs(csv_sep, os->fh);
+}
+
+static void print_metric_csv(void *ctx,
+			     const char *color __maybe_unused,
+			     const char *fmt, const char *unit, double val)
+{
+	struct outstate *os = ctx;
+	FILE *out = os->fh;
+	char buf[64], *vals, *ends;
+
+	if (unit == NULL) {
+		fprintf(out, "%s%s%s%s", csv_sep, csv_sep, csv_sep, csv_sep);
+		return;
+	}
+	fprintf(out, "%s%" PRIu64 "%s%.2f%s",
+		csv_sep,
+		os->run,
+		csv_sep,
+		os->ena ? 100.0 * os->run / os->ena : 100.0,
+		csv_sep);
+	snprintf(buf, sizeof(buf), fmt, val);
+	vals = buf;
+	while (isspace(*vals))
+		vals++;
+	ends = vals;
+	while (isdigit(*ends) || *ends == '.')
+		ends++;
+	*ends = 0;
+	while (isspace(*unit))
+		unit++;
+	fprintf(out, "%s%s%s", vals, csv_sep, unit);
+}
+
 static void nsec_printout(int id, int nr, struct perf_evsel *evsel, double avg,
 			  void *ctx, print_metric_t print_metric)
 {
@@ -735,9 +778,6 @@ static void abs_printout(int id, int nr, struct perf_evsel *evsel, double avg,
 	if (evsel->cgrp)
 		fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
 
-	if (csv_output)
-		return;
-
 	perf_stat__print_shadow_stats(evsel, avg, cpu,
 				      print_metric,
 				      new_line,
@@ -745,7 +785,7 @@ static void abs_printout(int id, int nr, struct perf_evsel *evsel, double avg,
 }
 
 static void printout(int id, int nr, struct perf_evsel *counter, double uval,
-		     char *prefix)
+		     char *prefix, u64 run, u64 ena, double noise)
 {
 	struct outstate os = {
 		.fh = output,
@@ -759,10 +799,35 @@ static void printout(int id, int nr, struct perf_evsel *counter, double uval,
 	else
 		nl = new_line_std;
 
+	if (csv_output) {
+		static int aggr_fields[] = {
+			[AGGR_GLOBAL] = 0,
+			[AGGR_THREAD] = 1,
+			[AGGR_NONE] = 1,
+			[AGGR_SOCKET] = 2,
+			[AGGR_CORE] = 2,
+		};
+
+		pm = print_metric_csv;
+		nl = new_line_csv;
+		os.nfields = 1;
+		os.nfields += aggr_fields[aggr_mode];
+		if (counter->cgrp)
+			os.nfields++;
+		os.run = run;
+		os.ena = ena;
+	}
+
 	if (nsec_counter(counter))
 		nsec_printout(id, nr, counter, uval, &os, pm);
 	else
 		abs_printout(id, nr, counter, uval, &os, pm, nl);
+
+	if (!csv_output) {
+		print_noise(counter, noise);
+		if (run != ena)
+			fprintf(output, "  (%.2f%%)", 100.0 * run / ena);
+	}
 }
 
 static void print_aggr(char *prefix)
@@ -818,11 +883,7 @@ static void print_aggr(char *prefix)
 				continue;
 			}
 			uval = val * counter->scale;
-			printout(id, nr, counter, uval, prefix);
-			if (!csv_output)
-				print_noise(counter, 1.0);
-
-			print_running(run, ena);
+			printout(id, nr, counter, uval, prefix, run, ena, 1.0);
 			fputc('\n', output);
 		}
 	}
@@ -848,12 +909,7 @@ static void print_aggr_thread(struct perf_evsel *counter, char *prefix)
 			fprintf(output, "%s", prefix);
 
 		uval = val * counter->scale;
-		printout(thread, 0, counter, uval, prefix);
-
-		if (!csv_output)
-			print_noise(counter, 1.0);
-
-		print_running(run, ena);
+		printout(thread, 0, counter, uval, prefix, run, ena, 1.0);
 		fputc('\n', output);
 	}
 }
@@ -897,11 +953,7 @@ static void print_counter_aggr(struct perf_evsel *counter, char *prefix)
 	}
 
 	uval = avg * counter->scale;
-	printout(-1, 0, counter, uval, prefix);
-
-	print_noise(counter, avg);
-
-	print_running(avg_running, avg_enabled);
+	printout(-1, 0, counter, uval, prefix, avg_running, avg_enabled, avg);
 	fprintf(output, "\n");
 }
 
@@ -949,11 +1001,7 @@ static void print_counter(struct perf_evsel *counter, char *prefix)
 		}
 
 		uval = val * counter->scale;
-		printout(cpu, 0, counter, uval, prefix);
-
-		if (!csv_output)
-			print_noise(counter, 1.0);
-		print_running(run, ena);
+		printout(cpu, 0, counter, uval, prefix, run, ena, 1.0);
 
 		fputc('\n', output);
 	}
Signature · 1 line
2.4.3
#11 lore raw

Re: [PATCH 4/4] perf, tools, stat: Implement CSV metrics output

On Wed, Jul 29, 2015 at 05:21:40PM -0700, Andi Kleen wrote:

SNIP

Quoted 45 lines
tools/perf/builtin-stat.c +4 -16
+	}
 }
 
 static void print_aggr(char *prefix)
@@ -818,11 +883,7 @@ static void print_aggr(char *prefix)
 				continue;
 			}
 			uval = val * counter->scale;
-			printout(id, nr, counter, uval, prefix);
-			if (!csv_output)
-				print_noise(counter, 1.0);
-
-			print_running(run, ena);
+			printout(id, nr, counter, uval, prefix, run, ena, 1.0);
 			fputc('\n', output);
 		}
 	}
@@ -848,12 +909,7 @@ static void print_aggr_thread(struct perf_evsel *counter, char *prefix)
 			fprintf(output, "%s", prefix);
 
 		uval = val * counter->scale;
-		printout(thread, 0, counter, uval, prefix);
-
-		if (!csv_output)
-			print_noise(counter, 1.0);
-
-		print_running(run, ena);
+		printout(thread, 0, counter, uval, prefix, run, ena, 1.0);
 		fputc('\n', output);
 	}
 }
@@ -897,11 +953,7 @@ static void print_counter_aggr(struct perf_evsel *counter, char *prefix)
 	}
 
 	uval = avg * counter->scale;
-	printout(-1, 0, counter, uval, prefix);
-
-	print_noise(counter, avg);
-
-	print_running(avg_running, avg_enabled);
+	printout(-1, 0, counter, uval, prefix, avg_running, avg_enabled, avg);
 	fprintf(output, "\n");
 }
 

tools/perf/builtin-stat.c static void print_counter(struct perf_evsel *counter, char *prefix) · line 1004

Quoted 9 lines
tools/perf/builtin-stat.c +1 -5
@@ -949,11 +1001,7 @@ static void print_counter(struct perf_evsel *counter, char *prefix)
 		}
 
 		uval = val * counter->scale;
-		printout(cpu, 0, counter, uval, prefix);
-
-		if (!csv_output)
-			print_noise(counter, 1.0);
-		print_running(run, ena);
+		printout(cpu, 0, counter, uval, prefix, run, ena, 1.0);

print_running still stayed in print_aggr

how about moving the (run == 0 || ena == 0) leg from print_aggr into printout

jirka

#12 lore raw

Re: [PATCH 4/4] perf, tools, stat: Implement CSV metrics output

In reply to Andi Kleen
Quoted 8 lines · Andi Kleen

From: Andi Kleen <[email protected]>

Now support CSV output for metrics. With the new output callbacks this is relatively straight forward by creating new callbacks.

The new line callback needs to know the number of fields to skip them correctly

Quoted 6 lines

To avoid reordering the existing CSV fields, I had to move the noise printing into the lower level print metrics call back, so that noise can be printed before metrics.

This actually cleans up the callers because it avoids a lot of duplicated code.

nice, please separate the cleanup from the actual addition of the metrics CSV fields

could you please also start some documentation in perf stat man? I dont think we have anything yet.. and it's getting bigger ;-)

thanks,
jirka