1 #include <sys/types.h> 2 #include <unistd.h> 3 #include <sys/prctl.h> 4 #include "tests.h" 5 #include "thread_map.h" 6 #include "debug.h" 7 8 #define NAME (const char *) "perf" 9 #define NAMEUL (unsigned long) NAME 10 11 int test__thread_map(int subtest __maybe_unused) 12 { 13 struct thread_map *map; 14 15 TEST_ASSERT_VAL("failed to set process name", 16 !prctl(PR_SET_NAME, NAMEUL, 0, 0, 0)); 17 18 /* test map on current pid */ 19 map = thread_map__new_by_pid(getpid()); 20 TEST_ASSERT_VAL("failed to alloc map", map); 21 22 thread_map__read_comms(map); 23 24 TEST_ASSERT_VAL("wrong nr", map->nr == 1); 25 TEST_ASSERT_VAL("wrong pid", 26 thread_map__pid(map, 0) == getpid()); 27 TEST_ASSERT_VAL("wrong comm", 28 thread_map__comm(map, 0) && 29 !strcmp(thread_map__comm(map, 0), NAME)); 30 TEST_ASSERT_VAL("wrong refcnt", 31 atomic_read(&map->refcnt) == 1); 32 thread_map__put(map); 33 34 /* test dummy pid */ 35 map = thread_map__new_dummy(); 36 TEST_ASSERT_VAL("failed to alloc map", map); 37 38 thread_map__read_comms(map); 39 40 TEST_ASSERT_VAL("wrong nr", map->nr == 1); 41 TEST_ASSERT_VAL("wrong pid", thread_map__pid(map, 0) == -1); 42 TEST_ASSERT_VAL("wrong comm", 43 thread_map__comm(map, 0) && 44 !strcmp(thread_map__comm(map, 0), "dummy")); 45 TEST_ASSERT_VAL("wrong refcnt", 46 atomic_read(&map->refcnt) == 1); 47 thread_map__put(map); 48 return 0; 49 } 50 51 static int process_event(struct perf_tool *tool __maybe_unused, 52 union perf_event *event, 53 struct perf_sample *sample __maybe_unused, 54 struct machine *machine __maybe_unused) 55 { 56 struct thread_map_event *map = &event->thread_map; 57 struct thread_map *threads; 58 59 TEST_ASSERT_VAL("wrong nr", map->nr == 1); 60 TEST_ASSERT_VAL("wrong pid", map->entries[0].pid == (u64) getpid()); 61 TEST_ASSERT_VAL("wrong comm", !strcmp(map->entries[0].comm, NAME)); 62 63 threads = thread_map__new_event(&event->thread_map); 64 TEST_ASSERT_VAL("failed to alloc map", threads); 65 66 TEST_ASSERT_VAL("wrong nr", threads->nr == 1); 67 TEST_ASSERT_VAL("wrong pid", 68 thread_map__pid(threads, 0) == getpid()); 69 TEST_ASSERT_VAL("wrong comm", 70 thread_map__comm(threads, 0) && 71 !strcmp(thread_map__comm(threads, 0), NAME)); 72 TEST_ASSERT_VAL("wrong refcnt", 73 atomic_read(&threads->refcnt) == 1); 74 thread_map__put(threads); 75 return 0; 76 } 77 78 int test__thread_map_synthesize(int subtest __maybe_unused) 79 { 80 struct thread_map *threads; 81 82 TEST_ASSERT_VAL("failed to set process name", 83 !prctl(PR_SET_NAME, NAMEUL, 0, 0, 0)); 84 85 /* test map on current pid */ 86 threads = thread_map__new_by_pid(getpid()); 87 TEST_ASSERT_VAL("failed to alloc map", threads); 88 89 thread_map__read_comms(threads); 90 91 TEST_ASSERT_VAL("failed to synthesize map", 92 !perf_event__synthesize_thread_map2(NULL, threads, process_event, NULL)); 93 94 return 0; 95 } 96