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