Skip to contentSkip to conversation

perf data: Allow filtering conversion by start and end time

PATCH Superseded Derek Foreman 2 participants 3 messages Nov 24, 2025 – Nov 28, 2025 perf events

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 perf data: Allow filtering conversion by start and end time

This adds a feature to allow restricting the range of converted samples with start and end times specified in nanoseconds.

Use it like this:

  $ perf data convert --to-json out.json -s 12345 -e 13340

Start, end, or both may be omitted.

---
 tools/perf/Documentation/perf-data.txt |  6 ++++++
 tools/perf/builtin-data.c              |  4 ++++
 tools/perf/util/data-convert-bt.c      | 18 ++++++++++++++++++
 tools/perf/util/data-convert-json.c    | 18 ++++++++++++++++++
 tools/perf/util/data-convert.h         |  3 +++
 5 files changed, 49 insertions(+)
tools/perf/Documentation/perf-data.txt +6 -0
index 417bf17e265c..db0de1f354a3 100644
@@ -40,6 +40,12 @@ OPTIONS for 'convert'
 --force::
 	Don't complain, do it.
 
+-s::
+	Start time, in nanoseconds. Samples before this time will not be converted.
+
+-e::
+	End time, in nanoseconds. Samples after this time will not be converted.
+
 -v::
 --verbose::
         Be more verbose (show counter open errors, etc).
tools/perf/builtin-data.c +4 -0
index ce51cbf6dc97..da813c62e284 100644
@@ -33,6 +33,8 @@ const char *to_ctf;
 struct perf_data_convert_opts opts = {
 	.force = false,
 	.all = false,
+	.range_start = 0,
+	.range_end = 0,
 };
 
 const struct option data_options[] = {
@@ -45,6 +47,8 @@ const struct option data_options[] = {
 #endif
 		OPT_BOOLEAN('f', "force", &opts.force, "don't complain, do it"),
 		OPT_BOOLEAN(0, "all", &opts.all, "Convert all events"),
+		OPT_U64('s', "start", &opts.range_start, "Earliest timestamp to convert"),
+		OPT_U64('e', "end", &opts.range_end, "Latest timestamp to convert"),
 		OPT_END()
 	};
 
tools/perf/util/data-convert-bt.c +18 -0
index 3d2e437e1354..6819f11fe900 100644
@@ -91,9 +91,13 @@ struct convert {
 	struct perf_tool	tool;
 	struct ctf_writer	writer;
 
+	u64			start;
+	u64			end;
+
 	u64			events_size;
 	u64			events_count;
 	u64			non_sample_count;
+	u64			skipped;
 
 	/* Ordered events configured queue size. */
 	u64			queue_size;
@@ -811,6 +815,12 @@ static int process_sample_event(const struct perf_tool *tool,
 	if (WARN_ONCE(!priv, "Failed to setup all events.\n"))
 		return 0;
 
+	if (sample->time < c->start ||
+	    (c->end && sample->time > c->end)) {
+		++c->skipped;
+		return 0;
+	}
+
 	event_class = priv->event_class;
 
 	/* update stats */
@@ -1626,6 +1636,9 @@ int bt_convert__perf2ctf(const char *input, const char *path,
 	c.tool.namespaces      = perf_event__process_namespaces;
 	c.tool.ordering_requires_timestamps = true;
 
+	c.start = opts->range_start;
+	c.end = opts->range_end;
+
 	if (opts->all) {
 		c.tool.comm = process_comm_event;
 		c.tool.exit = process_exit_event;
@@ -1677,6 +1690,11 @@ int bt_convert__perf2ctf(const char *input, const char *path,
 		"[ perf data convert: Converted '%s' into CTF data '%s' ]\n",
 		data.path, path);
 
+	if (c.skipped)
+		fprintf(stderr,
+			"[ perf data convert: Skipped %" PRIu64 " samples",
+			c.skipped);
+
 	fprintf(stderr,
 		"[ perf data convert: Converted and wrote %.3f MB (%" PRIu64 " samples",
 		(double) c.events_size / 1024.0 / 1024.0,
tools/perf/util/data-convert-json.c +18 -0
index 9dc1e184cf3c..dcbba34d6b08 100644
@@ -35,7 +35,11 @@ struct convert_json {
 	struct perf_tool tool;
 	FILE *out;
 	bool first;
+	u64 start;
+	u64 end;
+
 	u64 events_count;
+	u64 skipped;
 };
 
 // Outputs a JSON-encoded string surrounded by quotes with characters escaped.
@@ -165,6 +169,12 @@ static int process_sample_event(const struct perf_tool *tool,
 		return -1;
 	}
 
+	if (sample->time < c->start ||
+	    (c->end && sample->time > c->end)) {
+		++c->skipped;
+		return 0;
+	}
+
 	++c->events_count;
 
 	if (c->first)
@@ -320,6 +330,9 @@ int bt_convert__perf2json(const char *input_name, const char *output_name,
 	struct convert_json c = {
 		.first = true,
 		.events_count = 0,
+		.start = opts->range_start,
+		.end = opts->range_end,
+		.skipped = 0,
 	};
 	struct perf_data data = {
 		.mode = PERF_DATA_MODE_READ,
@@ -407,6 +420,11 @@ int bt_convert__perf2json(const char *input_name, const char *output_name,
 			"[ perf data convert: Converted '%s' into JSON data '%s' ]\n",
 			data.path, output_name);
 
+	if (c.skipped)
+		fprintf(stderr,
+				"[ perf data convert: Skipped %" PRIu64 " samples.\n",
+				c.skipped);
+
 	fprintf(stderr,
 			"[ perf data convert: Converted and wrote %.3f MB (%" PRIu64 " samples) ]\n",
 			(ftell(c.out)) / 1024.0 / 1024.0, c.events_count);
tools/perf/util/data-convert.h +3 -0
index 1b4c5f598415..5d055e1c31c8 100644
@@ -3,11 +3,14 @@
 #define __DATA_CONVERT_H
 
 #include <stdbool.h>
+#include <linux/types.h>
 
 struct perf_data_convert_opts {
 	bool force;
 	bool all;
 	bool tod;
+	u64 range_start;
+	u64 range_end;
 };
 
 #ifdef HAVE_LIBBABELTRACE_SUPPORT
Signature · 1 line
2.47.3
maintainer
#1 lore raw

Hello,

Quoted 8 lines · Derek Foreman

This adds a feature to allow restricting the range of converted samples with start and end times specified in nanoseconds.

Use it like this:

  $ perf data convert --to-json out.json -s 12345 -e 13340

Start, end, or both may be omitted.

Looks useful, thanks!

Quoted 15 lines
---
 tools/perf/Documentation/perf-data.txt |  6 ++++++
 tools/perf/builtin-data.c              |  4 ++++
 tools/perf/util/data-convert-bt.c      | 18 ++++++++++++++++++
 tools/perf/util/data-convert-json.c    | 18 ++++++++++++++++++
 tools/perf/util/data-convert.h         |  3 +++
 5 files changed, 49 insertions(+)
tools/perf/Documentation/perf-data.txt +0 -0
index 417bf17e265c..db0de1f354a3 100644

tools/perf/Documentation/perf-data.txt OPTIONS for 'convert' · line 43

Quoted 4 lines
tools/perf/Documentation/perf-data.txt +1 -0
@@ -40,6 +40,12 @@ OPTIONS for 'convert'
 --force::
 	Don't complain, do it.
 
+-s::

Please add this as well.

--start=<TIME>::

tools/perf/Documentation/perf-data.txt OPTIONS for 'convert' · line 44

Quoted 1 line
tools/perf/Documentation/perf-data.txt +1 -0
+	Start time, in nanoseconds. Samples before this time will not be converted.

It'd be nice if you mention that users can see the timestamp from perf script or so.

tools/perf/Documentation/perf-data.txt OPTIONS for 'convert' · line 46

Quoted 2 lines
tools/perf/Documentation/perf-data.txt +2 -0
+
+-e::

--end=<TIME>::

Quoted 69 lines
tools/perf/Documentation/perf-data.txt +2 -0
+	End time, in nanoseconds. Samples after this time will not be converted.
+
 -v::
 --verbose::
         Be more verbose (show counter open errors, etc).
tools/perf/builtin-data.c +4 -0
index ce51cbf6dc97..da813c62e284 100644
@@ -33,6 +33,8 @@ const char *to_ctf;
 struct perf_data_convert_opts opts = {
 	.force = false,
 	.all = false,
+	.range_start = 0,
+	.range_end = 0,
 };
 
 const struct option data_options[] = {
@@ -45,6 +47,8 @@ const struct option data_options[] = {
 #endif
 		OPT_BOOLEAN('f', "force", &opts.force, "don't complain, do it"),
 		OPT_BOOLEAN(0, "all", &opts.all, "Convert all events"),
+		OPT_U64('s', "start", &opts.range_start, "Earliest timestamp to convert"),
+		OPT_U64('e', "end", &opts.range_end, "Latest timestamp to convert"),
 		OPT_END()
 	};
 
tools/perf/util/data-convert-bt.c +13 -0
index 3d2e437e1354..6819f11fe900 100644
@@ -91,9 +91,13 @@ struct convert {
 	struct perf_tool	tool;
 	struct ctf_writer	writer;
 
+	u64			start;
+	u64			end;
+
 	u64			events_size;
 	u64			events_count;
 	u64			non_sample_count;
+	u64			skipped;
 
 	/* Ordered events configured queue size. */
 	u64			queue_size;
@@ -811,6 +815,12 @@ static int process_sample_event(const struct perf_tool *tool,
 	if (WARN_ONCE(!priv, "Failed to setup all events.\n"))
 		return 0;
 
+	if (sample->time < c->start ||
+	    (c->end && sample->time > c->end)) {
+		++c->skipped;
+		return 0;
+	}
+
 	event_class = priv->event_class;
 
 	/* update stats */
@@ -1626,6 +1636,9 @@ int bt_convert__perf2ctf(const char *input, const char *path,
 	c.tool.namespaces      = perf_event__process_namespaces;
 	c.tool.ordering_requires_timestamps = true;
 
+	c.start = opts->range_start;
+	c.end = opts->range_end;
+
 	if (opts->all) {
 		c.tool.comm = process_comm_event;
 		c.tool.exit = process_exit_event;

tools/perf/util/data-convert-bt.c int bt_convert__perf2ctf(const char *input, const char *path, · line 1696

Quoted 7 lines
tools/perf/util/data-convert-bt.c +4 -0
@@ -1677,6 +1690,11 @@ int bt_convert__perf2ctf(const char *input, const char *path,
 		"[ perf data convert: Converted '%s' into CTF data '%s' ]\n",
 		data.path, path);
 
+	if (c.skipped)
+		fprintf(stderr,
+			"[ perf data convert: Skipped %" PRIu64 " samples",
+			c.skipped);

This is a strange style to have the literal in a different line. It'd be nice if you can fix others too - probably in a separate commit.

And this line could come after the next, but it's up to you.

Also it'd be recommended to have a pair of parentheses for multi-line bodies even it's a single statement.

Quoted 44 lines
tools/perf/util/data-convert-bt.c +1 -0
+
 	fprintf(stderr,
 		"[ perf data convert: Converted and wrote %.3f MB (%" PRIu64 " samples",
 		(double) c.events_size / 1024.0 / 1024.0,
tools/perf/util/data-convert-json.c +13 -0
index 9dc1e184cf3c..dcbba34d6b08 100644
@@ -35,7 +35,11 @@ struct convert_json {
 	struct perf_tool tool;
 	FILE *out;
 	bool first;
+	u64 start;
+	u64 end;
+
 	u64 events_count;
+	u64 skipped;
 };
 
 // Outputs a JSON-encoded string surrounded by quotes with characters escaped.
@@ -165,6 +169,12 @@ static int process_sample_event(const struct perf_tool *tool,
 		return -1;
 	}
 
+	if (sample->time < c->start ||
+	    (c->end && sample->time > c->end)) {
+		++c->skipped;
+		return 0;
+	}
+
 	++c->events_count;
 
 	if (c->first)
@@ -320,6 +330,9 @@ int bt_convert__perf2json(const char *input_name, const char *output_name,
 	struct convert_json c = {
 		.first = true,
 		.events_count = 0,
+		.start = opts->range_start,
+		.end = opts->range_end,
+		.skipped = 0,
 	};
 	struct perf_data data = {
 		.mode = PERF_DATA_MODE_READ,

tools/perf/util/data-convert-json.c int bt_convert__perf2json(const char *input_name, const char *output_name, · line 426

Quoted 7 lines
tools/perf/util/data-convert-json.c +4 -0
@@ -407,6 +420,11 @@ int bt_convert__perf2json(const char *input_name, const char *output_name,
 			"[ perf data convert: Converted '%s' into JSON data '%s' ]\n",
 			data.path, output_name);
 
+	if (c.skipped)
+		fprintf(stderr,
+				"[ perf data convert: Skipped %" PRIu64 " samples.\n",
+				c.skipped);

Ditto.

Thanks,
Namhyung

Quoted 26 lines
tools/perf/util/data-convert-json.c +1 -0
+
 	fprintf(stderr,
 			"[ perf data convert: Converted and wrote %.3f MB (%" PRIu64 " samples) ]\n",
 			(ftell(c.out)) / 1024.0 / 1024.0, c.events_count);
tools/perf/util/data-convert.h +3 -0
index 1b4c5f598415..5d055e1c31c8 100644
@@ -3,11 +3,14 @@
 #define __DATA_CONVERT_H
 
 #include <stdbool.h>
+#include <linux/types.h>
 
 struct perf_data_convert_opts {
 	bool force;
 	bool all;
 	bool tod;
+	u64 range_start;
+	u64 range_end;
 };
 
 #ifdef HAVE_LIBBABELTRACE_SUPPORT
Signature · 1 line
2.47.3
#2 lore raw
Quoted 6 lines · Namhyung Kim

Hello,

Quoted 3 lines · Derek Foreman

This adds a feature to allow restricting the range of converted samples with start and end times specified in nanoseconds.

Quoted 6 lines
Quoted 5 lines

Use it like this:

   $ perf data convert --to-json out.json -s 12345 -e 13340

Start, end, or both may be omitted.

Looks useful, thanks!

Thanks for the review!

Quoted 17 lines
Quoted 17 lines
---
  tools/perf/Documentation/perf-data.txt |  6 ++++++
  tools/perf/builtin-data.c              |  4 ++++
  tools/perf/util/data-convert-bt.c      | 18 ++++++++++++++++++
  tools/perf/util/data-convert-json.c    | 18 ++++++++++++++++++
  tools/perf/util/data-convert.h         |  3 +++
  5 files changed, 49 insertions(+)
tools/perf/Documentation/perf-data.txt +0 -0
index 417bf17e265c..db0de1f354a3 100644
@@ -40,6 +40,12 @@ OPTIONS for 'convert'
  --force::
  	Don't complain, do it.
 
Quoted 8 lines

tools/perf/Documentation/perf-data.txt OPTIONS for 'convert' · line 43

Quoted 1 line
tools/perf/Documentation/perf-data.txt +1 -0
+-s::

Please add this as well.

--start=<TIME>::

tools/perf/Documentation/perf-data.txt OPTIONS for 'convert' · line 44

Quoted 1 line
tools/perf/Documentation/perf-data.txt +1 -0
+	Start time, in nanoseconds. Samples before this time will not be converted.

It'd be nice if you mention that users can see the timestamp from perf script or so.

After looking at perf script, I realized I'd reinvented the wheel here, so in v2 I've used --time and the util functions that already exist for parsing and filtering time strings.

I mostly copy and pasted the --time help from another file. I think asciidoc has a way to templatize this with an include file (it's in at least 3 files now), but I wasn't sure if I should try to do that.

Quoted 77 lines

tools/perf/Documentation/perf-data.txt OPTIONS for 'convert' · line 46

Quoted 3 lines
tools/perf/Documentation/perf-data.txt +2 -0
+
+-e::

--end=<TIME>::

Quoted 72 lines
tools/perf/Documentation/perf-data.txt +2 -0
+	End time, in nanoseconds. Samples after this time will not be converted.
+
  -v::
  --verbose::
          Be more verbose (show counter open errors, etc).
tools/perf/builtin-data.c +4 -0
index ce51cbf6dc97..da813c62e284 100644
@@ -33,6 +33,8 @@ const char *to_ctf;
  struct perf_data_convert_opts opts = {
  	.force = false,
  	.all = false,
+	.range_start = 0,
+	.range_end = 0,
  };
 
  const struct option data_options[] = {
@@ -45,6 +47,8 @@ const struct option data_options[] = {
  #endif
  		OPT_BOOLEAN('f', "force", &opts.force, "don't complain, do it"),
  		OPT_BOOLEAN(0, "all", &opts.all, "Convert all events"),
+		OPT_U64('s', "start", &opts.range_start, "Earliest timestamp to convert"),
+		OPT_U64('e', "end", &opts.range_end, "Latest timestamp to convert"),
  		OPT_END()
  	};
 
tools/perf/util/data-convert-bt.c +13 -0
index 3d2e437e1354..6819f11fe900 100644
@@ -91,9 +91,13 @@ struct convert {
  	struct perf_tool	tool;
  	struct ctf_writer	writer;
 
+	u64			start;
+	u64			end;
+
  	u64			events_size;
  	u64			events_count;
  	u64			non_sample_count;
+	u64			skipped;
 
  	/* Ordered events configured queue size. */
  	u64			queue_size;
@@ -811,6 +815,12 @@ static int process_sample_event(const struct perf_tool *tool,
  	if (WARN_ONCE(!priv, "Failed to setup all events.\n"))
  		return 0;
 
+	if (sample->time < c->start ||
+	    (c->end && sample->time > c->end)) {
+		++c->skipped;
+		return 0;
+	}
+
  	event_class = priv->event_class;
 
  	/* update stats */
@@ -1626,6 +1636,9 @@ int bt_convert__perf2ctf(const char *input, const char *path,
  	c.tool.namespaces      = perf_event__process_namespaces;
  	c.tool.ordering_requires_timestamps = true;
 
+	c.start = opts->range_start;
+	c.end = opts->range_end;
+
  	if (opts->all) {
  		c.tool.comm = process_comm_event;
  		c.tool.exit = process_exit_event;
@@ -1677,6 +1690,11 @@ int bt_convert__perf2ctf(const char *input, const char *path,
  		"[ perf data convert: Converted '%s' into CTF data '%s' ]\n",
  		data.path, path);
 
Quoted 8 lines

tools/perf/util/data-convert-bt.c int bt_convert__perf2ctf(const char *input, const char *path, · line 1696

Quoted 4 lines
tools/perf/util/data-convert-bt.c +4 -0
+	if (c.skipped)
+		fprintf(stderr,
+			"[ perf data convert: Skipped %" PRIu64 " samples",
+			c.skipped);

This is a strange style to have the literal in a different line. It'd be nice if you can fix others too - probably in a separate commit.

And this line could come after the next, but it's up to you.

I think it's better your way, yes.

Quoted 2 lines

Also it'd be recommended to have a pair of parentheses for multi-line bodies even it's a single statement.

I've added a second patch for v2. One of the lines exceeded 100 chars, which bothered checkpatch.pl, so I left the literal on the second line for now.

Thanks,
Derek

Quoted 82 lines
Quoted 44 lines
tools/perf/util/data-convert-bt.c +1 -0
+
  	fprintf(stderr,
  		"[ perf data convert: Converted and wrote %.3f MB (%" PRIu64 " samples",
  		(double) c.events_size / 1024.0 / 1024.0,
tools/perf/util/data-convert-json.c +13 -0
index 9dc1e184cf3c..dcbba34d6b08 100644
@@ -35,7 +35,11 @@ struct convert_json {
  	struct perf_tool tool;
  	FILE *out;
  	bool first;
+	u64 start;
+	u64 end;
+
  	u64 events_count;
+	u64 skipped;
  };
 
  // Outputs a JSON-encoded string surrounded by quotes with characters escaped.
@@ -165,6 +169,12 @@ static int process_sample_event(const struct perf_tool *tool,
  		return -1;
  	}
 
+	if (sample->time < c->start ||
+	    (c->end && sample->time > c->end)) {
+		++c->skipped;
+		return 0;
+	}
+
  	++c->events_count;
 
  	if (c->first)
@@ -320,6 +330,9 @@ int bt_convert__perf2json(const char *input_name, const char *output_name,
  	struct convert_json c = {
  		.first = true,
  		.events_count = 0,
+		.start = opts->range_start,
+		.end = opts->range_end,
+		.skipped = 0,
  	};
  	struct perf_data data = {
  		.mode = PERF_DATA_MODE_READ,

tools/perf/util/data-convert-json.c int bt_convert__perf2json(const char *input_name, const char *output_name, · line 426

Quoted 7 lines
tools/perf/util/data-convert-json.c +4 -0
@@ -407,6 +420,11 @@ int bt_convert__perf2json(const char *input_name, const char *output_name,
  			"[ perf data convert: Converted '%s' into JSON data '%s' ]\n",
  			data.path, output_name);
 
+	if (c.skipped)
+		fprintf(stderr,
+				"[ perf data convert: Skipped %" PRIu64 " samples.\n",
+				c.skipped);

Ditto.

Thanks,
Namhyung

Quoted 26 lines
tools/perf/util/data-convert-json.c +1 -0
+
  	fprintf(stderr,
  			"[ perf data convert: Converted and wrote %.3f MB (%" PRIu64 " samples) ]\n",
  			(ftell(c.out)) / 1024.0 / 1024.0, c.events_count);
tools/perf/util/data-convert.h +3 -0
index 1b4c5f598415..5d055e1c31c8 100644
@@ -3,11 +3,14 @@
  #define __DATA_CONVERT_H
 
  #include <stdbool.h>
+#include <linux/types.h>
 
  struct perf_data_convert_opts {
  	bool force;
  	bool all;
  	bool tod;
+	u64 range_start;
+	u64 range_end;
  };
 
  #ifdef HAVE_LIBBABELTRACE_SUPPORT
Signature · 1 line
2.47.3