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