Skip to contentSkip to conversation

Introduce --task-name and --fuzzy-name options in perf sched map

PATCH Applied Madadi Vineeth Reddy 2 participants 5 messages Jul 7, 2024 – Jul 12, 2024 perf events

lore
Share & tools
lore
#0 lore raw

v6 cover Introduce --task-name and --fuzzy-name options in perf sched map

This patch-set aims to reduce the amount of output printed on the terminal when using perf sched map, allowing users to focus only on the tasks of interest.

This helps in visualizing how a benchmark or a task is spread over the available CPUs while also knowing which CPUs are idle(.) and which are not(-). This will be more useful as number of CPUs increase.

Changelog:
=========
Changes in v6:
- Utilize strlist for simplified task matching. (Namhyung Kim)
- Include relevant symbols while printing row for sched-out. (Namhyung Kim)
- Rebase against perf-tools-next commit 608c3b1e612a ("perf install:
Don't propagate subdir to Documentation submake")
- Link to v5: https://lore.kernel.org/all/[email protected]/

Changes in v5:
- Split the changes into different commits. (Namhyung Kim)
- Rebase against perf-tools-next commit c7a5592e8e4d ("perf mem: Fix a segfault with NULL event->name")
- Link to v4: https://lore.kernel.org/all/[email protected]/

Changes in v4:
- Handle possible memory allocation failures. (Chen Yu)
- Link to v3: https://lore.kernel.org/all/[email protected]/

Changes in v3:
- Print the sched-out timestamp as a row when using the --command-name option. (Namhyung Kim)
- Refactor the code.
- Rebase against perf-tools-next commit eae7044b67a6 ("perf hist: Honor symbol conf.skip_empty")
- Link to v2: https://lore.kernel.org/all/[email protected]/

Changes in v2:
- Add support for giving multiple command-names in CSV. (Namhyung Kim)
- Add fuzzy name matching option. (Chen Yu)
- Add Reviewed-and-tested-by tag from Athira Rajeev.
- Rebase against perf-tools-next commit d2307fd4f989 ("perf maps: Add/use a sorted insert for fixup overlap and insert")
- Link to v1: https://lore.kernel.org/all/[email protected]/

Madadi Vineeth Reddy (3):
perf sched map: Add task-name option to filter the output map perf sched map: Add support for multiple task names using CSV perf sched map: Add --fuzzy-name option for fuzzy matching in task
names

 tools/perf/Documentation/perf-sched.txt |  10 ++
 tools/perf/builtin-sched.c              | 175 +++++++++++++++++++-----
 2 files changed, 149 insertions(+), 36 deletions(-)
Signature · 1 line
2.43.2
#1 lore raw

PATCH v6 1/3 perf sched map: Add task-name option to filter the output map

By default, perf sched map prints sched-in events for all the tasks which may not be required all the time as it prints lot of symbols and rows to the terminal.

With --task-name option, one could specify the specific task name for which the map has to be shown. This would help in analyzing the CPU usage patterns easier for that specific task. Since multiple PID's might have the same task name, using task-name filter would be more useful for debugging.

For other tasks, instead of printing the symbol, '-' is printed and the same '.' is used to represent idle. '-' is used instead of symbol for other tasks because it helps in clear visualization of task of interest and secondly the symbol itself doesn't mean anything because the sched-in of that symbol will not be printed(first sched-in contains pid and the corresponding symbol).

When using the --task-name option, the sched-out time is represented by a '*-'. Since not all task sched-in events are printed, the sched-out time of the relevant task might be lost. This representation ensures that the sched-out time of the interested task is not overlooked.

6.10.0-rc1
==========

*A0                              131040.639793 secs A0 => migration/0:19
*.                               131040.639801 secs .  => swapper:0
 .  *B0                          131040.639830 secs B0 => migration/1:24
 .  *.                           131040.639836 secs
 .   .  *C0                      131040.640108 secs C0 => migration/2:30
 .   .  *.                       131040.640163 secs
 .   .   .  *D0                  131040.640386 secs D0 => migration/3:36
 .   .   .  *.                   131040.640395 secs

6.10.0-rc1 + patch (--task-name wdavdaemon)
=============

 .  *A0  .   .   .   .   -   .   131040.641346 secs A0 => wdavdaemon:62509
 .   A0 *B0  .   .   .   -   .   131040.641378 secs B0 => wdavdaemon:62274
 -  *-   B0  .   .   .   -   .   131040.641379 secs
*C0  .   B0  .   .   .   .   .   131040.641572 secs C0 => wdavdaemon:62283
 C0  .   B0  .  *D0  .   .   .   131040.641572 secs D0 => wdavdaemon:62277
 C0  .   B0  .   D0  .  *E0  .   131040.641578 secs E0 => wdavdaemon:62270
*-   .   B0  .   D0  .   E0  .   131040.641581 secs
 .   .   B0  .   D0  .  *-   .   131040.641583 secs
---
 tools/perf/Documentation/perf-sched.txt |   6 +
 tools/perf/builtin-sched.c              | 147 ++++++++++++++++++------
 2 files changed, 117 insertions(+), 36 deletions(-)
tools/perf/Documentation/perf-sched.txt +6 -0
index f0e5617f6652..964036c48ac4 100644
@@ -130,6 +130,12 @@ OPTIONS for 'perf sched map'
 --color-pids::
 	Highlight the given pids.
 
+--task-name <task>::
+	Map output only for the given task name. The sched-out
+	time is printed and is represented by '*-' for the given
+	task name
+	('-' indicates other tasks while '.' is idle).
+
 OPTIONS for 'perf sched timehist'
 ---------------------------------
 -k::
tools/perf/builtin-sched.c +111 -36

236 lines not on this page · raw message

Signature · 1 line
2.43.2
#2 lore raw

PATCH v6 2/3 perf sched map: Add support for multiple task names using CSV

In reply to Madadi Vineeth Reddy

To track the scheduling patterns of multiple tasks simultaneously, multiple task names can be specified using a comma separator without any whitespace.

Sample output for --task-name perf,wdavdaemon
=============

 .  *A0  .   .   .   .   -   .   131040.641346 secs A0 => wdavdaemon:62509
 .   A0 *B0  .   .   .   -   .   131040.641378 secs B0 => wdavdaemon:62274
 .  *-   B0  .   .   .   -   .   131040.641379 secs
*C0  .   B0  .   .   .   .   .   131040.641572 secs C0 => wdavdaemon:62283

...

 .  *-   .   .   .   .   .   .   131041.395649 secs
 .   .   .   .   .   .   .  *X2  131041.403969 secs X2 => perf:70211
 .   .   .   .   .   .   .  *-   131041.404006 secs
---
 tools/perf/Documentation/perf-sched.txt |  5 ++--
 tools/perf/builtin-sched.c              | 32 +++++++++++++++++++++----
 2 files changed, 30 insertions(+), 7 deletions(-)
tools/perf/Documentation/perf-sched.txt +3 -2
index 964036c48ac4..4f8216ee4a74 100644
@@ -131,9 +131,10 @@ OPTIONS for 'perf sched map'
 	Highlight the given pids.
 
 --task-name <task>::
-	Map output only for the given task name. The sched-out
+	Map output only for the given task name(s). Separate the
+	task names with a comma (without whitespace). The sched-out
 	time is printed and is represented by '*-' for the given
-	task name
+	task name(s).
 	('-' indicates other tasks while '.' is idle).
 
 OPTIONS for 'perf sched timehist'
tools/perf/builtin-sched.c +27 -5
index 3ee3f7fd78b0..7de29c2f3d23 100644
@@ -157,6 +157,7 @@ struct perf_sched_map {
 	struct perf_cpu_map	*color_cpus;
 	const char		*color_cpus_str;
 	const char		*task_name;
+	struct strlist		*task_names;
 	struct perf_cpu_map	*cpus;
 	const char		*cpus_str;
 };
@@ -1540,6 +1541,18 @@ map__findnew_thread(struct perf_sched *sched, struct machine *machine, pid_t pid
 	return thread;
 }
 
+static bool sched_match_task(const char *comm_str, struct strlist *task_names)
+{
+	struct str_node *node;
+
+	strlist__for_each_entry(node, task_names) {
+		if (strcmp(comm_str, node->s) == 0)
+			return true;
+	}
+
+	return false;
+}
+
 static void print_sched_map(struct perf_sched *sched, struct perf_cpu this_cpu, int cpus_nr,
 								const char *color, bool sched_out)
 {
@@ -1609,6 +1622,7 @@ static int map_switch_event(struct perf_sched *sched, struct evsel *evsel,
 	const char *color = PERF_COLOR_NORMAL;
 	char stimestamp[32];
 	const char *str;
+	struct strlist *task_names = sched->map.task_names;
 
 	BUG_ON(this_cpu.cpu >= MAX_CPUS || this_cpu.cpu < 0);
 
@@ -1660,7 +1674,7 @@ static int map_switch_event(struct perf_sched *sched, struct evsel *evsel,
 			 */
 			tr->shortname[0] = '.';
 			tr->shortname[1] = ' ';
-		} else if (!sched->map.task_name || !strcmp(str, sched->map.task_name)) {
+		} else if (!sched->map.task_name || sched_match_task(str, task_names)) {
 			tr->shortname[0] = sched->next_shortname1;
 			tr->shortname[1] = sched->next_shortname2;
 
@@ -1689,15 +1703,15 @@ static int map_switch_event(struct perf_sched *sched, struct evsel *evsel,
 	 * Check which of sched_in and sched_out matches the passed --task-name
 	 * arguments and call the corresponding print_sched_map.
 	 */
-	if (sched->map.task_name && strcmp(str, sched->map.task_name)) {
-		if (strcmp(thread__comm_str(sched_out), sched->map.task_name))
+	if (sched->map.task_name && !sched_match_task(str, task_names)) {
+		if (!sched_match_task(thread__comm_str(sched_out), task_names))
 			goto out;
 		else
 			goto sched_out;
 
 	} else {
 		str = thread__comm_str(sched_out);
-		if (!(sched->map.task_name && strcmp(str, sched->map.task_name)))
+		if (!(sched->map.task_name && !sched_match_task(str, task_names)))
 			proceed = 1;
 	}
 
@@ -3640,7 +3654,7 @@ int cmd_sched(int argc, const char **argv)
 	OPT_STRING(0, "cpus", &sched.map.cpus_str, "cpus",
                     "display given CPUs in map"),
 	OPT_STRING(0, "task-name", &sched.map.task_name, "task",
-		"map output only for the given task name"),
+		"map output only for the given task name(s)."),
 	OPT_PARENT(sched_options)
 	};
 	const struct option timehist_options[] = {
@@ -3739,6 +3753,14 @@ int cmd_sched(int argc, const char **argv)
 			argc = parse_options(argc, argv, map_options, map_usage, 0);
 			if (argc)
 				usage_with_options(map_usage, map_options);
+
+			if (sched.map.task_name) {
+				sched.map.task_names = strlist__new(sched.map.task_name, NULL);
+				if (sched.map.task_names == NULL) {
+					fprintf(stderr, "Failed to parse task names\n");
+					return -1;
+				}
+			}
 		}
 		sched.tp_handler = &map_ops;
 		setup_sorting(&sched, latency_options, latency_usage);
Signature · 1 line
2.43.2
#3 lore raw

PATCH v6 3/3 perf sched map: Add --fuzzy-name option for fuzzy matching in task names

In reply to Madadi Vineeth Reddy

The --fuzzy-name option can be used if fuzzy name matching is required. For example, "taskname" can be matched to any string that contains "taskname" as its substring.

Sample output for --task-name wdav --fuzzy-name
=============

 .  *A0  .   .   .   .   -   .   131040.641346 secs A0 => wdavdaemon:62509
 .   A0 *B0  .   .   .   -   .   131040.641378 secs B0 => wdavdaemon:62274
 .  *-   B0  .   .   .   -   .   131040.641379 secs
*C0  .   B0  .   .   .   .   .   131040.641572 secs C0 => wdavdaemon:62283
 C0  .   B0  .  *D0  .   .   .   131040.641572 secs D0 => wdavdaemon:62277
 C0  .   B0  .   D0  .  *E0  .   131040.641578 secs E0 => wdavdaemon:62270
*-   .   B0  .   D0  .   E0  .   131040.641581 secs
---
 tools/perf/Documentation/perf-sched.txt |  3 +++
 tools/perf/builtin-sched.c              | 20 +++++++++++++-------
 2 files changed, 16 insertions(+), 7 deletions(-)
tools/perf/Documentation/perf-sched.txt +3 -0
index 4f8216ee4a74..84d49f9241b1 100644
@@ -137,6 +137,9 @@ OPTIONS for 'perf sched map'
 	task name(s).
 	('-' indicates other tasks while '.' is idle).
 
+--fuzzy-name::
+	Given task name(s) can be partially matched (fuzzy matching).
+
 OPTIONS for 'perf sched timehist'
 ---------------------------------
 -k::
tools/perf/builtin-sched.c +13 -7
index 7de29c2f3d23..8750b5f2d49b 100644
@@ -158,6 +158,7 @@ struct perf_sched_map {
 	const char		*color_cpus_str;
 	const char		*task_name;
 	struct strlist		*task_names;
+	bool			fuzzy;
 	struct perf_cpu_map	*cpus;
 	const char		*cpus_str;
 };
@@ -1541,12 +1542,16 @@ map__findnew_thread(struct perf_sched *sched, struct machine *machine, pid_t pid
 	return thread;
 }
 
-static bool sched_match_task(const char *comm_str, struct strlist *task_names)
+static bool sched_match_task(struct perf_sched *sched, const char *comm_str)
 {
+	bool fuzzy_match = sched->map.fuzzy;
+	struct strlist *task_names = sched->map.task_names;
 	struct str_node *node;
 
 	strlist__for_each_entry(node, task_names) {
-		if (strcmp(comm_str, node->s) == 0)
+		bool match_found = fuzzy_match ? !!strstr(comm_str, node->s) :
+							!strcmp(comm_str, node->s);
+		if (match_found)
 			return true;
 	}
 
@@ -1622,7 +1627,6 @@ static int map_switch_event(struct perf_sched *sched, struct evsel *evsel,
 	const char *color = PERF_COLOR_NORMAL;
 	char stimestamp[32];
 	const char *str;
-	struct strlist *task_names = sched->map.task_names;
 
 	BUG_ON(this_cpu.cpu >= MAX_CPUS || this_cpu.cpu < 0);
 
@@ -1674,7 +1678,7 @@ static int map_switch_event(struct perf_sched *sched, struct evsel *evsel,
 			 */
 			tr->shortname[0] = '.';
 			tr->shortname[1] = ' ';
-		} else if (!sched->map.task_name || sched_match_task(str, task_names)) {
+		} else if (!sched->map.task_name || sched_match_task(sched, str)) {
 			tr->shortname[0] = sched->next_shortname1;
 			tr->shortname[1] = sched->next_shortname2;
 
@@ -1703,15 +1707,15 @@ static int map_switch_event(struct perf_sched *sched, struct evsel *evsel,
 	 * Check which of sched_in and sched_out matches the passed --task-name
 	 * arguments and call the corresponding print_sched_map.
 	 */
-	if (sched->map.task_name && !sched_match_task(str, task_names)) {
-		if (!sched_match_task(thread__comm_str(sched_out), task_names))
+	if (sched->map.task_name && !sched_match_task(sched, str)) {
+		if (!sched_match_task(sched, thread__comm_str(sched_out)))
 			goto out;
 		else
 			goto sched_out;
 
 	} else {
 		str = thread__comm_str(sched_out);
-		if (!(sched->map.task_name && !sched_match_task(str, task_names)))
+		if (!(sched->map.task_name && !sched_match_task(sched, str)))
 			proceed = 1;
 	}
 
@@ -3655,6 +3659,8 @@ int cmd_sched(int argc, const char **argv)
                     "display given CPUs in map"),
 	OPT_STRING(0, "task-name", &sched.map.task_name, "task",
 		"map output only for the given task name(s)."),
+	OPT_BOOLEAN(0, "fuzzy-name", &sched.map.fuzzy,
+		"given command name can be partially matched (fuzzy matching)"),
 	OPT_PARENT(sched_options)
 	};
 	const struct option timehist_options[] = {
Signature · 1 line
2.43.2
maintainer Applied
#4 lore raw
In reply to Madadi Vineeth Reddy
Quoted 9 lines · Madadi Vineeth Reddy

This patch-set aims to reduce the amount of output printed on the terminal when using perf sched map, allowing users to focus only on the tasks of interest.

This helps in visualizing how a benchmark or a task is spread over the available CPUs while also knowing which CPUs are idle(.) and which are not(-). This will be more useful as number of CPUs increase.

[...]

Applied to perf-tools-next, thanks!

Best regards,
Namhyung