1 // SPDX-License-Identifier: GPL-2.0
5 #include <perf/cpumap.h>
12 #define TEMPL "/tmp/perf-test-XXXXXX"
15 static int get_temp(char *path)
23 perror("mkstemp failed");
31 static int session_write_header(char *path)
33 struct perf_session *session;
34 struct perf_data data = {
38 .mode = PERF_DATA_MODE_WRITE,
41 session = perf_session__new(&data, false, NULL);
42 TEST_ASSERT_VAL("can't get session", session);
44 session->evlist = perf_evlist__new_default();
45 TEST_ASSERT_VAL("can't get evlist", session->evlist);
47 perf_header__set_feat(&session->header, HEADER_CPU_TOPOLOGY);
48 perf_header__set_feat(&session->header, HEADER_NRCPUS);
49 perf_header__set_feat(&session->header, HEADER_ARCH);
51 session->header.data_size += DATA_SIZE;
53 TEST_ASSERT_VAL("failed to write header",
54 !perf_session__write_header(session, session->evlist, data.file.fd, true));
56 perf_session__delete(session);
61 static int check_cpu_topology(char *path, struct perf_cpu_map *map)
63 struct perf_session *session;
64 struct perf_data data = {
68 .mode = PERF_DATA_MODE_READ,
72 session = perf_session__new(&data, false, NULL);
73 TEST_ASSERT_VAL("can't get session", session);
75 /* On platforms with large numbers of CPUs process_cpu_topology()
76 * might issue an error while reading the perf.data file section
77 * HEADER_CPU_TOPOLOGY and the cpu_topology_map pointed to by member
78 * cpu is a NULL pointer.
80 * CPU 0 is on core_id 0 and physical_package_id 6
81 * CPU 1 is on core_id 1 and physical_package_id 3
83 * Core_id and physical_package_id are platform and architecture
84 * dependend and might have higher numbers than the CPU id.
85 * This actually depends on the configuration.
87 * In this case process_cpu_topology() prints error message:
88 * "socket_id number is too big. You may need to upgrade the
91 * This is the reason why this test might be skipped.
93 if (!session->header.env.cpu)
96 for (i = 0; i < session->header.env.nr_cpus_avail; i++) {
97 if (!cpu_map__has(map, i))
99 pr_debug("CPU %d, core %d, socket %d\n", i,
100 session->header.env.cpu[i].core_id,
101 session->header.env.cpu[i].socket_id);
104 for (i = 0; i < map->nr; i++) {
105 TEST_ASSERT_VAL("Core ID doesn't match",
106 (session->header.env.cpu[map->map[i]].core_id == (cpu_map__get_core(map, i, NULL) & 0xffff)));
108 TEST_ASSERT_VAL("Socket ID doesn't match",
109 (session->header.env.cpu[map->map[i]].socket_id == cpu_map__get_socket(map, i, NULL)));
112 perf_session__delete(session);
117 int test__session_topology(struct test *test __maybe_unused, int subtest __maybe_unused)
120 struct perf_cpu_map *map;
123 TEST_ASSERT_VAL("can't get templ file", !get_temp(path));
125 pr_debug("templ file: %s\n", path);
127 if (session_write_header(path))
130 map = perf_cpu_map__new(NULL);
132 pr_debug("failed to get system cpumap\n");
136 ret = check_cpu_topology(path, map);
137 perf_cpu_map__put(map);