1 // SPDX-License-Identifier: GPL-2.0
5 #include <perf/cpumap.h>
12 #include <linux/err.h>
14 #define TEMPL "/tmp/perf-test-XXXXXX"
17 static int get_temp(char *path)
25 perror("mkstemp failed");
33 static int session_write_header(char *path)
35 struct perf_session *session;
36 struct perf_data data = {
38 .mode = PERF_DATA_MODE_WRITE,
41 session = perf_session__new(&data, NULL);
42 TEST_ASSERT_VAL("can't get session", !IS_ERR(session));
44 if (!perf_pmu__has_hybrid()) {
45 session->evlist = evlist__new_default();
46 TEST_ASSERT_VAL("can't get evlist", session->evlist);
48 struct parse_events_error err;
50 session->evlist = evlist__new();
51 TEST_ASSERT_VAL("can't get evlist", session->evlist);
52 parse_events_error__init(&err);
53 parse_events(session->evlist, "cpu_core/cycles/", &err);
54 parse_events_error__exit(&err);
57 perf_header__set_feat(&session->header, HEADER_CPU_TOPOLOGY);
58 perf_header__set_feat(&session->header, HEADER_NRCPUS);
59 perf_header__set_feat(&session->header, HEADER_ARCH);
61 session->header.data_size += DATA_SIZE;
63 TEST_ASSERT_VAL("failed to write header",
64 !perf_session__write_header(session, session->evlist, data.file.fd, true));
66 evlist__delete(session->evlist);
67 perf_session__delete(session);
72 static int check_cpu_topology(char *path, struct perf_cpu_map *map)
74 struct perf_session *session;
75 struct perf_data data = {
77 .mode = PERF_DATA_MODE_READ,
80 struct aggr_cpu_id id;
82 session = perf_session__new(&data, NULL);
83 TEST_ASSERT_VAL("can't get session", !IS_ERR(session));
84 cpu__setup_cpunode_map();
86 /* On platforms with large numbers of CPUs process_cpu_topology()
87 * might issue an error while reading the perf.data file section
88 * HEADER_CPU_TOPOLOGY and the cpu_topology_map pointed to by member
89 * cpu is a NULL pointer.
91 * CPU 0 is on core_id 0 and physical_package_id 6
92 * CPU 1 is on core_id 1 and physical_package_id 3
94 * Core_id and physical_package_id are platform and architecture
95 * dependent and might have higher numbers than the CPU id.
96 * This actually depends on the configuration.
98 * In this case process_cpu_topology() prints error message:
99 * "socket_id number is too big. You may need to upgrade the
102 * This is the reason why this test might be skipped. aarch64 and
103 * s390 always write this part of the header, even when the above
104 * condition is true (see do_core_id_test in header.c). So always
105 * run this test on those platforms.
107 if (!session->header.env.cpu
108 && strncmp(session->header.env.arch, "s390", 4)
109 && strncmp(session->header.env.arch, "aarch64", 7))
112 TEST_ASSERT_VAL("Session header CPU map not set", session->header.env.cpu);
114 for (i = 0; i < session->header.env.nr_cpus_avail; i++) {
115 if (!cpu_map__has(map, i))
117 pr_debug("CPU %d, core %d, socket %d\n", i,
118 session->header.env.cpu[i].core_id,
119 session->header.env.cpu[i].socket_id);
122 // Test that core ID contains socket, die and core
123 for (i = 0; i < map->nr; i++) {
124 id = cpu_map__get_core(map, i, NULL);
125 TEST_ASSERT_VAL("Core map - Core ID doesn't match",
126 session->header.env.cpu[map->map[i]].core_id == id.core);
128 TEST_ASSERT_VAL("Core map - Socket ID doesn't match",
129 session->header.env.cpu[map->map[i]].socket_id == id.socket);
131 TEST_ASSERT_VAL("Core map - Die ID doesn't match",
132 session->header.env.cpu[map->map[i]].die_id == id.die);
133 TEST_ASSERT_VAL("Core map - Node ID is set", id.node == -1);
134 TEST_ASSERT_VAL("Core map - Thread is set", id.thread == -1);
137 // Test that die ID contains socket and die
138 for (i = 0; i < map->nr; i++) {
139 id = cpu_map__get_die(map, i, NULL);
140 TEST_ASSERT_VAL("Die map - Socket ID doesn't match",
141 session->header.env.cpu[map->map[i]].socket_id == id.socket);
143 TEST_ASSERT_VAL("Die map - Die ID doesn't match",
144 session->header.env.cpu[map->map[i]].die_id == id.die);
146 TEST_ASSERT_VAL("Die map - Node ID is set", id.node == -1);
147 TEST_ASSERT_VAL("Die map - Core is set", id.core == -1);
148 TEST_ASSERT_VAL("Die map - Thread is set", id.thread == -1);
151 // Test that socket ID contains only socket
152 for (i = 0; i < map->nr; i++) {
153 id = cpu_map__get_socket(map, i, NULL);
154 TEST_ASSERT_VAL("Socket map - Socket ID doesn't match",
155 session->header.env.cpu[map->map[i]].socket_id == id.socket);
157 TEST_ASSERT_VAL("Socket map - Node ID is set", id.node == -1);
158 TEST_ASSERT_VAL("Socket map - Die ID is set", id.die == -1);
159 TEST_ASSERT_VAL("Socket map - Core is set", id.core == -1);
160 TEST_ASSERT_VAL("Socket map - Thread is set", id.thread == -1);
163 // Test that node ID contains only node
164 for (i = 0; i < map->nr; i++) {
165 id = cpu_map__get_node(map, i, NULL);
166 TEST_ASSERT_VAL("Node map - Node ID doesn't match",
167 cpu__get_node(map->map[i]) == id.node);
168 TEST_ASSERT_VAL("Node map - Socket is set", id.socket == -1);
169 TEST_ASSERT_VAL("Node map - Die ID is set", id.die == -1);
170 TEST_ASSERT_VAL("Node map - Core is set", id.core == -1);
171 TEST_ASSERT_VAL("Node map - Thread is set", id.thread == -1);
173 perf_session__delete(session);
178 int test__session_topology(struct test *test __maybe_unused, int subtest __maybe_unused)
181 struct perf_cpu_map *map;
184 TEST_ASSERT_VAL("can't get templ file", !get_temp(path));
186 pr_debug("templ file: %s\n", path);
188 if (session_write_header(path))
191 map = perf_cpu_map__new(NULL);
193 pr_debug("failed to get system cpumap\n");
197 ret = check_cpu_topology(path, map);
198 perf_cpu_map__put(map);