Skip to contentSkip to conversation

perf evlist: Warn when 'sleep' workload is used without system-wide (-a) option

PATCH Superseded Ian Rogers 3 participants 6 messages Aug 6 – Aug 18 perf events

lore
Share & tools
lore

Ian Rogers

author reviewer
#0 lore raw

PATCH 1/1 perf evlist: Warn when 'sleep' workload is used without system-wide (-a) option

A common mistake when trying to record system-wide profiles for a given duration is running commands like 'perf record sleep 1' or 'perf stat sleep 1' without passing '-a' / '--all-cpus'. When '-a' is omitted, perf defaults to per-process monitoring of the sleep process itself, which does not collect system-wide activity and records very few events.

Add a warning in evlist__prepare_workload() when the workload executable is 'sleep' and system-wide mode is not enabled.

---
 tools/perf/util/evlist.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
tools/perf/util/evlist.c +14 -0
index eb7c0d7be064..d53f594ace86 100644
@@ -1562,6 +1562,20 @@ int evlist__prepare_workload(struct evlist *evlist, struct target *target, const
 	int child_ready_pipe[2], go_pipe[2];
 	char bf;
 
+	if (argv && argv[0] && target && !target->system_wide) {
+		const char *cmd = strrchr(argv[0], '/');
+
+		if (cmd)
+			cmd++;
+		else
+			cmd = argv[0];
+
+		if (!strcmp(cmd, "sleep")) {
+			ui__warning("workload '%s' specified without the system-wide (-a) option\n",
+				    cmd);
+		}
+	}
+
 	evlist__set_workload_cork_fd(evlist, -1);
 
 	if (pipe(child_ready_pipe) < 0) {
Signature · 1 line
2.55.0.629.g250fe7f194-goog
#1 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] The warning for using the 'sleep' workload incorrectly triggers for valid targeted profiling…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] The warning for using the 'sleep' workload incorrectly triggers for valid targeted profiling commands.
- [Low] Missing explicit POSIX <string.h> header inclusion for libc string functions.
--

--- Patch [1]: [PATCH v1] perf evlist: Warn when 'sleep' workload is used without system-wide (-a) option --- commit 2e20acd0f78b6776954ca8e2c6f58698c556aa50
Author: Ian Rogers <[email protected]>

perf evlist: Warn when 'sleep' workload is used without system-wide (-a) option

This commit adds a warning to evlist__prepare_workload() when the workload executable is 'sleep' and system-wide mode is not enabled. This is intended to catch cases where users forget the '-a' flag and unintentionally only profile the sleep process itself.

tools/perf/util/evlist.c int evlist__prepare_workload(struct evlist *evlist, struct target *target, const · line 1565

Quoted 9 lines
tools/perf/util/evlist.c +1 -0
index eb7c0d7be064e..d53f594ace86a 100644
@@ -1562,6 +1562,20 @@ int evlist__prepare_workload(struct evlist *evlist, struct target *target, const
 	int child_ready_pipe[2], go_pipe[2];
 	char bf;
 
+	if (argv && argv[0] && target && !target->system_wide) {

[Severity: Medium]
Does this warning incorrectly trigger when running a targeted profile?

If a user specifies a target (like -p <pid> or -C 0) and uses sleep as the duration, target->system_wide is false. However, in these targeted modes, perf correctly monitors the specified target rather than the sleep process, making the warning spurious.

Should this check instead verify if perf has fallen back to monitoring the workload itself (for instance, by checking target__none(target))?

tools/perf/util/evlist.c int evlist__prepare_workload(struct evlist *evlist, struct target *target, const · line 1573

Quoted 8 lines
tools/perf/util/evlist.c +8 -0
+		const char *cmd = strrchr(argv[0], '/');
+
+		if (cmd)
+			cmd++;
+		else
+			cmd = argv[0];
+
+		if (!strcmp(cmd, "sleep")) {

[Severity: Low]
This isn't a bug, but should <string.h> be explicitly included in this file to guarantee musl libc compatibility?

The perf subsystem guidelines require direct, explicit inclusion of POSIX headers for libc functions like strcmp() and strrchr() to prevent musl compilation failures. Relying on implicit inclusions can cause builds to break in environments where headers strictly separate declarations.

Quoted 5 lines
tools/perf/util/evlist.c +5 -0
+			ui__warning("workload '%s' specified without the system-wide (-a) option\n",
+				    cmd);
+		}
+	}
+

[ ... ]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author reviewer
#2 lore raw

PATCH v2 1/1 perf evlist: Warn when 'sleep' workload is used without system-wide (-a) option

In reply to Ian Rogers

A common mistake when trying to record system-wide profiles for a given duration is running commands like 'perf record sleep 1' or 'perf stat sleep 1' without passing '-a' / '--all-cpus'. When '-a' is omitted, perf defaults to per-process monitoring of the sleep process itself, which does not collect system-wide activity and records very few events.

Add a warning in evlist__prepare_workload() when the workload executable is 'sleep' and system-wide mode is not enabled.

Assisted-by Antigravity:gemini-3.6-flash
---
 tools/perf/util/evlist.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
tools/perf/util/evlist.c +15 -0
index eb7c0d7be064..b3dd097fbb2c 100644
@@ -11,6 +11,7 @@
 #include <inttypes.h>
 #include <signal.h>
 #include <stdlib.h>
+#include <string.h>
 
 #include <fcntl.h>
 #include <linux/bitops.h>
@@ -1562,6 +1563,20 @@ int evlist__prepare_workload(struct evlist *evlist, struct target *target, const
 	int child_ready_pipe[2], go_pipe[2];
 	char bf;
 
+	if (argv && argv[0] && target && target__none(target)) {
+		const char *cmd = strrchr(argv[0], '/');
+
+		if (cmd)
+			cmd++;
+		else
+			cmd = argv[0];
+
+		if (!strcmp(cmd, "sleep")) {
+			ui__warning("workload '%s' specified without the system-wide (-a) option\n",
+				    cmd);
+		}
+	}
+
 	evlist__set_workload_cork_fd(evlist, -1);
 
 	if (pipe(child_ready_pipe) < 0) {
Signature · 1 line
2.55.0.629.g250fe7f194-goog
author reviewer
#3 lore raw
Quoted 7 lines · Ian Rogers

A common mistake when trying to record system-wide profiles for a given duration is running commands like 'perf record sleep 1' or 'perf stat sleep 1' without passing '-a' / '--all-cpus'. When '-a' is omitted, perf defaults to per-process monitoring of the sleep process itself, which does not collect system-wide activity and records very few events.

Quoted 5 lines

Add a warning in evlist__prepare_workload() when the workload executable is 'sleep' and system-wide mode is not enabled.

Assisted-by Antigravity:gemini-3.6-flash

Ping.

Thanks,
Ian

Quoted 40 lines
---
 tools/perf/util/evlist.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
tools/perf/util/evlist.c +15 -1
index eb7c0d7be064..b3dd097fbb2c 100644
@@ -11,6 +11,7 @@
 #include <inttypes.h>
 #include <signal.h>
 #include <stdlib.h>
+#include <string.h>
 
 #include <fcntl.h>
 #include <linux/bitops.h>
@@ -1562,6 +1563,20 @@ int evlist__prepare_workload(struct evlist *evlist, struct target *target, const
        int child_ready_pipe[2], go_pipe[2];
        char bf;
 
+       if (argv && argv[0] && target && target__none(target)) {
+               const char *cmd = strrchr(argv[0], '/');
+
+               if (cmd)
+                       cmd++;
+               else
+                       cmd = argv[0];
+
+               if (!strcmp(cmd, "sleep")) {
+                       ui__warning("workload '%s' specified without the system-wide (-a) option\n",
+                                   cmd);
+               }
+       }
+
        evlist__set_workload_cork_fd(evlist, -1);
 
        if (pipe(child_ready_pipe) < 0) {
--

2.55.0.629.g250fe7f194-goog

maintainer
#4 lore raw
In reply to Ian Rogers
Quoted 28 lines · Ian Rogers

A common mistake when trying to record system-wide profiles for a given duration is running commands like 'perf record sleep 1' or 'perf stat sleep 1' without passing '-a' / '--all-cpus'. When '-a' is omitted, perf defaults to per-process monitoring of the sleep process itself, which does not collect system-wide activity and records very few events.

Add a warning in evlist__prepare_workload() when the workload executable is 'sleep' and system-wide mode is not enabled.

Assisted-by Antigravity:gemini-3.6-flash
---
 tools/perf/util/evlist.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
tools/perf/util/evlist.c +1 -0
index eb7c0d7be064..b3dd097fbb2c 100644
@@ -11,6 +11,7 @@
 #include <inttypes.h>
 #include <signal.h>
 #include <stdlib.h>
+#include <string.h>
 
 #include <fcntl.h>
 #include <linux/bitops.h>

tools/perf/util/evlist.c int evlist__prepare_workload(struct evlist *evlist, struct target *target, const · line 1566

Quoted 4 lines
tools/perf/util/evlist.c +1 -0
@@ -1562,6 +1563,20 @@ int evlist__prepare_workload(struct evlist *evlist, struct target *target, const
 	int child_ready_pipe[2], go_pipe[2];
 	char bf;
 
+	if (argv && argv[0] && target && target__none(target)) {

As I read the code, argv and target should be provided. So the condition simply can be target__none(). I'll make the change.

tools/perf/util/evlist.c int evlist__prepare_workload(struct evlist *evlist, struct target *target, const · line 1576

Quoted 10 lines
tools/perf/util/evlist.c +10 -0
+		const char *cmd = strrchr(argv[0], '/');
+
+		if (cmd)
+			cmd++;
+		else
+			cmd = argv[0];
+
+		if (!strcmp(cmd, "sleep")) {
+			ui__warning("workload '%s' specified without the system-wide (-a) option\n",
+				    cmd);

Now it's not just the system-wide option, but I guess it's most likey what users want. I hope this would improve UX of the tool a bit. :)

Thanks,
Namhyung

Quoted 9 lines
tools/perf/util/evlist.c +3 -0
+		}
+	}
+
 	evlist__set_workload_cork_fd(evlist, -1);
 
 	if (pipe(child_ready_pipe) < 0) {
Signature · 1 line
2.55.0.629.g250fe7f194-goog
maintainer Applied
#5 lore raw
In reply to Ian Rogers
Quoted 10 lines · Ian Rogers

A common mistake when trying to record system-wide profiles for a given duration is running commands like 'perf record sleep 1' or 'perf stat sleep 1' without passing '-a' / '--all-cpus'. When '-a' is omitted, perf defaults to per-process monitoring of the sleep process itself, which does not collect system-wide activity and records very few events.

Add a warning in evlist__prepare_workload() when the workload executable is 'sleep' and system-wide mode is not enabled.

[...]

Applied to perf-tools-next, thanks!

Best regards,
Namhyung