Skip to contentSkip to conversation

perf record: Add support for limit perf output file size

PATCH Superseded Jiwei Sun 2 participants 3 messages Feb 21, 2019 – Feb 22, 2019

Subsystems (3)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Jiwei Sun

author
#0 lore raw

PATCH 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.

---
 tools/perf/builtin-record.c | 39 +++++++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)
tools/perf/builtin-record.c +39 -0
index 882285fb9f64..28a03929166d 100644
@@ -81,6 +81,7 @@ struct record {
 	bool			timestamp_boundary;
 	struct switch_output	switch_output;
 	unsigned long long	samples;
+	unsigned long		output_max_size;	/* = 0: unlimited */
 };
 
 static volatile int auxtrace_record__snapshot_started;
@@ -106,6 +107,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)
 {
@@ -118,6 +125,9 @@ static int record__write(struct record *rec, struct perf_mmap *map __maybe_unuse
 
 	rec->bytes_written += size;
 
+	if (record__output_max_size_exceeded(rec))
+		raise(SIGTERM);
+
 	if (switch_output_size(rec))
 		trigger_hit(&switch_output_trigger);
 
@@ -1639,6 +1649,33 @@ static int parse_clockid(const struct option *opt, const char *str, int unset)
 	return -1;
 }
 
+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)
@@ -1946,6 +1983,8 @@ static struct option __record_options[] = {
 		     &nr_cblocks_default, "n", "Use <n> control blocks in asynchronous trace writing mode (default: 1, max: 4)",
 		     record__aio_parse),
 #endif
+	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
Quoted 14 lines · 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.

---
 tools/perf/builtin-record.c | 39 +++++++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)
tools/perf/builtin-record.c +0 -0
index 882285fb9f64..28a03929166d 100644

tools/perf/builtin-record.c struct record { · line 84

Quoted 4 lines
tools/perf/builtin-record.c +1 -0
@@ -81,6 +81,7 @@ struct record {
 	bool			timestamp_boundary;
 	struct switch_output	switch_output;
 	unsigned long long	samples;
+	unsigned long		output_max_size;	/* = 0: unlimited */

please rebase to latest Arnaldo's perf/core,
there are new fields in here now

Quoted 17 lines
tools/perf/builtin-record.c +6 -0
 };
 
 static volatile int auxtrace_record__snapshot_started;
@@ -106,6 +107,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)
 {

tools/perf/builtin-record.c static int record__write(struct record *rec, struct perf_mmap *map __maybe_unuse · line 129

Quoted 5 lines
tools/perf/builtin-record.c +2 -0
@@ -118,6 +125,9 @@ static int record__write(struct record *rec, struct perf_mmap *map __maybe_unuse
 
 	rec->bytes_written += size;
 
+	if (record__output_max_size_exceeded(rec))
+		raise(SIGTERM);

perhaps display some message saying we reached the limit

other than that looks good to me

thanks,
jirka

Quoted 50 lines
tools/perf/builtin-record.c +30 -0
+
 	if (switch_output_size(rec))
 		trigger_hit(&switch_output_trigger);
 
@@ -1639,6 +1649,33 @@ static int parse_clockid(const struct option *opt, const char *str, int unset)
 	return -1;
 }
 
+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)
@@ -1946,6 +1983,8 @@ static struct option __record_options[] = {
 		     &nr_cblocks_default, "n", "Use <n> control blocks in asynchronous trace writing mode (default: 1, max: 4)",
 		     record__aio_parse),
 #endif
+	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
author
#2 lore raw

Hi Jirka,

Quoted 10 lines · Jiri Olsa
Quoted 9 lines · 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.

---
 tools/perf/builtin-record.c | 39 +++++++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)
Quoted 12 lines

tools/perf/builtin-record.c struct record { · line 84

Quoted 9 lines
tools/perf/builtin-record.c +1 -0
index 882285fb9f64..28a03929166d 100644
@@ -81,6 +81,7 @@ struct record {
 	bool			timestamp_boundary;
 	struct switch_output	switch_output;
 	unsigned long long	samples;
+	unsigned long		output_max_size;	/* = 0: unlimited */

please rebase to latest Arnaldo's perf/core,
there are new fields in here now

OK, thanks for your response, and I will send a v2 patch based on the branch perf/core.

Quoted 19 lines
Quoted 19 lines
tools/perf/builtin-record.c +6 -0
 };
 
 static volatile int auxtrace_record__snapshot_started;
@@ -106,6 +107,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)
 {
@@ -118,6 +125,9 @@ static int record__write(struct record *rec, struct perf_mmap *map __maybe_unuse
 
Quoted 8 lines

tools/perf/builtin-record.c static int record__write(struct record *rec, struct perf_mmap *map __maybe_unuse · line 129

Quoted 4 lines
tools/perf/builtin-record.c +2 -0
 	rec->bytes_written += size;
 
+	if (record__output_max_size_exceeded(rec))
+		raise(SIGTERM);

perhaps display some message saying we reached the limit

other than that looks good to me

OK, I will add some warning message in the v2 patch.

Thanks,
Regards,
Jiwei

Quoted 56 lines

thanks,
jirka

Quoted 50 lines
tools/perf/builtin-record.c +30 -0
+
 	if (switch_output_size(rec))
 		trigger_hit(&switch_output_trigger);
 
@@ -1639,6 +1649,33 @@ static int parse_clockid(const struct option *opt, const char *str, int unset)
 	return -1;
 }
 
+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)
@@ -1946,6 +1983,8 @@ static struct option __record_options[] = {
 		     &nr_cblocks_default, "n", "Use <n> control blocks in asynchronous trace writing mode (default: 1, max: 4)",
 		     record__aio_parse),
 #endif
+	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