Skip to contentSkip to conversation

perf stat: allocation check when calculating cache instance ID

PATCH In review [email protected] 2 participants 3 messages May 27, 2024 – May 30, 2024 perf events

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 perf stat: allocation check when calculating cache instance ID

From: Yunseong Kim <[email protected]>

Adds an allocation check for cpu_map before perf_cpu_map__min() accessing

---
 tools/perf/builtin-stat.c | 24 +++++++++++++-----------
 1 file changed, 13 insertions(+), 11 deletions(-)
tools/perf/builtin-stat.c +13 -11
index 35f79b48e8dc..1f238824abb2 100644
@@ -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;
 }
Signature · 1 line
2.34.1
maintainer
#1 lore raw

Hello Yunseong,

Quoted 4 lines · On Mon, May 27, 2024 at 2:06 AM <[email protected]> wrote:

From: Yunseong Kim <[email protected]>

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

Quoted 47 lines
---
 tools/perf/builtin-stat.c | 24 +++++++++++++-----------
 1 file changed, 13 insertions(+), 11 deletions(-)
tools/perf/builtin-stat.c +13 -12
index 35f79b48e8dc..1f238824abb2 100644
@@ -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

#2 lore raw
Quoted 4 lines · Namhyung Kim

Hello Yunseong,

Quoted 1 line · On Mon, May 27, 2024 at 2:06 AM <[email protected]> wrote:
Quoted 8 lines
Quoted 3 lines

From: Yunseong Kim <[email protected]>

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

Quoted 47 lines
Quoted 47 lines
---
 tools/perf/builtin-stat.c | 24 +++++++++++++-----------
 1 file changed, 13 insertions(+), 11 deletions(-)
tools/perf/builtin-stat.c +13 -12
index 35f79b48e8dc..1f238824abb2 100644
@@ -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