Commit 56dce868 authored by Tzvetomir Stoyanov (VMware)'s avatar Tzvetomir Stoyanov (VMware) Committed by Arnaldo Carvalho de Melo
Browse files

libperf: Add API for allocating new thread map array



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: default avatarTzvetomir Stoyanov (VMware) <tz.stoyanov@gmail.com>
Acked-by: default avatarJiri 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: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
parent 41415b8a
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -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);
+1 −0
Original line number Diff line number Diff line
@@ -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);
+1 −0
Original line number Diff line number Diff line
@@ -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;
+41 −0
Original line number Diff line number Diff line
@@ -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;
}
+17 −7
Original line number Diff line number Diff line
@@ -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;

	if (threads != NULL) {
		perf_thread_map__set_pid(threads, 0, -1);
		threads->nr = 1;
	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);
	}

	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) {