All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCHv2 0/7] perf tools: Add switch-output size and time threshold options
@ 2017-01-09  9:51 Jiri Olsa
  2017-01-09  9:51 ` [PATCH 1/6] perf tools: Add unit_number__scnprint function Jiri Olsa
                   ` (5 more replies)
  0 siblings, 6 replies; 14+ messages in thread
From: Jiri Olsa @ 2017-01-09  9:51 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo
  Cc: Wang Nan, lkml, Ingo Molnar, Peter Zijlstra, Namhyung Kim, David Ahern

hi,
adding a way to configure switch data output
for size and time, like:

  $ sudo perf record -e 'sched:*' --switch-output=10M -avg
  callchain: type FP
  switch-output with 10M size threshold
  mmap size 528384B
  [ perf record: dump data: Woken up 37 times ]
  [ perf record: Dump perf.data.2017010309135512 ]
  [ perf record: dump data: Woken up 39 times ]
  [ perf record: Dump perf.data.2017010309135771 ]
  [ perf record: dump data: Woken up 38 times ]
  [ perf record: Dump perf.data.2017010309140005 ]
  ^C[ perf record: Woken up 16 times to write data ]
  [ perf record: Dump perf.data.2017010309140111 ]
  [ perf record: Captured and wrote 4.748 MB perf.data.<timestamp> ]
  ...

the default for switch-output option stays and does the SIGUSR2 output switch

v2 changes:
  - some patches already accepted
  - add warning for switched size lower than ring buffer wakeup size
  - got Wang Nan's Tested-by, but did not include it
    due to changes I made in v2

Also available in:
  git://git.kernel.org/pub/scm/linux/kernel/git/jolsa/perf.git
  perf/fixes

thanks,
jirka


Cc: Wang Nan <wangnan0@huawei.com>
---
Jiri Olsa (6):
      perf tools: Add unit_number__scnprint function
      perf record: Add struct switch_output
      perf record: Change switch-output option to take optional argument
      perf record: Add switch-output size option argument
      perf record: Add switch-output size warning
      perf record: Add switch-output time option argument

 tools/perf/Documentation/perf-record.txt |  14 +++++++--
 tools/perf/builtin-record.c              | 154 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------
 tools/perf/tests/Build                   |   1 +
 tools/perf/tests/builtin-test.c          |   4 +++
 tools/perf/tests/tests.h                 |   1 +
 tools/perf/tests/unit_number__scnprint.c |  37 ++++++++++++++++++++++
 tools/perf/util/evlist.c                 |  10 ++++--
 tools/perf/util/evlist.h                 |   2 ++
 tools/perf/util/util.c                   |  13 ++++++++
 tools/perf/util/util.h                   |   1 +
 10 files changed, 219 insertions(+), 18 deletions(-)
 create mode 100644 tools/perf/tests/unit_number__scnprint.c

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [PATCH 1/6] perf tools: Add unit_number__scnprint function
  2017-01-09  9:51 [PATCHv2 0/7] perf tools: Add switch-output size and time threshold options Jiri Olsa
@ 2017-01-09  9:51 ` Jiri Olsa
  2017-01-10 13:22   ` Arnaldo Carvalho de Melo
  2017-01-12  8:31   ` [tip:perf/core] perf tools: Add unit_number__scnprintf function tip-bot for Jiri Olsa
  2017-01-09  9:51 ` [PATCH 2/6] perf record: Add struct switch_output Jiri Olsa
                   ` (4 subsequent siblings)
  5 siblings, 2 replies; 14+ messages in thread
From: Jiri Olsa @ 2017-01-09  9:51 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo
  Cc: David Ahern, Namhyung Kim, Peter Zijlstra, Wang Nan, lkml, Ingo Molnar

Adding unit_number__scnprint function to display
size units and use it in -m option info message.

Before:
  $ perf record -m 10M ls
  rounding mmap pages size to 16777216 bytes (4096 pages)
  ...

After:
  $ perf record -m 10M ls
  rounding mmap pages size to 16M (4096 pages)
  ...

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Cc: David Ahern <dsahern@gmail.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Wang Nan <wangnan0@huawei.com>
Link: http://lkml.kernel.org/n/tip-0gafw11lesghnbfoiw90kc3q@git.kernel.org
---
 tools/perf/tests/Build                   |  1 +
 tools/perf/tests/builtin-test.c          |  4 ++++
 tools/perf/tests/tests.h                 |  1 +
 tools/perf/tests/unit_number__scnprint.c | 37 ++++++++++++++++++++++++++++++++
 tools/perf/util/evlist.c                 |  8 +++++--
 tools/perf/util/util.c                   | 13 +++++++++++
 tools/perf/util/util.h                   |  1 +
 7 files changed, 63 insertions(+), 2 deletions(-)
 create mode 100644 tools/perf/tests/unit_number__scnprint.c

diff --git a/tools/perf/tests/Build b/tools/perf/tests/Build
index 6676c2dd6dcb..4f653d257225 100644
--- a/tools/perf/tests/Build
+++ b/tools/perf/tests/Build
@@ -44,6 +44,7 @@ perf-y += is_printable_array.o
 perf-y += bitmap.o
 perf-y += perf-hooks.o
 perf-y += clang.o
+perf-y += unit_number__scnprint.o
 
 $(OUTPUT)tests/llvm-src-base.c: tests/bpf-script-example.c tests/Build
 	$(call rule_mkdir)
diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-test.c
index a77dcc0d24e3..5d93c41803d3 100644
--- a/tools/perf/tests/builtin-test.c
+++ b/tools/perf/tests/builtin-test.c
@@ -247,6 +247,10 @@ static struct test generic_tests[] = {
 		}
 	},
 	{
+		.desc = "unit_number__scnprint",
+		.func = test__unit_number__scnprint,
+	},
+	{
 		.func = NULL,
 	},
 };
diff --git a/tools/perf/tests/tests.h b/tools/perf/tests/tests.h
index a512f0c8ff5b..1fa9b9d83aa5 100644
--- a/tools/perf/tests/tests.h
+++ b/tools/perf/tests/tests.h
@@ -96,6 +96,7 @@ int test__perf_hooks(int subtest);
 int test__clang(int subtest);
 const char *test__clang_subtest_get_desc(int subtest);
 int test__clang_subtest_get_nr(void);
+int test__unit_number__scnprint(int subtest);
 
 #if defined(__arm__) || defined(__aarch64__)
 #ifdef HAVE_DWARF_UNWIND_SUPPORT
diff --git a/tools/perf/tests/unit_number__scnprint.c b/tools/perf/tests/unit_number__scnprint.c
new file mode 100644
index 000000000000..b336024b6c6a
--- /dev/null
+++ b/tools/perf/tests/unit_number__scnprint.c
@@ -0,0 +1,37 @@
+#include <linux/compiler.h>
+#include <linux/types.h>
+#include "tests.h"
+#include "util.h"
+#include "debug.h"
+
+int test__unit_number__scnprint(int subtest __maybe_unused)
+{
+	struct {
+		u64		 n;
+		const char	*str;
+	} test[] = {
+		{ 1,			"1B"	},
+		{ 10*1024,		"10K"	},
+		{ 20*1024*1024,		"20M"	},
+		{ 30*1024*1024*1024ULL,	"30G"	},
+		{ 0,			"0B"	},
+		{ 0,			NULL	},
+	};
+	unsigned i = 0;
+
+	while (test[i].str) {
+		char buf[100];
+
+		unit_number__scnprint(buf, sizeof(buf), test[i].n);
+
+		pr_debug("n %" PRIu64 ", str '%s', buf '%s'\n",
+			 test[i].n, test[i].str, buf);
+
+		if (strcmp(test[i].str, buf))
+			return TEST_FAIL;
+
+		i++;
+	}
+
+	return TEST_OK;
+}
diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c
index d92e02006fb8..435ff1c94a66 100644
--- a/tools/perf/util/evlist.c
+++ b/tools/perf/util/evlist.c
@@ -1224,12 +1224,16 @@ static long parse_pages_arg(const char *str, unsigned long min,
 	if (pages == 0 && min == 0) {
 		/* leave number of pages at 0 */
 	} else if (!is_power_of_2(pages)) {
+		char buf[100];
+
 		/* round pages up to next power of 2 */
 		pages = roundup_pow_of_two(pages);
 		if (!pages)
 			return -EINVAL;
-		pr_info("rounding mmap pages size to %lu bytes (%lu pages)\n",
-			pages * page_size, pages);
+
+		unit_number__scnprint(buf, sizeof(buf), pages * page_size);
+		pr_info("rounding mmap pages size to %s (%lu pages)\n",
+			buf, pages);
 	}
 
 	if (pages > max)
diff --git a/tools/perf/util/util.c b/tools/perf/util/util.c
index 9ddd98827d12..c2b941dd8826 100644
--- a/tools/perf/util/util.c
+++ b/tools/perf/util/util.c
@@ -789,3 +789,16 @@ int is_printable_array(char *p, unsigned int len)
 	}
 	return 1;
 }
+
+int unit_number__scnprint(char *buf, size_t size, u64 n)
+{
+	char unit[4] = "BKMG";
+	int i = 0;
+
+	while (((n / 1024) > 1) && (i < 3)) {
+		n /= 1024;
+		i++;
+	}
+
+	return scnprintf(buf, size, "%" PRIu64 "%c", n, unit[i]);
+}
diff --git a/tools/perf/util/util.h b/tools/perf/util/util.h
index 1d639e38aa82..e2a627cd11dd 100644
--- a/tools/perf/util/util.h
+++ b/tools/perf/util/util.h
@@ -363,4 +363,5 @@ int is_printable_array(char *p, unsigned int len);
 
 int timestamp__scnprintf_usec(u64 timestamp, char *buf, size_t sz);
 
+int unit_number__scnprint(char *buf, size_t size, u64 n);
 #endif /* GIT_COMPAT_UTIL_H */
-- 
2.7.4

^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH 2/6] perf record: Add struct switch_output
  2017-01-09  9:51 [PATCHv2 0/7] perf tools: Add switch-output size and time threshold options Jiri Olsa
  2017-01-09  9:51 ` [PATCH 1/6] perf tools: Add unit_number__scnprint function Jiri Olsa
@ 2017-01-09  9:51 ` Jiri Olsa
  2017-01-12  8:32   ` [tip:perf/core] " tip-bot for Jiri Olsa
  2017-01-09  9:51 ` [PATCH 3/6] perf record: Change switch-output option to take optional argument Jiri Olsa
                   ` (3 subsequent siblings)
  5 siblings, 1 reply; 14+ messages in thread
From: Jiri Olsa @ 2017-01-09  9:51 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo
  Cc: David Ahern, Namhyung Kim, Peter Zijlstra, Wang Nan, lkml, Ingo Molnar

Next patches will add more --switch-output option arguments,
so preparing the data holder.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Cc: David Ahern <dsahern@gmail.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Wang Nan <wangnan0@huawei.com>
Link: http://lkml.kernel.org/n/tip-tibmeq1qw3kgrfzrswt5xypx@git.kernel.org
---
 tools/perf/builtin-record.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)

diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
index 4ec10e9427d9..f7e805b30527 100644
--- a/tools/perf/builtin-record.c
+++ b/tools/perf/builtin-record.c
@@ -46,6 +46,10 @@
 #include <asm/bug.h>
 #include <linux/time64.h>
 
+struct switch_output {
+	bool		 signal;
+};
+
 struct record {
 	struct perf_tool	tool;
 	struct record_opts	opts;
@@ -62,7 +66,7 @@ struct record {
 	bool			no_buildid_cache_set;
 	bool			buildid_all;
 	bool			timestamp_filename;
-	bool			switch_output;
+	struct switch_output	switch_output;
 	unsigned long long	samples;
 };
 
@@ -842,11 +846,11 @@ static int __cmd_record(struct record *rec, int argc, const char **argv)
 	signal(SIGTERM, sig_handler);
 	signal(SIGSEGV, sigsegv_handler);
 
-	if (rec->opts.auxtrace_snapshot_mode || rec->switch_output) {
+	if (rec->opts.auxtrace_snapshot_mode || rec->switch_output.signal) {
 		signal(SIGUSR2, snapshot_sig_handler);
 		if (rec->opts.auxtrace_snapshot_mode)
 			trigger_on(&auxtrace_snapshot_trigger);
-		if (rec->switch_output)
+		if (rec->switch_output.signal)
 			trigger_on(&switch_output_trigger);
 	} else {
 		signal(SIGUSR2, SIG_IGN);
@@ -1519,7 +1523,7 @@ static struct option __record_options[] = {
 		    "Record build-id of all DSOs regardless of hits"),
 	OPT_BOOLEAN(0, "timestamp-filename", &record.timestamp_filename,
 		    "append timestamp to output filename"),
-	OPT_BOOLEAN(0, "switch-output", &record.switch_output,
+	OPT_BOOLEAN(0, "switch-output", &record.switch_output.signal,
 		    "Switch output when receive SIGUSR2"),
 	OPT_BOOLEAN(0, "dry-run", &dry_run,
 		    "Parse options then exit"),
@@ -1578,7 +1582,7 @@ int cmd_record(int argc, const char **argv, const char *prefix __maybe_unused)
 		return -EINVAL;
 	}
 
-	if (rec->switch_output)
+	if (rec->switch_output.signal)
 		rec->timestamp_filename = true;
 
 	if (!rec->itr) {
@@ -1629,7 +1633,7 @@ int cmd_record(int argc, const char **argv, const char *prefix __maybe_unused)
 
 	if (rec->no_buildid_cache || rec->no_buildid) {
 		disable_buildid_cache();
-	} else if (rec->switch_output) {
+	} else if (rec->switch_output.signal) {
 		/*
 		 * In 'perf record --switch-output', disable buildid
 		 * generation by default to reduce data file switching
-- 
2.7.4

^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH 3/6] perf record: Change switch-output option to take optional argument
  2017-01-09  9:51 [PATCHv2 0/7] perf tools: Add switch-output size and time threshold options Jiri Olsa
  2017-01-09  9:51 ` [PATCH 1/6] perf tools: Add unit_number__scnprint function Jiri Olsa
  2017-01-09  9:51 ` [PATCH 2/6] perf record: Add struct switch_output Jiri Olsa
@ 2017-01-09  9:51 ` Jiri Olsa
  2017-01-12  8:32   ` [tip:perf/core] " tip-bot for Jiri Olsa
  2017-01-09  9:51 ` [PATCH 4/6] perf record: Add switch-output size option argument Jiri Olsa
                   ` (2 subsequent siblings)
  5 siblings, 1 reply; 14+ messages in thread
From: Jiri Olsa @ 2017-01-09  9:51 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo
  Cc: David Ahern, Namhyung Kim, Peter Zijlstra, Wang Nan, lkml, Ingo Molnar

Next patches will add --switch-output option arguments,
changing the option to allow that and adding its default
value to 'signal'.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Cc: David Ahern <dsahern@gmail.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Wang Nan <wangnan0@huawei.com>
Link: http://lkml.kernel.org/n/tip-d3qy0tnc4m2dyt8yd6x5phyv@git.kernel.org
---
 tools/perf/builtin-record.c | 28 ++++++++++++++++++++++++++--
 1 file changed, 26 insertions(+), 2 deletions(-)

diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
index f7e805b30527..2bf811acaf8d 100644
--- a/tools/perf/builtin-record.c
+++ b/tools/perf/builtin-record.c
@@ -48,6 +48,8 @@
 
 struct switch_output {
 	bool		 signal;
+	const char	*str;
+	bool		 set;
 };
 
 struct record {
@@ -1356,6 +1358,22 @@ static int record__parse_mmap_pages(const struct option *opt,
 	return ret;
 }
 
+static int switch_output_setup(struct record *rec)
+{
+	struct switch_output *s = &rec->switch_output;
+
+	if (!s->set)
+		return 0;
+
+	if (!strcmp(s->str, "signal")) {
+		s->signal = true;
+		pr_debug("switch-output with SIGUSR2 signal\n");
+		return 0;
+	}
+
+	return -1;
+}
+
 static const char * const __record_usage[] = {
 	"perf record [<options>] [<command>]",
 	"perf record [<options>] -- <command> [<options>]",
@@ -1523,8 +1541,9 @@ static struct option __record_options[] = {
 		    "Record build-id of all DSOs regardless of hits"),
 	OPT_BOOLEAN(0, "timestamp-filename", &record.timestamp_filename,
 		    "append timestamp to output filename"),
-	OPT_BOOLEAN(0, "switch-output", &record.switch_output.signal,
-		    "Switch output when receive SIGUSR2"),
+	OPT_STRING_OPTARG_SET(0, "switch-output", &record.switch_output.str,
+			  &record.switch_output.set, "signal",
+			  "Switch output when receive SIGUSR2", "signal"),
 	OPT_BOOLEAN(0, "dry-run", &dry_run,
 		    "Parse options then exit"),
 	OPT_END()
@@ -1582,6 +1601,11 @@ int cmd_record(int argc, const char **argv, const char *prefix __maybe_unused)
 		return -EINVAL;
 	}
 
+	if (switch_output_setup(rec)) {
+		parse_options_usage(record_usage, record_options, "switch-output", 0);
+		return -EINVAL;
+	}
+
 	if (rec->switch_output.signal)
 		rec->timestamp_filename = true;
 
-- 
2.7.4

^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH 4/6] perf record: Add switch-output size option argument
  2017-01-09  9:51 [PATCHv2 0/7] perf tools: Add switch-output size and time threshold options Jiri Olsa
                   ` (2 preceding siblings ...)
  2017-01-09  9:51 ` [PATCH 3/6] perf record: Change switch-output option to take optional argument Jiri Olsa
@ 2017-01-09  9:51 ` Jiri Olsa
  2017-01-12  8:33   ` [tip:perf/core] " tip-bot for Jiri Olsa
  2017-01-09  9:51 ` [PATCH 5/6] perf record: Add switch-output size warning Jiri Olsa
  2017-01-09  9:52 ` [PATCH 6/6] perf record: Add switch-output time option argument Jiri Olsa
  5 siblings, 1 reply; 14+ messages in thread
From: Jiri Olsa @ 2017-01-09  9:51 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo
  Cc: David Ahern, Namhyung Kim, Peter Zijlstra, Wang Nan, lkml, Ingo Molnar

It's now possible to specify the threshold size for
perf.data like:

  $ perf record --switch-output=2G ...

Once it's reached, the current data are dumped in to the
perf.data.<timestamp> file and session does on.

  $ perf record --switch-output=2G ...
  [ perf record: dump data: Woken up 7244 times ]
  [ perf record: Dump perf.data.2017010214093746 ]
  ...

The size is expected to be a number with appended unit
character - B/K/M/G.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Cc: David Ahern <dsahern@gmail.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Wang Nan <wangnan0@huawei.com>
Link: http://lkml.kernel.org/n/tip-xw4x8qj6aojox378q9ley29e@git.kernel.org
---
 tools/perf/Documentation/perf-record.txt | 12 +++++-
 tools/perf/builtin-record.c              | 67 +++++++++++++++++++++++++-------
 2 files changed, 63 insertions(+), 16 deletions(-)

diff --git a/tools/perf/Documentation/perf-record.txt b/tools/perf/Documentation/perf-record.txt
index 5054d9147f0f..3d55d2fd48b3 100644
--- a/tools/perf/Documentation/perf-record.txt
+++ b/tools/perf/Documentation/perf-record.txt
@@ -421,9 +421,17 @@ Configure all used events to run in user space.
 --timestamp-filename
 Append timestamp to output file name.
 
---switch-output::
+--switch-output[=mode]::
 Generate multiple perf.data files, timestamp prefixed, switching to a new one
-when receiving a SIGUSR2.
+based on 'mode' value:
+  "signal" - when receiving a SIGUSR2 (default value) or
+  <size>   - when reaching the size threshold, size is expected to
+             be a number with appended unit character - B/K/M/G
+
+             Note: the precision of  the size  threshold  hugely depends
+             on your configuration  - the number and size of  your  ring
+             buffers (-m). It is generally more precise for higher sizes
+             (like >5M), for lower values expect different sizes.
 
 A possible use case is to, given an external event, slice the perf.data file
 that gets then processed, possibly via a perf script, to decide if that
diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
index 2bf811acaf8d..3fa64492ee62 100644
--- a/tools/perf/builtin-record.c
+++ b/tools/perf/builtin-record.c
@@ -47,7 +47,9 @@
 #include <linux/time64.h>
 
 struct switch_output {
+	bool		 enabled;
 	bool		 signal;
+	unsigned long	 size;
 	const char	*str;
 	bool		 set;
 };
@@ -72,6 +74,23 @@ struct record {
 	unsigned long long	samples;
 };
 
+static volatile int auxtrace_record__snapshot_started;
+static DEFINE_TRIGGER(auxtrace_snapshot_trigger);
+static DEFINE_TRIGGER(switch_output_trigger);
+
+static bool switch_output_signal(struct record *rec)
+{
+	return rec->switch_output.signal &&
+	       trigger_is_ready(&switch_output_trigger);
+}
+
+static bool switch_output_size(struct record *rec)
+{
+	return rec->switch_output.size &&
+	       trigger_is_ready(&switch_output_trigger) &&
+	       (rec->bytes_written >= rec->switch_output.size);
+}
+
 static int record__write(struct record *rec, void *bf, size_t size)
 {
 	if (perf_data_file__write(rec->session->file, bf, size) < 0) {
@@ -80,6 +99,10 @@ static int record__write(struct record *rec, void *bf, size_t size)
 	}
 
 	rec->bytes_written += size;
+
+	if (switch_output_size(rec))
+		trigger_hit(&switch_output_trigger);
+
 	return 0;
 }
 
@@ -199,10 +222,6 @@ static volatile int done;
 static volatile int signr = -1;
 static volatile int child_finished;
 
-static volatile int auxtrace_record__snapshot_started;
-static DEFINE_TRIGGER(auxtrace_snapshot_trigger);
-static DEFINE_TRIGGER(switch_output_trigger);
-
 static void sig_handler(int sig)
 {
 	if (sig == SIGCHLD)
@@ -848,11 +867,11 @@ static int __cmd_record(struct record *rec, int argc, const char **argv)
 	signal(SIGTERM, sig_handler);
 	signal(SIGSEGV, sigsegv_handler);
 
-	if (rec->opts.auxtrace_snapshot_mode || rec->switch_output.signal) {
+	if (rec->opts.auxtrace_snapshot_mode || rec->switch_output.enabled) {
 		signal(SIGUSR2, snapshot_sig_handler);
 		if (rec->opts.auxtrace_snapshot_mode)
 			trigger_on(&auxtrace_snapshot_trigger);
-		if (rec->switch_output.signal)
+		if (rec->switch_output.enabled)
 			trigger_on(&switch_output_trigger);
 	} else {
 		signal(SIGUSR2, SIG_IGN);
@@ -1361,6 +1380,14 @@ static int record__parse_mmap_pages(const struct option *opt,
 static int switch_output_setup(struct record *rec)
 {
 	struct switch_output *s = &rec->switch_output;
+	static struct parse_tag tags_size[] = {
+		{ .tag  = 'B', .mult = 1       },
+		{ .tag  = 'K', .mult = 1 << 10 },
+		{ .tag  = 'M', .mult = 1 << 20 },
+		{ .tag  = 'G', .mult = 1 << 30 },
+		{ .tag  = 0 },
+	};
+	unsigned long val;
 
 	if (!s->set)
 		return 0;
@@ -1368,10 +1395,22 @@ static int switch_output_setup(struct record *rec)
 	if (!strcmp(s->str, "signal")) {
 		s->signal = true;
 		pr_debug("switch-output with SIGUSR2 signal\n");
-		return 0;
+		goto enabled;
+	}
+
+	val = parse_tag_value(s->str, tags_size);
+	if (val != (unsigned long) -1) {
+		s->size = val;
+		pr_debug("switch-output with %s size threshold\n", s->str);
+		goto enabled;
 	}
 
 	return -1;
+
+enabled:
+	rec->timestamp_filename = true;
+	s->enabled              = true;
+	return 0;
 }
 
 static const char * const __record_usage[] = {
@@ -1542,8 +1581,9 @@ static struct option __record_options[] = {
 	OPT_BOOLEAN(0, "timestamp-filename", &record.timestamp_filename,
 		    "append timestamp to output filename"),
 	OPT_STRING_OPTARG_SET(0, "switch-output", &record.switch_output.str,
-			  &record.switch_output.set, "signal",
-			  "Switch output when receive SIGUSR2", "signal"),
+			  &record.switch_output.set, "signal,size",
+			  "Switch output when receive SIGUSR2 or cross size threshold",
+			  "signal"),
 	OPT_BOOLEAN(0, "dry-run", &dry_run,
 		    "Parse options then exit"),
 	OPT_END()
@@ -1606,9 +1646,6 @@ int cmd_record(int argc, const char **argv, const char *prefix __maybe_unused)
 		return -EINVAL;
 	}
 
-	if (rec->switch_output.signal)
-		rec->timestamp_filename = true;
-
 	if (!rec->itr) {
 		rec->itr = auxtrace_record__init(rec->evlist, &err);
 		if (err)
@@ -1657,7 +1694,7 @@ int cmd_record(int argc, const char **argv, const char *prefix __maybe_unused)
 
 	if (rec->no_buildid_cache || rec->no_buildid) {
 		disable_buildid_cache();
-	} else if (rec->switch_output.signal) {
+	} else if (rec->switch_output.enabled) {
 		/*
 		 * In 'perf record --switch-output', disable buildid
 		 * generation by default to reduce data file switching
@@ -1749,6 +1786,8 @@ int cmd_record(int argc, const char **argv, const char *prefix __maybe_unused)
 
 static void snapshot_sig_handler(int sig __maybe_unused)
 {
+	struct record *rec = &record;
+
 	if (trigger_is_ready(&auxtrace_snapshot_trigger)) {
 		trigger_hit(&auxtrace_snapshot_trigger);
 		auxtrace_record__snapshot_started = 1;
@@ -1756,6 +1795,6 @@ static void snapshot_sig_handler(int sig __maybe_unused)
 			trigger_error(&auxtrace_snapshot_trigger);
 	}
 
-	if (trigger_is_ready(&switch_output_trigger))
+	if (switch_output_signal(rec))
 		trigger_hit(&switch_output_trigger);
 }
-- 
2.7.4

^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH 5/6] perf record: Add switch-output size warning
  2017-01-09  9:51 [PATCHv2 0/7] perf tools: Add switch-output size and time threshold options Jiri Olsa
                   ` (3 preceding siblings ...)
  2017-01-09  9:51 ` [PATCH 4/6] perf record: Add switch-output size option argument Jiri Olsa
@ 2017-01-09  9:51 ` Jiri Olsa
  2017-01-12  8:33   ` [tip:perf/core] " tip-bot for Jiri Olsa
  2017-01-09  9:52 ` [PATCH 6/6] perf record: Add switch-output time option argument Jiri Olsa
  5 siblings, 1 reply; 14+ messages in thread
From: Jiri Olsa @ 2017-01-09  9:51 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo
  Cc: David Ahern, Namhyung Kim, Peter Zijlstra, Wang Nan, lkml, Ingo Molnar

Adding switch-output size warning if the requested
size of lower than the wakeup ring buffer size.

  $ perf record --switch-output=1K ls
  WARNING: switch-output data size lower than wakeup kernel buffer size (258K) expect bigger perf.data sizes
  ...

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Cc: David Ahern <dsahern@gmail.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Wang Nan <wangnan0@huawei.com>
Link: http://lkml.kernel.org/n/tip-0gafw11lesghnbfoiw90kc3q@git.kernel.org
---
 tools/perf/builtin-record.c | 21 +++++++++++++++++++++
 tools/perf/util/evlist.c    |  2 +-
 tools/perf/util/evlist.h    |  2 ++
 3 files changed, 24 insertions(+), 1 deletion(-)

diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
index 3fa64492ee62..4a70e964cd96 100644
--- a/tools/perf/builtin-record.c
+++ b/tools/perf/builtin-record.c
@@ -1377,6 +1377,23 @@ static int record__parse_mmap_pages(const struct option *opt,
 	return ret;
 }
 
+static void switch_output_size_warn(struct record *rec)
+{
+	u64 wakeup_size = perf_evlist__mmap_size(rec->opts.mmap_pages);
+	struct switch_output *s = &rec->switch_output;
+
+	wakeup_size /= 2;
+
+	if (s->size < wakeup_size) {
+		char buf[100];
+
+		unit_number__scnprint(buf, sizeof(buf), wakeup_size);
+		pr_warning("WARNING: switch-output data size lower than "
+			   "wakeup kernel buffer size (%s) "
+			   "expect bigger perf.data sizes\n", buf);
+	}
+}
+
 static int switch_output_setup(struct record *rec)
 {
 	struct switch_output *s = &rec->switch_output;
@@ -1410,6 +1427,10 @@ static int switch_output_setup(struct record *rec)
 enabled:
 	rec->timestamp_filename = true;
 	s->enabled              = true;
+
+	if (s->size && !rec->opts.no_buffering)
+		switch_output_size_warn(rec);
+
 	return 0;
 }
 
diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c
index 435ff1c94a66..9900ce5177bc 100644
--- a/tools/perf/util/evlist.c
+++ b/tools/perf/util/evlist.c
@@ -1184,7 +1184,7 @@ unsigned long perf_event_mlock_kb_in_pages(void)
 	return pages;
 }
 
-static size_t perf_evlist__mmap_size(unsigned long pages)
+size_t perf_evlist__mmap_size(unsigned long pages)
 {
 	if (pages == UINT_MAX)
 		pages = perf_event_mlock_kb_in_pages();
diff --git a/tools/perf/util/evlist.h b/tools/perf/util/evlist.h
index 4fd034f22d2f..389b9ccdf8c7 100644
--- a/tools/perf/util/evlist.h
+++ b/tools/perf/util/evlist.h
@@ -218,6 +218,8 @@ int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages,
 		      bool overwrite);
 void perf_evlist__munmap(struct perf_evlist *evlist);
 
+size_t perf_evlist__mmap_size(unsigned long pages);
+
 void perf_evlist__disable(struct perf_evlist *evlist);
 void perf_evlist__enable(struct perf_evlist *evlist);
 void perf_evlist__toggle_enable(struct perf_evlist *evlist);
-- 
2.7.4

^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH 6/6] perf record: Add switch-output time option argument
  2017-01-09  9:51 [PATCHv2 0/7] perf tools: Add switch-output size and time threshold options Jiri Olsa
                   ` (4 preceding siblings ...)
  2017-01-09  9:51 ` [PATCH 5/6] perf record: Add switch-output size warning Jiri Olsa
@ 2017-01-09  9:52 ` Jiri Olsa
  2017-01-12  8:34   ` [tip:perf/core] " tip-bot for Jiri Olsa
  5 siblings, 1 reply; 14+ messages in thread
From: Jiri Olsa @ 2017-01-09  9:52 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo
  Cc: David Ahern, Namhyung Kim, Peter Zijlstra, Wang Nan, lkml, Ingo Molnar

It's now possible to specify the threshold time for
perf.data like:

  $ perf record --switch-output=30s ...

Once it's reached, the current data are dumped in to the
perf.data.<timestamp> file and session does on.

  $ perf record --switch-output=30s ...
  [ perf record: dump data: Woken up 44 times ]
  [ perf record: Dump perf.data.2017010213043746 ]
  ...

The time is expected to be a number with appended unit
character - s/m/h/d.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Cc: David Ahern <dsahern@gmail.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Wang Nan <wangnan0@huawei.com>
Link: http://lkml.kernel.org/n/tip-2u6y3h89c0guejpn1nwv32lc@git.kernel.org
---
 tools/perf/Documentation/perf-record.txt |  2 ++
 tools/perf/builtin-record.c              | 44 ++++++++++++++++++++++++++++++--
 2 files changed, 44 insertions(+), 2 deletions(-)

diff --git a/tools/perf/Documentation/perf-record.txt b/tools/perf/Documentation/perf-record.txt
index 3d55d2fd48b3..27256bc68eda 100644
--- a/tools/perf/Documentation/perf-record.txt
+++ b/tools/perf/Documentation/perf-record.txt
@@ -427,6 +427,8 @@ based on 'mode' value:
   "signal" - when receiving a SIGUSR2 (default value) or
   <size>   - when reaching the size threshold, size is expected to
              be a number with appended unit character - B/K/M/G
+  <time>   - when reaching the time threshold, size is expected to
+             be a number with appended unit character - s/m/h/d
 
              Note: the precision of  the size  threshold  hugely depends
              on your configuration  - the number and size of  your  ring
diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
index 4a70e964cd96..4e437dba60dd 100644
--- a/tools/perf/builtin-record.c
+++ b/tools/perf/builtin-record.c
@@ -50,6 +50,7 @@ struct switch_output {
 	bool		 enabled;
 	bool		 signal;
 	unsigned long	 size;
+	unsigned long	 time;
 	const char	*str;
 	bool		 set;
 };
@@ -91,6 +92,12 @@ static bool switch_output_size(struct record *rec)
 	       (rec->bytes_written >= rec->switch_output.size);
 }
 
+static bool switch_output_time(struct record *rec)
+{
+	return rec->switch_output.time &&
+	       trigger_is_ready(&switch_output_trigger);
+}
+
 static int record__write(struct record *rec, void *bf, size_t size)
 {
 	if (perf_data_file__write(rec->session->file, bf, size) < 0) {
@@ -737,6 +744,7 @@ static void workload_exec_failed_signal(int signo __maybe_unused,
 }
 
 static void snapshot_sig_handler(int sig);
+static void alarm_sig_handler(int sig);
 
 int __weak
 perf_event__synth_time_conv(const struct perf_event_mmap_page *pc __maybe_unused,
@@ -1068,6 +1076,10 @@ static int __cmd_record(struct record *rec, int argc, const char **argv)
 				err = fd;
 				goto out_child;
 			}
+
+			/* re-arm the alarm */
+			if (rec->switch_output.time)
+				alarm(rec->switch_output.time);
 		}
 
 		if (hits == rec->samples) {
@@ -1404,6 +1416,13 @@ static int switch_output_setup(struct record *rec)
 		{ .tag  = 'G', .mult = 1 << 30 },
 		{ .tag  = 0 },
 	};
+	static struct parse_tag tags_time[] = {
+		{ .tag  = 's', .mult = 1        },
+		{ .tag  = 'm', .mult = 60       },
+		{ .tag  = 'h', .mult = 60*60    },
+		{ .tag  = 'd', .mult = 60*60*24 },
+		{ .tag  = 0 },
+	};
 	unsigned long val;
 
 	if (!s->set)
@@ -1422,6 +1441,14 @@ static int switch_output_setup(struct record *rec)
 		goto enabled;
 	}
 
+	val = parse_tag_value(s->str, tags_time);
+	if (val != (unsigned long) -1) {
+		s->time = val;
+		pr_debug("switch-output with %s time threshold (%lu seconds)\n",
+			 s->str, s->time);
+		goto enabled;
+	}
+
 	return -1;
 
 enabled:
@@ -1602,8 +1629,8 @@ static struct option __record_options[] = {
 	OPT_BOOLEAN(0, "timestamp-filename", &record.timestamp_filename,
 		    "append timestamp to output filename"),
 	OPT_STRING_OPTARG_SET(0, "switch-output", &record.switch_output.str,
-			  &record.switch_output.set, "signal,size",
-			  "Switch output when receive SIGUSR2 or cross size threshold",
+			  &record.switch_output.set, "signal,size,time",
+			  "Switch output when receive SIGUSR2 or cross size,time threshold",
 			  "signal"),
 	OPT_BOOLEAN(0, "dry-run", &dry_run,
 		    "Parse options then exit"),
@@ -1667,6 +1694,11 @@ int cmd_record(int argc, const char **argv, const char *prefix __maybe_unused)
 		return -EINVAL;
 	}
 
+	if (rec->switch_output.time) {
+		signal(SIGALRM, alarm_sig_handler);
+		alarm(rec->switch_output.time);
+	}
+
 	if (!rec->itr) {
 		rec->itr = auxtrace_record__init(rec->evlist, &err);
 		if (err)
@@ -1819,3 +1851,11 @@ static void snapshot_sig_handler(int sig __maybe_unused)
 	if (switch_output_signal(rec))
 		trigger_hit(&switch_output_trigger);
 }
+
+static void alarm_sig_handler(int sig __maybe_unused)
+{
+	struct record *rec = &record;
+
+	if (switch_output_time(rec))
+		trigger_hit(&switch_output_trigger);
+}
-- 
2.7.4

^ permalink raw reply related	[flat|nested] 14+ messages in thread

* Re: [PATCH 1/6] perf tools: Add unit_number__scnprint function
  2017-01-09  9:51 ` [PATCH 1/6] perf tools: Add unit_number__scnprint function Jiri Olsa
@ 2017-01-10 13:22   ` Arnaldo Carvalho de Melo
  2017-01-12  8:31   ` [tip:perf/core] perf tools: Add unit_number__scnprintf function tip-bot for Jiri Olsa
  1 sibling, 0 replies; 14+ messages in thread
From: Arnaldo Carvalho de Melo @ 2017-01-10 13:22 UTC (permalink / raw)
  To: Jiri Olsa
  Cc: David Ahern, Namhyung Kim, Peter Zijlstra, Wang Nan, lkml, Ingo Molnar

Em Mon, Jan 09, 2017 at 10:51:55AM +0100, Jiri Olsa escreveu:
> Adding unit_number__scnprint function to display

Renaming to unit_number__scnprintf() for consistency.

- Arnaldo

> size units and use it in -m option info message.
> 
> Before:
>   $ perf record -m 10M ls
>   rounding mmap pages size to 16777216 bytes (4096 pages)
>   ...
> 
> After:
>   $ perf record -m 10M ls
>   rounding mmap pages size to 16M (4096 pages)
>   ...
> 
> Signed-off-by: Jiri Olsa <jolsa@kernel.org>
> Cc: David Ahern <dsahern@gmail.com>
> Cc: Namhyung Kim <namhyung@kernel.org>
> Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
> Cc: Wang Nan <wangnan0@huawei.com>
> Link: http://lkml.kernel.org/n/tip-0gafw11lesghnbfoiw90kc3q@git.kernel.org
> ---
>  tools/perf/tests/Build                   |  1 +
>  tools/perf/tests/builtin-test.c          |  4 ++++
>  tools/perf/tests/tests.h                 |  1 +
>  tools/perf/tests/unit_number__scnprint.c | 37 ++++++++++++++++++++++++++++++++
>  tools/perf/util/evlist.c                 |  8 +++++--
>  tools/perf/util/util.c                   | 13 +++++++++++
>  tools/perf/util/util.h                   |  1 +
>  7 files changed, 63 insertions(+), 2 deletions(-)
>  create mode 100644 tools/perf/tests/unit_number__scnprint.c
> 
> diff --git a/tools/perf/tests/Build b/tools/perf/tests/Build
> index 6676c2dd6dcb..4f653d257225 100644
> --- a/tools/perf/tests/Build
> +++ b/tools/perf/tests/Build
> @@ -44,6 +44,7 @@ perf-y += is_printable_array.o
>  perf-y += bitmap.o
>  perf-y += perf-hooks.o
>  perf-y += clang.o
> +perf-y += unit_number__scnprint.o
>  
>  $(OUTPUT)tests/llvm-src-base.c: tests/bpf-script-example.c tests/Build
>  	$(call rule_mkdir)
> diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-test.c
> index a77dcc0d24e3..5d93c41803d3 100644
> --- a/tools/perf/tests/builtin-test.c
> +++ b/tools/perf/tests/builtin-test.c
> @@ -247,6 +247,10 @@ static struct test generic_tests[] = {
>  		}
>  	},
>  	{
> +		.desc = "unit_number__scnprint",
> +		.func = test__unit_number__scnprint,
> +	},
> +	{
>  		.func = NULL,
>  	},
>  };
> diff --git a/tools/perf/tests/tests.h b/tools/perf/tests/tests.h
> index a512f0c8ff5b..1fa9b9d83aa5 100644
> --- a/tools/perf/tests/tests.h
> +++ b/tools/perf/tests/tests.h
> @@ -96,6 +96,7 @@ int test__perf_hooks(int subtest);
>  int test__clang(int subtest);
>  const char *test__clang_subtest_get_desc(int subtest);
>  int test__clang_subtest_get_nr(void);
> +int test__unit_number__scnprint(int subtest);
>  
>  #if defined(__arm__) || defined(__aarch64__)
>  #ifdef HAVE_DWARF_UNWIND_SUPPORT
> diff --git a/tools/perf/tests/unit_number__scnprint.c b/tools/perf/tests/unit_number__scnprint.c
> new file mode 100644
> index 000000000000..b336024b6c6a
> --- /dev/null
> +++ b/tools/perf/tests/unit_number__scnprint.c
> @@ -0,0 +1,37 @@
> +#include <linux/compiler.h>
> +#include <linux/types.h>
> +#include "tests.h"
> +#include "util.h"
> +#include "debug.h"
> +
> +int test__unit_number__scnprint(int subtest __maybe_unused)
> +{
> +	struct {
> +		u64		 n;
> +		const char	*str;
> +	} test[] = {
> +		{ 1,			"1B"	},
> +		{ 10*1024,		"10K"	},
> +		{ 20*1024*1024,		"20M"	},
> +		{ 30*1024*1024*1024ULL,	"30G"	},
> +		{ 0,			"0B"	},
> +		{ 0,			NULL	},
> +	};
> +	unsigned i = 0;
> +
> +	while (test[i].str) {
> +		char buf[100];
> +
> +		unit_number__scnprint(buf, sizeof(buf), test[i].n);
> +
> +		pr_debug("n %" PRIu64 ", str '%s', buf '%s'\n",
> +			 test[i].n, test[i].str, buf);
> +
> +		if (strcmp(test[i].str, buf))
> +			return TEST_FAIL;
> +
> +		i++;
> +	}
> +
> +	return TEST_OK;
> +}
> diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c
> index d92e02006fb8..435ff1c94a66 100644
> --- a/tools/perf/util/evlist.c
> +++ b/tools/perf/util/evlist.c
> @@ -1224,12 +1224,16 @@ static long parse_pages_arg(const char *str, unsigned long min,
>  	if (pages == 0 && min == 0) {
>  		/* leave number of pages at 0 */
>  	} else if (!is_power_of_2(pages)) {
> +		char buf[100];
> +
>  		/* round pages up to next power of 2 */
>  		pages = roundup_pow_of_two(pages);
>  		if (!pages)
>  			return -EINVAL;
> -		pr_info("rounding mmap pages size to %lu bytes (%lu pages)\n",
> -			pages * page_size, pages);
> +
> +		unit_number__scnprint(buf, sizeof(buf), pages * page_size);
> +		pr_info("rounding mmap pages size to %s (%lu pages)\n",
> +			buf, pages);
>  	}
>  
>  	if (pages > max)
> diff --git a/tools/perf/util/util.c b/tools/perf/util/util.c
> index 9ddd98827d12..c2b941dd8826 100644
> --- a/tools/perf/util/util.c
> +++ b/tools/perf/util/util.c
> @@ -789,3 +789,16 @@ int is_printable_array(char *p, unsigned int len)
>  	}
>  	return 1;
>  }
> +
> +int unit_number__scnprint(char *buf, size_t size, u64 n)
> +{
> +	char unit[4] = "BKMG";
> +	int i = 0;
> +
> +	while (((n / 1024) > 1) && (i < 3)) {
> +		n /= 1024;
> +		i++;
> +	}
> +
> +	return scnprintf(buf, size, "%" PRIu64 "%c", n, unit[i]);
> +}
> diff --git a/tools/perf/util/util.h b/tools/perf/util/util.h
> index 1d639e38aa82..e2a627cd11dd 100644
> --- a/tools/perf/util/util.h
> +++ b/tools/perf/util/util.h
> @@ -363,4 +363,5 @@ int is_printable_array(char *p, unsigned int len);
>  
>  int timestamp__scnprintf_usec(u64 timestamp, char *buf, size_t sz);
>  
> +int unit_number__scnprint(char *buf, size_t size, u64 n);
>  #endif /* GIT_COMPAT_UTIL_H */
> -- 
> 2.7.4

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [tip:perf/core] perf tools: Add unit_number__scnprintf function
  2017-01-09  9:51 ` [PATCH 1/6] perf tools: Add unit_number__scnprint function Jiri Olsa
  2017-01-10 13:22   ` Arnaldo Carvalho de Melo
@ 2017-01-12  8:31   ` tip-bot for Jiri Olsa
  1 sibling, 0 replies; 14+ messages in thread
From: tip-bot for Jiri Olsa @ 2017-01-12  8:31 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: acme, namhyung, tglx, wangnan0, linux-kernel, a.p.zijlstra,
	dsahern, mingo, hpa, jolsa

Commit-ID:  9808143ba2e54818a3cf445d9b69b3f5f15451ee
Gitweb:     http://git.kernel.org/tip/9808143ba2e54818a3cf445d9b69b3f5f15451ee
Author:     Jiri Olsa <jolsa@kernel.org>
AuthorDate: Mon, 9 Jan 2017 10:51:55 +0100
Committer:  Arnaldo Carvalho de Melo <acme@redhat.com>
CommitDate: Wed, 11 Jan 2017 16:48:01 -0300

perf tools: Add unit_number__scnprintf function

Add unit_number__scnprintf function to display size units and use it in
-m option info message.

Before:
  $ perf record -m 10M ls
  rounding mmap pages size to 16777216 bytes (4096 pages)
  ...

After:
  $ perf record -m 10M ls
  rounding mmap pages size to 16M (4096 pages)
  ...

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Cc: David Ahern <dsahern@gmail.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Wang Nan <wangnan0@huawei.com>
Link: http://lkml.kernel.org/r/1483955520-29063-2-git-send-email-jolsa@kernel.org
[ Rename it to unit_number__scnprintf for consistency ]
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
 tools/perf/tests/Build                    |  1 +
 tools/perf/tests/builtin-test.c           |  4 ++++
 tools/perf/tests/tests.h                  |  1 +
 tools/perf/tests/unit_number__scnprintf.c | 37 +++++++++++++++++++++++++++++++
 tools/perf/util/evlist.c                  |  8 +++++--
 tools/perf/util/util.c                    | 13 +++++++++++
 tools/perf/util/util.h                    |  1 +
 7 files changed, 63 insertions(+), 2 deletions(-)

diff --git a/tools/perf/tests/Build b/tools/perf/tests/Build
index 6676c2d..1cb3d9b 100644
--- a/tools/perf/tests/Build
+++ b/tools/perf/tests/Build
@@ -44,6 +44,7 @@ perf-y += is_printable_array.o
 perf-y += bitmap.o
 perf-y += perf-hooks.o
 perf-y += clang.o
+perf-y += unit_number__scnprintf.o
 
 $(OUTPUT)tests/llvm-src-base.c: tests/bpf-script-example.c tests/Build
 	$(call rule_mkdir)
diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-test.c
index a77dcc0..37e326b 100644
--- a/tools/perf/tests/builtin-test.c
+++ b/tools/perf/tests/builtin-test.c
@@ -247,6 +247,10 @@ static struct test generic_tests[] = {
 		}
 	},
 	{
+		.desc = "unit_number__scnprintf",
+		.func = test__unit_number__scnprint,
+	},
+	{
 		.func = NULL,
 	},
 };
diff --git a/tools/perf/tests/tests.h b/tools/perf/tests/tests.h
index a512f0c..1fa9b9d 100644
--- a/tools/perf/tests/tests.h
+++ b/tools/perf/tests/tests.h
@@ -96,6 +96,7 @@ int test__perf_hooks(int subtest);
 int test__clang(int subtest);
 const char *test__clang_subtest_get_desc(int subtest);
 int test__clang_subtest_get_nr(void);
+int test__unit_number__scnprint(int subtest);
 
 #if defined(__arm__) || defined(__aarch64__)
 #ifdef HAVE_DWARF_UNWIND_SUPPORT
diff --git a/tools/perf/tests/unit_number__scnprintf.c b/tools/perf/tests/unit_number__scnprintf.c
new file mode 100644
index 0000000..623c2aa
--- /dev/null
+++ b/tools/perf/tests/unit_number__scnprintf.c
@@ -0,0 +1,37 @@
+#include <linux/compiler.h>
+#include <linux/types.h>
+#include "tests.h"
+#include "util.h"
+#include "debug.h"
+
+int test__unit_number__scnprint(int subtest __maybe_unused)
+{
+	struct {
+		u64		 n;
+		const char	*str;
+	} test[] = {
+		{ 1,			"1B"	},
+		{ 10*1024,		"10K"	},
+		{ 20*1024*1024,		"20M"	},
+		{ 30*1024*1024*1024ULL,	"30G"	},
+		{ 0,			"0B"	},
+		{ 0,			NULL	},
+	};
+	unsigned i = 0;
+
+	while (test[i].str) {
+		char buf[100];
+
+		unit_number__scnprintf(buf, sizeof(buf), test[i].n);
+
+		pr_debug("n %" PRIu64 ", str '%s', buf '%s'\n",
+			 test[i].n, test[i].str, buf);
+
+		if (strcmp(test[i].str, buf))
+			return TEST_FAIL;
+
+		i++;
+	}
+
+	return TEST_OK;
+}
diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c
index 23e6f33..dc4df3d 100644
--- a/tools/perf/util/evlist.c
+++ b/tools/perf/util/evlist.c
@@ -1224,12 +1224,16 @@ static long parse_pages_arg(const char *str, unsigned long min,
 	if (pages == 0 && min == 0) {
 		/* leave number of pages at 0 */
 	} else if (!is_power_of_2(pages)) {
+		char buf[100];
+
 		/* round pages up to next power of 2 */
 		pages = roundup_pow_of_two(pages);
 		if (!pages)
 			return -EINVAL;
-		pr_info("rounding mmap pages size to %lu bytes (%lu pages)\n",
-			pages * page_size, pages);
+
+		unit_number__scnprintf(buf, sizeof(buf), pages * page_size);
+		pr_info("rounding mmap pages size to %s (%lu pages)\n",
+			buf, pages);
 	}
 
 	if (pages > max)
diff --git a/tools/perf/util/util.c b/tools/perf/util/util.c
index 9ddd988..bf29aed 100644
--- a/tools/perf/util/util.c
+++ b/tools/perf/util/util.c
@@ -789,3 +789,16 @@ int is_printable_array(char *p, unsigned int len)
 	}
 	return 1;
 }
+
+int unit_number__scnprintf(char *buf, size_t size, u64 n)
+{
+	char unit[4] = "BKMG";
+	int i = 0;
+
+	while (((n / 1024) > 1) && (i < 3)) {
+		n /= 1024;
+		i++;
+	}
+
+	return scnprintf(buf, size, "%" PRIu64 "%c", n, unit[i]);
+}
diff --git a/tools/perf/util/util.h b/tools/perf/util/util.h
index 1d639e3..6e8be17 100644
--- a/tools/perf/util/util.h
+++ b/tools/perf/util/util.h
@@ -363,4 +363,5 @@ int is_printable_array(char *p, unsigned int len);
 
 int timestamp__scnprintf_usec(u64 timestamp, char *buf, size_t sz);
 
+int unit_number__scnprintf(char *buf, size_t size, u64 n);
 #endif /* GIT_COMPAT_UTIL_H */

^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [tip:perf/core] perf record: Add struct switch_output
  2017-01-09  9:51 ` [PATCH 2/6] perf record: Add struct switch_output Jiri Olsa
@ 2017-01-12  8:32   ` tip-bot for Jiri Olsa
  0 siblings, 0 replies; 14+ messages in thread
From: tip-bot for Jiri Olsa @ 2017-01-12  8:32 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: hpa, namhyung, acme, a.p.zijlstra, jolsa, tglx, linux-kernel,
	wangnan0, mingo, dsahern

Commit-ID:  1b43b70484a9617de5fe2c12e64bea006010ac1c
Gitweb:     http://git.kernel.org/tip/1b43b70484a9617de5fe2c12e64bea006010ac1c
Author:     Jiri Olsa <jolsa@kernel.org>
AuthorDate: Mon, 9 Jan 2017 10:51:56 +0100
Committer:  Arnaldo Carvalho de Melo <acme@redhat.com>
CommitDate: Wed, 11 Jan 2017 16:48:01 -0300

perf record: Add struct switch_output

Next patches will add more --switch-output option arguments,
so preparing the data holder.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Acked-by: Wang Nan <wangnan0@huawei.com>
Cc: David Ahern <dsahern@gmail.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Link: http://lkml.kernel.org/r/1483955520-29063-3-git-send-email-jolsa@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
 tools/perf/builtin-record.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)

diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
index 4ec10e9..f7e805b 100644
--- a/tools/perf/builtin-record.c
+++ b/tools/perf/builtin-record.c
@@ -46,6 +46,10 @@
 #include <asm/bug.h>
 #include <linux/time64.h>
 
+struct switch_output {
+	bool		 signal;
+};
+
 struct record {
 	struct perf_tool	tool;
 	struct record_opts	opts;
@@ -62,7 +66,7 @@ struct record {
 	bool			no_buildid_cache_set;
 	bool			buildid_all;
 	bool			timestamp_filename;
-	bool			switch_output;
+	struct switch_output	switch_output;
 	unsigned long long	samples;
 };
 
@@ -842,11 +846,11 @@ static int __cmd_record(struct record *rec, int argc, const char **argv)
 	signal(SIGTERM, sig_handler);
 	signal(SIGSEGV, sigsegv_handler);
 
-	if (rec->opts.auxtrace_snapshot_mode || rec->switch_output) {
+	if (rec->opts.auxtrace_snapshot_mode || rec->switch_output.signal) {
 		signal(SIGUSR2, snapshot_sig_handler);
 		if (rec->opts.auxtrace_snapshot_mode)
 			trigger_on(&auxtrace_snapshot_trigger);
-		if (rec->switch_output)
+		if (rec->switch_output.signal)
 			trigger_on(&switch_output_trigger);
 	} else {
 		signal(SIGUSR2, SIG_IGN);
@@ -1519,7 +1523,7 @@ static struct option __record_options[] = {
 		    "Record build-id of all DSOs regardless of hits"),
 	OPT_BOOLEAN(0, "timestamp-filename", &record.timestamp_filename,
 		    "append timestamp to output filename"),
-	OPT_BOOLEAN(0, "switch-output", &record.switch_output,
+	OPT_BOOLEAN(0, "switch-output", &record.switch_output.signal,
 		    "Switch output when receive SIGUSR2"),
 	OPT_BOOLEAN(0, "dry-run", &dry_run,
 		    "Parse options then exit"),
@@ -1578,7 +1582,7 @@ int cmd_record(int argc, const char **argv, const char *prefix __maybe_unused)
 		return -EINVAL;
 	}
 
-	if (rec->switch_output)
+	if (rec->switch_output.signal)
 		rec->timestamp_filename = true;
 
 	if (!rec->itr) {
@@ -1629,7 +1633,7 @@ int cmd_record(int argc, const char **argv, const char *prefix __maybe_unused)
 
 	if (rec->no_buildid_cache || rec->no_buildid) {
 		disable_buildid_cache();
-	} else if (rec->switch_output) {
+	} else if (rec->switch_output.signal) {
 		/*
 		 * In 'perf record --switch-output', disable buildid
 		 * generation by default to reduce data file switching

^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [tip:perf/core] perf record: Change switch-output option to take optional argument
  2017-01-09  9:51 ` [PATCH 3/6] perf record: Change switch-output option to take optional argument Jiri Olsa
@ 2017-01-12  8:32   ` tip-bot for Jiri Olsa
  0 siblings, 0 replies; 14+ messages in thread
From: tip-bot for Jiri Olsa @ 2017-01-12  8:32 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: mingo, namhyung, acme, wangnan0, dsahern, linux-kernel, jolsa,
	a.p.zijlstra, tglx, hpa

Commit-ID:  cb4e1ebb6a398ff5b0067034b0d16566af4d78e8
Gitweb:     http://git.kernel.org/tip/cb4e1ebb6a398ff5b0067034b0d16566af4d78e8
Author:     Jiri Olsa <jolsa@kernel.org>
AuthorDate: Mon, 9 Jan 2017 10:51:57 +0100
Committer:  Arnaldo Carvalho de Melo <acme@redhat.com>
CommitDate: Wed, 11 Jan 2017 16:48:01 -0300

perf record: Change switch-output option to take optional argument

Next patches will add --switch-output option arguments, changing the
option to allow that and adding its default value to 'signal'.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Acked-by: Wang Nan <wangnan0@huawei.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: David Ahern <dsahern@gmail.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Link: http://lkml.kernel.org/r/1483955520-29063-4-git-send-email-jolsa@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
 tools/perf/builtin-record.c | 28 ++++++++++++++++++++++++++--
 1 file changed, 26 insertions(+), 2 deletions(-)

diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
index f7e805b..2bf811a 100644
--- a/tools/perf/builtin-record.c
+++ b/tools/perf/builtin-record.c
@@ -48,6 +48,8 @@
 
 struct switch_output {
 	bool		 signal;
+	const char	*str;
+	bool		 set;
 };
 
 struct record {
@@ -1356,6 +1358,22 @@ out_free:
 	return ret;
 }
 
+static int switch_output_setup(struct record *rec)
+{
+	struct switch_output *s = &rec->switch_output;
+
+	if (!s->set)
+		return 0;
+
+	if (!strcmp(s->str, "signal")) {
+		s->signal = true;
+		pr_debug("switch-output with SIGUSR2 signal\n");
+		return 0;
+	}
+
+	return -1;
+}
+
 static const char * const __record_usage[] = {
 	"perf record [<options>] [<command>]",
 	"perf record [<options>] -- <command> [<options>]",
@@ -1523,8 +1541,9 @@ static struct option __record_options[] = {
 		    "Record build-id of all DSOs regardless of hits"),
 	OPT_BOOLEAN(0, "timestamp-filename", &record.timestamp_filename,
 		    "append timestamp to output filename"),
-	OPT_BOOLEAN(0, "switch-output", &record.switch_output.signal,
-		    "Switch output when receive SIGUSR2"),
+	OPT_STRING_OPTARG_SET(0, "switch-output", &record.switch_output.str,
+			  &record.switch_output.set, "signal",
+			  "Switch output when receive SIGUSR2", "signal"),
 	OPT_BOOLEAN(0, "dry-run", &dry_run,
 		    "Parse options then exit"),
 	OPT_END()
@@ -1582,6 +1601,11 @@ int cmd_record(int argc, const char **argv, const char *prefix __maybe_unused)
 		return -EINVAL;
 	}
 
+	if (switch_output_setup(rec)) {
+		parse_options_usage(record_usage, record_options, "switch-output", 0);
+		return -EINVAL;
+	}
+
 	if (rec->switch_output.signal)
 		rec->timestamp_filename = true;
 

^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [tip:perf/core] perf record: Add switch-output size option argument
  2017-01-09  9:51 ` [PATCH 4/6] perf record: Add switch-output size option argument Jiri Olsa
@ 2017-01-12  8:33   ` tip-bot for Jiri Olsa
  0 siblings, 0 replies; 14+ messages in thread
From: tip-bot for Jiri Olsa @ 2017-01-12  8:33 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: a.p.zijlstra, linux-kernel, mingo, tglx, jolsa, namhyung, hpa,
	dsahern, acme, wangnan0

Commit-ID:  dc0c6127c231d4d264570497a916fa19740c915b
Gitweb:     http://git.kernel.org/tip/dc0c6127c231d4d264570497a916fa19740c915b
Author:     Jiri Olsa <jolsa@kernel.org>
AuthorDate: Mon, 9 Jan 2017 10:51:58 +0100
Committer:  Arnaldo Carvalho de Melo <acme@redhat.com>
CommitDate: Wed, 11 Jan 2017 16:48:02 -0300

perf record: Add switch-output size option argument

It's now possible to specify the threshold size for perf.data like:

  $ perf record --switch-output=2G ...

Once it's reached, the current data are dumped in to the
perf.data.<timestamp> file and session does on.

  $ perf record --switch-output=2G ...
  [ perf record: dump data: Woken up 7244 times ]
  [ perf record: Dump perf.data.2017010214093746 ]
  ...

The size is expected to be a number with appended unit character -
B/K/M/G.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Acked-by: Wang Nan <wangnan0@huawei.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: David Ahern <dsahern@gmail.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Link: http://lkml.kernel.org/r/1483955520-29063-5-git-send-email-jolsa@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
 tools/perf/Documentation/perf-record.txt | 12 +++++-
 tools/perf/builtin-record.c              | 67 +++++++++++++++++++++++++-------
 2 files changed, 63 insertions(+), 16 deletions(-)

diff --git a/tools/perf/Documentation/perf-record.txt b/tools/perf/Documentation/perf-record.txt
index 5054d91..3d55d2f 100644
--- a/tools/perf/Documentation/perf-record.txt
+++ b/tools/perf/Documentation/perf-record.txt
@@ -421,9 +421,17 @@ Configure all used events to run in user space.
 --timestamp-filename
 Append timestamp to output file name.
 
---switch-output::
+--switch-output[=mode]::
 Generate multiple perf.data files, timestamp prefixed, switching to a new one
-when receiving a SIGUSR2.
+based on 'mode' value:
+  "signal" - when receiving a SIGUSR2 (default value) or
+  <size>   - when reaching the size threshold, size is expected to
+             be a number with appended unit character - B/K/M/G
+
+             Note: the precision of  the size  threshold  hugely depends
+             on your configuration  - the number and size of  your  ring
+             buffers (-m). It is generally more precise for higher sizes
+             (like >5M), for lower values expect different sizes.
 
 A possible use case is to, given an external event, slice the perf.data file
 that gets then processed, possibly via a perf script, to decide if that
diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
index 2bf811a..3fa6449 100644
--- a/tools/perf/builtin-record.c
+++ b/tools/perf/builtin-record.c
@@ -47,7 +47,9 @@
 #include <linux/time64.h>
 
 struct switch_output {
+	bool		 enabled;
 	bool		 signal;
+	unsigned long	 size;
 	const char	*str;
 	bool		 set;
 };
@@ -72,6 +74,23 @@ struct record {
 	unsigned long long	samples;
 };
 
+static volatile int auxtrace_record__snapshot_started;
+static DEFINE_TRIGGER(auxtrace_snapshot_trigger);
+static DEFINE_TRIGGER(switch_output_trigger);
+
+static bool switch_output_signal(struct record *rec)
+{
+	return rec->switch_output.signal &&
+	       trigger_is_ready(&switch_output_trigger);
+}
+
+static bool switch_output_size(struct record *rec)
+{
+	return rec->switch_output.size &&
+	       trigger_is_ready(&switch_output_trigger) &&
+	       (rec->bytes_written >= rec->switch_output.size);
+}
+
 static int record__write(struct record *rec, void *bf, size_t size)
 {
 	if (perf_data_file__write(rec->session->file, bf, size) < 0) {
@@ -80,6 +99,10 @@ static int record__write(struct record *rec, void *bf, size_t size)
 	}
 
 	rec->bytes_written += size;
+
+	if (switch_output_size(rec))
+		trigger_hit(&switch_output_trigger);
+
 	return 0;
 }
 
@@ -199,10 +222,6 @@ static volatile int done;
 static volatile int signr = -1;
 static volatile int child_finished;
 
-static volatile int auxtrace_record__snapshot_started;
-static DEFINE_TRIGGER(auxtrace_snapshot_trigger);
-static DEFINE_TRIGGER(switch_output_trigger);
-
 static void sig_handler(int sig)
 {
 	if (sig == SIGCHLD)
@@ -848,11 +867,11 @@ static int __cmd_record(struct record *rec, int argc, const char **argv)
 	signal(SIGTERM, sig_handler);
 	signal(SIGSEGV, sigsegv_handler);
 
-	if (rec->opts.auxtrace_snapshot_mode || rec->switch_output.signal) {
+	if (rec->opts.auxtrace_snapshot_mode || rec->switch_output.enabled) {
 		signal(SIGUSR2, snapshot_sig_handler);
 		if (rec->opts.auxtrace_snapshot_mode)
 			trigger_on(&auxtrace_snapshot_trigger);
-		if (rec->switch_output.signal)
+		if (rec->switch_output.enabled)
 			trigger_on(&switch_output_trigger);
 	} else {
 		signal(SIGUSR2, SIG_IGN);
@@ -1361,6 +1380,14 @@ out_free:
 static int switch_output_setup(struct record *rec)
 {
 	struct switch_output *s = &rec->switch_output;
+	static struct parse_tag tags_size[] = {
+		{ .tag  = 'B', .mult = 1       },
+		{ .tag  = 'K', .mult = 1 << 10 },
+		{ .tag  = 'M', .mult = 1 << 20 },
+		{ .tag  = 'G', .mult = 1 << 30 },
+		{ .tag  = 0 },
+	};
+	unsigned long val;
 
 	if (!s->set)
 		return 0;
@@ -1368,10 +1395,22 @@ static int switch_output_setup(struct record *rec)
 	if (!strcmp(s->str, "signal")) {
 		s->signal = true;
 		pr_debug("switch-output with SIGUSR2 signal\n");
-		return 0;
+		goto enabled;
+	}
+
+	val = parse_tag_value(s->str, tags_size);
+	if (val != (unsigned long) -1) {
+		s->size = val;
+		pr_debug("switch-output with %s size threshold\n", s->str);
+		goto enabled;
 	}
 
 	return -1;
+
+enabled:
+	rec->timestamp_filename = true;
+	s->enabled              = true;
+	return 0;
 }
 
 static const char * const __record_usage[] = {
@@ -1542,8 +1581,9 @@ static struct option __record_options[] = {
 	OPT_BOOLEAN(0, "timestamp-filename", &record.timestamp_filename,
 		    "append timestamp to output filename"),
 	OPT_STRING_OPTARG_SET(0, "switch-output", &record.switch_output.str,
-			  &record.switch_output.set, "signal",
-			  "Switch output when receive SIGUSR2", "signal"),
+			  &record.switch_output.set, "signal,size",
+			  "Switch output when receive SIGUSR2 or cross size threshold",
+			  "signal"),
 	OPT_BOOLEAN(0, "dry-run", &dry_run,
 		    "Parse options then exit"),
 	OPT_END()
@@ -1606,9 +1646,6 @@ int cmd_record(int argc, const char **argv, const char *prefix __maybe_unused)
 		return -EINVAL;
 	}
 
-	if (rec->switch_output.signal)
-		rec->timestamp_filename = true;
-
 	if (!rec->itr) {
 		rec->itr = auxtrace_record__init(rec->evlist, &err);
 		if (err)
@@ -1657,7 +1694,7 @@ int cmd_record(int argc, const char **argv, const char *prefix __maybe_unused)
 
 	if (rec->no_buildid_cache || rec->no_buildid) {
 		disable_buildid_cache();
-	} else if (rec->switch_output.signal) {
+	} else if (rec->switch_output.enabled) {
 		/*
 		 * In 'perf record --switch-output', disable buildid
 		 * generation by default to reduce data file switching
@@ -1749,6 +1786,8 @@ out:
 
 static void snapshot_sig_handler(int sig __maybe_unused)
 {
+	struct record *rec = &record;
+
 	if (trigger_is_ready(&auxtrace_snapshot_trigger)) {
 		trigger_hit(&auxtrace_snapshot_trigger);
 		auxtrace_record__snapshot_started = 1;
@@ -1756,6 +1795,6 @@ static void snapshot_sig_handler(int sig __maybe_unused)
 			trigger_error(&auxtrace_snapshot_trigger);
 	}
 
-	if (trigger_is_ready(&switch_output_trigger))
+	if (switch_output_signal(rec))
 		trigger_hit(&switch_output_trigger);
 }

^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [tip:perf/core] perf record: Add switch-output size warning
  2017-01-09  9:51 ` [PATCH 5/6] perf record: Add switch-output size warning Jiri Olsa
@ 2017-01-12  8:33   ` tip-bot for Jiri Olsa
  0 siblings, 0 replies; 14+ messages in thread
From: tip-bot for Jiri Olsa @ 2017-01-12  8:33 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: wangnan0, dsahern, tglx, linux-kernel, mingo, acme, hpa,
	namhyung, a.p.zijlstra, jolsa

Commit-ID:  0c5824498e8bd5b7d30dc03448cd89efaee4bead
Gitweb:     http://git.kernel.org/tip/0c5824498e8bd5b7d30dc03448cd89efaee4bead
Author:     Jiri Olsa <jolsa@kernel.org>
AuthorDate: Mon, 9 Jan 2017 10:51:59 +0100
Committer:  Arnaldo Carvalho de Melo <acme@redhat.com>
CommitDate: Wed, 11 Jan 2017 16:48:02 -0300

perf record: Add switch-output size warning

Adding switch-output size warning if the requested
size of lower than the wakeup ring buffer size.

  $ perf record --switch-output=1K ls
  WARNING: switch-output data size lower than wakeup kernel buffer size (258K) expect bigger perf.data sizes
  ...

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Suggested-and-Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: David Ahern <dsahern@gmail.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Wang Nan <wangnan0@huawei.com>
Link: http://lkml.kernel.org/r/1483955520-29063-6-git-send-email-jolsa@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
 tools/perf/builtin-record.c | 21 +++++++++++++++++++++
 tools/perf/util/evlist.c    |  2 +-
 tools/perf/util/evlist.h    |  2 ++
 3 files changed, 24 insertions(+), 1 deletion(-)

diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
index 3fa6449..93319e1 100644
--- a/tools/perf/builtin-record.c
+++ b/tools/perf/builtin-record.c
@@ -1377,6 +1377,23 @@ out_free:
 	return ret;
 }
 
+static void switch_output_size_warn(struct record *rec)
+{
+	u64 wakeup_size = perf_evlist__mmap_size(rec->opts.mmap_pages);
+	struct switch_output *s = &rec->switch_output;
+
+	wakeup_size /= 2;
+
+	if (s->size < wakeup_size) {
+		char buf[100];
+
+		unit_number__scnprintf(buf, sizeof(buf), wakeup_size);
+		pr_warning("WARNING: switch-output data size lower than "
+			   "wakeup kernel buffer size (%s) "
+			   "expect bigger perf.data sizes\n", buf);
+	}
+}
+
 static int switch_output_setup(struct record *rec)
 {
 	struct switch_output *s = &rec->switch_output;
@@ -1410,6 +1427,10 @@ static int switch_output_setup(struct record *rec)
 enabled:
 	rec->timestamp_filename = true;
 	s->enabled              = true;
+
+	if (s->size && !rec->opts.no_buffering)
+		switch_output_size_warn(rec);
+
 	return 0;
 }
 
diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c
index dc4df3d..b601f28 100644
--- a/tools/perf/util/evlist.c
+++ b/tools/perf/util/evlist.c
@@ -1184,7 +1184,7 @@ unsigned long perf_event_mlock_kb_in_pages(void)
 	return pages;
 }
 
-static size_t perf_evlist__mmap_size(unsigned long pages)
+size_t perf_evlist__mmap_size(unsigned long pages)
 {
 	if (pages == UINT_MAX)
 		pages = perf_event_mlock_kb_in_pages();
diff --git a/tools/perf/util/evlist.h b/tools/perf/util/evlist.h
index 4fd034f..389b9cc 100644
--- a/tools/perf/util/evlist.h
+++ b/tools/perf/util/evlist.h
@@ -218,6 +218,8 @@ int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages,
 		      bool overwrite);
 void perf_evlist__munmap(struct perf_evlist *evlist);
 
+size_t perf_evlist__mmap_size(unsigned long pages);
+
 void perf_evlist__disable(struct perf_evlist *evlist);
 void perf_evlist__enable(struct perf_evlist *evlist);
 void perf_evlist__toggle_enable(struct perf_evlist *evlist);

^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [tip:perf/core] perf record: Add switch-output time option argument
  2017-01-09  9:52 ` [PATCH 6/6] perf record: Add switch-output time option argument Jiri Olsa
@ 2017-01-12  8:34   ` tip-bot for Jiri Olsa
  0 siblings, 0 replies; 14+ messages in thread
From: tip-bot for Jiri Olsa @ 2017-01-12  8:34 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: a.p.zijlstra, hpa, dsahern, linux-kernel, tglx, jolsa, acme,
	namhyung, wangnan0, mingo

Commit-ID:  bfacbe3bf2443c805aec4c04ecb558d03d0d3ebc
Gitweb:     http://git.kernel.org/tip/bfacbe3bf2443c805aec4c04ecb558d03d0d3ebc
Author:     Jiri Olsa <jolsa@kernel.org>
AuthorDate: Mon, 9 Jan 2017 10:52:00 +0100
Committer:  Arnaldo Carvalho de Melo <acme@redhat.com>
CommitDate: Wed, 11 Jan 2017 16:48:02 -0300

perf record: Add switch-output time option argument

It's now possible to specify the threshold time for perf.data like:

  $ perf record --switch-output=30s ...

Once it's reached, the current data are dumped in to the
perf.data.<timestamp> file and session does on.

  $ perf record --switch-output=30s ...
  [ perf record: dump data: Woken up 44 times ]
  [ perf record: Dump perf.data.2017010213043746 ]
  ...

The time is expected to be a number with appended unit
character - s/m/h/d.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Acked-by: Wang Nan <wangnan0@huawei.com>
Cc: David Ahern <dsahern@gmail.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Link: http://lkml.kernel.org/r/1483955520-29063-7-git-send-email-jolsa@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
 tools/perf/Documentation/perf-record.txt |  2 ++
 tools/perf/builtin-record.c              | 44 ++++++++++++++++++++++++++++++--
 2 files changed, 44 insertions(+), 2 deletions(-)

diff --git a/tools/perf/Documentation/perf-record.txt b/tools/perf/Documentation/perf-record.txt
index 3d55d2f..27256bc 100644
--- a/tools/perf/Documentation/perf-record.txt
+++ b/tools/perf/Documentation/perf-record.txt
@@ -427,6 +427,8 @@ based on 'mode' value:
   "signal" - when receiving a SIGUSR2 (default value) or
   <size>   - when reaching the size threshold, size is expected to
              be a number with appended unit character - B/K/M/G
+  <time>   - when reaching the time threshold, size is expected to
+             be a number with appended unit character - s/m/h/d
 
              Note: the precision of  the size  threshold  hugely depends
              on your configuration  - the number and size of  your  ring
diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
index 93319e1..33a9eaa 100644
--- a/tools/perf/builtin-record.c
+++ b/tools/perf/builtin-record.c
@@ -50,6 +50,7 @@ struct switch_output {
 	bool		 enabled;
 	bool		 signal;
 	unsigned long	 size;
+	unsigned long	 time;
 	const char	*str;
 	bool		 set;
 };
@@ -91,6 +92,12 @@ static bool switch_output_size(struct record *rec)
 	       (rec->bytes_written >= rec->switch_output.size);
 }
 
+static bool switch_output_time(struct record *rec)
+{
+	return rec->switch_output.time &&
+	       trigger_is_ready(&switch_output_trigger);
+}
+
 static int record__write(struct record *rec, void *bf, size_t size)
 {
 	if (perf_data_file__write(rec->session->file, bf, size) < 0) {
@@ -737,6 +744,7 @@ static void workload_exec_failed_signal(int signo __maybe_unused,
 }
 
 static void snapshot_sig_handler(int sig);
+static void alarm_sig_handler(int sig);
 
 int __weak
 perf_event__synth_time_conv(const struct perf_event_mmap_page *pc __maybe_unused,
@@ -1068,6 +1076,10 @@ static int __cmd_record(struct record *rec, int argc, const char **argv)
 				err = fd;
 				goto out_child;
 			}
+
+			/* re-arm the alarm */
+			if (rec->switch_output.time)
+				alarm(rec->switch_output.time);
 		}
 
 		if (hits == rec->samples) {
@@ -1404,6 +1416,13 @@ static int switch_output_setup(struct record *rec)
 		{ .tag  = 'G', .mult = 1 << 30 },
 		{ .tag  = 0 },
 	};
+	static struct parse_tag tags_time[] = {
+		{ .tag  = 's', .mult = 1        },
+		{ .tag  = 'm', .mult = 60       },
+		{ .tag  = 'h', .mult = 60*60    },
+		{ .tag  = 'd', .mult = 60*60*24 },
+		{ .tag  = 0 },
+	};
 	unsigned long val;
 
 	if (!s->set)
@@ -1422,6 +1441,14 @@ static int switch_output_setup(struct record *rec)
 		goto enabled;
 	}
 
+	val = parse_tag_value(s->str, tags_time);
+	if (val != (unsigned long) -1) {
+		s->time = val;
+		pr_debug("switch-output with %s time threshold (%lu seconds)\n",
+			 s->str, s->time);
+		goto enabled;
+	}
+
 	return -1;
 
 enabled:
@@ -1602,8 +1629,8 @@ static struct option __record_options[] = {
 	OPT_BOOLEAN(0, "timestamp-filename", &record.timestamp_filename,
 		    "append timestamp to output filename"),
 	OPT_STRING_OPTARG_SET(0, "switch-output", &record.switch_output.str,
-			  &record.switch_output.set, "signal,size",
-			  "Switch output when receive SIGUSR2 or cross size threshold",
+			  &record.switch_output.set, "signal,size,time",
+			  "Switch output when receive SIGUSR2 or cross size,time threshold",
 			  "signal"),
 	OPT_BOOLEAN(0, "dry-run", &dry_run,
 		    "Parse options then exit"),
@@ -1667,6 +1694,11 @@ int cmd_record(int argc, const char **argv, const char *prefix __maybe_unused)
 		return -EINVAL;
 	}
 
+	if (rec->switch_output.time) {
+		signal(SIGALRM, alarm_sig_handler);
+		alarm(rec->switch_output.time);
+	}
+
 	if (!rec->itr) {
 		rec->itr = auxtrace_record__init(rec->evlist, &err);
 		if (err)
@@ -1819,3 +1851,11 @@ static void snapshot_sig_handler(int sig __maybe_unused)
 	if (switch_output_signal(rec))
 		trigger_hit(&switch_output_trigger);
 }
+
+static void alarm_sig_handler(int sig __maybe_unused)
+{
+	struct record *rec = &record;
+
+	if (switch_output_time(rec))
+		trigger_hit(&switch_output_trigger);
+}

^ permalink raw reply related	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2017-01-12  8:35 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-01-09  9:51 [PATCHv2 0/7] perf tools: Add switch-output size and time threshold options Jiri Olsa
2017-01-09  9:51 ` [PATCH 1/6] perf tools: Add unit_number__scnprint function Jiri Olsa
2017-01-10 13:22   ` Arnaldo Carvalho de Melo
2017-01-12  8:31   ` [tip:perf/core] perf tools: Add unit_number__scnprintf function tip-bot for Jiri Olsa
2017-01-09  9:51 ` [PATCH 2/6] perf record: Add struct switch_output Jiri Olsa
2017-01-12  8:32   ` [tip:perf/core] " tip-bot for Jiri Olsa
2017-01-09  9:51 ` [PATCH 3/6] perf record: Change switch-output option to take optional argument Jiri Olsa
2017-01-12  8:32   ` [tip:perf/core] " tip-bot for Jiri Olsa
2017-01-09  9:51 ` [PATCH 4/6] perf record: Add switch-output size option argument Jiri Olsa
2017-01-12  8:33   ` [tip:perf/core] " tip-bot for Jiri Olsa
2017-01-09  9:51 ` [PATCH 5/6] perf record: Add switch-output size warning Jiri Olsa
2017-01-12  8:33   ` [tip:perf/core] " tip-bot for Jiri Olsa
2017-01-09  9:52 ` [PATCH 6/6] perf record: Add switch-output time option argument Jiri Olsa
2017-01-12  8:34   ` [tip:perf/core] " tip-bot for Jiri Olsa

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.