1 // SPDX-License-Identifier: GPL-2.0
5 #include <perf/cpumap.h>
11 #include <linux/err.h>
13 #define TEMPL "/tmp/perf-test-XXXXXX"
16 static int get_temp(char *path)
24 perror("mkstemp failed");
32 static int session_write_header(char *path)
34 struct perf_session *session;
35 struct perf_data data = {
37 .mode = PERF_DATA_MODE_WRITE,
40 session = perf_session__new(&data, false, NULL);
41 TEST_ASSERT_VAL("can't get session", !IS_ERR(session));
43 session->evlist = evlist__new_default();
44 TEST_ASSERT_VAL("can't get evlist", session->evlist);
46 perf_header__set_feat(&session->header, HEADER_CPU_TOPOLOGY);
47 perf_header__set_feat(&session->header, HEADER_NRCPUS);
48 perf_header__set_feat(&session->header, HEADER_ARCH);
50 session->header.data_size += DATA_SIZE;
52 TEST_ASSERT_VAL("failed to write header",
53 !perf_session__write_header(session, session->evlist, data.file.fd, true));
55 perf_session__delete(session);
60 static int check_cpu_topology(char *path, struct perf_cpu_map *map)
62 struct perf_session *session;
63 struct perf_data data = {
65 .mode = PERF_DATA_MODE_READ,
68 struct aggr_cpu_id id;
70 session = perf_session__new(&data, false, NULL);
71 TEST_ASSERT_VAL("can't get session", !IS_ERR(session));
72 cpu__setup_cpunode_map();
74 /* On platforms with large numbers of CPUs process_cpu_topology()
75 * might issue an error while reading the perf.data file section
76 * HEADER_CPU_TOPOLOGY and the cpu_topology_map pointed to by member
77 * cpu is a NULL pointer.
79 * CPU 0 is on core_id 0 and physical_package_id 6
80 * CPU 1 is on core_id 1 and physical_package_id 3
82 * Core_id and physical_package_id are platform and architecture
83 * dependend and might have higher numbers than the CPU id.
84 * This actually depends on the configuration.
86 * In this case process_cpu_topology() prints error message:
87 * "socket_id number is too big. You may need to upgrade the
90 * This is the reason why this test might be skipped. aarch64 and
91 * s390 always write this part of the header, even when the above
92 * condition is true (see do_core_id_test in header.c). So always
93 * run this test on those platforms.
95 if (!session->header.env.cpu
96 && strncmp(session->header.env.arch, "s390", 4)
97 && strncmp(session->header.env.arch, "aarch64", 7))
100 TEST_ASSERT_VAL("Session header CPU map not set", session->header.env.cpu);
102 for (i = 0; i < session->header.env.nr_cpus_avail; i++) {
103 if (!cpu_map__has(map, i))
105 pr_debug("CPU %d, core %d, socket %d\n", i,
106 session->header.env.cpu[i].core_id,
107 session->header.env.cpu[i].socket_id);
110 // Test that core ID contains socket, die and core
111 for (i = 0; i < map->nr; i++) {
112 id = cpu_map__get_core(map, i, NULL);
113 TEST_ASSERT_VAL("Core map - Core ID doesn't match",
114 session->header.env.cpu[map->map[i]].core_id == cpu_map__id_to_cpu(id.id));
116 TEST_ASSERT_VAL("Core map - Socket ID doesn't match",
117 session->header.env.cpu[map->map[i]].socket_id ==
118 cpu_map__id_to_socket(id.id));
120 TEST_ASSERT_VAL("Core map - Die ID doesn't match",
121 session->header.env.cpu[map->map[i]].die_id == cpu_map__id_to_die(id.id));
124 // Test that die ID contains socket and die
125 for (i = 0; i < map->nr; i++) {
126 id = cpu_map__get_die(map, i, NULL);
127 TEST_ASSERT_VAL("Die map - Socket ID doesn't match",
128 session->header.env.cpu[map->map[i]].socket_id ==
129 cpu_map__id_to_socket(id.id << 16));
131 TEST_ASSERT_VAL("Die map - Die ID doesn't match",
132 session->header.env.cpu[map->map[i]].die_id ==
133 cpu_map__id_to_die(id.id << 16));
136 // Test that socket ID contains only socket
137 for (i = 0; i < map->nr; i++) {
138 id = cpu_map__get_socket(map, i, NULL);
139 TEST_ASSERT_VAL("Socket map - Socket ID doesn't match",
140 session->header.env.cpu[map->map[i]].socket_id == id.id);
143 // Test that node ID contains only node
144 for (i = 0; i < map->nr; i++) {
145 id = cpu_map__get_node(map, i, NULL);
146 TEST_ASSERT_VAL("Node map - Node ID doesn't match",
147 cpu__get_node(map->map[i]) == id.id);
149 perf_session__delete(session);
154 int test__session_topology(struct test *test __maybe_unused, int subtest __maybe_unused)
157 struct perf_cpu_map *map;
160 TEST_ASSERT_VAL("can't get templ file", !get_temp(path));
162 pr_debug("templ file: %s\n", path);
164 if (session_write_header(path))
167 map = perf_cpu_map__new(NULL);
169 pr_debug("failed to get system cpumap\n");
173 ret = check_cpu_topology(path, map);
174 perf_cpu_map__put(map);