Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include <stdarg.h>
0003 #include <stdio.h>
0004 #include <perf/threadmap.h>
0005 #include <internal/tests.h>
0006 #include "tests.h"
0007 
0008 static int libperf_print(enum libperf_print_level level,
0009              const char *fmt, va_list ap)
0010 {
0011     return vfprintf(stderr, fmt, ap);
0012 }
0013 
0014 static int test_threadmap_array(int nr, pid_t *array)
0015 {
0016     struct perf_thread_map *threads;
0017     int i;
0018 
0019     threads = perf_thread_map__new_array(nr, array);
0020     __T("Failed to allocate new thread map", threads);
0021 
0022     __T("Unexpected number of threads", perf_thread_map__nr(threads) == nr);
0023 
0024     for (i = 0; i < nr; i++) {
0025         __T("Unexpected initial value of thread",
0026             perf_thread_map__pid(threads, i) == (array ? array[i] : -1));
0027     }
0028 
0029     for (i = 1; i < nr; i++)
0030         perf_thread_map__set_pid(threads, i, i * 100);
0031 
0032     __T("Unexpected value of thread 0",
0033         perf_thread_map__pid(threads, 0) == (array ? array[0] : -1));
0034 
0035     for (i = 1; i < nr; i++) {
0036         __T("Unexpected thread value",
0037             perf_thread_map__pid(threads, i) == i * 100);
0038     }
0039 
0040     perf_thread_map__put(threads);
0041 
0042     return 0;
0043 }
0044 
0045 #define THREADS_NR  10
0046 int test_threadmap(int argc, char **argv)
0047 {
0048     struct perf_thread_map *threads;
0049     pid_t thr_array[THREADS_NR];
0050     int i;
0051 
0052     __T_START;
0053 
0054     libperf_init(libperf_print);
0055 
0056     threads = perf_thread_map__new_dummy();
0057     if (!threads)
0058         return -1;
0059 
0060     perf_thread_map__get(threads);
0061     perf_thread_map__put(threads);
0062     perf_thread_map__put(threads);
0063 
0064     test_threadmap_array(THREADS_NR, NULL);
0065 
0066     for (i = 0; i < THREADS_NR; i++)
0067         thr_array[i] = i + 100;
0068 
0069     test_threadmap_array(THREADS_NR, thr_array);
0070 
0071     __T_END;
0072     return tests_failed == 0 ? 0 : -1;
0073 }