Skip to contentSkip to conversation

perf trace: Add --syscall-sample option

PATCH In review Namhyung Kim 2 participants 2 messages Dec 19, 2024 – Dec 23, 2024

Subsystems (2)
lore
Share & tools
lore

Namhyung Kim

author maintainer
#0 lore raw

PATCH v2 1/1 perf trace: Add --syscall-sample option

This option is to implement the event sampling for system calls in BPF. When it's used, it picks up a syscall in the given sampling period and discard others. The period is in msec as it shows the time in msec.

  # perf trace -C 0 --syscall-sample 100 sleep 1
           ? (         ): fleetspeakd/1828559  ... [continued]: futex())                                            = -1 ETIMEDOUT (Connection timed out)
       0.050 (100.247 ms): gnome-shell/572531 recvmsg(fd: 10<socket:[3355761]>, msg: 0x7ffef8b39d20)                = 40
     100.357 (100.149 ms): pipewire-pulse/572245 read(fd: 5<anon_inode:[eventfd]>, buf: 0x7ffc0b9dc8f0, count: 8)      = 8
     200.553 (100.268 ms): NetworkManager/3424 epoll_wait(epfd: 19<anon_inode:[eventpoll]>, events: 0x5607b85bb880, maxevents: 6) = 0
     300.876 (         ): mon/4932 poll(ufds: 0x7fa392784df0, nfds: 1, timeout_msecs: 100)            ...
     400.901 ( 0.025 ms): TaskCon~ller #/620145 futex(uaddr: 0x7f3fc596fa00, op: WAKE|PRIVATE_FLAG, val: 1)           = 0
     300.876 (100.123 ms): mon/4932  ... [continued]: poll())                                             = 0 (Timeout)
     500.901 ( 0.012 ms): evdefer/2/2335122 futex(uaddr: 0x5640baac5198, op: WAKE|PRIVATE_FLAG, val: 1)           = 0
     602.701 ( 0.017 ms): Compositor/1992200 futex(uaddr: 0x7f1a51dfdd40, op: WAKE|PRIVATE_FLAG, val: 1)           = 0
     705.589 ( 0.017 ms): JS Watchdog/947933 futex(uaddr: 0x7f4cac1d4240, op: WAKE|PRIVATE_FLAG, val: 1)           = 0
     812.667 ( 0.027 ms): fix/1985151 futex(uaddr: 0xc0008f7148, op: WAKE|PRIVATE_FLAG, val: 1)             = 1
     912.807 ( 0.017 ms): Xorg/572315 setitimer(value: 0x7ffc375d6ba0)                                      = 0

The timestamp is kept in a per-cpu array and the allowed task is saved in a BPF hash map. For non-BPF use cases, it won't work so an error message would be displayed.

  # perf trace --syscall-sample 100 sleep 1
  ERROR: --syscall-sample works only for BPF

---
v2 changes)
* rename to --syscall-sample and update the description
* print error when BPF is not available (Arnaldo)
* rename to sample_period_ns (Ian)

 tools/perf/Documentation/perf-trace.txt       |  6 ++
 tools/perf/builtin-trace.c                    | 11 +++
 .../bpf_skel/augmented_raw_syscalls.bpf.c     | 67 ++++++++++++++++++-
 3 files changed, 82 insertions(+), 2 deletions(-)
tools/perf/Documentation/perf-trace.txt +6 -0
index 6e0cc50bbc13fc7f..e8a38ecc5eddab1c 100644
@@ -241,6 +241,12 @@ the thread executes on the designated CPUs. Default is to monitor all CPUs.
 	printing using the existing 'perf trace' syscall arg beautifiers to map integer
 	arguments to strings (pid to comm, syscall id to syscall name, etc).
 
+--syscall-sample=<period>::
+	Enable sampling of system calls with a given period in msec.
+	The sampling frequency would be 1 / period, in other words,
+	it will trace a system call only after the given period of
+	time is passed.  The sampling period is tracked per CPU.
+
 
 PAGEFAULTS
 ----------
tools/perf/builtin-trace.c +11 -0
index 6a1a128fe645014d..e70e634fbfaf33f5 100644
@@ -185,6 +185,7 @@ struct trace {
 	} stats;
 	unsigned int		max_stack;
 	unsigned int		min_stack;
+	unsigned long		sample_period_ms;
 	int			raw_augmented_syscalls_args_size;
 	bool			raw_augmented_syscalls;
 	bool			fd_path_disabled;
@@ -5186,6 +5187,7 @@ int cmd_trace(int argc, const char **argv)
 		     "start"),
 	OPT_BOOLEAN(0, "force-btf", &trace.force_btf, "Prefer btf_dump general pretty printer"
 		       "to customized ones"),
+	OPT_ULONG(0, "syscall-sample", &trace.sample_period_ms, "syscall sampling period in ms"),
 	OPTS_EVSWITCH(&trace.evswitch),
 	OPT_END()
 	};
@@ -5293,6 +5295,9 @@ int cmd_trace(int argc, const char **argv)
 				bpf_program__set_autoattach(prog, /*autoattach=*/false);
 		}
 
+		if (trace.sample_period_ms)
+			trace.skel->rodata->sample_period_ns = trace.sample_period_ms * NSEC_PER_MSEC;
+
 		err = augmented_raw_syscalls_bpf__load(trace.skel);
 
 		if (err < 0) {
@@ -5313,6 +5318,12 @@ int cmd_trace(int argc, const char **argv)
 	trace.syscalls.events.bpf_output = evlist__last(trace.evlist);
 	assert(evsel__name_is(trace.syscalls.events.bpf_output, "__augmented_syscalls__"));
 skip_augmentation:
+#else
+	if (trace.sample_period_ms) {
+		pr_err("ERROR: --syscall-sample works only for BPF\n");
+		err = -EINVAL;
+		goto out;
+	}
 #endif
 	err = -1;
 
tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c +65 -2

100 lines not on this page · raw message

Signature · 1 line
2.47.1.613.gc27f4b7a9f-goog
Acked-by on v2 1/1
#1 lore raw

Hello Namhyung,

Quoted 26 lines · Namhyung Kim

This option is to implement the event sampling for system calls in BPF. When it's used, it picks up a syscall in the given sampling period and discard others. The period is in msec as it shows the time in msec.

  # perf trace -C 0 --syscall-sample 100 sleep 1
           ? (         ): fleetspeakd/1828559  ... [continued]: futex())                                            = -1 ETIMEDOUT (Connection timed out)
       0.050 (100.247 ms): gnome-shell/572531 recvmsg(fd: 10<socket:[3355761]>, msg: 0x7ffef8b39d20)                = 40
     100.357 (100.149 ms): pipewire-pulse/572245 read(fd: 5<anon_inode:[eventfd]>, buf: 0x7ffc0b9dc8f0, count: 8)      = 8
     200.553 (100.268 ms): NetworkManager/3424 epoll_wait(epfd: 19<anon_inode:[eventpoll]>, events: 0x5607b85bb880, maxevents: 6) = 0
     300.876 (         ): mon/4932 poll(ufds: 0x7fa392784df0, nfds: 1, timeout_msecs: 100)            ...
     400.901 ( 0.025 ms): TaskCon~ller #/620145 futex(uaddr: 0x7f3fc596fa00, op: WAKE|PRIVATE_FLAG, val: 1)           = 0
     300.876 (100.123 ms): mon/4932  ... [continued]: poll())                                             = 0 (Timeout)
     500.901 ( 0.012 ms): evdefer/2/2335122 futex(uaddr: 0x5640baac5198, op: WAKE|PRIVATE_FLAG, val: 1)           = 0
     602.701 ( 0.017 ms): Compositor/1992200 futex(uaddr: 0x7f1a51dfdd40, op: WAKE|PRIVATE_FLAG, val: 1)           = 0
     705.589 ( 0.017 ms): JS Watchdog/947933 futex(uaddr: 0x7f4cac1d4240, op: WAKE|PRIVATE_FLAG, val: 1)           = 0
     812.667 ( 0.027 ms): fix/1985151 futex(uaddr: 0xc0008f7148, op: WAKE|PRIVATE_FLAG, val: 1)             = 1
     912.807 ( 0.017 ms): Xorg/572315 setitimer(value: 0x7ffc375d6ba0)                                      = 0

The timestamp is kept in a per-cpu array and the allowed task is saved in a BPF hash map. For non-BPF use cases, it won't work so an error message would be displayed.

  # perf trace --syscall-sample 100 sleep 1
  ERROR: --syscall-sample works only for BPF
Quoted 6 lines

---
v2 changes)
* rename to --syscall-sample and update the description
* print error when BPF is not available (Arnaldo)
* rename to sample_period_ns (Ian)

Thanks,
Howard