Commit | Line | Data |
---|---|---|
134aa44f JO |
1 | #include <sys/types.h> |
2 | #include <unistd.h> | |
8fbc38aa | 3 | #include <sys/prctl.h> |
134aa44f JO |
4 | #include "tests.h" |
5 | #include "thread_map.h" | |
6 | #include "debug.h" | |
7 | ||
8fbc38aa JO |
8 | #define NAME (const char *) "perf" |
9 | #define NAMEUL (unsigned long) NAME | |
10 | ||
721a1f53 | 11 | int test__thread_map(int subtest __maybe_unused) |
134aa44f JO |
12 | { |
13 | struct thread_map *map; | |
14 | ||
8fbc38aa JO |
15 | TEST_ASSERT_VAL("failed to set process name", |
16 | !prctl(PR_SET_NAME, NAMEUL, 0, 0, 0)); | |
17 | ||
134aa44f JO |
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) && | |
8fbc38aa | 29 | !strcmp(thread_map__comm(map, 0), NAME)); |
35318d20 JO |
30 | TEST_ASSERT_VAL("wrong refcnt", |
31 | atomic_read(&map->refcnt) == 1); | |
134aa44f JO |
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")); | |
35318d20 JO |
45 | TEST_ASSERT_VAL("wrong refcnt", |
46 | atomic_read(&map->refcnt) == 1); | |
134aa44f JO |
47 | thread_map__put(map); |
48 | return 0; | |
49 | } | |
99471c96 JO |
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; | |
59660942 | 57 | struct thread_map *threads; |
99471c96 JO |
58 | |
59 | TEST_ASSERT_VAL("wrong nr", map->nr == 1); | |
60 | TEST_ASSERT_VAL("wrong pid", map->entries[0].pid == (u64) getpid()); | |
8fbc38aa | 61 | TEST_ASSERT_VAL("wrong comm", !strcmp(map->entries[0].comm, NAME)); |
59660942 JO |
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) && | |
8fbc38aa | 71 | !strcmp(thread_map__comm(threads, 0), NAME)); |
59660942 JO |
72 | TEST_ASSERT_VAL("wrong refcnt", |
73 | atomic_read(&threads->refcnt) == 1); | |
74 | thread_map__put(threads); | |
99471c96 JO |
75 | return 0; |
76 | } | |
77 | ||
78 | int test__thread_map_synthesize(int subtest __maybe_unused) | |
79 | { | |
80 | struct thread_map *threads; | |
81 | ||
8fbc38aa JO |
82 | TEST_ASSERT_VAL("failed to set process name", |
83 | !prctl(PR_SET_NAME, NAMEUL, 0, 0, 0)); | |
84 | ||
99471c96 JO |
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 | } |