xref: /linux/tools/lib/perf/tests/test-threadmap.c (revision daa2be74b1b2302004945b2a5e32424e177cc7da)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <stdarg.h>
3 #include <stdio.h>
4 #include <perf/threadmap.h>
5 #include <internal/tests.h>
6 #include "tests.h"
7 
8 static int libperf_print(enum libperf_print_level level,
9 			 const char *fmt, va_list ap)
10 {
11 	return vfprintf(stderr, fmt, ap);
12 }
13 
14 static int test_threadmap_array(int nr, pid_t *array)
15 {
16 	struct perf_thread_map *threads;
17 	int i;
18 
19 	threads = perf_thread_map__new_array(nr, array);
20 	__T("Failed to allocate new thread map", threads);
21 
22 	__T("Unexpected number of threads", perf_thread_map__nr(threads) == nr);
23 
24 	for (i = 0; i < nr; i++) {
25 		__T("Unexpected initial value of thread",
26 		    perf_thread_map__pid(threads, i) == (array ? array[i] : -1));
27 	}
28 
29 	for (i = 1; i < nr; i++)
30 		perf_thread_map__set_pid(threads, i, i * 100);
31 
32 	__T("Unexpected value of thread 0",
33 	    perf_thread_map__pid(threads, 0) == (array ? array[0] : -1));
34 
35 	for (i = 1; i < nr; i++) {
36 		__T("Unexpected thread value",
37 		    perf_thread_map__pid(threads, i) == i * 100);
38 	}
39 
40 	perf_thread_map__put(threads);
41 
42 	return 0;
43 }
44 
45 #define THREADS_NR	10
46 int test_threadmap(int argc, char **argv)
47 {
48 	struct perf_thread_map *threads;
49 	pid_t thr_array[THREADS_NR];
50 	int i;
51 
52 	__T_START;
53 
54 	libperf_init(libperf_print);
55 
56 	threads = perf_thread_map__new_dummy();
57 	if (!threads)
58 		return -1;
59 
60 	perf_thread_map__get(threads);
61 	perf_thread_map__put(threads);
62 	perf_thread_map__put(threads);
63 
64 	test_threadmap_array(THREADS_NR, NULL);
65 
66 	for (i = 0; i < THREADS_NR; i++)
67 		thr_array[i] = i + 100;
68 
69 	test_threadmap_array(THREADS_NR, thr_array);
70 
71 	__T_END;
72 	return tests_failed == 0 ? 0 : -1;
73 }
74