Skip to contentSkip to conversation

perf data: Allow filtering conversion by time range

PATCH In review Derek Foreman 2 participants 3 messages Nov 28, 2025 – Jan 6 perf events

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 perf data: Allow filtering conversion by time range

This adds a feature to allow restricting the range of converted samples with a range string like perf-script and perf-report --time.

---
v2) Replace bespoke filtering with --time (util/time-utils.h)

 tools/perf/Documentation/perf-data.txt | 28 ++++++++++++++++++++++
 tools/perf/builtin-data.c              |  3 +++
 tools/perf/util/data-convert-bt.c      | 31 ++++++++++++++++++++++++
 tools/perf/util/data-convert-json.c    | 33 ++++++++++++++++++++++++++
 tools/perf/util/data-convert.h         |  1 +
 5 files changed, 96 insertions(+)
tools/perf/Documentation/perf-data.txt +28 -0
index 417bf17e265c..20f178d61ed7 100644
@@ -40,6 +40,34 @@ OPTIONS for 'convert'
 --force::
 	Don't complain, do it.
 
+--time::
+	Only convert samples within given time window: <start>,<stop>. Times
+	have the format seconds.nanoseconds. If start is not given (i.e. time
+	string is ',x.y') then analysis starts at the beginning of the file. If
+	stop time is not given (i.e. time string is 'x.y,') then analysis goes
+	to end of file. Multiple ranges can be separated by spaces, which
+	requires the argument to be quoted e.g. --time "1234.567,1234.789 1235,"
+
+	Also support time percent with multiple time ranges. Time string is
+	'a%/n,b%/m,...' or 'a%-b%,c%-%d,...'.
+
+	For example:
+	Select the second 10% time slice:
+
+	  perf data convert --to-json out.json --time 10%/2
+
+	Select from 0% to 10% time slice:
+
+	  perf data convert --to-json out.json --time 0%-10%
+
+	Select the first and second 10% time slices:
+
+	  perf data convert --to-json out.json --time 10%/1,10%/2
+
+	Select from 0% to 10% and 30% to 40% slices:
+
+	  perf data convert --to-json out.json --time 0%-10%,30%-40%
+
 -v::
 --verbose::
         Be more verbose (show counter open errors, etc).
tools/perf/builtin-data.c +3 -0
index ce51cbf6dc97..85f59886b5cf 100644
@@ -33,6 +33,7 @@ const char *to_ctf;
 struct perf_data_convert_opts opts = {
 	.force = false,
 	.all = false,
+	.time_str = NULL,
 };
 
 const struct option data_options[] = {
@@ -45,6 +46,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_STRING(0, "time", &opts.time_str, "str",
+			   "Time span of interest (start,stop)"),
 		OPT_END()
 	};
 
tools/perf/util/data-convert-bt.c +31 -0
index 3d2e437e1354..0bcbc0e309e0 100644
@@ -34,6 +34,7 @@
 #include "util.h"
 #include "clockid.h"
 #include "util/sample.h"
+#include "util/time-utils.h"
 
 #ifdef HAVE_LIBTRACEEVENT
 #include <event-parse.h>
@@ -91,9 +92,14 @@ struct convert {
 	struct perf_tool	tool;
 	struct ctf_writer	writer;
 
+	struct perf_time_interval *ptime_range;
+	int range_size;
+	int range_num;
+
 	u64			events_size;
 	u64			events_count;
 	u64			non_sample_count;
+	u64			skipped;
 
 	/* Ordered events configured queue size. */
 	u64			queue_size;
@@ -811,6 +817,11 @@ static int process_sample_event(const struct perf_tool *tool,
 	if (WARN_ONCE(!priv, "Failed to setup all events.\n"))
 		return 0;
 
+	if (perf_time__ranges_skip_sample(c->ptime_range, c->range_num, sample->time)) {
+		++c->skipped;
+		return 0;
+	}
+
 	event_class = priv->event_class;
 
 	/* update stats */
@@ -1644,6 +1655,15 @@ int bt_convert__perf2ctf(const char *input, const char *path,
 	if (IS_ERR(session))
 		return PTR_ERR(session);
 
+	if (opts->time_str) {
+		err = perf_time__parse_for_ranges(opts->time_str, session,
+						  &c.ptime_range,
+						  &c.range_size,
+						  &c.range_num);
+		if (err < 0)
+			goto free_session;
+	}
+
 	/* CTF writer */
 	if (ctf_writer__init(cw, path, session, opts->tod))
 		goto free_session;
@@ -1687,6 +1707,14 @@ int bt_convert__perf2ctf(const char *input, const char *path,
 	else
 		fprintf(stderr, ", %" PRIu64 " non-samples) ]\n", c.non_sample_count);
 
+	if (c.skipped) {
+		fprintf(stderr,	"[ perf data convert: Skipped %" PRIu64 " samples ]\n",
+			c.skipped);
+	}
+
+	if (c.ptime_range)
+		zfree(&c.ptime_range);
+
 	cleanup_events(session);
 	perf_session__delete(session);
 	ctf_writer__cleanup(cw);
@@ -1696,6 +1724,9 @@ int bt_convert__perf2ctf(const char *input, const char *path,
 free_writer:
 	ctf_writer__cleanup(cw);
 free_session:
+	if (c.ptime_range)
+		zfree(&c.ptime_range);
+
 	perf_session__delete(session);
 	pr_err("Error during conversion setup.\n");
 	return err;
tools/perf/util/data-convert-json.c +33 -0
index 9dc1e184cf3c..787039967916 100644
@@ -25,6 +25,7 @@
 #include "util/session.h"
 #include "util/symbol.h"
 #include "util/thread.h"
+#include "util/time-utils.h"
 #include "util/tool.h"
 
 #ifdef HAVE_LIBTRACEEVENT
@@ -35,7 +36,12 @@ struct convert_json {
 	struct perf_tool tool;
 	FILE *out;
 	bool first;
+	struct perf_time_interval *ptime_range;
+	int range_size;
+	int range_num;
+
 	u64 events_count;
+	u64 skipped;
 };
 
 // Outputs a JSON-encoded string surrounded by quotes with characters escaped.
@@ -165,6 +171,11 @@ static int process_sample_event(const struct perf_tool *tool,
 		return -1;
 	}
 
+	if (perf_time__ranges_skip_sample(c->ptime_range, c->range_num, sample->time)) {
+		++c->skipped;
+		return 0;
+	}
+
 	++c->events_count;
 
 	if (c->first)
@@ -320,6 +331,10 @@ int bt_convert__perf2json(const char *input_name, const char *output_name,
 	struct convert_json c = {
 		.first = true,
 		.events_count = 0,
+		.ptime_range = NULL,
+		.range_size = 0,
+		.range_num = 0,
+		.skipped = 0,
 	};
 	struct perf_data data = {
 		.mode = PERF_DATA_MODE_READ,
@@ -382,6 +397,15 @@ int bt_convert__perf2json(const char *input_name, const char *output_name,
 		goto err_session_delete;
 	}
 
+	if (opts->time_str) {
+		ret = perf_time__parse_for_ranges(opts->time_str, session,
+						  &c.ptime_range,
+						  &c.range_size,
+						  &c.range_num);
+		if (ret < 0)
+			goto err_session_delete;
+	}
+
 	// The opening brace is printed manually because it isn't delimited from a
 	// previous value (i.e. we don't want a leading newline)
 	fputc('{', c.out);
@@ -411,7 +435,16 @@ int bt_convert__perf2json(const char *input_name, const char *output_name,
 			"[ perf data convert: Converted and wrote %.3f MB (%" PRIu64 " samples) ]\n",
 			(ftell(c.out)) / 1024.0 / 1024.0, c.events_count);
 
+	if (c.skipped) {
+		fprintf(stderr,	"[ perf data convert: Skipped %" PRIu64 " samples ]\n",
+			c.skipped);
+	}
+
 	ret = 0;
+
+	if (c.ptime_range)
+		zfree(&c.ptime_range);
+
 err_session_delete:
 	perf_session__delete(session);
 err_fclose:
tools/perf/util/data-convert.h +1 -0
index 1b4c5f598415..ee651fa680a1 100644
@@ -8,6 +8,7 @@ struct perf_data_convert_opts {
 	bool force;
 	bool all;
 	bool tod;
+	const char *time_str;
 };
 
 #ifdef HAVE_LIBBABELTRACE_SUPPORT
Signature · 1 line
2.47.3
#1 lore raw

PATCH v2 2/2 perf data: Fix coding style

Adjust some oddly indented fprintf() calls.

---
 tools/perf/util/data-convert-bt.c   | 6 ++----
 tools/perf/util/data-convert-json.c | 9 ++++-----
 2 files changed, 6 insertions(+), 9 deletions(-)
tools/perf/util/data-convert-bt.c +2 -4
index 0bcbc0e309e0..a22e9049ff30 100644
@@ -1693,12 +1693,10 @@ int bt_convert__perf2ctf(const char *input, const char *path,
 	else
 		pr_err("Error during conversion.\n");
 
-	fprintf(stderr,
-		"[ perf data convert: Converted '%s' into CTF data '%s' ]\n",
+	fprintf(stderr,	"[ perf data convert: Converted '%s' into CTF data '%s' ]\n",
 		data.path, path);
 
-	fprintf(stderr,
-		"[ perf data convert: Converted and wrote %.3f MB (%" PRIu64 " samples",
+	fprintf(stderr,	"[ perf data convert: Converted and wrote %.3f MB (%" PRIu64 " samples",
 		(double) c.events_size / 1024.0 / 1024.0,
 		c.events_count);
 
tools/perf/util/data-convert-json.c +4 -5
index 787039967916..eefa3a94c813 100644
@@ -427,13 +427,12 @@ int bt_convert__perf2json(const char *input_name, const char *output_name,
 	output_json_format(c.out, false, 0, "}");
 	fputc('\n', c.out);
 
-	fprintf(stderr,
-			"[ perf data convert: Converted '%s' into JSON data '%s' ]\n",
-			data.path, output_name);
+	fprintf(stderr,	"[ perf data convert: Converted '%s' into JSON data '%s' ]\n",
+		data.path, output_name);
 
 	fprintf(stderr,
-			"[ perf data convert: Converted and wrote %.3f MB (%" PRIu64 " samples) ]\n",
-			(ftell(c.out)) / 1024.0 / 1024.0, c.events_count);
+		"[ perf data convert: Converted and wrote %.3f MB (%" PRIu64 " samples) ]\n",
+		(ftell(c.out)) / 1024.0 / 1024.0, c.events_count);
 
 	if (c.skipped) {
 		fprintf(stderr,	"[ perf data convert: Skipped %" PRIu64 " samples ]\n",
Signature · 1 line
2.47.3
#2 lore raw
In reply to Derek Foreman
Quoted 4 lines · Derek Foreman

This adds a feature to allow restricting the range of converted samples with a range string like perf-script and perf-report --time.

Thanks, tested and applied to perf-tools-next, see:

Committer testing:

Put a probe on the ICMP receive path handling broadcast packets:

  # perf probe icmp_rcv:64
  Added new event:
    probe:icmp_rcv_L64   (on icmp_rcv:64)

You can now use it in all perf tools, such as:

perf record -e probe:icmp_rcv_L64 -aR sleep 1

  # perf record -e probe:icmp_rcv_L64 ping -c 10 -b 127.255.255.255
  WARNING: pinging broadcast address
  PING 127.255.255.255 (127.255.255.255) 56(84) bytes of data.
  ^C
  --- 127.255.255.255 ping statistics ---
  10 packets transmitted, 0 received, 100% packet loss, time 9217ms

[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.034 MB perf.data (10 samples) ]

  # perf script
              ping   52785 [009]  5847.300394: probe:icmp_rcv_L64: (ffffffffaadb337e)
              ping   52785 [009]  5848.325018: probe:icmp_rcv_L64: (ffffffffaadb337e)
              ping   52785 [009]  5849.349007: probe:icmp_rcv_L64: (ffffffffaadb337e)
              ping   52785 [009]  5850.372979: probe:icmp_rcv_L64: (ffffffffaadb337e)
              ping   52785 [009]  5851.396988: probe:icmp_rcv_L64: (ffffffffaadb337e)
              ping   52785 [009]  5852.420954: probe:icmp_rcv_L64: (ffffffffaadb337e)
              ping   52785 [009]  5853.444934: probe:icmp_rcv_L64: (ffffffffaadb337e)
              ping   52785 [009]  5854.468926: probe:icmp_rcv_L64: (ffffffffaadb337e)
              ping   52785 [009]  5855.492914: probe:icmp_rcv_L64: (ffffffffaadb337e)
              ping   52785 [009]  5856.516883: probe:icmp_rcv_L64: (ffffffffaadb337e)
  # 

Now get some slices using perf script:

  # perf script --time 40%
            ping   52785 [009]  5847.300394: probe:icmp_rcv_L64: (ffffffffaadb337e)
            ping   52785 [009]  5848.325018: probe:icmp_rcv_L64: (ffffffffaadb337e)
            ping   52785 [009]  5849.349007: probe:icmp_rcv_L64: (ffffffffaadb337e)
            ping   52785 [009]  5850.372979: probe:icmp_rcv_L64: (ffffffffaadb337e)
  # perf script --time 40%-60%
            ping   52785 [009]  5851.396988: probe:icmp_rcv_L64: (ffffffffaadb337e)
            ping   52785 [009]  5852.420954: probe:icmp_rcv_L64: (ffffffffaadb337e)
  #

And finally use this new feature:

  # perf data convert --to-json out.json --time 0%-10%
  [ perf data convert: Converted 'perf.data' into JSON data 'out.json' ]
  [ perf data convert: Converted and wrote 0.001 MB (1 samples) ]
  [ perf data convert: Skipped 9 samples ]
  # cat out.json
  {
  	"linux-perf-json-version": 1,
  	"headers": {
  		"header-version": 1,
  		"captured-on": "2026-01-06T22:26:40Z",
  		"data-offset": 520,
  		"data-size": 34648,
  		"feat-offset": 35168,
  		"hostname": "number",
  		"os-release": "6.17.12-300.fc43.x86_64",
  		"arch": "x86_64",
  		"cpu-desc": "AMD Ryzen 9 9950X3D 16-Core Processor",
  		"cpuid": "AuthenticAMD,26,68,0",
  		"nrcpus-online": 32,
  		"nrcpus-avail": 32,
  		"perf-version": "6.19.rc4.gf4c270685d3d",
  		"cmdline": [
  			"/home/acme/bin/perf"
  		]
  	},
  	"samples": [
  		{
  			"timestamp": 5847300394661,
  			"pid": 52785,
  			"tid": 52785,
  			"cpu": 9,
  			"comm": "ping",
  			"callchain": [
  				{
  					"ip": "0xffffffffaadb337f",
  					"symbol": "icmp_rcv",
  					"dso": "[kernel.kallsyms]"
  				}
  			],
  			"__probe_ip": "ffffffffaadb337e"
  		}
  	]
  }
  #
Quoted 245 lines

---
v2) Replace bespoke filtering with --time (util/time-utils.h)

 tools/perf/Documentation/perf-data.txt | 28 ++++++++++++++++++++++
 tools/perf/builtin-data.c              |  3 +++
 tools/perf/util/data-convert-bt.c      | 31 ++++++++++++++++++++++++
 tools/perf/util/data-convert-json.c    | 33 ++++++++++++++++++++++++++
 tools/perf/util/data-convert.h         |  1 +
 5 files changed, 96 insertions(+)
tools/perf/Documentation/perf-data.txt +28 -0
index 417bf17e265c..20f178d61ed7 100644
@@ -40,6 +40,34 @@ OPTIONS for 'convert'
 --force::
 	Don't complain, do it.
 
+--time::
+	Only convert samples within given time window: <start>,<stop>. Times
+	have the format seconds.nanoseconds. If start is not given (i.e. time
+	string is ',x.y') then analysis starts at the beginning of the file. If
+	stop time is not given (i.e. time string is 'x.y,') then analysis goes
+	to end of file. Multiple ranges can be separated by spaces, which
+	requires the argument to be quoted e.g. --time "1234.567,1234.789 1235,"
+
+	Also support time percent with multiple time ranges. Time string is
+	'a%/n,b%/m,...' or 'a%-b%,c%-%d,...'.
+
+	For example:
+	Select the second 10% time slice:
+
+	  perf data convert --to-json out.json --time 10%/2
+
+	Select from 0% to 10% time slice:
+
+	  perf data convert --to-json out.json --time 0%-10%
+
+	Select the first and second 10% time slices:
+
+	  perf data convert --to-json out.json --time 10%/1,10%/2
+
+	Select from 0% to 10% and 30% to 40% slices:
+
+	  perf data convert --to-json out.json --time 0%-10%,30%-40%
+
 -v::
 --verbose::
         Be more verbose (show counter open errors, etc).
tools/perf/builtin-data.c +3 -0
index ce51cbf6dc97..85f59886b5cf 100644
@@ -33,6 +33,7 @@ const char *to_ctf;
 struct perf_data_convert_opts opts = {
 	.force = false,
 	.all = false,
+	.time_str = NULL,
 };
 
 const struct option data_options[] = {
@@ -45,6 +46,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_STRING(0, "time", &opts.time_str, "str",
+			   "Time span of interest (start,stop)"),
 		OPT_END()
 	};
 
tools/perf/util/data-convert-bt.c +31 -0
index 3d2e437e1354..0bcbc0e309e0 100644
@@ -34,6 +34,7 @@
 #include "util.h"
 #include "clockid.h"
 #include "util/sample.h"
+#include "util/time-utils.h"
 
 #ifdef HAVE_LIBTRACEEVENT
 #include <event-parse.h>
@@ -91,9 +92,14 @@ struct convert {
 	struct perf_tool	tool;
 	struct ctf_writer	writer;
 
+	struct perf_time_interval *ptime_range;
+	int range_size;
+	int range_num;
+
 	u64			events_size;
 	u64			events_count;
 	u64			non_sample_count;
+	u64			skipped;
 
 	/* Ordered events configured queue size. */
 	u64			queue_size;
@@ -811,6 +817,11 @@ static int process_sample_event(const struct perf_tool *tool,
 	if (WARN_ONCE(!priv, "Failed to setup all events.\n"))
 		return 0;
 
+	if (perf_time__ranges_skip_sample(c->ptime_range, c->range_num, sample->time)) {
+		++c->skipped;
+		return 0;
+	}
+
 	event_class = priv->event_class;
 
 	/* update stats */
@@ -1644,6 +1655,15 @@ int bt_convert__perf2ctf(const char *input, const char *path,
 	if (IS_ERR(session))
 		return PTR_ERR(session);
 
+	if (opts->time_str) {
+		err = perf_time__parse_for_ranges(opts->time_str, session,
+						  &c.ptime_range,
+						  &c.range_size,
+						  &c.range_num);
+		if (err < 0)
+			goto free_session;
+	}
+
 	/* CTF writer */
 	if (ctf_writer__init(cw, path, session, opts->tod))
 		goto free_session;
@@ -1687,6 +1707,14 @@ int bt_convert__perf2ctf(const char *input, const char *path,
 	else
 		fprintf(stderr, ", %" PRIu64 " non-samples) ]\n", c.non_sample_count);
 
+	if (c.skipped) {
+		fprintf(stderr,	"[ perf data convert: Skipped %" PRIu64 " samples ]\n",
+			c.skipped);
+	}
+
+	if (c.ptime_range)
+		zfree(&c.ptime_range);
+
 	cleanup_events(session);
 	perf_session__delete(session);
 	ctf_writer__cleanup(cw);
@@ -1696,6 +1724,9 @@ int bt_convert__perf2ctf(const char *input, const char *path,
 free_writer:
 	ctf_writer__cleanup(cw);
 free_session:
+	if (c.ptime_range)
+		zfree(&c.ptime_range);
+
 	perf_session__delete(session);
 	pr_err("Error during conversion setup.\n");
 	return err;
tools/perf/util/data-convert-json.c +33 -0
index 9dc1e184cf3c..787039967916 100644
@@ -25,6 +25,7 @@
 #include "util/session.h"
 #include "util/symbol.h"
 #include "util/thread.h"
+#include "util/time-utils.h"
 #include "util/tool.h"
 
 #ifdef HAVE_LIBTRACEEVENT
@@ -35,7 +36,12 @@ struct convert_json {
 	struct perf_tool tool;
 	FILE *out;
 	bool first;
+	struct perf_time_interval *ptime_range;
+	int range_size;
+	int range_num;
+
 	u64 events_count;
+	u64 skipped;
 };
 
 // Outputs a JSON-encoded string surrounded by quotes with characters escaped.
@@ -165,6 +171,11 @@ static int process_sample_event(const struct perf_tool *tool,
 		return -1;
 	}
 
+	if (perf_time__ranges_skip_sample(c->ptime_range, c->range_num, sample->time)) {
+		++c->skipped;
+		return 0;
+	}
+
 	++c->events_count;
 
 	if (c->first)
@@ -320,6 +331,10 @@ int bt_convert__perf2json(const char *input_name, const char *output_name,
 	struct convert_json c = {
 		.first = true,
 		.events_count = 0,
+		.ptime_range = NULL,
+		.range_size = 0,
+		.range_num = 0,
+		.skipped = 0,
 	};
 	struct perf_data data = {
 		.mode = PERF_DATA_MODE_READ,
@@ -382,6 +397,15 @@ int bt_convert__perf2json(const char *input_name, const char *output_name,
 		goto err_session_delete;
 	}
 
+	if (opts->time_str) {
+		ret = perf_time__parse_for_ranges(opts->time_str, session,
+						  &c.ptime_range,
+						  &c.range_size,
+						  &c.range_num);
+		if (ret < 0)
+			goto err_session_delete;
+	}
+
 	// The opening brace is printed manually because it isn't delimited from a
 	// previous value (i.e. we don't want a leading newline)
 	fputc('{', c.out);
@@ -411,7 +435,16 @@ int bt_convert__perf2json(const char *input_name, const char *output_name,
 			"[ perf data convert: Converted and wrote %.3f MB (%" PRIu64 " samples) ]\n",
 			(ftell(c.out)) / 1024.0 / 1024.0, c.events_count);
 
+	if (c.skipped) {
+		fprintf(stderr,	"[ perf data convert: Skipped %" PRIu64 " samples ]\n",
+			c.skipped);
+	}
+
 	ret = 0;
+
+	if (c.ptime_range)
+		zfree(&c.ptime_range);
+
 err_session_delete:
 	perf_session__delete(session);
 err_fclose:
tools/perf/util/data-convert.h +1 -0
index 1b4c5f598415..ee651fa680a1 100644
@@ -8,6 +8,7 @@ struct perf_data_convert_opts {
 	bool force;
 	bool all;
 	bool tod;
+	const char *time_str;
 };
 
 #ifdef HAVE_LIBBABELTRACE_SUPPORT
Signature · 1 line
2.47.3