libperf: Add API for allocating new thread map array
authorTzvetomir Stoyanov (VMware) <tz.stoyanov@gmail.com>
Mon, 21 Feb 2022 10:26:28 +0000 (12:26 +0200)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Wed, 23 Feb 2022 17:40:23 +0000 (14:40 -0300)
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()

Signed-off-by: Tzvetomir Stoyanov (VMware) <tz.stoyanov@gmail.com>
Acked-by: Jiri Olsa <jolsa@kernel.org>
Cc: Ian Rogers <irogers@google.com>
Link: https://lore.kernel.org/linux-perf-users/20220221102628.43904-1-tz.stoyanov@gmail.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/lib/perf/Documentation/libperf.txt
tools/lib/perf/include/perf/threadmap.h
tools/lib/perf/libperf.map
tools/lib/perf/tests/test-threadmap.c
tools/lib/perf/threadmap.c

index a21f733b95b17ace341324beada7c949d97cf6b2..a8f1a237931b19b182d5a197f7ce6fe4ec019351 100644 (file)
@@ -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);
index 58f7fbdce4463d7989073aab517df43f7700b766..8b40e7777cea7d9ce435f66bd4050a88924eca61 100644 (file)
@@ -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);
index 6fa0d651576b6957d5d0c1d7fd93c1769470b2cf..190b56ae923addf23446aedbe4215c124bc4cb53 100644 (file)
@@ -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;
index 5e2a0291e94c2ba85c62be79f90a3bc7fde5194f..f728ad7002bb9ee5996846c3009167b0a463d3fb 100644 (file)
@@ -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;
 }
index 84fa07c79d00703f1328a275426e1f3f526d11ad..07968f3ea0931ceb0a4358f14a75f416e55db484 100644 (file)
@@ -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) {