1 // SPDX-License-Identifier: GPL-2.0
7 #include "thread_map.h"
9 #include <linux/zalloc.h>
11 #define NAME (const char *) "perf"
12 #define NAMEUL (unsigned long) NAME
14 int test__thread_map(struct test
*test __maybe_unused
, int subtest __maybe_unused
)
16 struct thread_map
*map
;
18 TEST_ASSERT_VAL("failed to set process name",
19 !prctl(PR_SET_NAME
, NAMEUL
, 0, 0, 0));
21 /* test map on current pid */
22 map
= thread_map__new_by_pid(getpid());
23 TEST_ASSERT_VAL("failed to alloc map", map
);
25 thread_map__read_comms(map
);
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);
38 map
= thread_map__new_dummy();
39 TEST_ASSERT_VAL("failed to alloc map", map
);
41 thread_map__read_comms(map
);
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);
54 static 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
)
59 struct thread_map_event
*map
= &event
->thread_map
;
60 struct thread_map
*threads
;
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
));
66 threads
= thread_map__new_event(&event
->thread_map
);
67 TEST_ASSERT_VAL("failed to alloc map", threads
);
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
);
81 int test__thread_map_synthesize(struct test
*test __maybe_unused
, int subtest __maybe_unused
)
83 struct thread_map
*threads
;
85 TEST_ASSERT_VAL("failed to set process name",
86 !prctl(PR_SET_NAME
, NAMEUL
, 0, 0, 0));
88 /* test map on current pid */
89 threads
= thread_map__new_by_pid(getpid());
90 TEST_ASSERT_VAL("failed to alloc map", threads
);
92 thread_map__read_comms(threads
);
94 TEST_ASSERT_VAL("failed to synthesize map",
95 !perf_event__synthesize_thread_map2(NULL
, threads
, process_event
, NULL
));
100 int test__thread_map_remove(struct test
*test __maybe_unused
, int subtest __maybe_unused
)
102 struct thread_map
*threads
;
106 TEST_ASSERT_VAL("failed to allocate map string",
107 asprintf(&str
, "%d,%d", getpid(), getppid()) >= 0);
109 threads
= thread_map__new_str(str
, NULL
, 0, false);
111 TEST_ASSERT_VAL("failed to allocate thread_map",
115 thread_map__fprintf(threads
, stderr
);
117 TEST_ASSERT_VAL("failed to remove thread",
118 !thread_map__remove(threads
, 0));
120 TEST_ASSERT_VAL("thread_map count != 1", threads
->nr
== 1);
123 thread_map__fprintf(threads
, stderr
);
125 TEST_ASSERT_VAL("failed to remove thread",
126 !thread_map__remove(threads
, 0));
128 TEST_ASSERT_VAL("thread_map count != 0", threads
->nr
== 0);
131 thread_map__fprintf(threads
, stderr
);
133 TEST_ASSERT_VAL("failed to not remove thread",
134 thread_map__remove(threads
, 0));
136 for (i
= 0; i
< threads
->nr
; i
++)
137 zfree(&threads
->map
[i
].comm
);