All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v5] perf record: Add support for limit perf output file size
@ 2019-10-22  8:09 jsun4
  2019-11-01  2:47 ` Jiwei Sun
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: jsun4 @ 2019-10-22  8:09 UTC (permalink / raw)
  To: acme, jolsa, arnaldo.melo, linux-kernel
  Cc: alexander.shishkin, mpetlan, namhyung, a.p.zijlstra,
	adrian.hunter, Richard.Danter, jiwei.sun, jiwei.sun.bj

The patch adds a new option to limit the output file size, then based
on it, we can create a wrapper of the perf command that uses the option
to avoid exhausting the disk space by the unconscious user.

In order to make the perf.data parsable, we just limit the sample data
size, since the perf.data consists of many headers and sample data and
other data, the actual size of the recorded file will bigger than the
setting value.

Testing it:

 # ./perf record -a -g --max-size=10M
 Couldn't synthesize bpf events.
 [ perf record: perf size limit reached (10249 KB), stopping session ]
 [ perf record: Woken up 32 times to write data ]
 [ perf record: Captured and wrote 10.133 MB perf.data (71964 samples) ]

 # ls -lh perf.data
 -rw------- 1 root root 11M Oct 22 14:32 perf.data

 # ./perf record -a -g --max-size=10K
 [ perf record: perf size limit reached (10 KB), stopping session ]
 Couldn't synthesize bpf events.
 [ perf record: Woken up 0 times to write data ]
 [ perf record: Captured and wrote 1.546 MB perf.data (69 samples) ]

 # ls -l perf.data
 -rw------- 1 root root 1626952 Oct 22 14:36 perf.data

Signed-off-by: Jiwei Sun <jiwei.sun@windriver.com>
---
v5 changes:
  - Change the output format like [ perf record: perf size limit XX ]
  - change the killing perf way from "raise(SIGTERM)" to set "done == 1"

v4 changes:
  - Just show one WARNING message after reached the limit.

v3 changes:
  - add a test result
  - add the new option to tools/perf/Documentation/perf-record.txt

v2 changes:
  - make patch based on latest Arnaldo's perf/core,
  - display warning message when reached the limit.
---
 tools/perf/Documentation/perf-record.txt |  4 +++
 tools/perf/builtin-record.c              | 46 +++++++++++++++++++++++-
 2 files changed, 49 insertions(+), 1 deletion(-)

diff --git a/tools/perf/Documentation/perf-record.txt b/tools/perf/Documentation/perf-record.txt
index 8a4506113d9f..ebcba1f95513 100644
--- a/tools/perf/Documentation/perf-record.txt
+++ b/tools/perf/Documentation/perf-record.txt
@@ -574,6 +574,10 @@ Implies --tail-synthesize.
 --kcore::
 Make a copy of /proc/kcore and place it into a directory with the perf data file.
 
+--max-size=<size>::
+Limit the sample data max size, <size> is expected to be a number with
+appended unit character - B/K/M/G
+
 SEE ALSO
 --------
 linkperf:perf-stat[1], linkperf:perf-list[1]
diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
index f6664bb08b26..b9ddfcda9611 100644
--- a/tools/perf/builtin-record.c
+++ b/tools/perf/builtin-record.c
@@ -94,8 +94,11 @@ struct record {
 	struct switch_output	switch_output;
 	unsigned long long	samples;
 	cpu_set_t		affinity_mask;
+	unsigned long		output_max_size;	/* = 0: unlimited */
 };
 
+static volatile int done;
+
 static volatile int auxtrace_record__snapshot_started;
 static DEFINE_TRIGGER(auxtrace_snapshot_trigger);
 static DEFINE_TRIGGER(switch_output_trigger);
@@ -123,6 +126,12 @@ static bool switch_output_time(struct record *rec)
 	       trigger_is_ready(&switch_output_trigger);
 }
 
+static bool record__output_max_size_exceeded(struct record *rec)
+{
+	return rec->output_max_size &&
+	       (rec->bytes_written >= rec->output_max_size);
+}
+
 static int record__write(struct record *rec, struct mmap *map __maybe_unused,
 			 void *bf, size_t size)
 {
@@ -135,6 +144,13 @@ static int record__write(struct record *rec, struct mmap *map __maybe_unused,
 
 	rec->bytes_written += size;
 
+	if (record__output_max_size_exceeded(rec) && !done) {
+		fprintf(stderr, "[ perf record: perf size limit reached (%lu KB),"
+				" stopping session ]\n",
+				rec->bytes_written >> 10);
+		done = 1;
+	}
+
 	if (switch_output_size(rec))
 		trigger_hit(&switch_output_trigger);
 
@@ -499,7 +515,6 @@ static int record__pushfn(struct mmap *map, void *to, void *bf, size_t size)
 	return record__write(rec, map, bf, size);
 }
 
-static volatile int done;
 static volatile int signr = -1;
 static volatile int child_finished;
 
@@ -1984,6 +1999,33 @@ static int record__parse_affinity(const struct option *opt, const char *str, int
 	return 0;
 }
 
+static int parse_output_max_size(const struct option *opt,
+				 const char *str, int unset)
+{
+	unsigned long *s = (unsigned long *)opt->value;
+	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 (unset) {
+		*s = 0;
+		return 0;
+	}
+
+	val = parse_tag_value(str, tags_size);
+	if (val != (unsigned long) -1) {
+		*s = val;
+		return 0;
+	}
+
+	return -1;
+}
+
 static int record__parse_mmap_pages(const struct option *opt,
 				    const char *str,
 				    int unset __maybe_unused)
@@ -2311,6 +2353,8 @@ static struct option __record_options[] = {
 			    "n", "Compressed records using specified level (default: 1 - fastest compression, 22 - greatest compression)",
 			    record__parse_comp_level),
 #endif
+	OPT_CALLBACK(0, "max-size", &record.output_max_size,
+		     "size", "Limit the maximum size of the output file", parse_output_max_size),
 	OPT_END()
 };
 
-- 
2.20.1


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

* Re: [PATCH v5] perf record: Add support for limit perf output file size
  2019-10-22  8:09 [PATCH v5] perf record: Add support for limit perf output file size jsun4
@ 2019-11-01  2:47 ` Jiwei Sun
  2019-11-01  8:13 ` Jiri Olsa
  2019-11-12 11:17 ` [tip: perf/core] " tip-bot2 for Jiwei Sun
  2 siblings, 0 replies; 7+ messages in thread
From: Jiwei Sun @ 2019-11-01  2:47 UTC (permalink / raw)
  To: acme, jolsa, arnaldo.melo, linux-kernel
  Cc: alexander.shishkin, mpetlan, namhyung, a.p.zijlstra,
	adrian.hunter, Richard.Danter, jiwei.sun.bj

Hi Jirka & Arnaldo,

I'm sorry to bother you, could you please help me to review the v5 patch again?
Thank you very much for your suggestions for the v4 patch.

Best regards,
Jiwei

On 2019e9410f\x1c\b22f\x17% 16:09, jsun4 wrote:
> The patch adds a new option to limit the output file size, then based
> on it, we can create a wrapper of the perf command that uses the option
> to avoid exhausting the disk space by the unconscious user.
> 
> In order to make the perf.data parsable, we just limit the sample data
> size, since the perf.data consists of many headers and sample data and
> other data, the actual size of the recorded file will bigger than the
> setting value.
> 
> Testing it:
> 
>  # ./perf record -a -g --max-size=10M
>  Couldn't synthesize bpf events.
>  [ perf record: perf size limit reached (10249 KB), stopping session ]
>  [ perf record: Woken up 32 times to write data ]
>  [ perf record: Captured and wrote 10.133 MB perf.data (71964 samples) ]
> 
>  # ls -lh perf.data
>  -rw------- 1 root root 11M Oct 22 14:32 perf.data
> 
>  # ./perf record -a -g --max-size=10K
>  [ perf record: perf size limit reached (10 KB), stopping session ]
>  Couldn't synthesize bpf events.
>  [ perf record: Woken up 0 times to write data ]
>  [ perf record: Captured and wrote 1.546 MB perf.data (69 samples) ]
> 
>  # ls -l perf.data
>  -rw------- 1 root root 1626952 Oct 22 14:36 perf.data
> 
> Signed-off-by: Jiwei Sun <jiwei.sun@windriver.com>
> ---
> v5 changes:
>   - Change the output format like [ perf record: perf size limit XX ]
>   - change the killing perf way from "raise(SIGTERM)" to set "done == 1"
> 
> v4 changes:
>   - Just show one WARNING message after reached the limit.
> 
> v3 changes:
>   - add a test result
>   - add the new option to tools/perf/Documentation/perf-record.txt
> 
> v2 changes:
>   - make patch based on latest Arnaldo's perf/core,
>   - display warning message when reached the limit.
> ---
>  tools/perf/Documentation/perf-record.txt |  4 +++
>  tools/perf/builtin-record.c              | 46 +++++++++++++++++++++++-
>  2 files changed, 49 insertions(+), 1 deletion(-)
> 
> diff --git a/tools/perf/Documentation/perf-record.txt b/tools/perf/Documentation/perf-record.txt
> index 8a4506113d9f..ebcba1f95513 100644
> --- a/tools/perf/Documentation/perf-record.txt
> +++ b/tools/perf/Documentation/perf-record.txt
> @@ -574,6 +574,10 @@ Implies --tail-synthesize.
>  --kcore::
>  Make a copy of /proc/kcore and place it into a directory with the perf data file.
>  
> +--max-size=<size>::
> +Limit the sample data max size, <size> is expected to be a number with
> +appended unit character - B/K/M/G
> +
>  SEE ALSO
>  --------
>  linkperf:perf-stat[1], linkperf:perf-list[1]
> diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
> index f6664bb08b26..b9ddfcda9611 100644
> --- a/tools/perf/builtin-record.c
> +++ b/tools/perf/builtin-record.c
> @@ -94,8 +94,11 @@ struct record {
>  	struct switch_output	switch_output;
>  	unsigned long long	samples;
>  	cpu_set_t		affinity_mask;
> +	unsigned long		output_max_size;	/* = 0: unlimited */
>  };
>  
> +static volatile int done;
> +
>  static volatile int auxtrace_record__snapshot_started;
>  static DEFINE_TRIGGER(auxtrace_snapshot_trigger);
>  static DEFINE_TRIGGER(switch_output_trigger);
> @@ -123,6 +126,12 @@ static bool switch_output_time(struct record *rec)
>  	       trigger_is_ready(&switch_output_trigger);
>  }
>  
> +static bool record__output_max_size_exceeded(struct record *rec)
> +{
> +	return rec->output_max_size &&
> +	       (rec->bytes_written >= rec->output_max_size);
> +}
> +
>  static int record__write(struct record *rec, struct mmap *map __maybe_unused,
>  			 void *bf, size_t size)
>  {
> @@ -135,6 +144,13 @@ static int record__write(struct record *rec, struct mmap *map __maybe_unused,
>  
>  	rec->bytes_written += size;
>  
> +	if (record__output_max_size_exceeded(rec) && !done) {
> +		fprintf(stderr, "[ perf record: perf size limit reached (%lu KB),"
> +				" stopping session ]\n",
> +				rec->bytes_written >> 10);
> +		done = 1;
> +	}
> +
>  	if (switch_output_size(rec))
>  		trigger_hit(&switch_output_trigger);
>  
> @@ -499,7 +515,6 @@ static int record__pushfn(struct mmap *map, void *to, void *bf, size_t size)
>  	return record__write(rec, map, bf, size);
>  }
>  
> -static volatile int done;
>  static volatile int signr = -1;
>  static volatile int child_finished;
>  
> @@ -1984,6 +1999,33 @@ static int record__parse_affinity(const struct option *opt, const char *str, int
>  	return 0;
>  }
>  
> +static int parse_output_max_size(const struct option *opt,
> +				 const char *str, int unset)
> +{
> +	unsigned long *s = (unsigned long *)opt->value;
> +	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 (unset) {
> +		*s = 0;
> +		return 0;
> +	}
> +
> +	val = parse_tag_value(str, tags_size);
> +	if (val != (unsigned long) -1) {
> +		*s = val;
> +		return 0;
> +	}
> +
> +	return -1;
> +}
> +
>  static int record__parse_mmap_pages(const struct option *opt,
>  				    const char *str,
>  				    int unset __maybe_unused)
> @@ -2311,6 +2353,8 @@ static struct option __record_options[] = {
>  			    "n", "Compressed records using specified level (default: 1 - fastest compression, 22 - greatest compression)",
>  			    record__parse_comp_level),
>  #endif
> +	OPT_CALLBACK(0, "max-size", &record.output_max_size,
> +		     "size", "Limit the maximum size of the output file", parse_output_max_size),
>  	OPT_END()
>  };
>  
> 

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

* Re: [PATCH v5] perf record: Add support for limit perf output file size
  2019-10-22  8:09 [PATCH v5] perf record: Add support for limit perf output file size jsun4
  2019-11-01  2:47 ` Jiwei Sun
@ 2019-11-01  8:13 ` Jiri Olsa
  2019-11-06 18:58   ` Arnaldo Carvalho de Melo
  2019-11-07 11:13   ` Arnaldo Carvalho de Melo
  2019-11-12 11:17 ` [tip: perf/core] " tip-bot2 for Jiwei Sun
  2 siblings, 2 replies; 7+ messages in thread
From: Jiri Olsa @ 2019-11-01  8:13 UTC (permalink / raw)
  To: jsun4
  Cc: acme, arnaldo.melo, linux-kernel, alexander.shishkin, mpetlan,
	namhyung, a.p.zijlstra, adrian.hunter, Richard.Danter,
	jiwei.sun.bj

On Tue, Oct 22, 2019 at 04:09:01PM +0800, jsun4 wrote:
> The patch adds a new option to limit the output file size, then based
> on it, we can create a wrapper of the perf command that uses the option
> to avoid exhausting the disk space by the unconscious user.
> 
> In order to make the perf.data parsable, we just limit the sample data
> size, since the perf.data consists of many headers and sample data and
> other data, the actual size of the recorded file will bigger than the
> setting value.
> 
> Testing it:
> 
>  # ./perf record -a -g --max-size=10M
>  Couldn't synthesize bpf events.
>  [ perf record: perf size limit reached (10249 KB), stopping session ]
>  [ perf record: Woken up 32 times to write data ]
>  [ perf record: Captured and wrote 10.133 MB perf.data (71964 samples) ]
> 
>  # ls -lh perf.data
>  -rw------- 1 root root 11M Oct 22 14:32 perf.data
> 
>  # ./perf record -a -g --max-size=10K
>  [ perf record: perf size limit reached (10 KB), stopping session ]
>  Couldn't synthesize bpf events.
>  [ perf record: Woken up 0 times to write data ]
>  [ perf record: Captured and wrote 1.546 MB perf.data (69 samples) ]
> 
>  # ls -l perf.data
>  -rw------- 1 root root 1626952 Oct 22 14:36 perf.data
> 
> Signed-off-by: Jiwei Sun <jiwei.sun@windriver.com>
> ---
> v5 changes:
>   - Change the output format like [ perf record: perf size limit XX ]
>   - change the killing perf way from "raise(SIGTERM)" to set "done == 1"

Acked-by: Jiri Olsa <jolsa@kernel.org>

thanks,
jirka


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

* Re: [PATCH v5] perf record: Add support for limit perf output file size
  2019-11-01  8:13 ` Jiri Olsa
@ 2019-11-06 18:58   ` Arnaldo Carvalho de Melo
  2019-11-07 11:13   ` Arnaldo Carvalho de Melo
  1 sibling, 0 replies; 7+ messages in thread
From: Arnaldo Carvalho de Melo @ 2019-11-06 18:58 UTC (permalink / raw)
  To: Jiri Olsa
  Cc: jsun4, acme, arnaldo.melo, linux-kernel, alexander.shishkin,
	mpetlan, namhyung, a.p.zijlstra, adrian.hunter, Richard.Danter,
	jiwei.sun.bj

Em Fri, Nov 01, 2019 at 09:13:00AM +0100, Jiri Olsa escreveu:
> On Tue, Oct 22, 2019 at 04:09:01PM +0800, jsun4 wrote:
> > The patch adds a new option to limit the output file size, then based
> > on it, we can create a wrapper of the perf command that uses the option
> > to avoid exhausting the disk space by the unconscious user.
> > 
> > In order to make the perf.data parsable, we just limit the sample data
> > size, since the perf.data consists of many headers and sample data and
> > other data, the actual size of the recorded file will bigger than the
> > setting value.
> > 
> > Testing it:
> > 
> >  # ./perf record -a -g --max-size=10M
> >  Couldn't synthesize bpf events.
> >  [ perf record: perf size limit reached (10249 KB), stopping session ]
> >  [ perf record: Woken up 32 times to write data ]
> >  [ perf record: Captured and wrote 10.133 MB perf.data (71964 samples) ]
> > 
> >  # ls -lh perf.data
> >  -rw------- 1 root root 11M Oct 22 14:32 perf.data
> > 
> >  # ./perf record -a -g --max-size=10K
> >  [ perf record: perf size limit reached (10 KB), stopping session ]
> >  Couldn't synthesize bpf events.
> >  [ perf record: Woken up 0 times to write data ]
> >  [ perf record: Captured and wrote 1.546 MB perf.data (69 samples) ]
> > 
> >  # ls -l perf.data
> >  -rw------- 1 root root 1626952 Oct 22 14:36 perf.data
> > 
> > Signed-off-by: Jiwei Sun <jiwei.sun@windriver.com>
> > ---
> > v5 changes:
> >   - Change the output format like [ perf record: perf size limit XX ]
> >   - change the killing perf way from "raise(SIGTERM)" to set "done == 1"
> 
> Acked-by: Jiri Olsa <jolsa@kernel.org>

Thanks, applied.

- Arnaldo

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

* Re: [PATCH v5] perf record: Add support for limit perf output file size
  2019-11-01  8:13 ` Jiri Olsa
  2019-11-06 18:58   ` Arnaldo Carvalho de Melo
@ 2019-11-07 11:13   ` Arnaldo Carvalho de Melo
  2019-11-08  1:39     ` Jiwei Sun
  1 sibling, 1 reply; 7+ messages in thread
From: Arnaldo Carvalho de Melo @ 2019-11-07 11:13 UTC (permalink / raw)
  To: Jiri Olsa
  Cc: jsun4, acme, arnaldo.melo, linux-kernel, alexander.shishkin,
	mpetlan, namhyung, a.p.zijlstra, adrian.hunter, Richard.Danter,
	jiwei.sun.bj

Em Fri, Nov 01, 2019 at 09:13:00AM +0100, Jiri Olsa escreveu:
> On Tue, Oct 22, 2019 at 04:09:01PM +0800, jsun4 wrote:
> > The patch adds a new option to limit the output file size, then based
> > on it, we can create a wrapper of the perf command that uses the option
> > to avoid exhausting the disk space by the unconscious user.
> > 
> > In order to make the perf.data parsable, we just limit the sample data
> > size, since the perf.data consists of many headers and sample data and
> > other data, the actual size of the recorded file will bigger than the
> > setting value.
> > 
> > Testing it:
> > 
> >  # ./perf record -a -g --max-size=10M
> >  Couldn't synthesize bpf events.
> >  [ perf record: perf size limit reached (10249 KB), stopping session ]
> >  [ perf record: Woken up 32 times to write data ]
> >  [ perf record: Captured and wrote 10.133 MB perf.data (71964 samples) ]
> > 
> >  # ls -lh perf.data
> >  -rw------- 1 root root 11M Oct 22 14:32 perf.data
> > 
> >  # ./perf record -a -g --max-size=10K
> >  [ perf record: perf size limit reached (10 KB), stopping session ]
> >  Couldn't synthesize bpf events.
> >  [ perf record: Woken up 0 times to write data ]
> >  [ perf record: Captured and wrote 1.546 MB perf.data (69 samples) ]
> > 
> >  # ls -l perf.data
> >  -rw------- 1 root root 1626952 Oct 22 14:36 perf.data
> > 
> > Signed-off-by: Jiwei Sun <jiwei.sun@windriver.com>
> > ---
> > v5 changes:
> >   - Change the output format like [ perf record: perf size limit XX ]
> >   - change the killing perf way from "raise(SIGTERM)" to set "done == 1"
> 
> Acked-by: Jiri Olsa <jolsa@kernel.org>

So, had to add this on top to fix the build on multiple building
environments, rec->bytes_written is an u64, so we must use PRIu64 or
else get errors like:

  builtin-record.c: In function 'record__write':
  builtin-record.c:150:5: error: format '%lu' expects argument of type 'long unsigned int', but argument 3 has type 'u64' [-Werror=format=]
       rec->bytes_written >> 10);
       ^
    CC       /tmp/build/pe


- Arnaldo

diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
index b9ddfcda9611..b95c000c1ed9 100644
--- a/tools/perf/builtin-record.c
+++ b/tools/perf/builtin-record.c
@@ -145,7 +145,7 @@ static int record__write(struct record *rec, struct mmap *map __maybe_unused,
 	rec->bytes_written += size;
 
 	if (record__output_max_size_exceeded(rec) && !done) {
-		fprintf(stderr, "[ perf record: perf size limit reached (%lu KB),"
+		fprintf(stderr, "[ perf record: perf size limit reached (%" PRIu64 " KB),"
 				" stopping session ]\n",
 				rec->bytes_written >> 10);
 		done = 1;

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

* Re: [PATCH v5] perf record: Add support for limit perf output file size
  2019-11-07 11:13   ` Arnaldo Carvalho de Melo
@ 2019-11-08  1:39     ` Jiwei Sun
  0 siblings, 0 replies; 7+ messages in thread
From: Jiwei Sun @ 2019-11-08  1:39 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo, Jiri Olsa
  Cc: acme, linux-kernel, alexander.shishkin, mpetlan, namhyung,
	a.p.zijlstra, adrian.hunter, Richard.Danter, jiwei.sun.bj

Hi Arnaldo,

On 2019e9411f\x1c\b07f\x17% 19:13, Arnaldo Carvalho de Melo wrote:
> Em Fri, Nov 01, 2019 at 09:13:00AM +0100, Jiri Olsa escreveu:
>> On Tue, Oct 22, 2019 at 04:09:01PM +0800, jsun4 wrote:
>>> The patch adds a new option to limit the output file size, then based
>>> on it, we can create a wrapper of the perf command that uses the option
>>> to avoid exhausting the disk space by the unconscious user.
>>>
>>> In order to make the perf.data parsable, we just limit the sample data
>>> size, since the perf.data consists of many headers and sample data and
>>> other data, the actual size of the recorded file will bigger than the
>>> setting value.
>>>
>>> Testing it:
>>>
>>>  # ./perf record -a -g --max-size=10M
>>>  Couldn't synthesize bpf events.
>>>  [ perf record: perf size limit reached (10249 KB), stopping session ]
>>>  [ perf record: Woken up 32 times to write data ]
>>>  [ perf record: Captured and wrote 10.133 MB perf.data (71964 samples) ]
>>>
>>>  # ls -lh perf.data
>>>  -rw------- 1 root root 11M Oct 22 14:32 perf.data
>>>
>>>  # ./perf record -a -g --max-size=10K
>>>  [ perf record: perf size limit reached (10 KB), stopping session ]
>>>  Couldn't synthesize bpf events.
>>>  [ perf record: Woken up 0 times to write data ]
>>>  [ perf record: Captured and wrote 1.546 MB perf.data (69 samples) ]
>>>
>>>  # ls -l perf.data
>>>  -rw------- 1 root root 1626952 Oct 22 14:36 perf.data
>>>
>>> Signed-off-by: Jiwei Sun <jiwei.sun@windriver.com>
>>> ---
>>> v5 changes:
>>>   - Change the output format like [ perf record: perf size limit XX ]
>>>   - change the killing perf way from "raise(SIGTERM)" to set "done == 1"
>>
>> Acked-by: Jiri Olsa <jolsa@kernel.org>
> 
> So, had to add this on top to fix the build on multiple building
> environments, rec->bytes_written is an u64, so we must use PRIu64 or
> else get errors like:
> 
>   builtin-record.c: In function 'record__write':
>   builtin-record.c:150:5: error: format '%lu' expects argument of type 'long unsigned int', but argument 3 has type 'u64' [-Werror=format=]
>        rec->bytes_written >> 10);
>        ^
>     CC       /tmp/build/pe

Sorry for the flaw, and thanks for your suggestion and rectification. 
And I will pay attention to avoid such mistake next time.

Regards,
Jiwei

> 
> 
> - Arnaldo
> 
> diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
> index b9ddfcda9611..b95c000c1ed9 100644
> --- a/tools/perf/builtin-record.c
> +++ b/tools/perf/builtin-record.c
> @@ -145,7 +145,7 @@ static int record__write(struct record *rec, struct mmap *map __maybe_unused,
>  	rec->bytes_written += size;
>  
>  	if (record__output_max_size_exceeded(rec) && !done) {
> -		fprintf(stderr, "[ perf record: perf size limit reached (%lu KB),"
> +		fprintf(stderr, "[ perf record: perf size limit reached (%" PRIu64 " KB),"
>  				" stopping session ]\n",
>  				rec->bytes_written >> 10);
>  		done = 1;
> 

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

* [tip: perf/core] perf record: Add support for limit perf output file size
  2019-10-22  8:09 [PATCH v5] perf record: Add support for limit perf output file size jsun4
  2019-11-01  2:47 ` Jiwei Sun
  2019-11-01  8:13 ` Jiri Olsa
@ 2019-11-12 11:17 ` tip-bot2 for Jiwei Sun
  2 siblings, 0 replies; 7+ messages in thread
From: tip-bot2 for Jiwei Sun @ 2019-11-12 11:17 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: Jiwei Sun, Jiri Olsa, Arnaldo Carvalho de Melo, Adrian Hunter,
	Alexander Shishkin, Michael Petlan, Namhyung Kim, Peter Zijlstra,
	Richard Danter, Ingo Molnar, Borislav Petkov, linux-kernel

The following commit has been merged into the perf/core branch of tip:

Commit-ID:     6d57581659f7229903d141455c7308e309056e89
Gitweb:        https://git.kernel.org/tip/6d57581659f7229903d141455c7308e309056e89
Author:        Jiwei Sun <jiwei.sun@windriver.com>
AuthorDate:    Tue, 22 Oct 2019 16:09:01 +08:00
Committer:     Arnaldo Carvalho de Melo <acme@redhat.com>
CommitterDate: Thu, 07 Nov 2019 08:30:19 -03:00

perf record: Add support for limit perf output file size

The patch adds a new option to limit the output file size, then based on
it, we can create a wrapper of the perf command that uses the option to
avoid exhausting the disk space by the unconscious user.

In order to make the perf.data parsable, we just limit the sample data
size, since the perf.data consists of many headers and sample data and
other data, the actual size of the recorded file will bigger than the
setting value.

Testing it:

  # ./perf record -a -g --max-size=10M
  Couldn't synthesize bpf events.
  [ perf record: perf size limit reached (10249 KB), stopping session ]
  [ perf record: Woken up 32 times to write data ]
  [ perf record: Captured and wrote 10.133 MB perf.data (71964 samples) ]

  # ls -lh perf.data
  -rw------- 1 root root 11M Oct 22 14:32 perf.data

  # ./perf record -a -g --max-size=10K
  [ perf record: perf size limit reached (10 KB), stopping session ]
  Couldn't synthesize bpf events.
  [ perf record: Woken up 0 times to write data ]
  [ perf record: Captured and wrote 1.546 MB perf.data (69 samples) ]

  # ls -l perf.data
  -rw------- 1 root root 1626952 Oct 22 14:36 perf.data

Committer notes:

Fixed the build in multiple distros by using PRIu64 to print u64 struct
members, fixing this:

  builtin-record.c: In function 'record__write':
  builtin-record.c:150:5: error: format '%lu' expects argument of type 'long unsigned int', but argument 3 has type 'u64' [-Werror=format=]
       rec->bytes_written >> 10);
       ^
    CC       /tmp/build/pe

Signed-off-by: Jiwei Sun <jiwei.sun@windriver.com>
Acked-by: Jiri Olsa <jolsa@kernel.org>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Michael Petlan <mpetlan@redhat.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Richard Danter <richard.danter@windriver.com>
Link: http://lore.kernel.org/lkml/20191022080901.3841-1-jiwei.sun@windriver.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
 tools/perf/Documentation/perf-record.txt |  4 ++-
 tools/perf/builtin-record.c              | 46 ++++++++++++++++++++++-
 2 files changed, 49 insertions(+), 1 deletion(-)

diff --git a/tools/perf/Documentation/perf-record.txt b/tools/perf/Documentation/perf-record.txt
index 8a45061..ebcba1f 100644
--- a/tools/perf/Documentation/perf-record.txt
+++ b/tools/perf/Documentation/perf-record.txt
@@ -574,6 +574,10 @@ Implies --tail-synthesize.
 --kcore::
 Make a copy of /proc/kcore and place it into a directory with the perf data file.
 
+--max-size=<size>::
+Limit the sample data max size, <size> is expected to be a number with
+appended unit character - B/K/M/G
+
 SEE ALSO
 --------
 linkperf:perf-stat[1], linkperf:perf-list[1]
diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
index f6664bb..b95c000 100644
--- a/tools/perf/builtin-record.c
+++ b/tools/perf/builtin-record.c
@@ -94,8 +94,11 @@ struct record {
 	struct switch_output	switch_output;
 	unsigned long long	samples;
 	cpu_set_t		affinity_mask;
+	unsigned long		output_max_size;	/* = 0: unlimited */
 };
 
+static volatile int done;
+
 static volatile int auxtrace_record__snapshot_started;
 static DEFINE_TRIGGER(auxtrace_snapshot_trigger);
 static DEFINE_TRIGGER(switch_output_trigger);
@@ -123,6 +126,12 @@ static bool switch_output_time(struct record *rec)
 	       trigger_is_ready(&switch_output_trigger);
 }
 
+static bool record__output_max_size_exceeded(struct record *rec)
+{
+	return rec->output_max_size &&
+	       (rec->bytes_written >= rec->output_max_size);
+}
+
 static int record__write(struct record *rec, struct mmap *map __maybe_unused,
 			 void *bf, size_t size)
 {
@@ -135,6 +144,13 @@ static int record__write(struct record *rec, struct mmap *map __maybe_unused,
 
 	rec->bytes_written += size;
 
+	if (record__output_max_size_exceeded(rec) && !done) {
+		fprintf(stderr, "[ perf record: perf size limit reached (%" PRIu64 " KB),"
+				" stopping session ]\n",
+				rec->bytes_written >> 10);
+		done = 1;
+	}
+
 	if (switch_output_size(rec))
 		trigger_hit(&switch_output_trigger);
 
@@ -499,7 +515,6 @@ static int record__pushfn(struct mmap *map, void *to, void *bf, size_t size)
 	return record__write(rec, map, bf, size);
 }
 
-static volatile int done;
 static volatile int signr = -1;
 static volatile int child_finished;
 
@@ -1984,6 +1999,33 @@ static int record__parse_affinity(const struct option *opt, const char *str, int
 	return 0;
 }
 
+static int parse_output_max_size(const struct option *opt,
+				 const char *str, int unset)
+{
+	unsigned long *s = (unsigned long *)opt->value;
+	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 (unset) {
+		*s = 0;
+		return 0;
+	}
+
+	val = parse_tag_value(str, tags_size);
+	if (val != (unsigned long) -1) {
+		*s = val;
+		return 0;
+	}
+
+	return -1;
+}
+
 static int record__parse_mmap_pages(const struct option *opt,
 				    const char *str,
 				    int unset __maybe_unused)
@@ -2311,6 +2353,8 @@ static struct option __record_options[] = {
 			    "n", "Compressed records using specified level (default: 1 - fastest compression, 22 - greatest compression)",
 			    record__parse_comp_level),
 #endif
+	OPT_CALLBACK(0, "max-size", &record.output_max_size,
+		     "size", "Limit the maximum size of the output file", parse_output_max_size),
 	OPT_END()
 };
 

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

end of thread, other threads:[~2019-11-12 11:22 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-10-22  8:09 [PATCH v5] perf record: Add support for limit perf output file size jsun4
2019-11-01  2:47 ` Jiwei Sun
2019-11-01  8:13 ` Jiri Olsa
2019-11-06 18:58   ` Arnaldo Carvalho de Melo
2019-11-07 11:13   ` Arnaldo Carvalho de Melo
2019-11-08  1:39     ` Jiwei Sun
2019-11-12 11:17 ` [tip: perf/core] " tip-bot2 for Jiwei Sun

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.