linux-perf-users.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] perf stat: allocation check when calculating cache instance ID
@ 2024-05-27  9:03 yskelg
  2024-05-28 18:56 ` Namhyung Kim
  0 siblings, 1 reply; 3+ messages in thread
From: yskelg @ 2024-05-27  9:03 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
	Ian Rogers, Adrian Hunter, Liang, Kan
  Cc: Gautham Shenoy, K Prateek Nayak, skhan, Austin Kim, shjy180909,
	linux-perf-users, linux-kernel, linux-kernel-mentees,
	Yunseong Kim

From: Yunseong Kim <yskelg@gmail.com>

Adds an allocation check for cpu_map before perf_cpu_map__min() accessing

Signed-off-by: Yunseong Kim <yskelg@gmail.com>
---
 tools/perf/builtin-stat.c | 24 +++++++++++++-----------
 1 file changed, 13 insertions(+), 11 deletions(-)

diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c
index 35f79b48e8dc..1f238824abb2 100644
--- a/tools/perf/builtin-stat.c
+++ b/tools/perf/builtin-stat.c
@@ -1291,20 +1291,22 @@ static struct option stat_options[] = {
  */
 static int cpu__get_cache_id_from_map(struct perf_cpu cpu, char *map)
 {
-	int id;
+	int id = cpu.cpu;
 	struct perf_cpu_map *cpu_map = perf_cpu_map__new(map);
 
-	/*
-	 * If the map contains no CPU, consider the current CPU to
-	 * be the first online CPU in the cache domain else use the
-	 * first online CPU of the cache domain as the ID.
-	 */
-	id = perf_cpu_map__min(cpu_map).cpu;
-	if (id == -1)
-		id = cpu.cpu;
+	if (cpu_map) {
+		/*
+		 * If the map contains no CPU, consider the current CPU to
+		 * be the first online CPU in the cache domain else use the
+		 * first online CPU of the cache domain as the ID.
+		 */
+		id = perf_cpu_map__min(cpu_map).cpu;
+		if (id == -1)
+			id = cpu.cpu;
 
-	/* Free the perf_cpu_map used to find the cache ID */
-	perf_cpu_map__put(cpu_map);
+		/* Free the perf_cpu_map used to find the cache ID */
+		perf_cpu_map__put(cpu_map);
+	}
 
 	return id;
 }
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] perf stat: allocation check when calculating cache instance ID
  2024-05-27  9:03 [PATCH] perf stat: allocation check when calculating cache instance ID yskelg
@ 2024-05-28 18:56 ` Namhyung Kim
  2024-05-30 11:43   ` Yunseong Kim
  0 siblings, 1 reply; 3+ messages in thread
From: Namhyung Kim @ 2024-05-28 18:56 UTC (permalink / raw)
  To: yskelg
  Cc: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Mark Rutland, Alexander Shishkin, Jiri Olsa, Ian Rogers,
	Adrian Hunter, Liang, Kan, Gautham Shenoy, K Prateek Nayak, skhan,
	Austin Kim, shjy180909, linux-perf-users, linux-kernel,
	linux-kernel-mentees

Hello Yunseong,

On Mon, May 27, 2024 at 2:06 AM <yskelg@gmail.com> wrote:
>
> From: Yunseong Kim <yskelg@gmail.com>
>
> Adds an allocation check for cpu_map before perf_cpu_map__min() accessing

I think perf_cpu_map__min() works well with NULL.

Thanks,
Namhyung

>
> Signed-off-by: Yunseong Kim <yskelg@gmail.com>
> ---
>  tools/perf/builtin-stat.c | 24 +++++++++++++-----------
>  1 file changed, 13 insertions(+), 11 deletions(-)
>
> diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c
> index 35f79b48e8dc..1f238824abb2 100644
> --- a/tools/perf/builtin-stat.c
> +++ b/tools/perf/builtin-stat.c
> @@ -1291,20 +1291,22 @@ static struct option stat_options[] = {
>   */
>  static int cpu__get_cache_id_from_map(struct perf_cpu cpu, char *map)
>  {
> -       int id;
> +       int id = cpu.cpu;
>         struct perf_cpu_map *cpu_map = perf_cpu_map__new(map);
>
> -       /*
> -        * If the map contains no CPU, consider the current CPU to
> -        * be the first online CPU in the cache domain else use the
> -        * first online CPU of the cache domain as the ID.
> -        */
> -       id = perf_cpu_map__min(cpu_map).cpu;
> -       if (id == -1)
> -               id = cpu.cpu;
> +       if (cpu_map) {
> +               /*
> +                * If the map contains no CPU, consider the current CPU to
> +                * be the first online CPU in the cache domain else use the
> +                * first online CPU of the cache domain as the ID.
> +                */
> +               id = perf_cpu_map__min(cpu_map).cpu;
> +               if (id == -1)
> +                       id = cpu.cpu;
>
> -       /* Free the perf_cpu_map used to find the cache ID */
> -       perf_cpu_map__put(cpu_map);
> +               /* Free the perf_cpu_map used to find the cache ID */
> +               perf_cpu_map__put(cpu_map);
> +       }
>
>         return id;
>  }
> --
> 2.34.1
>

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] perf stat: allocation check when calculating cache instance ID
  2024-05-28 18:56 ` Namhyung Kim
@ 2024-05-30 11:43   ` Yunseong Kim
  0 siblings, 0 replies; 3+ messages in thread
From: Yunseong Kim @ 2024-05-30 11:43 UTC (permalink / raw)
  To: Namhyung Kim
  Cc: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Mark Rutland, Alexander Shishkin, Jiri Olsa, Ian Rogers,
	Adrian Hunter, Liang, Kan, Gautham Shenoy, K Prateek Nayak, skhan,
	Austin Kim, shjy180909, linux-perf-users, linux-kernel,
	linux-kernel-mentees



On 5/29/24 3:56 오전, Namhyung Kim wrote:
> Hello Yunseong,
> 
> On Mon, May 27, 2024 at 2:06 AM <yskelg@gmail.com> wrote:
>>
>> From: Yunseong Kim <yskelg@gmail.com>
>>
>> Adds an allocation check for cpu_map before perf_cpu_map__min() accessing
> 
> I think perf_cpu_map__min() works well with NULL.
> 
> Thanks,
> Namhyung

Thank you for your code review Namhyung.

I'll try to find a more useful improvement and patch it next time!

Warm Regards,
Yunseong Kim

>>
>> Signed-off-by: Yunseong Kim <yskelg@gmail.com>
>> ---
>>  tools/perf/builtin-stat.c | 24 +++++++++++++-----------
>>  1 file changed, 13 insertions(+), 11 deletions(-)
>>
>> diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c
>> index 35f79b48e8dc..1f238824abb2 100644
>> --- a/tools/perf/builtin-stat.c
>> +++ b/tools/perf/builtin-stat.c
>> @@ -1291,20 +1291,22 @@ static struct option stat_options[] = {
>>   */
>>  static int cpu__get_cache_id_from_map(struct perf_cpu cpu, char *map)
>>  {
>> -       int id;
>> +       int id = cpu.cpu;
>>         struct perf_cpu_map *cpu_map = perf_cpu_map__new(map);
>>
>> -       /*
>> -        * If the map contains no CPU, consider the current CPU to
>> -        * be the first online CPU in the cache domain else use the
>> -        * first online CPU of the cache domain as the ID.
>> -        */
>> -       id = perf_cpu_map__min(cpu_map).cpu;
>> -       if (id == -1)
>> -               id = cpu.cpu;
>> +       if (cpu_map) {
>> +               /*
>> +                * If the map contains no CPU, consider the current CPU to
>> +                * be the first online CPU in the cache domain else use the
>> +                * first online CPU of the cache domain as the ID.
>> +                */
>> +               id = perf_cpu_map__min(cpu_map).cpu;
>> +               if (id == -1)
>> +                       id = cpu.cpu;
>>
>> -       /* Free the perf_cpu_map used to find the cache ID */
>> -       perf_cpu_map__put(cpu_map);
>> +               /* Free the perf_cpu_map used to find the cache ID */
>> +               perf_cpu_map__put(cpu_map);
>> +       }
>>
>>         return id;
>>  }
>> --
>> 2.34.1
>>

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-05-30 11:43 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-05-27  9:03 [PATCH] perf stat: allocation check when calculating cache instance ID yskelg
2024-05-28 18:56 ` Namhyung Kim
2024-05-30 11:43   ` Yunseong Kim

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).