perf debug: Remove needless include directives from debug.h
[linux-2.6-block.git] / tools / perf / tests / thread-map.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
38af91f0 2#include <stdlib.h>
8520a98d 3#include <string.h>
134aa44f
JO
4#include <sys/types.h>
5#include <unistd.h>
8fbc38aa 6#include <sys/prctl.h>
134aa44f
JO
7#include "tests.h"
8#include "thread_map.h"
9#include "debug.h"
8520a98d 10#include "event.h"
d8f9da24 11#include <linux/zalloc.h>
8520a98d
ACM
12#include <perf/event.h>
13
14struct perf_sample;
15struct perf_tool;
16struct machine;
134aa44f 17
8fbc38aa
JO
18#define NAME (const char *) "perf"
19#define NAMEUL (unsigned long) NAME
20
81f17c90 21int test__thread_map(struct test *test __maybe_unused, int subtest __maybe_unused)
134aa44f 22{
9749b90e 23 struct perf_thread_map *map;
134aa44f 24
8fbc38aa
JO
25 TEST_ASSERT_VAL("failed to set process name",
26 !prctl(PR_SET_NAME, NAMEUL, 0, 0, 0));
27
134aa44f
JO
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",
a2f354e3 36 perf_thread_map__pid(map, 0) == getpid());
134aa44f 37 TEST_ASSERT_VAL("wrong comm",
7836e52e
JO
38 perf_thread_map__comm(map, 0) &&
39 !strcmp(perf_thread_map__comm(map, 0), NAME));
35318d20 40 TEST_ASSERT_VAL("wrong refcnt",
364fed35 41 refcount_read(&map->refcnt) == 1);
7836e52e 42 perf_thread_map__put(map);
134aa44f
JO
43
44 /* test dummy pid */
4b49cce2 45 map = perf_thread_map__new_dummy();
134aa44f
JO
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);
a2f354e3 51 TEST_ASSERT_VAL("wrong pid", perf_thread_map__pid(map, 0) == -1);
134aa44f 52 TEST_ASSERT_VAL("wrong comm",
7836e52e
JO
53 perf_thread_map__comm(map, 0) &&
54 !strcmp(perf_thread_map__comm(map, 0), "dummy"));
35318d20 55 TEST_ASSERT_VAL("wrong refcnt",
364fed35 56 refcount_read(&map->refcnt) == 1);
7836e52e 57 perf_thread_map__put(map);
134aa44f
JO
58 return 0;
59}
99471c96
JO
60
61static 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{
72932371 66 struct perf_record_thread_map *map = &event->thread_map;
9749b90e 67 struct perf_thread_map *threads;
99471c96
JO
68
69 TEST_ASSERT_VAL("wrong nr", map->nr == 1);
70 TEST_ASSERT_VAL("wrong pid", map->entries[0].pid == (u64) getpid());
8fbc38aa 71 TEST_ASSERT_VAL("wrong comm", !strcmp(map->entries[0].comm, NAME));
59660942
JO
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",
a2f354e3 78 perf_thread_map__pid(threads, 0) == getpid());
59660942 79 TEST_ASSERT_VAL("wrong comm",
7836e52e
JO
80 perf_thread_map__comm(threads, 0) &&
81 !strcmp(perf_thread_map__comm(threads, 0), NAME));
59660942 82 TEST_ASSERT_VAL("wrong refcnt",
364fed35 83 refcount_read(&threads->refcnt) == 1);
7836e52e 84 perf_thread_map__put(threads);
99471c96
JO
85 return 0;
86}
87
81f17c90 88int test__thread_map_synthesize(struct test *test __maybe_unused, int subtest __maybe_unused)
99471c96 89{
9749b90e 90 struct perf_thread_map *threads;
99471c96 91
8fbc38aa
JO
92 TEST_ASSERT_VAL("failed to set process name",
93 !prctl(PR_SET_NAME, NAMEUL, 0, 0, 0));
94
99471c96
JO
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}
38af91f0 106
81f17c90 107int test__thread_map_remove(struct test *test __maybe_unused, int subtest __maybe_unused)
38af91f0 108{
9749b90e 109 struct perf_thread_map *threads;
38af91f0
JO
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
73c0ca1e 116 threads = thread_map__new_str(str, NULL, 0, false);
38af91f0
JO
117
118 TEST_ASSERT_VAL("failed to allocate thread_map",
119 threads);
120
bb963e16 121 if (verbose > 0)
38af91f0
JO
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
bb963e16 129 if (verbose > 0)
38af91f0
JO
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
bb963e16 137 if (verbose > 0)
38af91f0
JO
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++)
d8f9da24 144 zfree(&threads->map[i].comm);
38af91f0
JO
145
146 free(threads);
147 return 0;
148}