1 // SPDX-License-Identifier: GPL-2.0 2 #include <stdlib.h> 3 #include <string.h> 4 #include <sys/types.h> 5 #include <unistd.h> 6 #include <sys/prctl.h> 7 #include "tests.h" 8 #include "thread_map.h" 9 #include "debug.h" 10 #include "event.h" 11 #include <linux/zalloc.h> 12 #include <perf/event.h> 13 14 struct perf_sample; 15 struct perf_tool; 16 struct machine; 17 18 #define NAME (const char *) "perf" 19 #define NAMEUL (unsigned long) NAME 20 21 int test__thread_map(struct test *test __maybe_unused, int subtest __maybe_unused) 22 { 23 struct perf_thread_map *map; 24 25 TEST_ASSERT_VAL("failed to set process name", 26 !prctl(PR_SET_NAME, NAMEUL, 0, 0, 0)); 27 28 /* test map on current pid */ 29 map = thread_map__new_by_pid(getpid()); 30 TEST_ASSERT_VAL("failed to alloc map", map); 31 32 thread_map__read_comms(map); 33 34 TEST_ASSERT_VAL("wrong nr", map->nr == 1); 35 TEST_ASSERT_VAL("wrong pid", 36 perf_thread_map__pid(map, 0) == getpid()); 37 TEST_ASSERT_VAL("wrong comm", 38 perf_thread_map__comm(map, 0) && 39 !strcmp(perf_thread_map__comm(map, 0), NAME)); 40 TEST_ASSERT_VAL("wrong refcnt", 41 refcount_read(&map->refcnt) == 1); 42 perf_thread_map__put(map); 43 44 /* test dummy pid */ 45 map = perf_thread_map__new_dummy(); 46 TEST_ASSERT_VAL("failed to alloc map", map); 47 48 thread_map__read_comms(map); 49 50 TEST_ASSERT_VAL("wrong nr", map->nr == 1); 51 TEST_ASSERT_VAL("wrong pid", perf_thread_map__pid(map, 0) == -1); 52 TEST_ASSERT_VAL("wrong comm", 53 perf_thread_map__comm(map, 0) && 54 !strcmp(perf_thread_map__comm(map, 0), "dummy")); 55 TEST_ASSERT_VAL("wrong refcnt", 56 refcount_read(&map->refcnt) == 1); 57 perf_thread_map__put(map); 58 return 0; 59 } 60 61 static int process_event(struct perf_tool *tool __maybe_unused, 62 union perf_event *event, 63 struct perf_sample *sample __maybe_unused, 64 struct machine *machine __maybe_unused) 65 { 66 struct perf_record_thread_map *map = &event->thread_map; 67 struct perf_thread_map *threads; 68 69 TEST_ASSERT_VAL("wrong nr", map->nr == 1); 70 TEST_ASSERT_VAL("wrong pid", map->entries[0].pid == (u64) getpid()); 71 TEST_ASSERT_VAL("wrong comm", !strcmp(map->entries[0].comm, NAME)); 72 73 threads = thread_map__new_event(&event->thread_map); 74 TEST_ASSERT_VAL("failed to alloc map", threads); 75 76 TEST_ASSERT_VAL("wrong nr", threads->nr == 1); 77 TEST_ASSERT_VAL("wrong pid", 78 perf_thread_map__pid(threads, 0) == getpid()); 79 TEST_ASSERT_VAL("wrong comm", 80 perf_thread_map__comm(threads, 0) && 81 !strcmp(perf_thread_map__comm(threads, 0), NAME)); 82 TEST_ASSERT_VAL("wrong refcnt", 83 refcount_read(&threads->refcnt) == 1); 84 perf_thread_map__put(threads); 85 return 0; 86 } 87 88 int test__thread_map_synthesize(struct test *test __maybe_unused, int subtest __maybe_unused) 89 { 90 struct perf_thread_map *threads; 91 92 TEST_ASSERT_VAL("failed to set process name", 93 !prctl(PR_SET_NAME, NAMEUL, 0, 0, 0)); 94 95 /* test map on current pid */ 96 threads = thread_map__new_by_pid(getpid()); 97 TEST_ASSERT_VAL("failed to alloc map", threads); 98 99 thread_map__read_comms(threads); 100 101 TEST_ASSERT_VAL("failed to synthesize map", 102 !perf_event__synthesize_thread_map2(NULL, threads, process_event, NULL)); 103 104 return 0; 105 } 106 107 int test__thread_map_remove(struct test *test __maybe_unused, int subtest __maybe_unused) 108 { 109 struct perf_thread_map *threads; 110 char *str; 111 int i; 112 113 TEST_ASSERT_VAL("failed to allocate map string", 114 asprintf(&str, "%d,%d", getpid(), getppid()) >= 0); 115 116 threads = thread_map__new_str(str, NULL, 0, false); 117 118 TEST_ASSERT_VAL("failed to allocate thread_map", 119 threads); 120 121 if (verbose > 0) 122 thread_map__fprintf(threads, stderr); 123 124 TEST_ASSERT_VAL("failed to remove thread", 125 !thread_map__remove(threads, 0)); 126 127 TEST_ASSERT_VAL("thread_map count != 1", threads->nr == 1); 128 129 if (verbose > 0) 130 thread_map__fprintf(threads, stderr); 131 132 TEST_ASSERT_VAL("failed to remove thread", 133 !thread_map__remove(threads, 0)); 134 135 TEST_ASSERT_VAL("thread_map count != 0", threads->nr == 0); 136 137 if (verbose > 0) 138 thread_map__fprintf(threads, stderr); 139 140 TEST_ASSERT_VAL("failed to not remove thread", 141 thread_map__remove(threads, 0)); 142 143 for (i = 0; i < threads->nr; i++) 144 zfree(&threads->map[i].comm); 145 146 free(threads); 147 return 0; 148 } 149