Skip to contentSkip to conversation

perf bench: flush stats when pipe it to a file or to tee

PATCH In review Sohaib Mohamed 2 participants 5 messages Nov 12, 2021 – Nov 16, 2021 perf events

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 perf bench: flush stats when pipe it to a file or to tee

The output of perf bench gets buffered when I pipe it to a file or to tee, in such a way that I can see it only at the end.

E.g.

$ perf bench internals synthesize -t
< output comes out fine after each test run >
$ perf bench internals synthesize -t | tee file.txt
< output comes out only at the end of all tests >

See, also:

$ perf bench mem all | tee file.txt
$ perf bench sched all | tee file.txt
$ perf bench internals all -t | tee file.txt
$ perf bench internals all | tee file.txt
---
 tools/perf/bench/find-bit-bench.c | 1 +
 tools/perf/bench/synthesize.c     | 2 ++
 2 files changed, 3 insertions(+)
tools/perf/bench/find-bit-bench.c +1 -0
index 22b5cfe97023..39fb4184fbcb 100644
@@ -113,6 +113,7 @@ static int do_for_each_set_bit(unsigned int num_bits)
 
 		if (use_of_val == accumulator)  /* Try to avoid compiler tricks. */
 			printf("\n");
+		fflush(stdout);
 	}
 	bitmap_free(to_test);
 	return 0;
tools/perf/bench/synthesize.c +2 -0
index 7401ebbac100..d21d311df61b 100644
@@ -92,6 +92,7 @@ static int do_run_single_threaded(struct perf_session *session,
 		update_stats(&event_stats, atomic_read(&event_count));
 	}
 
+	fflush(stdout);
 	time_average = avg_stats(&time_stats);
 	time_stddev = stddev_stats(&time_stats);
 	printf("  Average %ssynthesis took: %.3f usec (+- %.3f usec)\n",
@@ -186,6 +187,7 @@ static int do_run_multi_threaded(struct target *target,
 		perf_session__delete(session);
 	}
 
+	fflush(stdout);
 	time_average = avg_stats(&time_stats);
 	time_stddev = stddev_stats(&time_stats);
 	printf("    Average synthesis took: %.3f usec (+- %.3f usec)\n",

--
2.25.1

#1 lore raw

Em Fri, Nov 12, 2021 at 11:53:12PM +0200, Sohaib Mohamed escreveu:

Quoted 2 lines

The output of perf bench gets buffered when I pipe it to a file or to tee, in such a way that I can see it only at the end.

Good, now try with 'perf test' :-)

Applying,

- Arnaldo

Quoted 54 lines

E.g.

$ perf bench internals synthesize -t
< output comes out fine after each test run >
$ perf bench internals synthesize -t | tee file.txt
< output comes out only at the end of all tests >

See, also:

$ perf bench mem all | tee file.txt
$ perf bench sched all | tee file.txt
$ perf bench internals all -t | tee file.txt
$ perf bench internals all | tee file.txt
---
 tools/perf/bench/find-bit-bench.c | 1 +
 tools/perf/bench/synthesize.c     | 2 ++
 2 files changed, 3 insertions(+)
tools/perf/bench/find-bit-bench.c +1 -0
index 22b5cfe97023..39fb4184fbcb 100644
@@ -113,6 +113,7 @@ static int do_for_each_set_bit(unsigned int num_bits)
 
 		if (use_of_val == accumulator)  /* Try to avoid compiler tricks. */
 			printf("\n");
+		fflush(stdout);
 	}
 	bitmap_free(to_test);
 	return 0;
tools/perf/bench/synthesize.c +2 -1
index 7401ebbac100..d21d311df61b 100644
@@ -92,6 +92,7 @@ static int do_run_single_threaded(struct perf_session *session,
 		update_stats(&event_stats, atomic_read(&event_count));
 	}
 
+	fflush(stdout);
 	time_average = avg_stats(&time_stats);
 	time_stddev = stddev_stats(&time_stats);
 	printf("  Average %ssynthesis took: %.3f usec (+- %.3f usec)\n",
@@ -186,6 +187,7 @@ static int do_run_multi_threaded(struct target *target,
 		perf_session__delete(session);
 	}
 
+	fflush(stdout);
 	time_average = avg_stats(&time_stats);
 	time_stddev = stddev_stats(&time_stats);
 	printf("    Average synthesis took: %.3f usec (+- %.3f usec)\n",
--

2.25.1

Signature · 1 line
- Arnaldo
#2 lore raw

Em Sat, Nov 13, 2021 at 09:16:42AM -0300, Arnaldo Carvalho de Melo escreveu:

Quoted 7 lines

Em Fri, Nov 12, 2021 at 11:53:12PM +0200, Sohaib Mohamed escreveu:

Quoted 2 lines

The output of perf bench gets buffered when I pipe it to a file or to tee, in such a way that I can see it only at the end.

Good, now try with 'perf test' :-)

Applying,

Humm, take that back, when I almost did it for 'perf test' I used setvbuf(), take a look at it so that you don't have to go sprinkling fflush() calls and missing some places.

- Arnaldo

Quoted 60 lines

- Arnaldo

Quoted 54 lines

E.g.

$ perf bench internals synthesize -t
< output comes out fine after each test run >
$ perf bench internals synthesize -t | tee file.txt
< output comes out only at the end of all tests >

See, also:

$ perf bench mem all | tee file.txt
$ perf bench sched all | tee file.txt
$ perf bench internals all -t | tee file.txt
$ perf bench internals all | tee file.txt
---
 tools/perf/bench/find-bit-bench.c | 1 +
 tools/perf/bench/synthesize.c     | 2 ++
 2 files changed, 3 insertions(+)
tools/perf/bench/find-bit-bench.c +1 -0
index 22b5cfe97023..39fb4184fbcb 100644
@@ -113,6 +113,7 @@ static int do_for_each_set_bit(unsigned int num_bits)
 
 		if (use_of_val == accumulator)  /* Try to avoid compiler tricks. */
 			printf("\n");
+		fflush(stdout);
 	}
 	bitmap_free(to_test);
 	return 0;
tools/perf/bench/synthesize.c +2 -1
index 7401ebbac100..d21d311df61b 100644
@@ -92,6 +92,7 @@ static int do_run_single_threaded(struct perf_session *session,
 		update_stats(&event_stats, atomic_read(&event_count));
 	}
 
+	fflush(stdout);
 	time_average = avg_stats(&time_stats);
 	time_stddev = stddev_stats(&time_stats);
 	printf("  Average %ssynthesis took: %.3f usec (+- %.3f usec)\n",
@@ -186,6 +187,7 @@ static int do_run_multi_threaded(struct target *target,
 		perf_session__delete(session);
 	}
 
+	fflush(stdout);
 	time_average = avg_stats(&time_stats);
 	time_stddev = stddev_stats(&time_stats);
 	printf("    Average synthesis took: %.3f usec (+- %.3f usec)\n",
--

2.25.1

Signature · 1 line
- Arnaldo
Signature · 1 line
- Arnaldo
#3 lore raw

Hello, Arnaldo

Quoted 7 lines · Arnaldo Carvalho de Melo

Em Sat, Nov 13, 2021 at 09:16:42AM -0300, Arnaldo Carvalho de Melo escreveu:

Quoted 6 lines

Em Fri, Nov 12, 2021 at 11:53:12PM +0200, Sohaib Mohamed escreveu:

Quoted 2 lines

The output of perf bench gets buffered when I pipe it to a file or to tee, in such a way that I can see it only at the end.

Good, now try with 'perf test' :-)

Quoted 7 lines
Quoted 1 line

Applying,

Humm, take that back, when I almost did it for 'perf test' I used setvbuf(), take a look at it so that you don't have to go sprinkling fflush() calls and missing some places.

- Arnaldo

I'm not sure about the meaning of 'perf test' here, Should I write new unit tests?

I found I could invoke this function at the beginning of perf-bench:
setvbuf(stdout, NULL, _IONBF, 0);
This looks to me will resolve the issue for all perf-bench subcommands. I'm not sure if a good practice...

This probably is version 2 of this patch:

tools/perf/builtin-bench.c +2 -2
index d0895162c2ba..a0b812cde1ee 100644
@@ -226,7 +226,6 @@ static void run_collection(struct collection *coll)
                if (!bench->fn)
                        break;
                printf("# Running %s/%s benchmark...\n", coll->name, bench->name);
-               fflush(stdout);
 
                argv[1] = bench->name;
                run_bench(coll->name, bench->name, bench->fn, 1, argv);
@@ -247,6 +246,8 @@ int cmd_bench(int argc, const char **argv)
        struct collection *coll;
        int ret = 0;
 
+       setvbuf(stdout, NULL, _IONBF, 0);
+
        if (argc < 2) {
                /* No collection specified. */
                print_usage();
@@ -300,7 +301,6 @@ int cmd_bench(int argc, const char **argv)
 
                        if (bench_format == BENCH_FORMAT_DEFAULT)
                                printf("# Running '%s/%s' benchmark:\n", coll->name, bench->name);
-                       fflush(stdout);
                        ret = run_bench(coll->name, bench->name, bench->fn, argc-1, argv+1);
                        goto end;
                }

Thanks,
- Sohaib

Quoted 65 lines
Quoted 60 lines

- Arnaldo

Quoted 53 lines

E.g.

$ perf bench internals synthesize -t
< output comes out fine after each test run >
$ perf bench internals synthesize -t | tee file.txt
< output comes out only at the end of all tests >

See, also:

$ perf bench mem all | tee file.txt
$ perf bench sched all | tee file.txt
$ perf bench internals all -t | tee file.txt
$ perf bench internals all | tee file.txt
---
 tools/perf/bench/find-bit-bench.c | 1 +
 tools/perf/bench/synthesize.c     | 2 ++
 2 files changed, 3 insertions(+)
tools/perf/bench/find-bit-bench.c +1 -0
index 22b5cfe97023..39fb4184fbcb 100644
@@ -113,6 +113,7 @@ static int do_for_each_set_bit(unsigned int num_bits)
 
 		if (use_of_val == accumulator)  /* Try to avoid compiler tricks. */
 			printf("\n");
+		fflush(stdout);
 	}
 	bitmap_free(to_test);
 	return 0;
tools/perf/bench/synthesize.c +2 -1
index 7401ebbac100..d21d311df61b 100644
@@ -92,6 +92,7 @@ static int do_run_single_threaded(struct perf_session *session,
 		update_stats(&event_stats, atomic_read(&event_count));
 	}
 
+	fflush(stdout);
 	time_average = avg_stats(&time_stats);
 	time_stddev = stddev_stats(&time_stats);
 	printf("  Average %ssynthesis took: %.3f usec (+- %.3f usec)\n",
@@ -186,6 +187,7 @@ static int do_run_multi_threaded(struct target *target,
 		perf_session__delete(session);
 	}
 
+	fflush(stdout);
 	time_average = avg_stats(&time_stats);
 	time_stddev = stddev_stats(&time_stats);
 	printf("    Average synthesis took: %.3f usec (+- %.3f usec)\n",
--
Quoted 1 line

2.25.1

--

Quoted 1 line

- Arnaldo

--

- Arnaldo

#4 lore raw

Em Tue, Nov 16, 2021 at 12:41:51AM +0200, Sohaib Mohamed escreveu:

Quoted 12 lines

Hello, Arnaldo

Quoted 9 lines · Arnaldo Carvalho de Melo

Em Sat, Nov 13, 2021 at 09:16:42AM -0300, Arnaldo Carvalho de Melo escreveu:

Quoted 7 lines

Em Fri, Nov 12, 2021 at 11:53:12PM +0200, Sohaib Mohamed escreveu:

Quoted 2 lines

The output of perf bench gets buffered when I pipe it to a file or to tee, in such a way that I can see it only at the end.

Good, now try with 'perf test' :-)

Applying,

Quoted 8 lines
Quoted 5 lines

Humm, take that back, when I almost did it for 'perf test' I used setvbuf(), take a look at it so that you don't have to go sprinkling fflush() calls and missing some places.

- Arnaldo

I'm not sure about the meaning of 'perf test' here, Should I write new unit tests?

Try:

$ perf test 2>&1 | tee /tmp/bla

It uses buffered output, so we see the output in bumps, would be better to use unbuffered.

Quoted 4 lines

I found I could invoke this function at the beginning of perf-bench:
setvbuf(stdout, NULL, _IONBF, 0);
This looks to me will resolve the issue for all perf-bench subcommands. I'm not sure if a good practice...

Well, it will achieve what you want, right? Unbuffered output, which I think is desirable.

Quoted 1 line

This probably is version 2 of this patch:

Yeah, looks ok.

- Arnaldo

Quoted 101 lines
tools/perf/builtin-bench.c +2 -2
index d0895162c2ba..a0b812cde1ee 100644
@@ -226,7 +226,6 @@ static void run_collection(struct collection *coll)
                if (!bench->fn)
                        break;
                printf("# Running %s/%s benchmark...\n", coll->name, bench->name);
-               fflush(stdout);
 
                argv[1] = bench->name;
                run_bench(coll->name, bench->name, bench->fn, 1, argv);
@@ -247,6 +246,8 @@ int cmd_bench(int argc, const char **argv)
        struct collection *coll;
        int ret = 0;
 
+       setvbuf(stdout, NULL, _IONBF, 0);
+
        if (argc < 2) {
                /* No collection specified. */
                print_usage();
@@ -300,7 +301,6 @@ int cmd_bench(int argc, const char **argv)
 
                        if (bench_format == BENCH_FORMAT_DEFAULT)
                                printf("# Running '%s/%s' benchmark:\n", coll->name, bench->name);
-                       fflush(stdout);
                        ret = run_bench(coll->name, bench->name, bench->fn, argc-1, argv+1);
                        goto end;
                }

Thanks,
- Sohaib

Quoted 65 lines
Quoted 60 lines

- Arnaldo

Quoted 53 lines

E.g.

$ perf bench internals synthesize -t
< output comes out fine after each test run >
$ perf bench internals synthesize -t | tee file.txt
< output comes out only at the end of all tests >

See, also:

$ perf bench mem all | tee file.txt
$ perf bench sched all | tee file.txt
$ perf bench internals all -t | tee file.txt
$ perf bench internals all | tee file.txt
---
 tools/perf/bench/find-bit-bench.c | 1 +
 tools/perf/bench/synthesize.c     | 2 ++
 2 files changed, 3 insertions(+)
tools/perf/bench/find-bit-bench.c +1 -0
index 22b5cfe97023..39fb4184fbcb 100644
@@ -113,6 +113,7 @@ static int do_for_each_set_bit(unsigned int num_bits)
 
 		if (use_of_val == accumulator)  /* Try to avoid compiler tricks. */
 			printf("\n");
+		fflush(stdout);
 	}
 	bitmap_free(to_test);
 	return 0;
tools/perf/bench/synthesize.c +2 -1
index 7401ebbac100..d21d311df61b 100644
@@ -92,6 +92,7 @@ static int do_run_single_threaded(struct perf_session *session,
 		update_stats(&event_stats, atomic_read(&event_count));
 	}
 
+	fflush(stdout);
 	time_average = avg_stats(&time_stats);
 	time_stddev = stddev_stats(&time_stats);
 	printf("  Average %ssynthesis took: %.3f usec (+- %.3f usec)\n",
@@ -186,6 +187,7 @@ static int do_run_multi_threaded(struct target *target,
 		perf_session__delete(session);
 	}
 
+	fflush(stdout);
 	time_average = avg_stats(&time_stats);
 	time_stddev = stddev_stats(&time_stats);
 	printf("    Average synthesis took: %.3f usec (+- %.3f usec)\n",
--
Quoted 1 line

2.25.1

--

Quoted 1 line

- Arnaldo

--

- Arnaldo

Signature · 1 line
- Arnaldo