Skip to contentSkip to conversation

perf record: Add support for limit perf output file size

PATCH Superseded Jiwei Sun 2 participants 3 messages Feb 22, 2019 – Feb 25, 2019 perf events

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Jiwei Sun

author
#0 lore raw

PATCH v2 1/1 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.

---
v2 changes:
- make patch based on latest Arnaldo's perf/core,
- display warning message when reached the limit.

---
 tools/perf/builtin-record.c | 42 +++++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)
tools/perf/builtin-record.c +42 -0
index 6c3719ac901d..dc3648c0816d 100644
@@ -83,6 +83,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;
@@ -112,6 +113,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)
 {
@@ -124,6 +131,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)) {
+		pr_warning("WARNING: The perf data has already reached "
+			   "the limit, stop recording!\n");
+		raise(SIGTERM);
+	}
+
 	if (switch_output_size(rec))
 		trigger_hit(&switch_output_trigger);
 
@@ -1671,6 +1684,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)
@@ -1982,6 +2022,8 @@ static struct option __record_options[] = {
 	OPT_CALLBACK(0, "affinity", &record.opts, "node|cpu",
 		     "Set affinity mask of trace reading thread to NUMA node cpu mask or cpu of processed mmap buffer",
 		     record__parse_affinity),
+	OPT_CALLBACK(0, "output-max-size", &record.output_max_size,
+		     "size", "Output file maximum size", parse_output_max_size),
 	OPT_END()
 };
 
Signature · 1 line
2.20.1
#1 lore raw

Em Fri, Feb 22, 2019 at 03:19:04PM +0800, Jiwei Sun escreveu:

Quoted 7 lines

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.

---
v2 changes:

Please do a v3 adding this new option to
tools/perf/Documentation/perf-record.txt

Please also rename it to --max-size-output, because then we coudl use:

perf record --max-size-output

or:

perf record --max-size

or even:

perf record --max saving typing :-)

Also please show it in use, i.e. the output of it working.

- Arnaldo

Quoted 89 lines

- make patch based on latest Arnaldo's perf/core,
- display warning message when reached the limit.

---
 tools/perf/builtin-record.c | 42 +++++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)
tools/perf/builtin-record.c +42 -0
index 6c3719ac901d..dc3648c0816d 100644
@@ -83,6 +83,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;
@@ -112,6 +113,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)
 {
@@ -124,6 +131,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)) {
+		pr_warning("WARNING: The perf data has already reached "
+			   "the limit, stop recording!\n");
+		raise(SIGTERM);
+	}
+
 	if (switch_output_size(rec))
 		trigger_hit(&switch_output_trigger);
 
@@ -1671,6 +1684,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)
@@ -1982,6 +2022,8 @@ static struct option __record_options[] = {
 	OPT_CALLBACK(0, "affinity", &record.opts, "node|cpu",
 		     "Set affinity mask of trace reading thread to NUMA node cpu mask or cpu of processed mmap buffer",
 		     record__parse_affinity),
+	OPT_CALLBACK(0, "output-max-size", &record.output_max_size,
+		     "size", "Output file maximum size", parse_output_max_size),
 	OPT_END()
 };
 
Signature · 1 line
2.20.1
Signature · 1 line
- Arnaldo
author
#2 lore raw

Hi Arnaldo,

Quoted 18 lines · Arnaldo Carvalho de Melo

Em Fri, Feb 22, 2019 at 03:19:04PM +0800, Jiwei Sun escreveu:

Quoted 7 lines

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.

---
v2 changes:

Please do a v3 adding this new option to
tools/perf/Documentation/perf-record.txt

Please also rename it to --max-size-output, because then we coudl use:

perf record --max-size-output

or:

Quoted 7 lines

perf record --max-size

or even:

perf record --max saving typing :-)

Also please show it in use, i.e. the output of it working.

Thanks for your advice, I will modify the patch and send a v3.

Thanks,
Regards,
Jiwei

Quoted 94 lines

- Arnaldo

Quoted 89 lines

- make patch based on latest Arnaldo's perf/core,
- display warning message when reached the limit.

---
 tools/perf/builtin-record.c | 42 +++++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)
tools/perf/builtin-record.c +42 -0
index 6c3719ac901d..dc3648c0816d 100644
@@ -83,6 +83,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;
@@ -112,6 +113,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)
 {
@@ -124,6 +131,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)) {
+		pr_warning("WARNING: The perf data has already reached "
+			   "the limit, stop recording!\n");
+		raise(SIGTERM);
+	}
+
 	if (switch_output_size(rec))
 		trigger_hit(&switch_output_trigger);
 
@@ -1671,6 +1684,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)
@@ -1982,6 +2022,8 @@ static struct option __record_options[] = {
 	OPT_CALLBACK(0, "affinity", &record.opts, "node|cpu",
 		     "Set affinity mask of trace reading thread to NUMA node cpu mask or cpu of processed mmap buffer",
 		     record__parse_affinity),
+	OPT_CALLBACK(0, "output-max-size", &record.output_max_size,
+		     "size", "Output file maximum size", parse_output_max_size),
 	OPT_END()
 };
 
Signature · 1 line
2.20.1