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

Madadi Vineeth Reddy posted 3 patches 1 year, 5 months ago
There is a newer version of this series
[PATCH v5 3/3] perf sched map: Add --fuzzy-name option for fuzzy matching in task names
Posted by Madadi Vineeth Reddy 1 year, 5 months ago
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
 -  *-   -   -   -   -   -   -   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
*-   -   -   -   -   -   -   -   131040.641581 secs

Suggested-by: Chen Yu <yu.c.chen@intel.com>
Reviewed-and-tested-by: Athira Rajeev <atrajeev@linux.vnet.ibm.com>
Signed-off-by: Madadi Vineeth Reddy <vineethr@linux.ibm.com>
---
 tools/perf/Documentation/perf-sched.txt |  3 +++
 tools/perf/builtin-sched.c              | 22 +++++++++++++++-------
 2 files changed, 18 insertions(+), 7 deletions(-)

diff --git a/tools/perf/Documentation/perf-sched.txt b/tools/perf/Documentation/perf-sched.txt
index 3095e280eb92..938b41a3befa 100644
--- a/tools/perf/Documentation/perf-sched.txt
+++ b/tools/perf/Documentation/perf-sched.txt
@@ -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::
diff --git a/tools/perf/builtin-sched.c b/tools/perf/builtin-sched.c
index ecb43deb9d74..c801c38e8b9a 100644
--- a/tools/perf/builtin-sched.c
+++ b/tools/perf/builtin-sched.c
@@ -157,6 +157,7 @@ struct perf_sched_map {
 	struct perf_cpu_map	*color_cpus;
 	const char		*color_cpus_str;
 	const char		*task_name;
+	bool			fuzzy;
 	struct perf_cpu_map	*cpus;
 	const char		*cpus_str;
 };
@@ -1599,12 +1600,16 @@ static struct CommandList *parse_commands(const char *commands)
 	return cmd_list;
 }
 
-static bool sched_match_task(const char *comm_str, struct CommandList *cmd_list)
+static bool sched_match_task(const char *comm_str, struct CommandList *cmd_list, bool fuzzy_match)
 {
 	bool match_found = false;
 
-	for (int i = 0; i < cmd_list->command_count && !match_found; i++)
-		match_found = !strcmp(comm_str, cmd_list->command_list[i]);
+	for (int i = 0; i < cmd_list->command_count && !match_found; i++) {
+		if (fuzzy_match)
+			match_found = !!strstr(comm_str, cmd_list->command_list[i]);
+		else
+			match_found = !strcmp(comm_str, cmd_list->command_list[i]);
+	}
 
 	return match_found;
 }
@@ -1727,7 +1732,8 @@ 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, cmd_list)) {
+		} else if (!sched->map.task_name || sched_match_task(str, cmd_list,
+								sched->map.fuzzy)) {
 			tr->shortname[0] = sched->next_shortname1;
 			tr->shortname[1] = sched->next_shortname2;
 
@@ -1756,15 +1762,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, cmd_list)) {
-		if (!sched_match_task(thread__comm_str(sched_out), cmd_list))
+	if (sched->map.task_name && !sched_match_task(str, cmd_list, sched->map.fuzzy)) {
+		if (!sched_match_task(thread__comm_str(sched_out), cmd_list, sched->map.fuzzy))
 			goto out;
 		else
 			goto sched_out;
 
 	} else {
 		str = thread__comm_str(sched_out);
-		if (!(sched->map.task_name && !sched_match_task(str, cmd_list)))
+		if (!(sched->map.task_name && !sched_match_task(str, cmd_list, sched->map.fuzzy)))
 			proceed = 1;
 	}
 
@@ -3707,6 +3713,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[] = {
-- 
2.43.2
Re: [PATCH v5 3/3] perf sched map: Add --fuzzy-name option for fuzzy matching in task names
Posted by Namhyung Kim 1 year, 5 months ago
On Wed, Jun 26, 2024 at 02:45:50PM +0530, Madadi Vineeth Reddy wrote:
> 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
>  -  *-   -   -   -   -   -   -   131040.641379 secs

   -  *-   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
> *-   -   -   -   -   -   -   -   131040.641581 secs

  *-   -   B0  -   D0  -   E0  -   131040.641581 secs

Thanks,
Namhyung

> 
> Suggested-by: Chen Yu <yu.c.chen@intel.com>
> Reviewed-and-tested-by: Athira Rajeev <atrajeev@linux.vnet.ibm.com>
> Signed-off-by: Madadi Vineeth Reddy <vineethr@linux.ibm.com>
> ---
>  tools/perf/Documentation/perf-sched.txt |  3 +++
>  tools/perf/builtin-sched.c              | 22 +++++++++++++++-------
>  2 files changed, 18 insertions(+), 7 deletions(-)
> 
> diff --git a/tools/perf/Documentation/perf-sched.txt b/tools/perf/Documentation/perf-sched.txt
> index 3095e280eb92..938b41a3befa 100644
> --- a/tools/perf/Documentation/perf-sched.txt
> +++ b/tools/perf/Documentation/perf-sched.txt
> @@ -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::
> diff --git a/tools/perf/builtin-sched.c b/tools/perf/builtin-sched.c
> index ecb43deb9d74..c801c38e8b9a 100644
> --- a/tools/perf/builtin-sched.c
> +++ b/tools/perf/builtin-sched.c
> @@ -157,6 +157,7 @@ struct perf_sched_map {
>  	struct perf_cpu_map	*color_cpus;
>  	const char		*color_cpus_str;
>  	const char		*task_name;
> +	bool			fuzzy;
>  	struct perf_cpu_map	*cpus;
>  	const char		*cpus_str;
>  };
> @@ -1599,12 +1600,16 @@ static struct CommandList *parse_commands(const char *commands)
>  	return cmd_list;
>  }
>  
> -static bool sched_match_task(const char *comm_str, struct CommandList *cmd_list)
> +static bool sched_match_task(const char *comm_str, struct CommandList *cmd_list, bool fuzzy_match)
>  {
>  	bool match_found = false;
>  
> -	for (int i = 0; i < cmd_list->command_count && !match_found; i++)
> -		match_found = !strcmp(comm_str, cmd_list->command_list[i]);
> +	for (int i = 0; i < cmd_list->command_count && !match_found; i++) {
> +		if (fuzzy_match)
> +			match_found = !!strstr(comm_str, cmd_list->command_list[i]);
> +		else
> +			match_found = !strcmp(comm_str, cmd_list->command_list[i]);
> +	}
>  
>  	return match_found;
>  }
> @@ -1727,7 +1732,8 @@ 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, cmd_list)) {
> +		} else if (!sched->map.task_name || sched_match_task(str, cmd_list,
> +								sched->map.fuzzy)) {
>  			tr->shortname[0] = sched->next_shortname1;
>  			tr->shortname[1] = sched->next_shortname2;
>  
> @@ -1756,15 +1762,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, cmd_list)) {
> -		if (!sched_match_task(thread__comm_str(sched_out), cmd_list))
> +	if (sched->map.task_name && !sched_match_task(str, cmd_list, sched->map.fuzzy)) {
> +		if (!sched_match_task(thread__comm_str(sched_out), cmd_list, sched->map.fuzzy))
>  			goto out;
>  		else
>  			goto sched_out;
>  
>  	} else {
>  		str = thread__comm_str(sched_out);
> -		if (!(sched->map.task_name && !sched_match_task(str, cmd_list)))
> +		if (!(sched->map.task_name && !sched_match_task(str, cmd_list, sched->map.fuzzy)))
>  			proceed = 1;
>  	}
>  
> @@ -3707,6 +3713,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[] = {
> -- 
> 2.43.2
>
Re: [PATCH v5 3/3] perf sched map: Add --fuzzy-name option for fuzzy matching in task names
Posted by Madadi Vineeth Reddy 1 year, 5 months ago
Hi Namhyung,

On 28/06/24 05:09, Namhyung Kim wrote:
> On Wed, Jun 26, 2024 at 02:45:50PM +0530, Madadi Vineeth Reddy wrote:
>> 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
>>  -  *-   -   -   -   -   -   -   131040.641379 secs
> 
>    -  *-   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
>> *-   -   -   -   -   -   -   -   131040.641581 secs
> 
>   *-   -   B0  -   D0  -   E0  -   131040.641581 secs
> 

Understood. Thank you for the review.

Thanks and Regards
Madadi Vineeth Reddy

> Thanks,
> Namhyung
> 
>>
>> Suggested-by: Chen Yu <yu.c.chen@intel.com>
>> Reviewed-and-tested-by: Athira Rajeev <atrajeev@linux.vnet.ibm.com>
>> Signed-off-by: Madadi Vineeth Reddy <vineethr@linux.ibm.com>
>> ---
>>  tools/perf/Documentation/perf-sched.txt |  3 +++
>>  tools/perf/builtin-sched.c              | 22 +++++++++++++++-------
>>  2 files changed, 18 insertions(+), 7 deletions(-)
>>
>> diff --git a/tools/perf/Documentation/perf-sched.txt b/tools/perf/Documentation/perf-sched.txt
>> index 3095e280eb92..938b41a3befa 100644
>> --- a/tools/perf/Documentation/perf-sched.txt
>> +++ b/tools/perf/Documentation/perf-sched.txt
>> @@ -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::
>> diff --git a/tools/perf/builtin-sched.c b/tools/perf/builtin-sched.c
>> index ecb43deb9d74..c801c38e8b9a 100644
>> --- a/tools/perf/builtin-sched.c
>> +++ b/tools/perf/builtin-sched.c
>> @@ -157,6 +157,7 @@ struct perf_sched_map {
>>  	struct perf_cpu_map	*color_cpus;
>>  	const char		*color_cpus_str;
>>  	const char		*task_name;
>> +	bool			fuzzy;
>>  	struct perf_cpu_map	*cpus;
>>  	const char		*cpus_str;
>>  };
>> @@ -1599,12 +1600,16 @@ static struct CommandList *parse_commands(const char *commands)
>>  	return cmd_list;
>>  }
>>  
>> -static bool sched_match_task(const char *comm_str, struct CommandList *cmd_list)
>> +static bool sched_match_task(const char *comm_str, struct CommandList *cmd_list, bool fuzzy_match)
>>  {
>>  	bool match_found = false;
>>  
>> -	for (int i = 0; i < cmd_list->command_count && !match_found; i++)
>> -		match_found = !strcmp(comm_str, cmd_list->command_list[i]);
>> +	for (int i = 0; i < cmd_list->command_count && !match_found; i++) {
>> +		if (fuzzy_match)
>> +			match_found = !!strstr(comm_str, cmd_list->command_list[i]);
>> +		else
>> +			match_found = !strcmp(comm_str, cmd_list->command_list[i]);
>> +	}
>>  
>>  	return match_found;
>>  }
>> @@ -1727,7 +1732,8 @@ 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, cmd_list)) {
>> +		} else if (!sched->map.task_name || sched_match_task(str, cmd_list,
>> +								sched->map.fuzzy)) {
>>  			tr->shortname[0] = sched->next_shortname1;
>>  			tr->shortname[1] = sched->next_shortname2;
>>  
>> @@ -1756,15 +1762,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, cmd_list)) {
>> -		if (!sched_match_task(thread__comm_str(sched_out), cmd_list))
>> +	if (sched->map.task_name && !sched_match_task(str, cmd_list, sched->map.fuzzy)) {
>> +		if (!sched_match_task(thread__comm_str(sched_out), cmd_list, sched->map.fuzzy))
>>  			goto out;
>>  		else
>>  			goto sched_out;
>>  
>>  	} else {
>>  		str = thread__comm_str(sched_out);
>> -		if (!(sched->map.task_name && !sched_match_task(str, cmd_list)))
>> +		if (!(sched->map.task_name && !sched_match_task(str, cmd_list, sched->map.fuzzy)))
>>  			proceed = 1;
>>  	}
>>  
>> @@ -3707,6 +3713,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[] = {
>> -- 
>> 2.43.2
>>