Skip to contentSkip to conversation

libperf: Add API for allocating new thread map

PATCH In review Tzvetomir Stoyanov (VMware) 4 participants 15 messages Feb 21, 2022 – Mar 21, 2022 perf events

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 libperf: Add API for allocating new thread map

The existing API perf_thread_map__new_dummy() allocates new thread map for one thread. I couldn't find a way to reallocate the map with more threads, or to allocate a new map for more than one thread. Having multiple threads in a thread map is essential for some use cases. That's why a new API is proposed, which allocates a new thread map for given number of threads:
perf_thread_map__new_array()

---
v2 changes:
- Changed the new API as Arnaldo suggested - get number of threads in the map and array with initial values (optional).

 tools/lib/perf/Documentation/libperf.txt |  1 +
 tools/lib/perf/include/perf/threadmap.h  |  1 +
 tools/lib/perf/libperf.map               |  1 +
 tools/lib/perf/tests/test-threadmap.c    | 41 ++++++++++++++++++++++++
 tools/lib/perf/threadmap.c               | 24 ++++++++++----
 5 files changed, 61 insertions(+), 7 deletions(-)
tools/lib/perf/Documentation/libperf.txt +1 -0
index a21f733b95b1..a8f1a237931b 100644
@@ -62,6 +62,7 @@ SYNOPSIS
   struct perf_thread_map;
 
   struct perf_thread_map *perf_thread_map__new_dummy(void);
+  struct perf_thread_map *perf_thread_map__new_array(int nr_threads, pid_t *array);
 
   void perf_thread_map__set_pid(struct perf_thread_map *map, int idx, pid_t pid);
   char *perf_thread_map__comm(struct perf_thread_map *map, int idx);
tools/lib/perf/include/perf/threadmap.h +1 -0
index 58f7fbdce446..8b40e7777cea 100644
@@ -8,6 +8,7 @@
 struct perf_thread_map;
 
 LIBPERF_API struct perf_thread_map *perf_thread_map__new_dummy(void);
+LIBPERF_API struct perf_thread_map *perf_thread_map__new_array(int nr_threads, pid_t *array);
 
 LIBPERF_API void perf_thread_map__set_pid(struct perf_thread_map *map, int idx, pid_t pid);
 LIBPERF_API char *perf_thread_map__comm(struct perf_thread_map *map, int idx);
tools/lib/perf/libperf.map +1 -0
index 6fa0d651576b..190b56ae923a 100644
@@ -12,6 +12,7 @@ LIBPERF_0.0.1 {
 		perf_cpu_map__empty;
 		perf_cpu_map__max;
 		perf_cpu_map__has;
+		perf_thread_map__new_array;
 		perf_thread_map__new_dummy;
 		perf_thread_map__set_pid;
 		perf_thread_map__comm;
tools/lib/perf/tests/test-threadmap.c +41 -0

56 lines not on this page · raw message

tools/lib/perf/threadmap.c +17 -7
index 84fa07c79d00..07968f3ea093 100644
@@ -42,18 +42,28 @@ char *perf_thread_map__comm(struct perf_thread_map *map, int idx)
 	return map->map[idx].comm;
 }
 
-struct perf_thread_map *perf_thread_map__new_dummy(void)
+struct perf_thread_map *perf_thread_map__new_array(int nr_threads, pid_t *array)
 {
-	struct perf_thread_map *threads = thread_map__alloc(1);
+	struct perf_thread_map *threads = thread_map__alloc(nr_threads);
+	int i;
+
+	if (!threads)
+		return NULL;
+
+	for (i = 0; i < nr_threads; i++)
+		perf_thread_map__set_pid(threads, i, array ? array[i] : -1);
+
+	threads->nr = nr_threads;
+	refcount_set(&threads->refcnt, 1);
 
-	if (threads != NULL) {
-		perf_thread_map__set_pid(threads, 0, -1);
-		threads->nr = 1;
-		refcount_set(&threads->refcnt, 1);
-	}
 	return threads;
 }
 
+struct perf_thread_map *perf_thread_map__new_dummy(void)
+{
+	return perf_thread_map__new_array(1, NULL);
+}
+
 static void perf_thread_map__delete(struct perf_thread_map *threads)
 {
 	if (threads) {
Signature · 1 line
2.34.1
#1 lore raw
Quoted 8 lines · Tzvetomir Stoyanov (VMware)

The existing API perf_thread_map__new_dummy() allocates new thread map for one thread. I couldn't find a way to reallocate the map with more threads, or to allocate a new map for more than one thread. Having multiple threads in a thread map is essential for some use cases. That's why a new API is proposed, which allocates a new thread map for given number of threads:
perf_thread_map__new_array()

Quoted 5 lines

---
v2 changes:
- Changed the new API as Arnaldo suggested - get number of threads in the map and array with initial values (optional).

looks good, would you also find useful in your use case the comm support in thread_map? the thread_map__read_comms function? we could move it from perf to libperf

thanks,
jirka

Quoted 149 lines
 tools/lib/perf/Documentation/libperf.txt |  1 +
 tools/lib/perf/include/perf/threadmap.h  |  1 +
 tools/lib/perf/libperf.map               |  1 +
 tools/lib/perf/tests/test-threadmap.c    | 41 ++++++++++++++++++++++++
 tools/lib/perf/threadmap.c               | 24 ++++++++++----
 5 files changed, 61 insertions(+), 7 deletions(-)
tools/lib/perf/Documentation/libperf.txt +1 -0
index a21f733b95b1..a8f1a237931b 100644
@@ -62,6 +62,7 @@ SYNOPSIS
   struct perf_thread_map;
 
   struct perf_thread_map *perf_thread_map__new_dummy(void);
+  struct perf_thread_map *perf_thread_map__new_array(int nr_threads, pid_t *array);
 
   void perf_thread_map__set_pid(struct perf_thread_map *map, int idx, pid_t pid);
   char *perf_thread_map__comm(struct perf_thread_map *map, int idx);
tools/lib/perf/include/perf/threadmap.h +1 -0
index 58f7fbdce446..8b40e7777cea 100644
@@ -8,6 +8,7 @@
 struct perf_thread_map;
 
 LIBPERF_API struct perf_thread_map *perf_thread_map__new_dummy(void);
+LIBPERF_API struct perf_thread_map *perf_thread_map__new_array(int nr_threads, pid_t *array);
 
 LIBPERF_API void perf_thread_map__set_pid(struct perf_thread_map *map, int idx, pid_t pid);
 LIBPERF_API char *perf_thread_map__comm(struct perf_thread_map *map, int idx);
tools/lib/perf/libperf.map +1 -0
index 6fa0d651576b..190b56ae923a 100644
@@ -12,6 +12,7 @@ LIBPERF_0.0.1 {
 		perf_cpu_map__empty;
 		perf_cpu_map__max;
 		perf_cpu_map__has;
+		perf_thread_map__new_array;
 		perf_thread_map__new_dummy;
 		perf_thread_map__set_pid;
 		perf_thread_map__comm;
tools/lib/perf/tests/test-threadmap.c +41 -0
index 5e2a0291e94c..f728ad7002bb 100644
@@ -11,9 +11,43 @@ static int libperf_print(enum libperf_print_level level,
 	return vfprintf(stderr, fmt, ap);
 }
 
+static int test_threadmap_array(int nr, pid_t *array)
+{
+	struct perf_thread_map *threads;
+	int i;
+
+	threads = perf_thread_map__new_array(nr, array);
+	__T("Failed to allocate new thread map", threads);
+
+	__T("Unexpected number of threads", perf_thread_map__nr(threads) == nr);
+
+	for (i = 0; i < nr; i++) {
+		__T("Unexpected initial value of thread",
+		    perf_thread_map__pid(threads, i) == (array ? array[i] : -1));
+	}
+
+	for (i = 1; i < nr; i++)
+		perf_thread_map__set_pid(threads, i, i * 100);
+
+	__T("Unexpected value of thread 0",
+	    perf_thread_map__pid(threads, 0) == (array ? array[0] : -1));
+
+	for (i = 1; i < nr; i++) {
+		__T("Unexpected thread value",
+		    perf_thread_map__pid(threads, i) == i * 100);
+	}
+
+	perf_thread_map__put(threads);
+
+	return 0;
+}
+
+#define THREADS_NR	10
 int test_threadmap(int argc, char **argv)
 {
 	struct perf_thread_map *threads;
+	pid_t thr_array[THREADS_NR];
+	int i;
 
 	__T_START;
 
@@ -27,6 +61,13 @@ int test_threadmap(int argc, char **argv)
 	perf_thread_map__put(threads);
 	perf_thread_map__put(threads);
 
+	test_threadmap_array(THREADS_NR, NULL);
+
+	for (i = 0; i < THREADS_NR; i++)
+		thr_array[i] = i + 100;
+
+	test_threadmap_array(THREADS_NR, thr_array);
+
 	__T_END;
 	return tests_failed == 0 ? 0 : -1;
 }
tools/lib/perf/threadmap.c +17 -7
index 84fa07c79d00..07968f3ea093 100644
@@ -42,18 +42,28 @@ char *perf_thread_map__comm(struct perf_thread_map *map, int idx)
 	return map->map[idx].comm;
 }
 
-struct perf_thread_map *perf_thread_map__new_dummy(void)
+struct perf_thread_map *perf_thread_map__new_array(int nr_threads, pid_t *array)
 {
-	struct perf_thread_map *threads = thread_map__alloc(1);
+	struct perf_thread_map *threads = thread_map__alloc(nr_threads);
+	int i;
+
+	if (!threads)
+		return NULL;
+
+	for (i = 0; i < nr_threads; i++)
+		perf_thread_map__set_pid(threads, i, array ? array[i] : -1);
+
+	threads->nr = nr_threads;
+	refcount_set(&threads->refcnt, 1);
 
-	if (threads != NULL) {
-		perf_thread_map__set_pid(threads, 0, -1);
-		threads->nr = 1;
-		refcount_set(&threads->refcnt, 1);
-	}
 	return threads;
 }
 
+struct perf_thread_map *perf_thread_map__new_dummy(void)
+{
+	return perf_thread_map__new_array(1, NULL);
+}
+
 static void perf_thread_map__delete(struct perf_thread_map *threads)
 {
 	if (threads) {
Signature · 1 line
2.34.1
#2 lore raw
Quoted 15 lines · Jiri Olsa
Quoted 8 lines · Tzvetomir Stoyanov (VMware)

The existing API perf_thread_map__new_dummy() allocates new thread map for one thread. I couldn't find a way to reallocate the map with more threads, or to allocate a new map for more than one thread. Having multiple threads in a thread map is essential for some use cases. That's why a new API is proposed, which allocates a new thread map for given number of threads:
perf_thread_map__new_array()

Quoted 5 lines

---
v2 changes:
- Changed the new API as Arnaldo suggested - get number of threads in the map and array with initial values (optional).

Quoted 3 lines

looks good, would you also find useful in your use case the comm support in thread_map? the thread_map__read_comms function? we could move it from perf to libperf

IOW: we're happy that you're working on libperf, so feel encouraged to move things from tools/perf/util/ to tools/lib/perf/ if you find a supporting use case, brownie points if you also add an entry to tools/perf/tests/, AKA 'perf test'.

Thanks!

- Arnaldo

Quoted 153 lines

thanks,
jirka

Quoted 149 lines
 tools/lib/perf/Documentation/libperf.txt |  1 +
 tools/lib/perf/include/perf/threadmap.h  |  1 +
 tools/lib/perf/libperf.map               |  1 +
 tools/lib/perf/tests/test-threadmap.c    | 41 ++++++++++++++++++++++++
 tools/lib/perf/threadmap.c               | 24 ++++++++++----
 5 files changed, 61 insertions(+), 7 deletions(-)
tools/lib/perf/Documentation/libperf.txt +1 -0
index a21f733b95b1..a8f1a237931b 100644
@@ -62,6 +62,7 @@ SYNOPSIS
   struct perf_thread_map;
 
   struct perf_thread_map *perf_thread_map__new_dummy(void);
+  struct perf_thread_map *perf_thread_map__new_array(int nr_threads, pid_t *array);
 
   void perf_thread_map__set_pid(struct perf_thread_map *map, int idx, pid_t pid);
   char *perf_thread_map__comm(struct perf_thread_map *map, int idx);
tools/lib/perf/include/perf/threadmap.h +1 -0
index 58f7fbdce446..8b40e7777cea 100644
@@ -8,6 +8,7 @@
 struct perf_thread_map;
 
 LIBPERF_API struct perf_thread_map *perf_thread_map__new_dummy(void);
+LIBPERF_API struct perf_thread_map *perf_thread_map__new_array(int nr_threads, pid_t *array);
 
 LIBPERF_API void perf_thread_map__set_pid(struct perf_thread_map *map, int idx, pid_t pid);
 LIBPERF_API char *perf_thread_map__comm(struct perf_thread_map *map, int idx);
tools/lib/perf/libperf.map +1 -0
index 6fa0d651576b..190b56ae923a 100644
@@ -12,6 +12,7 @@ LIBPERF_0.0.1 {
 		perf_cpu_map__empty;
 		perf_cpu_map__max;
 		perf_cpu_map__has;
+		perf_thread_map__new_array;
 		perf_thread_map__new_dummy;
 		perf_thread_map__set_pid;
 		perf_thread_map__comm;
tools/lib/perf/tests/test-threadmap.c +41 -0
index 5e2a0291e94c..f728ad7002bb 100644
@@ -11,9 +11,43 @@ static int libperf_print(enum libperf_print_level level,
 	return vfprintf(stderr, fmt, ap);
 }
 
+static int test_threadmap_array(int nr, pid_t *array)
+{
+	struct perf_thread_map *threads;
+	int i;
+
+	threads = perf_thread_map__new_array(nr, array);
+	__T("Failed to allocate new thread map", threads);
+
+	__T("Unexpected number of threads", perf_thread_map__nr(threads) == nr);
+
+	for (i = 0; i < nr; i++) {
+		__T("Unexpected initial value of thread",
+		    perf_thread_map__pid(threads, i) == (array ? array[i] : -1));
+	}
+
+	for (i = 1; i < nr; i++)
+		perf_thread_map__set_pid(threads, i, i * 100);
+
+	__T("Unexpected value of thread 0",
+	    perf_thread_map__pid(threads, 0) == (array ? array[0] : -1));
+
+	for (i = 1; i < nr; i++) {
+		__T("Unexpected thread value",
+		    perf_thread_map__pid(threads, i) == i * 100);
+	}
+
+	perf_thread_map__put(threads);
+
+	return 0;
+}
+
+#define THREADS_NR	10
 int test_threadmap(int argc, char **argv)
 {
 	struct perf_thread_map *threads;
+	pid_t thr_array[THREADS_NR];
+	int i;
 
 	__T_START;
 
@@ -27,6 +61,13 @@ int test_threadmap(int argc, char **argv)
 	perf_thread_map__put(threads);
 	perf_thread_map__put(threads);
 
+	test_threadmap_array(THREADS_NR, NULL);
+
+	for (i = 0; i < THREADS_NR; i++)
+		thr_array[i] = i + 100;
+
+	test_threadmap_array(THREADS_NR, thr_array);
+
 	__T_END;
 	return tests_failed == 0 ? 0 : -1;
 }
tools/lib/perf/threadmap.c +17 -7
index 84fa07c79d00..07968f3ea093 100644
@@ -42,18 +42,28 @@ char *perf_thread_map__comm(struct perf_thread_map *map, int idx)
 	return map->map[idx].comm;
 }
 
-struct perf_thread_map *perf_thread_map__new_dummy(void)
+struct perf_thread_map *perf_thread_map__new_array(int nr_threads, pid_t *array)
 {
-	struct perf_thread_map *threads = thread_map__alloc(1);
+	struct perf_thread_map *threads = thread_map__alloc(nr_threads);
+	int i;
+
+	if (!threads)
+		return NULL;
+
+	for (i = 0; i < nr_threads; i++)
+		perf_thread_map__set_pid(threads, i, array ? array[i] : -1);
+
+	threads->nr = nr_threads;
+	refcount_set(&threads->refcnt, 1);
 
-	if (threads != NULL) {
-		perf_thread_map__set_pid(threads, 0, -1);
-		threads->nr = 1;
-		refcount_set(&threads->refcnt, 1);
-	}
 	return threads;
 }
 
+struct perf_thread_map *perf_thread_map__new_dummy(void)
+{
+	return perf_thread_map__new_array(1, NULL);
+}
+
 static void perf_thread_map__delete(struct perf_thread_map *threads)
 {
 	if (threads) {
Signature · 1 line
2.34.1
#3 lore raw
Quoted 19 lines · Arnaldo Carvalho de Melo
Quoted 15 lines · Jiri Olsa
Quoted 13 lines · Tzvetomir Stoyanov (VMware)

The existing API perf_thread_map__new_dummy() allocates new thread map for one thread. I couldn't find a way to reallocate the map with more threads, or to allocate a new map for more than one thread. Having multiple threads in a thread map is essential for some use cases. That's why a new API is proposed, which allocates a new thread map for given number of threads:
perf_thread_map__new_array()

---
v2 changes:
- Changed the new API as Arnaldo suggested - get number of threads in the map and array with initial values (optional).

Quoted 6 lines
Quoted 3 lines

looks good, would you also find useful in your use case the comm support in thread_map? the thread_map__read_comms function? we could move it from perf to libperf

IOW: we're happy that you're working on libperf, so feel encouraged to move things from tools/perf/util/ to tools/lib/perf/ if you find a supporting use case, brownie points if you also add an entry to tools/perf/tests/, AKA 'perf test'.

Thanks, I see that there is a lot of functionality that could be moved from perf to libperf and which will be useful for the library users. We are going to use libperf in trace-cruncher,
https://github.com/vmware/trace-cruncher, as an interface to perf.

Quoted 157 lines

Thanks!

- Arnaldo

Quoted 153 lines

thanks,
jirka

Quoted 149 lines
 tools/lib/perf/Documentation/libperf.txt |  1 +
 tools/lib/perf/include/perf/threadmap.h  |  1 +
 tools/lib/perf/libperf.map               |  1 +
 tools/lib/perf/tests/test-threadmap.c    | 41 ++++++++++++++++++++++++
 tools/lib/perf/threadmap.c               | 24 ++++++++++----
 5 files changed, 61 insertions(+), 7 deletions(-)
tools/lib/perf/Documentation/libperf.txt +1 -0
index a21f733b95b1..a8f1a237931b 100644
@@ -62,6 +62,7 @@ SYNOPSIS
   struct perf_thread_map;
 
   struct perf_thread_map *perf_thread_map__new_dummy(void);
+  struct perf_thread_map *perf_thread_map__new_array(int nr_threads, pid_t *array);
 
   void perf_thread_map__set_pid(struct perf_thread_map *map, int idx, pid_t pid);
   char *perf_thread_map__comm(struct perf_thread_map *map, int idx);
tools/lib/perf/include/perf/threadmap.h +1 -0
index 58f7fbdce446..8b40e7777cea 100644
@@ -8,6 +8,7 @@
 struct perf_thread_map;
 
 LIBPERF_API struct perf_thread_map *perf_thread_map__new_dummy(void);
+LIBPERF_API struct perf_thread_map *perf_thread_map__new_array(int nr_threads, pid_t *array);
 
 LIBPERF_API void perf_thread_map__set_pid(struct perf_thread_map *map, int idx, pid_t pid);
 LIBPERF_API char *perf_thread_map__comm(struct perf_thread_map *map, int idx);
tools/lib/perf/libperf.map +1 -0
index 6fa0d651576b..190b56ae923a 100644
@@ -12,6 +12,7 @@ LIBPERF_0.0.1 {
             perf_cpu_map__empty;
             perf_cpu_map__max;
             perf_cpu_map__has;
+            perf_thread_map__new_array;
             perf_thread_map__new_dummy;
             perf_thread_map__set_pid;
             perf_thread_map__comm;
tools/lib/perf/tests/test-threadmap.c +41 -0
index 5e2a0291e94c..f728ad7002bb 100644
@@ -11,9 +11,43 @@ static int libperf_print(enum libperf_print_level level,
     return vfprintf(stderr, fmt, ap);
 }
 
+static int test_threadmap_array(int nr, pid_t *array)
+{
+    struct perf_thread_map *threads;
+    int i;
+
+    threads = perf_thread_map__new_array(nr, array);
+    __T("Failed to allocate new thread map", threads);
+
+    __T("Unexpected number of threads", perf_thread_map__nr(threads) == nr);
+
+    for (i = 0; i < nr; i++) {
+            __T("Unexpected initial value of thread",
+                perf_thread_map__pid(threads, i) == (array ? array[i] : -1));
+    }
+
+    for (i = 1; i < nr; i++)
+            perf_thread_map__set_pid(threads, i, i * 100);
+
+    __T("Unexpected value of thread 0",
+        perf_thread_map__pid(threads, 0) == (array ? array[0] : -1));
+
+    for (i = 1; i < nr; i++) {
+            __T("Unexpected thread value",
+                perf_thread_map__pid(threads, i) == i * 100);
+    }
+
+    perf_thread_map__put(threads);
+
+    return 0;
+}
+
+#define THREADS_NR  10
 int test_threadmap(int argc, char **argv)
 {
     struct perf_thread_map *threads;
+    pid_t thr_array[THREADS_NR];
+    int i;
 
     __T_START;
 
@@ -27,6 +61,13 @@ int test_threadmap(int argc, char **argv)
     perf_thread_map__put(threads);
     perf_thread_map__put(threads);
 
+    test_threadmap_array(THREADS_NR, NULL);
+
+    for (i = 0; i < THREADS_NR; i++)
+            thr_array[i] = i + 100;
+
+    test_threadmap_array(THREADS_NR, thr_array);
+
     __T_END;
     return tests_failed == 0 ? 0 : -1;
 }
tools/lib/perf/threadmap.c +17 -8
index 84fa07c79d00..07968f3ea093 100644
@@ -42,18 +42,28 @@ char *perf_thread_map__comm(struct perf_thread_map *map, int idx)
     return map->map[idx].comm;
 }
 
-struct perf_thread_map *perf_thread_map__new_dummy(void)
+struct perf_thread_map *perf_thread_map__new_array(int nr_threads, pid_t *array)
 {
-    struct perf_thread_map *threads = thread_map__alloc(1);
+    struct perf_thread_map *threads = thread_map__alloc(nr_threads);
+    int i;
+
+    if (!threads)
+            return NULL;
+
+    for (i = 0; i < nr_threads; i++)
+            perf_thread_map__set_pid(threads, i, array ? array[i] : -1);
+
+    threads->nr = nr_threads;
+    refcount_set(&threads->refcnt, 1);
 
-    if (threads != NULL) {
-            perf_thread_map__set_pid(threads, 0, -1);
-            threads->nr = 1;
-            refcount_set(&threads->refcnt, 1);
-    }
     return threads;
 }
 
+struct perf_thread_map *perf_thread_map__new_dummy(void)
+{
+    return perf_thread_map__new_array(1, NULL);
+}
+
 static void perf_thread_map__delete(struct perf_thread_map *threads)
 {
     if (threads) {
--

2.34.1

Signature · 2 lines
Tzvetomir (Ceco) Stoyanov
VMware Open Source Technology Center
#4 lore raw

Em Tue, Feb 22, 2022 at 04:32:05AM +0200, Tzvetomir Stoyanov escreveu:

Quoted 12 lines · Arnaldo Carvalho de Melo
Quoted 10 lines · Jiri Olsa
Quoted 4 lines · Tzvetomir Stoyanov (VMware)

looks good, would you also find useful in your use case the comm support in thread_map? the thread_map__read_comms function? we could move it from perf to libperf

IOW: we're happy that you're working on libperf, so feel encouraged to move things from tools/perf/util/ to tools/lib/perf/ if you find a supporting use case, brownie points if you also add an entry to tools/perf/tests/, AKA 'perf test'.

Quoted 4 lines

Thanks, I see that there is a lot of functionality that could be moved from perf to libperf and which will be useful for the library users. We are going to use libperf in trace-cruncher,
https://github.com/vmware/trace-cruncher, as an interface to perf.

Cool, so as you go on adding functionality to trace cruncher and notice that something that is in tools/perf/util/ that is usable, please submit patches to move things to tools/lib/perf/, adding tests to 'perf test' as you go.

Thanks a lot!

- Arnaldo

#5 lore raw
Quoted 19 lines · Arnaldo Carvalho de Melo

Em Tue, Feb 22, 2022 at 04:32:05AM +0200, Tzvetomir Stoyanov escreveu:

Quoted 12 lines · Arnaldo Carvalho de Melo
Quoted 10 lines · Jiri Olsa
Quoted 4 lines · Tzvetomir Stoyanov (VMware)

looks good, would you also find useful in your use case the comm support in thread_map? the thread_map__read_comms function? we could move it from perf to libperf

IOW: we're happy that you're working on libperf, so feel encouraged to move things from tools/perf/util/ to tools/lib/perf/ if you find a supporting use case, brownie points if you also add an entry to tools/perf/tests/, AKA 'perf test'.

Quoted 4 lines

Thanks, I see that there is a lot of functionality that could be moved from perf to libperf and which will be useful for the library users. We are going to use libperf in trace-cruncher,
https://github.com/vmware/trace-cruncher, as an interface to perf.

Quoted 8 lines

Cool, so as you go on adding functionality to trace cruncher and notice that something that is in tools/perf/util/ that is usable, please submit patches to move things to tools/lib/perf/, adding tests to 'perf test' as you go.

Thanks a lot!

- Arnaldo

Sure, I'll do it. I have one more question - do you plan to release the next version of libperf soon, so we can use this new functionality in trace-cruncher ?

Signature · 2 lines
Tzvetomir (Ceco) Stoyanov
VMware Open Source Technology Center
#6 lore raw
Quoted 26 lines · Tzvetomir Stoyanov
Quoted 24 lines · Arnaldo Carvalho de Melo

Em Tue, Feb 22, 2022 at 04:32:05AM +0200, Tzvetomir Stoyanov escreveu:

Quoted 12 lines · Arnaldo Carvalho de Melo
Quoted 10 lines · Jiri Olsa
Quoted 4 lines · Tzvetomir Stoyanov (VMware)

looks good, would you also find useful in your use case the comm support in thread_map? the thread_map__read_comms function? we could move it from perf to libperf

IOW: we're happy that you're working on libperf, so feel encouraged to move things from tools/perf/util/ to tools/lib/perf/ if you find a supporting use case, brownie points if you also add an entry to tools/perf/tests/, AKA 'perf test'.

Quoted 4 lines

Thanks, I see that there is a lot of functionality that could be moved from perf to libperf and which will be useful for the library users. We are going to use libperf in trace-cruncher,
https://github.com/vmware/trace-cruncher, as an interface to perf.

Cool, so as you go on adding functionality to trace cruncher and notice that something that is in tools/perf/util/ that is usable, please submit patches to move things to tools/lib/perf/, adding tests to 'perf test' as you go.

Quoted 7 lines
Quoted 3 lines

Thanks a lot!

- Arnaldo

Sure, I'll do it. I have one more question - do you plan to release the next version of libperf soon, so we can use this new functionality in trace-cruncher ?

we can discuss that, can be fast ;-)

btw I took a look on that and already put some change together, please take a look and feel free to use it

I just moved thread_map__read_comms, but I wonder we want it to change to return status if it's in libperf

jirka

---
 tools/lib/perf/Build                    |  7 ++-
 tools/lib/perf/Makefile                 |  1 +
 tools/lib/perf/include/perf/threadmap.h |  2 +
 tools/lib/perf/libperf.map              |  5 +++
 tools/lib/perf/threadmap.c              | 57 +++++++++++++++++++++++++
 tools/perf/builtin-stat.c               |  2 +-
 tools/perf/tests/thread-map.c           |  6 +--
 tools/perf/util/Build                   |  6 ---
 tools/perf/util/python-ext-sources      |  1 -
 tools/perf/util/thread_map.c            | 53 -----------------------
 tools/perf/util/thread_map.h            |  1 -
 11 files changed, 75 insertions(+), 66 deletions(-)
tools/lib/perf/Build +6 -1
index e8f5b7fb9973..6d3f63793a51 100644
@@ -7,8 +7,13 @@ libperf-y += mmap.o
 libperf-y += zalloc.o
 libperf-y += xyarray.o
 libperf-y += lib.o
+libperf-y += string.o
 
-$(OUTPUT)zalloc.o: ../../lib/zalloc.c FORCE
+$(OUTPUT)zalloc.o: ../zalloc.c FORCE
+	$(call rule_mkdir)
+	$(call if_changed_dep,cc_o_c)
+
+$(OUTPUT)string.o: ../string.c FORCE
 	$(call rule_mkdir)
 	$(call if_changed_dep,cc_o_c)
 
tools/lib/perf/Makefile +1 -0
index 08fe6e3c4089..81b495af3cf1 100644
@@ -75,6 +75,7 @@ override CFLAGS += -Werror -Wall
 override CFLAGS += -fPIC
 override CFLAGS += $(INCLUDES)
 override CFLAGS += -fvisibility=hidden
+override CFLAGS += -D_GNU_SOURCE
 
 all:
 
tools/lib/perf/include/perf/threadmap.h +2 -0
index a7c50de8d010..6301f0db1db4 100644
@@ -17,4 +17,6 @@ LIBPERF_API pid_t perf_thread_map__pid(struct perf_thread_map *map, int thread);
 LIBPERF_API struct perf_thread_map *perf_thread_map__get(struct perf_thread_map *map);
 LIBPERF_API void perf_thread_map__put(struct perf_thread_map *map);
 
+LIBPERF_API void perf_thread_map__read_comms(struct perf_thread_map *map);
+
 #endif /* __LIBPERF_THREADMAP_H */
tools/lib/perf/libperf.map +5 -0
index 6fa0d651576b..4345f7b9b0c5 100644
@@ -56,3 +56,8 @@ LIBPERF_0.0.1 {
 	local:
 		*;
 };
+
+LIBPERF_0.0.2 {
+	global:
+		perf_thread_map__read_comms;
+} LIBPERF_0.0.1;
tools/lib/perf/threadmap.c +57 -0

66 lines not on this page · raw message

tools/perf/builtin-stat.c +1 -1
index 3f98689dd687..8456ad3e73b8 100644
@@ -2477,7 +2477,7 @@ int cmd_stat(int argc, const char **argv)
 	 * so we could print it out on output.
 	 */
 	if (stat_config.aggr_mode == AGGR_THREAD) {
-		thread_map__read_comms(evsel_list->core.threads);
+		perf_thread_map__read_comms(evsel_list->core.threads);
 		if (target.system_wide) {
 			if (runtime_stat_new(&stat_config,
 				perf_thread_map__nr(evsel_list->core.threads))) {
tools/perf/tests/thread-map.c +3 -3
index e413c1387fcb..00180b56948e 100644
@@ -30,7 +30,7 @@ static int test__thread_map(struct test_suite *test __maybe_unused, int subtest
 	map = thread_map__new_by_pid(getpid());
 	TEST_ASSERT_VAL("failed to alloc map", map);
 
-	thread_map__read_comms(map);
+	perf_thread_map__read_comms(map);
 
 	TEST_ASSERT_VAL("wrong nr", map->nr == 1);
 	TEST_ASSERT_VAL("wrong pid",
@@ -46,7 +46,7 @@ static int test__thread_map(struct test_suite *test __maybe_unused, int subtest
 	map = perf_thread_map__new_dummy();
 	TEST_ASSERT_VAL("failed to alloc map", map);
 
-	thread_map__read_comms(map);
+	perf_thread_map__read_comms(map);
 
 	TEST_ASSERT_VAL("wrong nr", map->nr == 1);
 	TEST_ASSERT_VAL("wrong pid", perf_thread_map__pid(map, 0) == -1);
@@ -97,7 +97,7 @@ static int test__thread_map_synthesize(struct test_suite *test __maybe_unused, i
 	threads = thread_map__new_by_pid(getpid());
 	TEST_ASSERT_VAL("failed to alloc map", threads);
 
-	thread_map__read_comms(threads);
+	perf_thread_map__read_comms(threads);
 
 	TEST_ASSERT_VAL("failed to synthesize map",
 		!perf_event__synthesize_thread_map2(NULL, threads, process_event, NULL));
tools/perf/util/Build +0 -6
index 9a7209a99e16..285b466ac1c3 100644
@@ -32,7 +32,6 @@ perf-y += print_binary.o
 perf-y += rlimit.o
 perf-y += argv_split.o
 perf-y += rbtree.o
-perf-y += libstring.o
 perf-y += bitmap.o
 perf-y += hweight.o
 perf-y += smt.o
@@ -279,7 +278,6 @@ $(OUTPUT)util/expr.o: $(OUTPUT)util/expr-flex.c $(OUTPUT)util/expr-bison.c
 CFLAGS_bitmap.o        += -Wno-unused-parameter -DETC_PERFCONFIG="BUILD_STR($(ETC_PERFCONFIG_SQ))"
 CFLAGS_find_bit.o      += -Wno-unused-parameter -DETC_PERFCONFIG="BUILD_STR($(ETC_PERFCONFIG_SQ))"
 CFLAGS_rbtree.o        += -Wno-unused-parameter -DETC_PERFCONFIG="BUILD_STR($(ETC_PERFCONFIG_SQ))"
-CFLAGS_libstring.o     += -Wno-unused-parameter -DETC_PERFCONFIG="BUILD_STR($(ETC_PERFCONFIG_SQ))"
 CFLAGS_hweight.o       += -Wno-unused-parameter -DETC_PERFCONFIG="BUILD_STR($(ETC_PERFCONFIG_SQ))"
 CFLAGS_parse-events.o  += -Wno-redundant-decls
 CFLAGS_expr.o          += -Wno-redundant-decls
@@ -309,10 +307,6 @@ $(OUTPUT)util/rbtree.o: ../lib/rbtree.c FORCE
 	$(call rule_mkdir)
 	$(call if_changed_dep,cc_o_c)
 
-$(OUTPUT)util/libstring.o: ../lib/string.c FORCE
-	$(call rule_mkdir)
-	$(call if_changed_dep,cc_o_c)
-
 $(OUTPUT)util/hweight.o: ../lib/hweight.c FORCE
 	$(call rule_mkdir)
 	$(call if_changed_dep,cc_o_c)
tools/perf/util/python-ext-sources +0 -1
index a685d20165f7..b4cbd727c8b1 100644
@@ -20,7 +20,6 @@ util/namespaces.c
 ../lib/find_bit.c
 ../lib/list_sort.c
 ../lib/hweight.c
-../lib/string.c
 ../lib/vsprintf.c
 util/thread_map.c
 util/util.c
tools/perf/util/thread_map.c +0 -53

59 lines not on this page · raw message

tools/perf/util/thread_map.h +0 -1
index 3bb860a32b8e..1f84edd38b17 100644
@@ -25,7 +25,6 @@ struct perf_thread_map *thread_map__new_by_tid_str(const char *tid_str);
 
 size_t thread_map__fprintf(struct perf_thread_map *threads, FILE *fp);
 
-void thread_map__read_comms(struct perf_thread_map *threads);
 bool thread_map__has(struct perf_thread_map *threads, pid_t pid);
 int thread_map__remove(struct perf_thread_map *threads, int idx);
 #endif	/* __PERF_THREAD_MAP_H */
Signature · 1 line
2.35.1
#7 lore raw
Quoted 38 lines · Jiri Olsa
Quoted 26 lines · Tzvetomir Stoyanov
Quoted 24 lines · Arnaldo Carvalho de Melo

Em Tue, Feb 22, 2022 at 04:32:05AM +0200, Tzvetomir Stoyanov escreveu:

Quoted 12 lines · Arnaldo Carvalho de Melo
Quoted 10 lines · Jiri Olsa
Quoted 4 lines · Tzvetomir Stoyanov (VMware)

looks good, would you also find useful in your use case the comm support in thread_map? the thread_map__read_comms function? we could move it from perf to libperf

IOW: we're happy that you're working on libperf, so feel encouraged to move things from tools/perf/util/ to tools/lib/perf/ if you find a supporting use case, brownie points if you also add an entry to tools/perf/tests/, AKA 'perf test'.

Quoted 4 lines

Thanks, I see that there is a lot of functionality that could be moved from perf to libperf and which will be useful for the library users. We are going to use libperf in trace-cruncher,
https://github.com/vmware/trace-cruncher, as an interface to perf.

Cool, so as you go on adding functionality to trace cruncher and notice that something that is in tools/perf/util/ that is usable, please submit patches to move things to tools/lib/perf/, adding tests to 'perf test' as you go.

Quoted 7 lines
Quoted 3 lines

Thanks a lot!

- Arnaldo

Sure, I'll do it. I have one more question - do you plan to release the next version of libperf soon, so we can use this new functionality in trace-cruncher ?

we can discuss that, can be fast ;-)

Quoted 8 lines

btw I took a look on that and already put some change together, please take a look and feel free to use it

I just moved thread_map__read_comms, but I wonder we want it to change to return status if it's in libperf

jirka

Thanks Jiri, I played a little with the patch but it does not fit my use case. It resolves only PID to command, but in my case I put threads in the map. How do you handle that case in perf ? I use perf to collect samplings from a given PID, and put all threads of this process in the map, all entries from /proc/PID/task/ folder. In my logic, as I know the PID, the commands are resolved using /proc/PID/task/TID/comm files.
The other problem that I see is - there should be a way to dynamically resize and add new entries in the thread map. There are cases when new threads are spawned during the trace - these should be added in the thread map. In my code, I handle the PERF_RECORD_COMM event - resize my array and add the new entry. That can be implemented in libperf also, I think it will be very useful.
Anyway, the patch is useful for PID to command resolving, if there are only PIDs in the map. It could be extended with additional APIs for TID to command resolving. Maybe we can have these APIs:
void perf_thread_map__read_pid_comms(struct perf_thread_map *threads); void perf_thread_map__read_tid_comms(pid_t pid, struct
perf_thread_map *threads);

Quoted 20 lines
---
 tools/lib/perf/Build                    |  7 ++-
 tools/lib/perf/Makefile                 |  1 +
 tools/lib/perf/include/perf/threadmap.h |  2 +
 tools/lib/perf/libperf.map              |  5 +++
 tools/lib/perf/threadmap.c              | 57 +++++++++++++++++++++++++
 tools/perf/builtin-stat.c               |  2 +-
 tools/perf/tests/thread-map.c           |  6 +--
 tools/perf/util/Build                   |  6 ---
 tools/perf/util/python-ext-sources      |  1 -
 tools/perf/util/thread_map.c            | 53 -----------------------
 tools/perf/util/thread_map.h            |  1 -
 11 files changed, 75 insertions(+), 66 deletions(-)
tools/lib/perf/Build +0 -0
index e8f5b7fb9973..6d3f63793a51 100644

tools/lib/perf/Build libperf-y += mmap.o · line 10

Quoted 4 lines
tools/lib/perf/Build +1 -0
@@ -7,8 +7,13 @@ libperf-y += mmap.o
 libperf-y += zalloc.o
 libperf-y += xyarray.o
 libperf-y += lib.o
+libperf-y += string.o

Looks like ctype is required also:
libperf-y += ctype.o

tools/lib/perf/Build libperf-y += mmap.o · line 18

Quoted 9 lines
tools/lib/perf/Build +5 -1
-$(OUTPUT)zalloc.o: ../../lib/zalloc.c FORCE
+$(OUTPUT)zalloc.o: ../zalloc.c FORCE
+       $(call rule_mkdir)
+       $(call if_changed_dep,cc_o_c)
+
+$(OUTPUT)string.o: ../string.c FORCE
        $(call rule_mkdir)
        $(call if_changed_dep,cc_o_c)

+$(OUTPUT)ctype.o: ../ctype.c FORCE

         $(call rule_mkdir)
         $(call if_changed_dep,cc_o_c)
Quoted 275 lines
tools/lib/perf/Makefile +1 -0
index 08fe6e3c4089..81b495af3cf1 100644
@@ -75,6 +75,7 @@ override CFLAGS += -Werror -Wall
 override CFLAGS += -fPIC
 override CFLAGS += $(INCLUDES)
 override CFLAGS += -fvisibility=hidden
+override CFLAGS += -D_GNU_SOURCE
 
 all:
 
tools/lib/perf/include/perf/threadmap.h +2 -0
index a7c50de8d010..6301f0db1db4 100644
@@ -17,4 +17,6 @@ LIBPERF_API pid_t perf_thread_map__pid(struct perf_thread_map *map, int thread);
 LIBPERF_API struct perf_thread_map *perf_thread_map__get(struct perf_thread_map *map);
 LIBPERF_API void perf_thread_map__put(struct perf_thread_map *map);
 
+LIBPERF_API void perf_thread_map__read_comms(struct perf_thread_map *map);
+
 #endif /* __LIBPERF_THREADMAP_H */
tools/lib/perf/libperf.map +5 -0
index 6fa0d651576b..4345f7b9b0c5 100644
@@ -56,3 +56,8 @@ LIBPERF_0.0.1 {
        local:
                *;
 };
+
+LIBPERF_0.0.2 {
+       global:
+               perf_thread_map__read_comms;
+} LIBPERF_0.0.1;
tools/lib/perf/threadmap.c +57 -0
index e92c368b0a6c..f5ad6d939365 100644
@@ -6,6 +6,10 @@
 #include <string.h>
 #include <asm/bug.h>
 #include <stdio.h>
+#include <errno.h>
+#include <api/fs/fs.h>
+#include <linux/string.h>
+#include "internal.h"
 
 static void perf_thread_map__reset(struct perf_thread_map *map, int start, int nr)
 {
@@ -89,3 +93,56 @@ pid_t perf_thread_map__pid(struct perf_thread_map *map, int thread)
 {
        return map->map[thread].pid;
 }
+
+static int get_comm(char **comm, pid_t pid)
+{
+       char *path;
+       size_t size;
+       int err;
+
+       if (asprintf(&path, "%s/%d/comm", procfs__mountpoint(), pid) == -1)
+               return -ENOMEM;
+
+       err = filename__read_str(path, comm, &size);
+       if (!err) {
+               /*
+                * We're reading 16 bytes, while filename__read_str
+                * allocates data per BUFSIZ bytes, so we can safely
+                * mark the end of the string.
+                */
+               (*comm)[size] = 0;
+               strim(*comm);
+       }
+
+       free(path);
+       return err;
+}
+
+static void comm_init(struct perf_thread_map *map, int i)
+{
+       pid_t pid = perf_thread_map__pid(map, i);
+       char *comm = NULL;
+
+       /* dummy pid comm initialization */
+       if (pid == -1) {
+               map->map[i].comm = strdup("dummy");
+               return;
+       }
+
+       /*
+        * The comm name is like extra bonus ;-),
+        * so just warn if we fail for any reason.
+        */
+       if (get_comm(&comm, pid))
+               pr_warning("Couldn't resolve comm name for pid %d\n", pid);
+
+       map->map[i].comm = comm;
+}
+
+void perf_thread_map__read_comms(struct perf_thread_map *threads)
+{
+       int i;
+
+       for (i = 0; i < threads->nr; ++i)
+               comm_init(threads, i);
+}
tools/perf/builtin-stat.c +1 -1
index 3f98689dd687..8456ad3e73b8 100644
@@ -2477,7 +2477,7 @@ int cmd_stat(int argc, const char **argv)
         * so we could print it out on output.
         */
        if (stat_config.aggr_mode == AGGR_THREAD) {
-               thread_map__read_comms(evsel_list->core.threads);
+               perf_thread_map__read_comms(evsel_list->core.threads);
                if (target.system_wide) {
                        if (runtime_stat_new(&stat_config,
                                perf_thread_map__nr(evsel_list->core.threads))) {
tools/perf/tests/thread-map.c +3 -3
index e413c1387fcb..00180b56948e 100644
@@ -30,7 +30,7 @@ static int test__thread_map(struct test_suite *test __maybe_unused, int subtest
        map = thread_map__new_by_pid(getpid());
        TEST_ASSERT_VAL("failed to alloc map", map);
 
-       thread_map__read_comms(map);
+       perf_thread_map__read_comms(map);
 
        TEST_ASSERT_VAL("wrong nr", map->nr == 1);
        TEST_ASSERT_VAL("wrong pid",
@@ -46,7 +46,7 @@ static int test__thread_map(struct test_suite *test __maybe_unused, int subtest
        map = perf_thread_map__new_dummy();
        TEST_ASSERT_VAL("failed to alloc map", map);
 
-       thread_map__read_comms(map);
+       perf_thread_map__read_comms(map);
 
        TEST_ASSERT_VAL("wrong nr", map->nr == 1);
        TEST_ASSERT_VAL("wrong pid", perf_thread_map__pid(map, 0) == -1);
@@ -97,7 +97,7 @@ static int test__thread_map_synthesize(struct test_suite *test __maybe_unused, i
        threads = thread_map__new_by_pid(getpid());
        TEST_ASSERT_VAL("failed to alloc map", threads);
 
-       thread_map__read_comms(threads);
+       perf_thread_map__read_comms(threads);
 
        TEST_ASSERT_VAL("failed to synthesize map",
                !perf_event__synthesize_thread_map2(NULL, threads, process_event, NULL));
tools/perf/util/Build +0 -6
index 9a7209a99e16..285b466ac1c3 100644
@@ -32,7 +32,6 @@ perf-y += print_binary.o
 perf-y += rlimit.o
 perf-y += argv_split.o
 perf-y += rbtree.o
-perf-y += libstring.o
 perf-y += bitmap.o
 perf-y += hweight.o
 perf-y += smt.o
@@ -279,7 +278,6 @@ $(OUTPUT)util/expr.o: $(OUTPUT)util/expr-flex.c $(OUTPUT)util/expr-bison.c
 CFLAGS_bitmap.o        += -Wno-unused-parameter -DETC_PERFCONFIG="BUILD_STR($(ETC_PERFCONFIG_SQ))"
 CFLAGS_find_bit.o      += -Wno-unused-parameter -DETC_PERFCONFIG="BUILD_STR($(ETC_PERFCONFIG_SQ))"
 CFLAGS_rbtree.o        += -Wno-unused-parameter -DETC_PERFCONFIG="BUILD_STR($(ETC_PERFCONFIG_SQ))"
-CFLAGS_libstring.o     += -Wno-unused-parameter -DETC_PERFCONFIG="BUILD_STR($(ETC_PERFCONFIG_SQ))"
 CFLAGS_hweight.o       += -Wno-unused-parameter -DETC_PERFCONFIG="BUILD_STR($(ETC_PERFCONFIG_SQ))"
 CFLAGS_parse-events.o  += -Wno-redundant-decls
 CFLAGS_expr.o          += -Wno-redundant-decls
@@ -309,10 +307,6 @@ $(OUTPUT)util/rbtree.o: ../lib/rbtree.c FORCE
        $(call rule_mkdir)
        $(call if_changed_dep,cc_o_c)
 
-$(OUTPUT)util/libstring.o: ../lib/string.c FORCE
-       $(call rule_mkdir)
-       $(call if_changed_dep,cc_o_c)
-
 $(OUTPUT)util/hweight.o: ../lib/hweight.c FORCE
        $(call rule_mkdir)
        $(call if_changed_dep,cc_o_c)
tools/perf/util/python-ext-sources +0 -1
index a685d20165f7..b4cbd727c8b1 100644
@@ -20,7 +20,6 @@ util/namespaces.c
 ../lib/find_bit.c
 ../lib/list_sort.c
 ../lib/hweight.c
-../lib/string.c
 ../lib/vsprintf.c
 util/thread_map.c
 util/util.c
tools/perf/util/thread_map.c +0 -53
index c9bfe4696943..7dff6e315aa9 100644
@@ -315,59 +315,6 @@ size_t thread_map__fprintf(struct perf_thread_map *threads, FILE *fp)
        return printed + fprintf(fp, "\n");
 }
 
-static int get_comm(char **comm, pid_t pid)
-{
-       char *path;
-       size_t size;
-       int err;
-
-       if (asprintf(&path, "%s/%d/comm", procfs__mountpoint(), pid) == -1)
-               return -ENOMEM;
-
-       err = filename__read_str(path, comm, &size);
-       if (!err) {
-               /*
-                * We're reading 16 bytes, while filename__read_str
-                * allocates data per BUFSIZ bytes, so we can safely
-                * mark the end of the string.
-                */
-               (*comm)[size] = 0;
-               strim(*comm);
-       }
-
-       free(path);
-       return err;
-}
-
-static void comm_init(struct perf_thread_map *map, int i)
-{
-       pid_t pid = perf_thread_map__pid(map, i);
-       char *comm = NULL;
-
-       /* dummy pid comm initialization */
-       if (pid == -1) {
-               map->map[i].comm = strdup("dummy");
-               return;
-       }
-
-       /*
-        * The comm name is like extra bonus ;-),
-        * so just warn if we fail for any reason.
-        */
-       if (get_comm(&comm, pid))
-               pr_warning("Couldn't resolve comm name for pid %d\n", pid);
-
-       map->map[i].comm = comm;
-}
-
-void thread_map__read_comms(struct perf_thread_map *threads)
-{
-       int i;
-
-       for (i = 0; i < threads->nr; ++i)
-               comm_init(threads, i);
-}
-
 static void thread_map__copy_event(struct perf_thread_map *threads,
                                   struct perf_record_thread_map *event)
 {
tools/perf/util/thread_map.h +0 -2
index 3bb860a32b8e..1f84edd38b17 100644
@@ -25,7 +25,6 @@ struct perf_thread_map *thread_map__new_by_tid_str(const char *tid_str);
 
 size_t thread_map__fprintf(struct perf_thread_map *threads, FILE *fp);
 
-void thread_map__read_comms(struct perf_thread_map *threads);
 bool thread_map__has(struct perf_thread_map *threads, pid_t pid);
 int thread_map__remove(struct perf_thread_map *threads, int idx);
 #endif /* __PERF_THREAD_MAP_H */
--

2.35.1

Signature · 2 lines
Tzvetomir (Ceco) Stoyanov
VMware Open Source Technology Center
#8 lore raw
Quoted 48 lines · Tzvetomir Stoyanov
Quoted 38 lines · Jiri Olsa
Quoted 26 lines · Tzvetomir Stoyanov
Quoted 24 lines · Arnaldo Carvalho de Melo

Em Tue, Feb 22, 2022 at 04:32:05AM +0200, Tzvetomir Stoyanov escreveu:

Quoted 12 lines · Arnaldo Carvalho de Melo
Quoted 10 lines · Jiri Olsa
Quoted 4 lines · Tzvetomir Stoyanov (VMware)

looks good, would you also find useful in your use case the comm support in thread_map? the thread_map__read_comms function? we could move it from perf to libperf

IOW: we're happy that you're working on libperf, so feel encouraged to move things from tools/perf/util/ to tools/lib/perf/ if you find a supporting use case, brownie points if you also add an entry to tools/perf/tests/, AKA 'perf test'.

Quoted 4 lines

Thanks, I see that there is a lot of functionality that could be moved from perf to libperf and which will be useful for the library users. We are going to use libperf in trace-cruncher,
https://github.com/vmware/trace-cruncher, as an interface to perf.

Cool, so as you go on adding functionality to trace cruncher and notice that something that is in tools/perf/util/ that is usable, please submit patches to move things to tools/lib/perf/, adding tests to 'perf test' as you go.

Quoted 7 lines
Quoted 3 lines

Thanks a lot!

- Arnaldo

Sure, I'll do it. I have one more question - do you plan to release the next version of libperf soon, so we can use this new functionality in trace-cruncher ?

we can discuss that, can be fast ;-)

Quoted 8 lines

btw I took a look on that and already put some change together, please take a look and feel free to use it

I just moved thread_map__read_comms, but I wonder we want it to change to return status if it's in libperf

jirka

Quoted 13 lines

Thanks Jiri, I played a little with the patch but it does not fit my use case. It resolves only PID to command, but in my case I put threads in the map. How do you handle that case in perf ? I use perf to collect samplings from a given PID, and put all threads of this process in the map, all entries from /proc/PID/task/ folder. In my logic, as I know the PID, the commands are resolved using /proc/PID/task/TID/comm files.
The other problem that I see is - there should be a way to dynamically resize and add new entries in the thread map. There are cases when new threads are spawned during the trace - these should be added in the thread map. In my code, I handle the PERF_RECORD_COMM event - resize my array and add the new entry. That can be implemented in libperf also, I think it will be very useful.

there's perf_thread_map__realloc, but it's not exported

Quoted 6 lines

Anyway, the patch is useful for PID to command resolving, if there are only PIDs in the map. It could be extended with additional APIs for TID to command resolving. Maybe we can have these APIs:
void perf_thread_map__read_pid_comms(struct perf_thread_map *threads); void perf_thread_map__read_tid_comms(pid_t pid, struct
perf_thread_map *threads);

so thread_map__read_comms just resolves everything that's in the map, so if you have thread ids in there, they should get resolved

if you expected some other functionality, like unwind pid to threads, the thread_map__new_by_pid_str function does that

jirka

#9 lore raw
In reply to Jiri Olsa
Quoted 16 lines · Jiri Olsa
Quoted 8 lines · Tzvetomir Stoyanov (VMware)

The existing API perf_thread_map__new_dummy() allocates new thread map for one thread. I couldn't find a way to reallocate the map with more threads, or to allocate a new map for more than one thread. Having multiple threads in a thread map is essential for some use cases. That's why a new API is proposed, which allocates a new thread map for given number of threads:
perf_thread_map__new_array()

Quoted 5 lines

---
v2 changes:
- Changed the new API as Arnaldo suggested - get number of threads in the map and array with initial values (optional).

Quoted 4 lines

looks good, would you also find useful in your use case the comm support in thread_map? the thread_map__read_comms function? we could move it from perf to libperf

Yes, we definitely need that functionality. I implemented that in our code, but if there is support in libperf - I prefer to use it. Thanks!

Quoted 152 lines

thanks,
jirka

Quoted 149 lines
 tools/lib/perf/Documentation/libperf.txt |  1 +
 tools/lib/perf/include/perf/threadmap.h  |  1 +
 tools/lib/perf/libperf.map               |  1 +
 tools/lib/perf/tests/test-threadmap.c    | 41 ++++++++++++++++++++++++
 tools/lib/perf/threadmap.c               | 24 ++++++++++----
 5 files changed, 61 insertions(+), 7 deletions(-)
tools/lib/perf/Documentation/libperf.txt +1 -0
index a21f733b95b1..a8f1a237931b 100644
@@ -62,6 +62,7 @@ SYNOPSIS
   struct perf_thread_map;
 
   struct perf_thread_map *perf_thread_map__new_dummy(void);
+  struct perf_thread_map *perf_thread_map__new_array(int nr_threads, pid_t *array);
 
   void perf_thread_map__set_pid(struct perf_thread_map *map, int idx, pid_t pid);
   char *perf_thread_map__comm(struct perf_thread_map *map, int idx);
tools/lib/perf/include/perf/threadmap.h +1 -0
index 58f7fbdce446..8b40e7777cea 100644
@@ -8,6 +8,7 @@
 struct perf_thread_map;
 
 LIBPERF_API struct perf_thread_map *perf_thread_map__new_dummy(void);
+LIBPERF_API struct perf_thread_map *perf_thread_map__new_array(int nr_threads, pid_t *array);
 
 LIBPERF_API void perf_thread_map__set_pid(struct perf_thread_map *map, int idx, pid_t pid);
 LIBPERF_API char *perf_thread_map__comm(struct perf_thread_map *map, int idx);
tools/lib/perf/libperf.map +1 -0
index 6fa0d651576b..190b56ae923a 100644
@@ -12,6 +12,7 @@ LIBPERF_0.0.1 {
              perf_cpu_map__empty;
              perf_cpu_map__max;
              perf_cpu_map__has;
+             perf_thread_map__new_array;
              perf_thread_map__new_dummy;
              perf_thread_map__set_pid;
              perf_thread_map__comm;
tools/lib/perf/tests/test-threadmap.c +41 -0
index 5e2a0291e94c..f728ad7002bb 100644
@@ -11,9 +11,43 @@ static int libperf_print(enum libperf_print_level level,
      return vfprintf(stderr, fmt, ap);
 }
 
+static int test_threadmap_array(int nr, pid_t *array)
+{
+     struct perf_thread_map *threads;
+     int i;
+
+     threads = perf_thread_map__new_array(nr, array);
+     __T("Failed to allocate new thread map", threads);
+
+     __T("Unexpected number of threads", perf_thread_map__nr(threads) == nr);
+
+     for (i = 0; i < nr; i++) {
+             __T("Unexpected initial value of thread",
+                 perf_thread_map__pid(threads, i) == (array ? array[i] : -1));
+     }
+
+     for (i = 1; i < nr; i++)
+             perf_thread_map__set_pid(threads, i, i * 100);
+
+     __T("Unexpected value of thread 0",
+         perf_thread_map__pid(threads, 0) == (array ? array[0] : -1));
+
+     for (i = 1; i < nr; i++) {
+             __T("Unexpected thread value",
+                 perf_thread_map__pid(threads, i) == i * 100);
+     }
+
+     perf_thread_map__put(threads);
+
+     return 0;
+}
+
+#define THREADS_NR   10
 int test_threadmap(int argc, char **argv)
 {
      struct perf_thread_map *threads;
+     pid_t thr_array[THREADS_NR];
+     int i;
 
      __T_START;
 
@@ -27,6 +61,13 @@ int test_threadmap(int argc, char **argv)
      perf_thread_map__put(threads);
      perf_thread_map__put(threads);
 
+     test_threadmap_array(THREADS_NR, NULL);
+
+     for (i = 0; i < THREADS_NR; i++)
+             thr_array[i] = i + 100;
+
+     test_threadmap_array(THREADS_NR, thr_array);
+
      __T_END;
      return tests_failed == 0 ? 0 : -1;
 }
tools/lib/perf/threadmap.c +17 -8
index 84fa07c79d00..07968f3ea093 100644
@@ -42,18 +42,28 @@ char *perf_thread_map__comm(struct perf_thread_map *map, int idx)
      return map->map[idx].comm;
 }
 
-struct perf_thread_map *perf_thread_map__new_dummy(void)
+struct perf_thread_map *perf_thread_map__new_array(int nr_threads, pid_t *array)
 {
-     struct perf_thread_map *threads = thread_map__alloc(1);
+     struct perf_thread_map *threads = thread_map__alloc(nr_threads);
+     int i;
+
+     if (!threads)
+             return NULL;
+
+     for (i = 0; i < nr_threads; i++)
+             perf_thread_map__set_pid(threads, i, array ? array[i] : -1);
+
+     threads->nr = nr_threads;
+     refcount_set(&threads->refcnt, 1);
 
-     if (threads != NULL) {
-             perf_thread_map__set_pid(threads, 0, -1);
-             threads->nr = 1;
-             refcount_set(&threads->refcnt, 1);
-     }
      return threads;
 }
 
+struct perf_thread_map *perf_thread_map__new_dummy(void)
+{
+     return perf_thread_map__new_array(1, NULL);
+}
+
 static void perf_thread_map__delete(struct perf_thread_map *threads)
 {
      if (threads) {
--

2.34.1

Signature · 2 lines
Tzvetomir (Ceco) Stoyanov
VMware Open Source Technology Center
#10 lore raw
In reply to Jiri Olsa

Em Mon, Feb 21, 2022 at 08:46:49PM +0100, Jiri Olsa escreveu:

Quoted 9 lines · Tzvetomir Stoyanov (VMware)
Quoted 8 lines

The existing API perf_thread_map__new_dummy() allocates new thread map for one thread. I couldn't find a way to reallocate the map with more threads, or to allocate a new map for more than one thread. Having multiple threads in a thread map is essential for some use cases. That's why a new API is proposed, which allocates a new thread map for given number of threads:
perf_thread_map__new_array()

Quoted 7 lines
Quoted 5 lines

---
v2 changes:
- Changed the new API as Arnaldo suggested - get number of threads in the map and array with initial values (optional).

looks good, would you also find useful in your use case the comm support

Is that an Acked-by? :-)

- Arnaldo

Quoted 155 lines

in thread_map? the thread_map__read_comms function? we could move it from perf to libperf

thanks,
jirka

Quoted 149 lines
 tools/lib/perf/Documentation/libperf.txt |  1 +
 tools/lib/perf/include/perf/threadmap.h  |  1 +
 tools/lib/perf/libperf.map               |  1 +
 tools/lib/perf/tests/test-threadmap.c    | 41 ++++++++++++++++++++++++
 tools/lib/perf/threadmap.c               | 24 ++++++++++----
 5 files changed, 61 insertions(+), 7 deletions(-)
tools/lib/perf/Documentation/libperf.txt +1 -0
index a21f733b95b1..a8f1a237931b 100644
@@ -62,6 +62,7 @@ SYNOPSIS
   struct perf_thread_map;
 
   struct perf_thread_map *perf_thread_map__new_dummy(void);
+  struct perf_thread_map *perf_thread_map__new_array(int nr_threads, pid_t *array);
 
   void perf_thread_map__set_pid(struct perf_thread_map *map, int idx, pid_t pid);
   char *perf_thread_map__comm(struct perf_thread_map *map, int idx);
tools/lib/perf/include/perf/threadmap.h +1 -0
index 58f7fbdce446..8b40e7777cea 100644
@@ -8,6 +8,7 @@
 struct perf_thread_map;
 
 LIBPERF_API struct perf_thread_map *perf_thread_map__new_dummy(void);
+LIBPERF_API struct perf_thread_map *perf_thread_map__new_array(int nr_threads, pid_t *array);
 
 LIBPERF_API void perf_thread_map__set_pid(struct perf_thread_map *map, int idx, pid_t pid);
 LIBPERF_API char *perf_thread_map__comm(struct perf_thread_map *map, int idx);
tools/lib/perf/libperf.map +1 -0
index 6fa0d651576b..190b56ae923a 100644
@@ -12,6 +12,7 @@ LIBPERF_0.0.1 {
 		perf_cpu_map__empty;
 		perf_cpu_map__max;
 		perf_cpu_map__has;
+		perf_thread_map__new_array;
 		perf_thread_map__new_dummy;
 		perf_thread_map__set_pid;
 		perf_thread_map__comm;
tools/lib/perf/tests/test-threadmap.c +41 -0
index 5e2a0291e94c..f728ad7002bb 100644
@@ -11,9 +11,43 @@ static int libperf_print(enum libperf_print_level level,
 	return vfprintf(stderr, fmt, ap);
 }
 
+static int test_threadmap_array(int nr, pid_t *array)
+{
+	struct perf_thread_map *threads;
+	int i;
+
+	threads = perf_thread_map__new_array(nr, array);
+	__T("Failed to allocate new thread map", threads);
+
+	__T("Unexpected number of threads", perf_thread_map__nr(threads) == nr);
+
+	for (i = 0; i < nr; i++) {
+		__T("Unexpected initial value of thread",
+		    perf_thread_map__pid(threads, i) == (array ? array[i] : -1));
+	}
+
+	for (i = 1; i < nr; i++)
+		perf_thread_map__set_pid(threads, i, i * 100);
+
+	__T("Unexpected value of thread 0",
+	    perf_thread_map__pid(threads, 0) == (array ? array[0] : -1));
+
+	for (i = 1; i < nr; i++) {
+		__T("Unexpected thread value",
+		    perf_thread_map__pid(threads, i) == i * 100);
+	}
+
+	perf_thread_map__put(threads);
+
+	return 0;
+}
+
+#define THREADS_NR	10
 int test_threadmap(int argc, char **argv)
 {
 	struct perf_thread_map *threads;
+	pid_t thr_array[THREADS_NR];
+	int i;
 
 	__T_START;
 
@@ -27,6 +61,13 @@ int test_threadmap(int argc, char **argv)
 	perf_thread_map__put(threads);
 	perf_thread_map__put(threads);
 
+	test_threadmap_array(THREADS_NR, NULL);
+
+	for (i = 0; i < THREADS_NR; i++)
+		thr_array[i] = i + 100;
+
+	test_threadmap_array(THREADS_NR, thr_array);
+
 	__T_END;
 	return tests_failed == 0 ? 0 : -1;
 }
tools/lib/perf/threadmap.c +17 -7
index 84fa07c79d00..07968f3ea093 100644
@@ -42,18 +42,28 @@ char *perf_thread_map__comm(struct perf_thread_map *map, int idx)
 	return map->map[idx].comm;
 }
 
-struct perf_thread_map *perf_thread_map__new_dummy(void)
+struct perf_thread_map *perf_thread_map__new_array(int nr_threads, pid_t *array)
 {
-	struct perf_thread_map *threads = thread_map__alloc(1);
+	struct perf_thread_map *threads = thread_map__alloc(nr_threads);
+	int i;
+
+	if (!threads)
+		return NULL;
+
+	for (i = 0; i < nr_threads; i++)
+		perf_thread_map__set_pid(threads, i, array ? array[i] : -1);
+
+	threads->nr = nr_threads;
+	refcount_set(&threads->refcnt, 1);
 
-	if (threads != NULL) {
-		perf_thread_map__set_pid(threads, 0, -1);
-		threads->nr = 1;
-		refcount_set(&threads->refcnt, 1);
-	}
 	return threads;
 }
 
+struct perf_thread_map *perf_thread_map__new_dummy(void)
+{
+	return perf_thread_map__new_array(1, NULL);
+}
+
 static void perf_thread_map__delete(struct perf_thread_map *threads)
 {
 	if (threads) {
Signature · 1 line
2.34.1
Signature · 1 line
- Arnaldo
Acked-by
#11 lore raw
Quoted 16 lines · Arnaldo Carvalho de Melo

Em Mon, Feb 21, 2022 at 08:46:49PM +0100, Jiri Olsa escreveu:

Quoted 15 lines · Tzvetomir Stoyanov (VMware)
Quoted 13 lines

The existing API perf_thread_map__new_dummy() allocates new thread map for one thread. I couldn't find a way to reallocate the map with more threads, or to allocate a new map for more than one thread. Having multiple threads in a thread map is essential for some use cases. That's why a new API is proposed, which allocates a new thread map for given number of threads:
perf_thread_map__new_array()

---
v2 changes:
- Changed the new API as Arnaldo suggested - get number of threads in the map and array with initial values (optional).

Quoted 3 lines
Quoted 1 line

looks good, would you also find useful in your use case the comm support

Is that an Acked-by? :-)

yes, I wasn't sure about the previous args name changes, this one is fine

thanks,
jirka

Quoted 162 lines

- Arnaldo

Quoted 155 lines

in thread_map? the thread_map__read_comms function? we could move it from perf to libperf

thanks,
jirka

Quoted 149 lines
 tools/lib/perf/Documentation/libperf.txt |  1 +
 tools/lib/perf/include/perf/threadmap.h  |  1 +
 tools/lib/perf/libperf.map               |  1 +
 tools/lib/perf/tests/test-threadmap.c    | 41 ++++++++++++++++++++++++
 tools/lib/perf/threadmap.c               | 24 ++++++++++----
 5 files changed, 61 insertions(+), 7 deletions(-)
tools/lib/perf/Documentation/libperf.txt +1 -0
index a21f733b95b1..a8f1a237931b 100644
@@ -62,6 +62,7 @@ SYNOPSIS
   struct perf_thread_map;
 
   struct perf_thread_map *perf_thread_map__new_dummy(void);
+  struct perf_thread_map *perf_thread_map__new_array(int nr_threads, pid_t *array);
 
   void perf_thread_map__set_pid(struct perf_thread_map *map, int idx, pid_t pid);
   char *perf_thread_map__comm(struct perf_thread_map *map, int idx);
tools/lib/perf/include/perf/threadmap.h +1 -0
index 58f7fbdce446..8b40e7777cea 100644
@@ -8,6 +8,7 @@
 struct perf_thread_map;
 
 LIBPERF_API struct perf_thread_map *perf_thread_map__new_dummy(void);
+LIBPERF_API struct perf_thread_map *perf_thread_map__new_array(int nr_threads, pid_t *array);
 
 LIBPERF_API void perf_thread_map__set_pid(struct perf_thread_map *map, int idx, pid_t pid);
 LIBPERF_API char *perf_thread_map__comm(struct perf_thread_map *map, int idx);
tools/lib/perf/libperf.map +1 -0
index 6fa0d651576b..190b56ae923a 100644
@@ -12,6 +12,7 @@ LIBPERF_0.0.1 {
 		perf_cpu_map__empty;
 		perf_cpu_map__max;
 		perf_cpu_map__has;
+		perf_thread_map__new_array;
 		perf_thread_map__new_dummy;
 		perf_thread_map__set_pid;
 		perf_thread_map__comm;
tools/lib/perf/tests/test-threadmap.c +41 -0
index 5e2a0291e94c..f728ad7002bb 100644
@@ -11,9 +11,43 @@ static int libperf_print(enum libperf_print_level level,
 	return vfprintf(stderr, fmt, ap);
 }
 
+static int test_threadmap_array(int nr, pid_t *array)
+{
+	struct perf_thread_map *threads;
+	int i;
+
+	threads = perf_thread_map__new_array(nr, array);
+	__T("Failed to allocate new thread map", threads);
+
+	__T("Unexpected number of threads", perf_thread_map__nr(threads) == nr);
+
+	for (i = 0; i < nr; i++) {
+		__T("Unexpected initial value of thread",
+		    perf_thread_map__pid(threads, i) == (array ? array[i] : -1));
+	}
+
+	for (i = 1; i < nr; i++)
+		perf_thread_map__set_pid(threads, i, i * 100);
+
+	__T("Unexpected value of thread 0",
+	    perf_thread_map__pid(threads, 0) == (array ? array[0] : -1));
+
+	for (i = 1; i < nr; i++) {
+		__T("Unexpected thread value",
+		    perf_thread_map__pid(threads, i) == i * 100);
+	}
+
+	perf_thread_map__put(threads);
+
+	return 0;
+}
+
+#define THREADS_NR	10
 int test_threadmap(int argc, char **argv)
 {
 	struct perf_thread_map *threads;
+	pid_t thr_array[THREADS_NR];
+	int i;
 
 	__T_START;
 
@@ -27,6 +61,13 @@ int test_threadmap(int argc, char **argv)
 	perf_thread_map__put(threads);
 	perf_thread_map__put(threads);
 
+	test_threadmap_array(THREADS_NR, NULL);
+
+	for (i = 0; i < THREADS_NR; i++)
+		thr_array[i] = i + 100;
+
+	test_threadmap_array(THREADS_NR, thr_array);
+
 	__T_END;
 	return tests_failed == 0 ? 0 : -1;
 }
tools/lib/perf/threadmap.c +17 -7
index 84fa07c79d00..07968f3ea093 100644
@@ -42,18 +42,28 @@ char *perf_thread_map__comm(struct perf_thread_map *map, int idx)
 	return map->map[idx].comm;
 }
 
-struct perf_thread_map *perf_thread_map__new_dummy(void)
+struct perf_thread_map *perf_thread_map__new_array(int nr_threads, pid_t *array)
 {
-	struct perf_thread_map *threads = thread_map__alloc(1);
+	struct perf_thread_map *threads = thread_map__alloc(nr_threads);
+	int i;
+
+	if (!threads)
+		return NULL;
+
+	for (i = 0; i < nr_threads; i++)
+		perf_thread_map__set_pid(threads, i, array ? array[i] : -1);
+
+	threads->nr = nr_threads;
+	refcount_set(&threads->refcnt, 1);
 
-	if (threads != NULL) {
-		perf_thread_map__set_pid(threads, 0, -1);
-		threads->nr = 1;
-		refcount_set(&threads->refcnt, 1);
-	}
 	return threads;
 }
 
+struct perf_thread_map *perf_thread_map__new_dummy(void)
+{
+	return perf_thread_map__new_array(1, NULL);
+}
+
 static void perf_thread_map__delete(struct perf_thread_map *threads)
 {
 	if (threads) {
Signature · 1 line
2.34.1
Signature · 1 line
- Arnaldo
#12 lore raw

Em Wed, Feb 23, 2022 at 02:31:01PM +0100, Jiri Olsa escreveu:

Quoted 17 lines · Arnaldo Carvalho de Melo
Quoted 16 lines

Em Mon, Feb 21, 2022 at 08:46:49PM +0100, Jiri Olsa escreveu:

Quoted 15 lines · Tzvetomir Stoyanov (VMware)
Quoted 13 lines

The existing API perf_thread_map__new_dummy() allocates new thread map for one thread. I couldn't find a way to reallocate the map with more threads, or to allocate a new map for more than one thread. Having multiple threads in a thread map is essential for some use cases. That's why a new API is proposed, which allocates a new thread map for given number of threads:
perf_thread_map__new_array()

---
v2 changes:
- Changed the new API as Arnaldo suggested - get number of threads in the map and array with initial values (optional).

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

looks good, would you also find useful in your use case the comm support

Is that an Acked-by? :-)

yes, I wasn't sure about the previous args name changes, this one is fine

Ok, applying this one after applying the arg name changes one, please confirm if you agree with that one.

- Arnaldo

Quoted 165 lines

thanks,
jirka

Quoted 162 lines

- Arnaldo

Quoted 155 lines

in thread_map? the thread_map__read_comms function? we could move it from perf to libperf

thanks,
jirka

Quoted 149 lines
 tools/lib/perf/Documentation/libperf.txt |  1 +
 tools/lib/perf/include/perf/threadmap.h  |  1 +
 tools/lib/perf/libperf.map               |  1 +
 tools/lib/perf/tests/test-threadmap.c    | 41 ++++++++++++++++++++++++
 tools/lib/perf/threadmap.c               | 24 ++++++++++----
 5 files changed, 61 insertions(+), 7 deletions(-)
tools/lib/perf/Documentation/libperf.txt +1 -0
index a21f733b95b1..a8f1a237931b 100644
@@ -62,6 +62,7 @@ SYNOPSIS
   struct perf_thread_map;
 
   struct perf_thread_map *perf_thread_map__new_dummy(void);
+  struct perf_thread_map *perf_thread_map__new_array(int nr_threads, pid_t *array);
 
   void perf_thread_map__set_pid(struct perf_thread_map *map, int idx, pid_t pid);
   char *perf_thread_map__comm(struct perf_thread_map *map, int idx);
tools/lib/perf/include/perf/threadmap.h +1 -0
index 58f7fbdce446..8b40e7777cea 100644
@@ -8,6 +8,7 @@
 struct perf_thread_map;
 
 LIBPERF_API struct perf_thread_map *perf_thread_map__new_dummy(void);
+LIBPERF_API struct perf_thread_map *perf_thread_map__new_array(int nr_threads, pid_t *array);
 
 LIBPERF_API void perf_thread_map__set_pid(struct perf_thread_map *map, int idx, pid_t pid);
 LIBPERF_API char *perf_thread_map__comm(struct perf_thread_map *map, int idx);
tools/lib/perf/libperf.map +1 -0
index 6fa0d651576b..190b56ae923a 100644
@@ -12,6 +12,7 @@ LIBPERF_0.0.1 {
 		perf_cpu_map__empty;
 		perf_cpu_map__max;
 		perf_cpu_map__has;
+		perf_thread_map__new_array;
 		perf_thread_map__new_dummy;
 		perf_thread_map__set_pid;
 		perf_thread_map__comm;
tools/lib/perf/tests/test-threadmap.c +41 -0
index 5e2a0291e94c..f728ad7002bb 100644
@@ -11,9 +11,43 @@ static int libperf_print(enum libperf_print_level level,
 	return vfprintf(stderr, fmt, ap);
 }
 
+static int test_threadmap_array(int nr, pid_t *array)
+{
+	struct perf_thread_map *threads;
+	int i;
+
+	threads = perf_thread_map__new_array(nr, array);
+	__T("Failed to allocate new thread map", threads);
+
+	__T("Unexpected number of threads", perf_thread_map__nr(threads) == nr);
+
+	for (i = 0; i < nr; i++) {
+		__T("Unexpected initial value of thread",
+		    perf_thread_map__pid(threads, i) == (array ? array[i] : -1));
+	}
+
+	for (i = 1; i < nr; i++)
+		perf_thread_map__set_pid(threads, i, i * 100);
+
+	__T("Unexpected value of thread 0",
+	    perf_thread_map__pid(threads, 0) == (array ? array[0] : -1));
+
+	for (i = 1; i < nr; i++) {
+		__T("Unexpected thread value",
+		    perf_thread_map__pid(threads, i) == i * 100);
+	}
+
+	perf_thread_map__put(threads);
+
+	return 0;
+}
+
+#define THREADS_NR	10
 int test_threadmap(int argc, char **argv)
 {
 	struct perf_thread_map *threads;
+	pid_t thr_array[THREADS_NR];
+	int i;
 
 	__T_START;
 
@@ -27,6 +61,13 @@ int test_threadmap(int argc, char **argv)
 	perf_thread_map__put(threads);
 	perf_thread_map__put(threads);
 
+	test_threadmap_array(THREADS_NR, NULL);
+
+	for (i = 0; i < THREADS_NR; i++)
+		thr_array[i] = i + 100;
+
+	test_threadmap_array(THREADS_NR, thr_array);
+
 	__T_END;
 	return tests_failed == 0 ? 0 : -1;
 }
tools/lib/perf/threadmap.c +17 -7
index 84fa07c79d00..07968f3ea093 100644
@@ -42,18 +42,28 @@ char *perf_thread_map__comm(struct perf_thread_map *map, int idx)
 	return map->map[idx].comm;
 }
 
-struct perf_thread_map *perf_thread_map__new_dummy(void)
+struct perf_thread_map *perf_thread_map__new_array(int nr_threads, pid_t *array)
 {
-	struct perf_thread_map *threads = thread_map__alloc(1);
+	struct perf_thread_map *threads = thread_map__alloc(nr_threads);
+	int i;
+
+	if (!threads)
+		return NULL;
+
+	for (i = 0; i < nr_threads; i++)
+		perf_thread_map__set_pid(threads, i, array ? array[i] : -1);
+
+	threads->nr = nr_threads;
+	refcount_set(&threads->refcnt, 1);
 
-	if (threads != NULL) {
-		perf_thread_map__set_pid(threads, 0, -1);
-		threads->nr = 1;
-		refcount_set(&threads->refcnt, 1);
-	}
 	return threads;
 }
 
+struct perf_thread_map *perf_thread_map__new_dummy(void)
+{
+	return perf_thread_map__new_array(1, NULL);
+}
+
 static void perf_thread_map__delete(struct perf_thread_map *threads)
 {
 	if (threads) {
Signature · 1 line
2.34.1
Signature · 1 line
- Arnaldo
Signature · 1 line
- Arnaldo
#13 lore raw
Quoted 26 lines · Arnaldo Carvalho de Melo

Em Wed, Feb 23, 2022 at 02:31:01PM +0100, Jiri Olsa escreveu:

Quoted 24 lines · Arnaldo Carvalho de Melo
Quoted 16 lines

Em Mon, Feb 21, 2022 at 08:46:49PM +0100, Jiri Olsa escreveu:

Quoted 15 lines · Tzvetomir Stoyanov (VMware)
Quoted 13 lines

The existing API perf_thread_map__new_dummy() allocates new thread map for one thread. I couldn't find a way to reallocate the map with more threads, or to allocate a new map for more than one thread. Having multiple threads in a thread map is essential for some use cases. That's why a new API is proposed, which allocates a new thread map for given number of threads:
perf_thread_map__new_array()

---
v2 changes:
- Changed the new API as Arnaldo suggested - get number of threads in the map and array with initial values (optional).

Quoted 3 lines
Quoted 1 line

looks good, would you also find useful in your use case the comm support

Is that an Acked-by? :-)

yes, I wasn't sure about the previous args name changes, this one is fine

Quoted 6 lines
Quoted 1 line

Ok, applying this one after applying the arg name changes one, please confirm if you agree with that one.

- Arnaldo

ping
Please, can you merge this one, If there are no problems with the code. We would like to use the new API perf_thread_map__new_array(). Thanks!

Quoted 165 lines
Quoted 165 lines

thanks,
jirka

Quoted 161 lines

- Arnaldo

Quoted 144 lines

in thread_map? the thread_map__read_comms function? we could move it from perf to libperf

thanks,
jirka

Quoted 138 lines
 tools/lib/perf/Documentation/libperf.txt |  1 +
 tools/lib/perf/include/perf/threadmap.h  |  1 +
 tools/lib/perf/libperf.map               |  1 +
 tools/lib/perf/tests/test-threadmap.c    | 41 ++++++++++++++++++++++++
 tools/lib/perf/threadmap.c               | 24 ++++++++++----
 5 files changed, 61 insertions(+), 7 deletions(-)
tools/lib/perf/Documentation/libperf.txt +1 -0
index a21f733b95b1..a8f1a237931b 100644
@@ -62,6 +62,7 @@ SYNOPSIS
   struct perf_thread_map;
 
   struct perf_thread_map *perf_thread_map__new_dummy(void);
+  struct perf_thread_map *perf_thread_map__new_array(int nr_threads, pid_t *array);
 
   void perf_thread_map__set_pid(struct perf_thread_map *map, int idx, pid_t pid);
   char *perf_thread_map__comm(struct perf_thread_map *map, int idx);
tools/lib/perf/include/perf/threadmap.h +1 -0
index 58f7fbdce446..8b40e7777cea 100644
@@ -8,6 +8,7 @@
 struct perf_thread_map;
 
 LIBPERF_API struct perf_thread_map *perf_thread_map__new_dummy(void);
+LIBPERF_API struct perf_thread_map *perf_thread_map__new_array(int nr_threads, pid_t *array);
 
 LIBPERF_API void perf_thread_map__set_pid(struct perf_thread_map *map, int idx, pid_t pid);
 LIBPERF_API char *perf_thread_map__comm(struct perf_thread_map *map, int idx);
tools/lib/perf/libperf.map +1 -0
index 6fa0d651576b..190b56ae923a 100644
@@ -12,6 +12,7 @@ LIBPERF_0.0.1 {
                perf_cpu_map__empty;
                perf_cpu_map__max;
                perf_cpu_map__has;
+               perf_thread_map__new_array;
                perf_thread_map__new_dummy;
                perf_thread_map__set_pid;
                perf_thread_map__comm;
tools/lib/perf/tests/test-threadmap.c +41 -0
index 5e2a0291e94c..f728ad7002bb 100644
@@ -11,9 +11,43 @@ static int libperf_print(enum libperf_print_level level,
        return vfprintf(stderr, fmt, ap);
 }
 
+static int test_threadmap_array(int nr, pid_t *array)
+{
+       struct perf_thread_map *threads;
+       int i;
+
+       threads = perf_thread_map__new_array(nr, array);
+       __T("Failed to allocate new thread map", threads);
+
+       __T("Unexpected number of threads", perf_thread_map__nr(threads) == nr);
+
+       for (i = 0; i < nr; i++) {
+               __T("Unexpected initial value of thread",
+                   perf_thread_map__pid(threads, i) == (array ? array[i] : -1));
+       }
+
+       for (i = 1; i < nr; i++)
+               perf_thread_map__set_pid(threads, i, i * 100);
+
+       __T("Unexpected value of thread 0",
+           perf_thread_map__pid(threads, 0) == (array ? array[0] : -1));
+
+       for (i = 1; i < nr; i++) {
+               __T("Unexpected thread value",
+                   perf_thread_map__pid(threads, i) == i * 100);
+       }
+
+       perf_thread_map__put(threads);
+
+       return 0;
+}
+
+#define THREADS_NR     10
 int test_threadmap(int argc, char **argv)
 {
        struct perf_thread_map *threads;
+       pid_t thr_array[THREADS_NR];
+       int i;
 
        __T_START;
 
@@ -27,6 +61,13 @@ int test_threadmap(int argc, char **argv)
        perf_thread_map__put(threads);
        perf_thread_map__put(threads);
 
+       test_threadmap_array(THREADS_NR, NULL);
+
+       for (i = 0; i < THREADS_NR; i++)
+               thr_array[i] = i + 100;
+
+       test_threadmap_array(THREADS_NR, thr_array);
+
        __T_END;
        return tests_failed == 0 ? 0 : -1;
 }
tools/lib/perf/threadmap.c +12 -7
index 84fa07c79d00..07968f3ea093 100644
@@ -42,18 +42,28 @@ char *perf_thread_map__comm(struct perf_thread_map *map, int idx)
        return map->map[idx].comm;
 }
 
-struct perf_thread_map *perf_thread_map__new_dummy(void)
+struct perf_thread_map *perf_thread_map__new_array(int nr_threads, pid_t *array)
 {
-       struct perf_thread_map *threads = thread_map__alloc(1);
+       struct perf_thread_map *threads = thread_map__alloc(nr_threads);
+       int i;
+
+       if (!threads)
+               return NULL;
+
+       for (i = 0; i < nr_threads; i++)
+               perf_thread_map__set_pid(threads, i, array ? array[i] : -1);
+
+       threads->nr = nr_threads;
+       refcount_set(&threads->refcnt, 1);
 
-       if (threads != NULL) {
-               perf_thread_map__set_pid(threads, 0, -1);
-               threads->nr = 1;
-               refcount_set(&threads->refcnt, 1);
-       }
        return threads;
 }
 
Quoted 11 lines
Quoted 11 lines
tools/lib/perf/threadmap.c +5 -1
+struct perf_thread_map *perf_thread_map__new_dummy(void)
+{
+       return perf_thread_map__new_array(1, NULL);
+}
+
 static void perf_thread_map__delete(struct perf_thread_map *threads)
 {
        if (threads) {
--

2.34.1

--

Quoted 5 lines
Quoted 1 line
Quoted 1 line

- Arnaldo

--

- Arnaldo

--
Tzvetomir (Ceco) Stoyanov
VMware Open Source Technology Center

#14 lore raw

Em Mon, Mar 21, 2022 at 01:10:49PM +0200, Tzvetomir Stoyanov escreveu:

Quoted 33 lines
Quoted 31 lines · Arnaldo Carvalho de Melo

Em Wed, Feb 23, 2022 at 02:31:01PM +0100, Jiri Olsa escreveu:

Quoted 17 lines · Arnaldo Carvalho de Melo
Quoted 16 lines

Em Mon, Feb 21, 2022 at 08:46:49PM +0100, Jiri Olsa escreveu:

Quoted 15 lines · Tzvetomir Stoyanov (VMware)
Quoted 13 lines

The existing API perf_thread_map__new_dummy() allocates new thread map for one thread. I couldn't find a way to reallocate the map with more threads, or to allocate a new map for more than one thread. Having multiple threads in a thread map is essential for some use cases. That's why a new API is proposed, which allocates a new thread map for given number of threads:
perf_thread_map__new_array()

---
v2 changes:
- Changed the new API as Arnaldo suggested - get number of threads in the map and array with initial values (optional).

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

looks good, would you also find useful in your use case the comm support

Is that an Acked-by? :-)

yes, I wasn't sure about the previous args name changes, this one is fine

Ok, applying this one after applying the arg name changes one, please confirm if you agree with that one.

Quoted 6 lines
Quoted 1 line

- Arnaldo

ping
Please, can you merge this one, If there are no problems with the code. We would like to use the new API perf_thread_map__new_array(). Thanks!

Its there already, was merged one month ago:

commit 56dce868198cd01b023e05d72bbbba7c87cc384d

Author: Tzvetomir Stoyanov (VMware) <[email protected]>
Date:   Mon Feb 21 12:26:28 2022 +0200

libperf: Add API for allocating new thread map array

Its in perf/core, will go to Linus this week.

- Arnaldo

Quoted 175 lines
Quoted 164 lines
Quoted 164 lines

thanks,
jirka

Quoted 161 lines

- Arnaldo

Quoted 144 lines

in thread_map? the thread_map__read_comms function? we could move it from perf to libperf

thanks,
jirka

Quoted 138 lines
 tools/lib/perf/Documentation/libperf.txt |  1 +
 tools/lib/perf/include/perf/threadmap.h  |  1 +
 tools/lib/perf/libperf.map               |  1 +
 tools/lib/perf/tests/test-threadmap.c    | 41 ++++++++++++++++++++++++
 tools/lib/perf/threadmap.c               | 24 ++++++++++----
 5 files changed, 61 insertions(+), 7 deletions(-)
tools/lib/perf/Documentation/libperf.txt +1 -0
index a21f733b95b1..a8f1a237931b 100644
@@ -62,6 +62,7 @@ SYNOPSIS
   struct perf_thread_map;
 
   struct perf_thread_map *perf_thread_map__new_dummy(void);
+  struct perf_thread_map *perf_thread_map__new_array(int nr_threads, pid_t *array);
 
   void perf_thread_map__set_pid(struct perf_thread_map *map, int idx, pid_t pid);
   char *perf_thread_map__comm(struct perf_thread_map *map, int idx);
tools/lib/perf/include/perf/threadmap.h +1 -0
index 58f7fbdce446..8b40e7777cea 100644
@@ -8,6 +8,7 @@
 struct perf_thread_map;
 
 LIBPERF_API struct perf_thread_map *perf_thread_map__new_dummy(void);
+LIBPERF_API struct perf_thread_map *perf_thread_map__new_array(int nr_threads, pid_t *array);
 
 LIBPERF_API void perf_thread_map__set_pid(struct perf_thread_map *map, int idx, pid_t pid);
 LIBPERF_API char *perf_thread_map__comm(struct perf_thread_map *map, int idx);
tools/lib/perf/libperf.map +1 -0
index 6fa0d651576b..190b56ae923a 100644
@@ -12,6 +12,7 @@ LIBPERF_0.0.1 {
                perf_cpu_map__empty;
                perf_cpu_map__max;
                perf_cpu_map__has;
+               perf_thread_map__new_array;
                perf_thread_map__new_dummy;
                perf_thread_map__set_pid;
                perf_thread_map__comm;
tools/lib/perf/tests/test-threadmap.c +41 -0
index 5e2a0291e94c..f728ad7002bb 100644
@@ -11,9 +11,43 @@ static int libperf_print(enum libperf_print_level level,
        return vfprintf(stderr, fmt, ap);
 }
 
+static int test_threadmap_array(int nr, pid_t *array)
+{
+       struct perf_thread_map *threads;
+       int i;
+
+       threads = perf_thread_map__new_array(nr, array);
+       __T("Failed to allocate new thread map", threads);
+
+       __T("Unexpected number of threads", perf_thread_map__nr(threads) == nr);
+
+       for (i = 0; i < nr; i++) {
+               __T("Unexpected initial value of thread",
+                   perf_thread_map__pid(threads, i) == (array ? array[i] : -1));
+       }
+
+       for (i = 1; i < nr; i++)
+               perf_thread_map__set_pid(threads, i, i * 100);
+
+       __T("Unexpected value of thread 0",
+           perf_thread_map__pid(threads, 0) == (array ? array[0] : -1));
+
+       for (i = 1; i < nr; i++) {
+               __T("Unexpected thread value",
+                   perf_thread_map__pid(threads, i) == i * 100);
+       }
+
+       perf_thread_map__put(threads);
+
+       return 0;
+}
+
+#define THREADS_NR     10
 int test_threadmap(int argc, char **argv)
 {
        struct perf_thread_map *threads;
+       pid_t thr_array[THREADS_NR];
+       int i;
 
        __T_START;
 
@@ -27,6 +61,13 @@ int test_threadmap(int argc, char **argv)
        perf_thread_map__put(threads);
        perf_thread_map__put(threads);
 
+       test_threadmap_array(THREADS_NR, NULL);
+
+       for (i = 0; i < THREADS_NR; i++)
+               thr_array[i] = i + 100;
+
+       test_threadmap_array(THREADS_NR, thr_array);
+
        __T_END;
        return tests_failed == 0 ? 0 : -1;
 }
tools/lib/perf/threadmap.c +12 -7
index 84fa07c79d00..07968f3ea093 100644
@@ -42,18 +42,28 @@ char *perf_thread_map__comm(struct perf_thread_map *map, int idx)
        return map->map[idx].comm;
 }
 
-struct perf_thread_map *perf_thread_map__new_dummy(void)
+struct perf_thread_map *perf_thread_map__new_array(int nr_threads, pid_t *array)
 {
-       struct perf_thread_map *threads = thread_map__alloc(1);
+       struct perf_thread_map *threads = thread_map__alloc(nr_threads);
+       int i;
+
+       if (!threads)
+               return NULL;
+
+       for (i = 0; i < nr_threads; i++)
+               perf_thread_map__set_pid(threads, i, array ? array[i] : -1);
+
+       threads->nr = nr_threads;
+       refcount_set(&threads->refcnt, 1);
 
-       if (threads != NULL) {
-               perf_thread_map__set_pid(threads, 0, -1);
-               threads->nr = 1;
-               refcount_set(&threads->refcnt, 1);
-       }
        return threads;
 }
 
Quoted 11 lines
Quoted 11 lines
tools/lib/perf/threadmap.c +5 -1
+struct perf_thread_map *perf_thread_map__new_dummy(void)
+{
+       return perf_thread_map__new_array(1, NULL);
+}
+
 static void perf_thread_map__delete(struct perf_thread_map *threads)
 {
        if (threads) {
--

2.34.1

--

Quoted 5 lines
Quoted 1 line
Quoted 1 line

- Arnaldo

--

- Arnaldo

--
Tzvetomir (Ceco) Stoyanov
VMware Open Source Technology Center

Signature · 1 line
- Arnaldo