linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v4] perf record: Add support for limit perf output file size
@ 2019-07-17  8:19 Jiwei Sun
  0 siblings, 0 replies; 6+ messages in thread
From: Jiwei Sun @ 2019-07-17  8:19 UTC (permalink / raw)
  Cc: peterz, mingo, acme, alexander.shishkin, jolsa, namhyung,
	arnaldo.melo, linux-kernel

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.
WARNING: The perf data has already reached the limit, stop recording!
[ perf record: Woken up 30 times to write data ]
[ perf record: Captured and wrote 10.233 MB perf.data (175650 samples) ]
Terminated

 # ls -lh perf.data
-rw------- 1 root root 11M Jul 17 14:01 perf.data

 # ./perf record -a -g --max-size=10K
WARNING: The perf data has already reached the limit, stop recording!
Couldn't synthesize bpf events.
[ perf record: Woken up 0 times to write data ]
[ perf record: Captured and wrote 1.824 MB perf.data (67 samples) ]
Terminated

 # ls -lh perf.data
-rw------- 1 root root 1.9M Jul 17 14:05 perf.data

Signed-off-by: Jiwei Sun <jiwei.sun@windriver.com>
---
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              | 42 ++++++++++++++++++++++++
 2 files changed, 46 insertions(+)

diff --git a/tools/perf/Documentation/perf-record.txt b/tools/perf/Documentation/perf-record.txt
index 15e0fa87241b..918a0844cbcf 100644
--- a/tools/perf/Documentation/perf-record.txt
+++ b/tools/perf/Documentation/perf-record.txt
@@ -564,6 +564,10 @@ config terms. For example: 'cycles/overwrite/' and 'instructions/no-overwrite/'.
 
 Implies --tail-synthesize.
 
+--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 8779cee58185..8c49dce3abd8 100644
--- a/tools/perf/builtin-record.c
+++ b/tools/perf/builtin-record.c
@@ -86,6 +86,7 @@ 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 auxtrace_record__snapshot_started;
@@ -115,6 +116,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 perf_mmap *map __maybe_unused,
 			 void *bf, size_t size)
 {
@@ -127,6 +134,12 @@ static int record__write(struct record *rec, struct perf_mmap *map __maybe_unuse
 
 	rec->bytes_written += size;
 
+	if (record__output_max_size_exceeded(rec)) {
+		WARN_ONCE(1, "WARNING: The perf data has already reached "
+			     "the limit, stop recording!\n");
+		raise(SIGTERM);
+	}
+
 	if (switch_output_size(rec))
 		trigger_hit(&switch_output_trigger);
 
@@ -1902,6 +1915,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)
@@ -2228,6 +2268,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] 6+ messages in thread

* Re: [PATCH v4] perf record: Add support for limit perf output file size
  2019-10-21 13:41 ` Jiri Olsa
@ 2019-10-22  5:56   ` Jiwei Sun
  0 siblings, 0 replies; 6+ messages in thread
From: Jiwei Sun @ 2019-10-22  5:56 UTC (permalink / raw)
  To: Jiri Olsa
  Cc: acme, arnaldo.melo, linux-kernel, alexander.shishkin, mpetlan,
	namhyung, a.p.zijlstra, adrian.hunter, Richard.Danter

Hi Jirka,

On 2019e9410f\x1c\b21f\x17% 21:41, Jiri Olsa wrote:
> On Wed, Sep 25, 2019 at 03:06:37PM +0800, Jiwei Sun wrote:
> 
> SNIP
> 
>>  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 48600c90cc7e..30904d2a3407 100644
>> --- a/tools/perf/builtin-record.c
>> +++ b/tools/perf/builtin-record.c
>> @@ -91,6 +91,7 @@ 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 auxtrace_record__snapshot_started;
>> @@ -120,6 +121,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)
>>  {
>> @@ -132,6 +139,12 @@ static int record__write(struct record *rec, struct mmap *map __maybe_unused,
>>  
>>  	rec->bytes_written += size;
>>  
>> +	if (record__output_max_size_exceeded(rec)) {
>> +		WARN_ONCE(1, "WARNING: The perf data has already reached "
>> +			     "the limit, stop recording!\n");
> 
> I think the message whouldn't be a warning, the user asked for
> that, maybe something more like:
> 
>   [ perf record: perf size limit reached (XXMB), stopping session ]
> 
>> +		raise(SIGTERM);
> 
> could we just set 'done = 1' what's the benefit in killing perf?

Thanks for your suggestions. Yes, if just set "done == 1" is more efficient and more concise.
And I will modify it and the output format, and then send a v5 patch.
Thanks again.

Regards,
Jiwei

> 
> thanks,
> jirka
> 
> 

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

* Re: [PATCH v4] perf record: Add support for limit perf output file size
  2019-09-25  7:06 Jiwei Sun
  2019-10-21 11:51 ` Jiwei Sun
@ 2019-10-21 13:41 ` Jiri Olsa
  2019-10-22  5:56   ` Jiwei Sun
  1 sibling, 1 reply; 6+ messages in thread
From: Jiri Olsa @ 2019-10-21 13:41 UTC (permalink / raw)
  To: Jiwei Sun
  Cc: acme, arnaldo.melo, linux-kernel, alexander.shishkin, mpetlan,
	namhyung, a.p.zijlstra, adrian.hunter, Richard.Danter

On Wed, Sep 25, 2019 at 03:06:37PM +0800, Jiwei Sun wrote:

SNIP

>  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 48600c90cc7e..30904d2a3407 100644
> --- a/tools/perf/builtin-record.c
> +++ b/tools/perf/builtin-record.c
> @@ -91,6 +91,7 @@ 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 auxtrace_record__snapshot_started;
> @@ -120,6 +121,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)
>  {
> @@ -132,6 +139,12 @@ static int record__write(struct record *rec, struct mmap *map __maybe_unused,
>  
>  	rec->bytes_written += size;
>  
> +	if (record__output_max_size_exceeded(rec)) {
> +		WARN_ONCE(1, "WARNING: The perf data has already reached "
> +			     "the limit, stop recording!\n");

I think the message whouldn't be a warning, the user asked for
that, maybe something more like:

  [ perf record: perf size limit reached (XXMB), stopping session ]

> +		raise(SIGTERM);

could we just set 'done = 1' what's the benefit in killing perf?

thanks,
jirka


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

* Re: [PATCH v4] perf record: Add support for limit perf output file size
  2019-10-21 11:51 ` Jiwei Sun
@ 2019-10-21 11:55   ` Jiri Olsa
  0 siblings, 0 replies; 6+ messages in thread
From: Jiri Olsa @ 2019-10-21 11:55 UTC (permalink / raw)
  To: Jiwei Sun
  Cc: acme, arnaldo.melo, linux-kernel, alexander.shishkin, mpetlan,
	namhyung, a.p.zijlstra, adrian.hunter, Richard.Danter

On Mon, Oct 21, 2019 at 07:51:17PM +0800, Jiwei Sun wrote:
> Hello Arnaldo & Jirka,
> 
> Do you have any other suggestions regarding the patch? 
> Any suggestions are welcome. Thank you very much.

oops, overlooked this one, sry..  I'll check on it today

jirka

> 
> Regards,
> Jiwei
> 
> On 2019e9409f\x1c\b25f\x17% 15:06, Jiwei Sun 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.
> > WARNING: The perf data has already reached the limit, stop recording!
> > [ perf record: Woken up 30 times to write data ]
> > [ perf record: Captured and wrote 10.233 MB perf.data (175650 samples) ]
> > Terminated
> > 
> >  # ls -lh perf.data
> > -rw------- 1 root root 11M Jul 17 14:01 perf.data
> > 
> >  # ./perf record -a -g --max-size=10K
> > WARNING: The perf data has already reached the limit, stop recording!
> > Couldn't synthesize bpf events.
> > [ perf record: Woken up 0 times to write data ]
> > [ perf record: Captured and wrote 1.824 MB perf.data (67 samples) ]
> > Terminated
> > 
> >  # ls -lh perf.data
> > -rw------- 1 root root 1.9M Jul 17 14:05 perf.data
> > 
> > Signed-off-by: Jiwei Sun <jiwei.sun@windriver.com>
> > ---
> > 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              | 42 ++++++++++++++++++++++++
> >  2 files changed, 46 insertions(+)
> > 
> > diff --git a/tools/perf/Documentation/perf-record.txt b/tools/perf/Documentation/perf-record.txt
> > index c6f9f31b6039..f1c6113fbc82 100644
> > --- a/tools/perf/Documentation/perf-record.txt
> > +++ b/tools/perf/Documentation/perf-record.txt
> > @@ -571,6 +571,10 @@ config terms. For example: 'cycles/overwrite/' and 'instructions/no-overwrite/'.
> >  
> >  Implies --tail-synthesize.
> >  
> > +--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 48600c90cc7e..30904d2a3407 100644
> > --- a/tools/perf/builtin-record.c
> > +++ b/tools/perf/builtin-record.c
> > @@ -91,6 +91,7 @@ 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 auxtrace_record__snapshot_started;
> > @@ -120,6 +121,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)
> >  {
> > @@ -132,6 +139,12 @@ static int record__write(struct record *rec, struct mmap *map __maybe_unused,
> >  
> >  	rec->bytes_written += size;
> >  
> > +	if (record__output_max_size_exceeded(rec)) {
> > +		WARN_ONCE(1, "WARNING: The perf data has already reached "
> > +			     "the limit, stop recording!\n");
> > +		raise(SIGTERM);
> > +	}
> > +
> >  	if (switch_output_size(rec))
> >  		trigger_hit(&switch_output_trigger);
> >  
> > @@ -1936,6 +1949,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)
> > @@ -2262,6 +2302,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] 6+ messages in thread

* Re: [PATCH v4] perf record: Add support for limit perf output file size
  2019-09-25  7:06 Jiwei Sun
@ 2019-10-21 11:51 ` Jiwei Sun
  2019-10-21 11:55   ` Jiri Olsa
  2019-10-21 13:41 ` Jiri Olsa
  1 sibling, 1 reply; 6+ messages in thread
From: Jiwei Sun @ 2019-10-21 11:51 UTC (permalink / raw)
  To: acme, jolsa, arnaldo.melo, linux-kernel
  Cc: alexander.shishkin, mpetlan, namhyung, a.p.zijlstra,
	adrian.hunter, Richard.Danter

Hello Arnaldo & Jirka,

Do you have any other suggestions regarding the patch? 
Any suggestions are welcome. Thank you very much.

Regards,
Jiwei

On 2019e9409f\x1c\b25f\x17% 15:06, Jiwei Sun 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.
> WARNING: The perf data has already reached the limit, stop recording!
> [ perf record: Woken up 30 times to write data ]
> [ perf record: Captured and wrote 10.233 MB perf.data (175650 samples) ]
> Terminated
> 
>  # ls -lh perf.data
> -rw------- 1 root root 11M Jul 17 14:01 perf.data
> 
>  # ./perf record -a -g --max-size=10K
> WARNING: The perf data has already reached the limit, stop recording!
> Couldn't synthesize bpf events.
> [ perf record: Woken up 0 times to write data ]
> [ perf record: Captured and wrote 1.824 MB perf.data (67 samples) ]
> Terminated
> 
>  # ls -lh perf.data
> -rw------- 1 root root 1.9M Jul 17 14:05 perf.data
> 
> Signed-off-by: Jiwei Sun <jiwei.sun@windriver.com>
> ---
> 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              | 42 ++++++++++++++++++++++++
>  2 files changed, 46 insertions(+)
> 
> diff --git a/tools/perf/Documentation/perf-record.txt b/tools/perf/Documentation/perf-record.txt
> index c6f9f31b6039..f1c6113fbc82 100644
> --- a/tools/perf/Documentation/perf-record.txt
> +++ b/tools/perf/Documentation/perf-record.txt
> @@ -571,6 +571,10 @@ config terms. For example: 'cycles/overwrite/' and 'instructions/no-overwrite/'.
>  
>  Implies --tail-synthesize.
>  
> +--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 48600c90cc7e..30904d2a3407 100644
> --- a/tools/perf/builtin-record.c
> +++ b/tools/perf/builtin-record.c
> @@ -91,6 +91,7 @@ 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 auxtrace_record__snapshot_started;
> @@ -120,6 +121,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)
>  {
> @@ -132,6 +139,12 @@ static int record__write(struct record *rec, struct mmap *map __maybe_unused,
>  
>  	rec->bytes_written += size;
>  
> +	if (record__output_max_size_exceeded(rec)) {
> +		WARN_ONCE(1, "WARNING: The perf data has already reached "
> +			     "the limit, stop recording!\n");
> +		raise(SIGTERM);
> +	}
> +
>  	if (switch_output_size(rec))
>  		trigger_hit(&switch_output_trigger);
>  
> @@ -1936,6 +1949,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)
> @@ -2262,6 +2302,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] 6+ messages in thread

* [PATCH v4] perf record: Add support for limit perf output file size
@ 2019-09-25  7:06 Jiwei Sun
  2019-10-21 11:51 ` Jiwei Sun
  2019-10-21 13:41 ` Jiri Olsa
  0 siblings, 2 replies; 6+ messages in thread
From: Jiwei Sun @ 2019-09-25  7:06 UTC (permalink / raw)
  To: acme, jolsa, arnaldo.melo, linux-kernel
  Cc: alexander.shishkin, mpetlan, namhyung, a.p.zijlstra,
	adrian.hunter, Richard.Danter, jiwei.sun

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.
WARNING: The perf data has already reached the limit, stop recording!
[ perf record: Woken up 30 times to write data ]
[ perf record: Captured and wrote 10.233 MB perf.data (175650 samples) ]
Terminated

 # ls -lh perf.data
-rw------- 1 root root 11M Jul 17 14:01 perf.data

 # ./perf record -a -g --max-size=10K
WARNING: The perf data has already reached the limit, stop recording!
Couldn't synthesize bpf events.
[ perf record: Woken up 0 times to write data ]
[ perf record: Captured and wrote 1.824 MB perf.data (67 samples) ]
Terminated

 # ls -lh perf.data
-rw------- 1 root root 1.9M Jul 17 14:05 perf.data

Signed-off-by: Jiwei Sun <jiwei.sun@windriver.com>
---
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              | 42 ++++++++++++++++++++++++
 2 files changed, 46 insertions(+)

diff --git a/tools/perf/Documentation/perf-record.txt b/tools/perf/Documentation/perf-record.txt
index c6f9f31b6039..f1c6113fbc82 100644
--- a/tools/perf/Documentation/perf-record.txt
+++ b/tools/perf/Documentation/perf-record.txt
@@ -571,6 +571,10 @@ config terms. For example: 'cycles/overwrite/' and 'instructions/no-overwrite/'.
 
 Implies --tail-synthesize.
 
+--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 48600c90cc7e..30904d2a3407 100644
--- a/tools/perf/builtin-record.c
+++ b/tools/perf/builtin-record.c
@@ -91,6 +91,7 @@ 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 auxtrace_record__snapshot_started;
@@ -120,6 +121,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)
 {
@@ -132,6 +139,12 @@ static int record__write(struct record *rec, struct mmap *map __maybe_unused,
 
 	rec->bytes_written += size;
 
+	if (record__output_max_size_exceeded(rec)) {
+		WARN_ONCE(1, "WARNING: The perf data has already reached "
+			     "the limit, stop recording!\n");
+		raise(SIGTERM);
+	}
+
 	if (switch_output_size(rec))
 		trigger_hit(&switch_output_trigger);
 
@@ -1936,6 +1949,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)
@@ -2262,6 +2302,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] 6+ messages in thread

end of thread, other threads:[~2019-10-22  5:56 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-07-17  8:19 [PATCH v4] perf record: Add support for limit perf output file size Jiwei Sun
2019-09-25  7:06 Jiwei Sun
2019-10-21 11:51 ` Jiwei Sun
2019-10-21 11:55   ` Jiri Olsa
2019-10-21 13:41 ` Jiri Olsa
2019-10-22  5:56   ` Jiwei Sun

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).