Skip to contentSkip to conversation

perf stat: add interval counter printing

PATCH Superseded Stephane Eranian 2 participants 8 messages Jan 18, 2013 perf events

v2 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v2 cover perf stat: add interval counter printing

This small patchset extends perf stat with the -I option.

The -I option enables interval printing. It expects an interval duration in milliseconds. Minimum is 100ms. Once, activated perf stat prints events deltas since last printout. All modes are supported.

$ perf stat -I 1000 -e cycles noploop 10
noploop for 10 seconds
1.000086918         2385155642 cycles                    #    0.000 GHz
2.000267937         2392279774 cycles                    #    0.000 GHz
3.000385400         2390971450 cycles                    #    0.000 GHz
4.000504408         2390996752 cycles                    #    0.000 GHz
5.000626878         2390853097 cycles                    #    0.000 GHz

The output format makes it easy to feed into a plotting program such as gnuplot when the -I option is used in combination with the -x option:

$ perf stat -x, -I 1000 -e cycles noploop 10
noploop for 10 seconds
1.000084113,2378775498,cycles
2.000245798,2391056897,cycles
3.000354445,2392089414,cycles
4.000459115,2390936603,cycles
5.000565341,2392108173,cycles

The first patch adds the code to keep previous counts. The second is the actual extension of perf stat.

In v2, we changed the option from -T to -I based on feedback from Andi Kleen (who is using -T in an upcoming patch).

Stephane Eranian (2):
perf tools: add evsel prev_raw_count field
perf stat: add interval printing

 tools/perf/Documentation/perf-stat.txt |    4 +
 tools/perf/builtin-stat.c              |  157 ++++++++++++++++++++++++++++----
 tools/perf/util/evsel.c                |   21 +++++
 tools/perf/util/evsel.h                |    1 +
 4 files changed, 167 insertions(+), 16 deletions(-)
Signature · 1 line
1.7.9.5
#1 lore raw

PATCH v2 1/2 perf tools: add evsel prev_raw_count field

This field will be used by commands which print
counter deltas on regular timer intervals,
such as perf stat -I.

---
 tools/perf/util/evsel.c |   21 +++++++++++++++++++++
 tools/perf/util/evsel.h |    1 +
 2 files changed, 22 insertions(+)
tools/perf/util/evsel.c +21 -0
index 1b16dd1..132da7c 100644
@@ -646,6 +646,17 @@ int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
 	if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
 		return -errno;
 
+	if (evsel->prev_raw_counts) {
+		struct perf_counts_values tmp;
+
+		tmp = evsel->prev_raw_counts->cpu[cpu];
+		evsel->prev_raw_counts->cpu[cpu] = count;
+
+		count.val = count.val - tmp.val;
+		count.ena = count.ena - tmp.ena;
+		count.run = count.run - tmp.run;
+	}
+
 	if (scale) {
 		if (count.run == 0)
 			count.val = 0;
@@ -684,6 +695,16 @@ int __perf_evsel__read(struct perf_evsel *evsel,
 		}
 	}
 
+	if (evsel->prev_raw_counts) {
+		struct perf_counts_values tmp = evsel->prev_raw_counts->aggr;
+
+		evsel->prev_raw_counts->aggr = *aggr;
+
+		aggr->val = aggr->val - tmp.val;
+		aggr->ena = aggr->ena - tmp.ena;
+		aggr->run = aggr->run - tmp.run;
+	}
+
 	evsel->counts->scaled = 0;
 	if (scale) {
 		if (aggr->run == 0) {
tools/perf/util/evsel.h +1 -0
index 3d2b801..63ea570 100644
@@ -53,6 +53,7 @@ struct perf_evsel {
 	struct xyarray		*sample_id;
 	u64			*id;
 	struct perf_counts	*counts;
+	struct perf_counts	*prev_raw_counts;
 	int			idx;
 	u32			ids;
 	struct hists		hists;
Signature · 1 line
1.7.9.5
#2 lore raw

Re: [PATCH v2 1/2] perf tools: add evsel prev_raw_count field

Quoted 35 lines · Stephane Eranian

This field will be used by commands which print
counter deltas on regular timer intervals,
such as perf stat -I.

---
 tools/perf/util/evsel.c |   21 +++++++++++++++++++++
 tools/perf/util/evsel.h |    1 +
 2 files changed, 22 insertions(+)
tools/perf/util/evsel.c +11 -0
index 1b16dd1..132da7c 100644
@@ -646,6 +646,17 @@ int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
 	if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
 		return -errno;
 
+	if (evsel->prev_raw_counts) {
+		struct perf_counts_values tmp;
+
+		tmp = evsel->prev_raw_counts->cpu[cpu];
+		evsel->prev_raw_counts->cpu[cpu] = count;
+
+		count.val = count.val - tmp.val;
+		count.ena = count.ena - tmp.ena;
+		count.run = count.run - tmp.run;
+	}
+
 	if (scale) {
 		if (count.run == 0)
 			count.val = 0;
@@ -684,6 +695,16 @@ int __perf_evsel__read(struct perf_evsel *evsel,
 		}
 	}

tools/perf/util/evsel.c int __perf_evsel__read(struct perf_evsel *evsel, · line 706

Quoted 10 lines
tools/perf/util/evsel.c +9 -0
 
+	if (evsel->prev_raw_counts) {
+		struct perf_counts_values tmp = evsel->prev_raw_counts->aggr;
+
+		evsel->prev_raw_counts->aggr = *aggr;
+
+		aggr->val = aggr->val - tmp.val;
+		aggr->ena = aggr->ena - tmp.ena;
+		aggr->run = aggr->run - tmp.run;
+	}

this and the hunk above looks like candidates for function

jirka

#3 lore raw

PATCH v2 2/2 perf stat: add interval printing

In reply to Stephane Eranian

This patch adds a new printing mode for perf stat. It allows internval printing. That means perf stat can now print event deltas at regular time interval. This is useful to detect phases in programs.

The -I option enables interval printing. It expects an interval duration in milliseconds. Minimum is 100ms. Once, activated perf stat prints events deltas since last printout. All modes are supported.

$ perf stat -I 1000 -e cycles noploop 10
noploop for 10 seconds
1.000086918         2385155642 cycles                    #    0.000 GHz
2.000267937         2392279774 cycles                    #    0.000 GHz
3.000385400         2390971450 cycles                    #    0.000 GHz
4.000504408         2390996752 cycles                    #    0.000 GHz
5.000626878         2390853097 cycles                    #    0.000 GHz

The output format makes it easy to feed into a plotting program such as gnuplot when the -I option is used in combination with the -x option:

$ perf stat -x, -I 1000 -e cycles noploop 10
noploop for 10 seconds
1.000084113,2378775498,cycles
2.000245798,2391056897,cycles
3.000354445,2392089414,cycles
4.000459115,2390936603,cycles
5.000565341,2392108173,cycles
---
 tools/perf/Documentation/perf-stat.txt |    4 +
 tools/perf/builtin-stat.c              |  157 ++++++++++++++++++++++++++++----
 2 files changed, 145 insertions(+), 16 deletions(-)
tools/perf/Documentation/perf-stat.txt +4 -0
index cf0c310..5289da3 100644
@@ -114,6 +114,10 @@ Log output to fd, instead of stderr.  Complementary to --output, and mutually ex
 
 perf stat --repeat 10 --null --sync --pre 'make -s O=defconfig-build/clean' -- make -s -j64 O=defconfig-build/ bzImage
 
+-I msecs::
+--interval-print msecs::
+	print count deltas every N milliseconds (minimum: 100ms)
+	example: perf stat -I 1000 -e cycles -a sleep 5
 
 EXAMPLES
 --------
tools/perf/builtin-stat.c +141 -16

306 lines not on this page · raw message

Signature · 1 line
1.7.9.5
#4 lore raw

Re: [PATCH v2 2/2] perf stat: add interval printing

Quoted 2 lines · Stephane Eranian

This patch adds a new printing mode for perf stat. It allows internval printing. That means perf stat

SNIP

Quoted 12 lines
tools/perf/builtin-stat.c +5 -0
 	const char * const stat_usage[] = {
@@ -1245,12 +1356,23 @@ int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused)
 		usage_with_options(stat_usage, options);
 		return -1;
 	}
+	if (interval < 0 || (interval > 0 && interval < 100)) {
+		pr_err("print interval must be >= 100ms\n");
+		usage_with_options(stat_usage, options);
+		return -1;
+	}
 
 	list_for_each_entry(pos, &evsel_list->entries, node) {

tools/perf/builtin-stat.c int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused) · line 1375

Quoted 10 lines
tools/perf/builtin-stat.c +6 -0
 		if (perf_evsel__alloc_stat_priv(pos) < 0 ||
 		    perf_evsel__alloc_counts(pos, perf_evsel__nr_cpus(pos)) < 0)
 			goto out_free_fd;
 	}
+	if (interval) {
+		list_for_each_entry(pos, &evsel_list->entries, node) {
+			if (perf_evsel__alloc_prev_raw_counts(pos) < 0)
+				goto out_free_fd;
+		}
+	}

this could be squeezed up into the previous loop,

the perf_evsel__alloc_counts function does the same job for 'counts', maybe it could be unified

Quoted 12 lines
tools/perf/builtin-stat.c +1 -0
 	/*
 	 * We dont want to block the signals - that would cause
@@ -1260,6 +1382,7 @@ int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused)
 	 */
 	atexit(sig_atexit);
 	signal(SIGINT,  skip_signal);
+	signal(SIGCHLD, skip_signal);
 	signal(SIGALRM, skip_signal);
 	signal(SIGABRT, skip_signal);
 

tools/perf/builtin-stat.c int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused) · line 1399

Quoted 6 lines
tools/perf/builtin-stat.c +1 -1
@@ -1272,11 +1395,13 @@ int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused)
 		status = run_perf_stat(argc, argv);
 	}
 
-	if (status != -1)
+	if (status != -1 && !interval)
 		print_stat(argc, argv);

The way this is done prevents final stats to be printed, which seems unfortunate.

Actually I don't mind that much, because I haven't even thought about this feature, but looks like it could be done separately with no affect to final counts.

But since you probably use it mostly for feeding some graph ploting program, it's probably fine.. just feels wrong ;-)

jirka

#5 lore raw

Re: [PATCH v2 2/2] perf stat: add interval printing

Quoted 17 lines · Jiri Olsa
Quoted 2 lines · Stephane Eranian

This patch adds a new printing mode for perf stat. It allows internval printing. That means perf stat

SNIP

Quoted 11 lines
      const char * const stat_usage[] = {
tools/perf/builtin-stat.c +5 -0
@@ -1245,12 +1356,23 @@ int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused)
              usage_with_options(stat_usage, options);
              return -1;
      }
+     if (interval < 0 || (interval > 0 && interval < 100)) {
+             pr_err("print interval must be >= 100ms\n");
+             usage_with_options(stat_usage, options);
+             return -1;
+     }
 
Quoted 14 lines

tools/perf/builtin-stat.c int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused) · line 1375

Quoted 11 lines
tools/perf/builtin-stat.c +6 -0
      list_for_each_entry(pos, &evsel_list->entries, node) {
              if (perf_evsel__alloc_stat_priv(pos) < 0 ||
                  perf_evsel__alloc_counts(pos, perf_evsel__nr_cpus(pos)) < 0)
                      goto out_free_fd;
      }
+     if (interval) {
+             list_for_each_entry(pos, &evsel_list->entries, node) {
+                     if (perf_evsel__alloc_prev_raw_counts(pos) < 0)
+                             goto out_free_fd;
+             }
+     }

this could be squeezed up into the previous loop,

The raw_counts are lazily allocated. So you'd have to have

if (interval)

     perf_evsel__alloc_prev_raw_counts(pos);

in the body of the loop.

Quoted 19 lines

the perf_evsel__alloc_counts function does the same job for 'counts', maybe it could be unified

Quoted 15 lines
tools/perf/builtin-stat.c +1 -0
      /*
       * We dont want to block the signals - that would cause
@@ -1260,6 +1382,7 @@ int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused)
       */
      atexit(sig_atexit);
      signal(SIGINT,  skip_signal);
+     signal(SIGCHLD, skip_signal);
      signal(SIGALRM, skip_signal);
      signal(SIGABRT, skip_signal);
 
@@ -1272,11 +1395,13 @@ int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused)
              status = run_perf_stat(argc, argv);
      }
 
Quoted 7 lines

tools/perf/builtin-stat.c int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused) · line 1399

Quoted 3 lines
tools/perf/builtin-stat.c +1 -1
-     if (status != -1)
+     if (status != -1 && !interval)
              print_stat(argc, argv);

The way this is done prevents final stats to be printed, which seems unfortunate.

This is on purpose. All we care about here is the spaced deltas. Note that I also removed the shadow stats. Tried to make them work for a while but it did not make sense especially for -a -A. I don't think it even works right now.

Quoted 4 lines

Actually I don't mind that much, because I haven't even thought about this feature, but looks like it could be done separately with no affect to final counts.

Initially I had it as raw_counts, prev_raw_counts. But then it would cause problems with the stats. So I simplified everything, it uses less memory and gives me the output I care about.

If you want total count, then don't use this new option. The results are to be fed to a plotter, so the final counts are useless and complicate the post-processing.

Quoted 4 lines

But since you probably use it mostly for feeding some graph ploting program, it's probably fine.. just feels wrong ;-)

jirka

#6 lore raw

Re: [PATCH v2 2/2] perf stat: add interval printing

Quoted 32 lines · Stephane Eranian
Quoted 17 lines · Jiri Olsa
Quoted 2 lines · Stephane Eranian

This patch adds a new printing mode for perf stat. It allows internval printing. That means perf stat

SNIP

Quoted 11 lines
      const char * const stat_usage[] = {
tools/perf/builtin-stat.c +5 -0
@@ -1245,12 +1356,23 @@ int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused)
              usage_with_options(stat_usage, options);
              return -1;
      }
+     if (interval < 0 || (interval > 0 && interval < 100)) {
+             pr_err("print interval must be >= 100ms\n");
+             usage_with_options(stat_usage, options);
+             return -1;
+     }
 
Quoted 14 lines

tools/perf/builtin-stat.c int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused) · line 1375

Quoted 11 lines
tools/perf/builtin-stat.c +6 -0
      list_for_each_entry(pos, &evsel_list->entries, node) {
              if (perf_evsel__alloc_stat_priv(pos) < 0 ||
                  perf_evsel__alloc_counts(pos, perf_evsel__nr_cpus(pos)) < 0)
                      goto out_free_fd;
      }
+     if (interval) {
+             list_for_each_entry(pos, &evsel_list->entries, node) {
+                     if (perf_evsel__alloc_prev_raw_counts(pos) < 0)
+                             goto out_free_fd;
+             }
+     }

this could be squeezed up into the previous loop,

Quoted 6 lines

The raw_counts are lazily allocated. So you'd have to have

if (interval)

     perf_evsel__alloc_prev_raw_counts(pos);

in the body of the loop.

right,
jirka

#7 lore raw

Re: [PATCH v2 2/2] perf stat: add interval printing

In reply to Stephane Eranian

SNIP

Quoted 16 lines
Quoted 7 lines

tools/perf/builtin-stat.c int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused) · line 1399

Quoted 3 lines
tools/perf/builtin-stat.c +1 -1
-     if (status != -1)
+     if (status != -1 && !interval)
              print_stat(argc, argv);

The way this is done prevents final stats to be printed, which seems unfortunate.

This is on purpose. All we care about here is the spaced deltas. Note that I also removed the shadow stats. Tried to make them work for a while but it did not make sense especially for -a -A. I don't think it even works right now.

Quoted 4 lines

Actually I don't mind that much, because I haven't even thought about this feature, but looks like it could be done separately with no affect to final counts.

Quoted 7 lines

Initially I had it as raw_counts, prev_raw_counts. But then it would cause problems with the stats. So I simplified everything, it uses less memory and gives me the output I care about.

If you want total count, then don't use this new option. The results are to be fed to a plotter, so the final counts are useless and complicate the post-processing.

ok, agreed

jirka

Quoted 3 lines
Quoted 3 lines

But since you probably use it mostly for feeding some graph ploting program, it's probably fine.. just feels wrong ;-)