xref: /linux/tools/perf/tests/topology.c (revision d5312a7ef79f5de574bce7b140ea1e48ce7e9262)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <string.h>
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <perf/cpumap.h>
6 #include "cpumap.h"
7 #include "tests.h"
8 #include "session.h"
9 #include "evlist.h"
10 #include "debug.h"
11 #include "pmus.h"
12 #include "target.h"
13 #include <linux/err.h>
14 #include "dwarf-regs.h"
15 
16 #define TEMPL "/tmp/perf-test-XXXXXX"
17 #define DATA_SIZE	10
18 
19 static int get_temp(char *path)
20 {
21 	int fd;
22 
23 	strcpy(path, TEMPL);
24 
25 	fd = mkstemp(path);
26 	if (fd < 0) {
27 		perror("mkstemp failed");
28 		return -1;
29 	}
30 
31 	close(fd);
32 	return 0;
33 }
34 
35 static int session_write_header(char *path)
36 {
37 	struct perf_session *session;
38 	struct perf_data data = {
39 		.path = path,
40 		.mode = PERF_DATA_MODE_WRITE,
41 	};
42 	struct target target = {};
43 
44 	session = perf_session__new(&data, NULL);
45 	TEST_ASSERT_VAL("can't get session", !IS_ERR(session));
46 
47 	session->evlist = evlist__new_default(&target, /*sample_callchains=*/false);
48 	TEST_ASSERT_VAL("can't get evlist", session->evlist);
49 	session->evlist->session = session;
50 
51 	perf_header__set_feat(&session->header, HEADER_CPU_TOPOLOGY);
52 	perf_header__set_feat(&session->header, HEADER_NRCPUS);
53 	perf_header__set_feat(&session->header, HEADER_ARCH);
54 
55 	session->header.data_size += DATA_SIZE;
56 
57 	TEST_ASSERT_VAL("failed to write header",
58 			!perf_session__write_header(session, session->evlist,
59 						    perf_data__fd(&data), true));
60 
61 	evlist__delete(session->evlist);
62 	perf_session__delete(session);
63 
64 	return 0;
65 }
66 
67 static int check_cpu_topology(char *path, struct perf_cpu_map *map)
68 {
69 	struct perf_session *session;
70 	struct perf_data data = {
71 		.path = path,
72 		.mode = PERF_DATA_MODE_READ,
73 	};
74 	unsigned int i;
75 	struct aggr_cpu_id id;
76 	struct perf_cpu cpu;
77 	struct perf_env *env;
78 	uint16_t e_machine;
79 
80 	session = perf_session__new(&data, NULL);
81 	TEST_ASSERT_VAL("can't get session", !IS_ERR(session));
82 	env = perf_session__env(session);
83 	cpu__setup_cpunode_map();
84 
85 	/* On platforms with large numbers of CPUs process_cpu_topology()
86 	 * might issue an error while reading the perf.data file section
87 	 * HEADER_CPU_TOPOLOGY and the cpu_topology_map pointed to by member
88 	 * cpu is a NULL pointer.
89 	 * Example: On s390
90 	 *   CPU 0 is on core_id 0 and physical_package_id 6
91 	 *   CPU 1 is on core_id 1 and physical_package_id 3
92 	 *
93 	 *   Core_id and physical_package_id are platform and architecture
94 	 *   dependent and might have higher numbers than the CPU id.
95 	 *   This actually depends on the configuration.
96 	 *
97 	 *  In this case process_cpu_topology() prints error message:
98 	 *  "socket_id number is too big. You may need to upgrade the
99 	 *  perf tool."
100 	 *
101 	 *  This is the reason why this test might be skipped. aarch64 and
102 	 *  s390 always write this part of the header, even when the above
103 	 *  condition is true (see do_core_id_test in header.c). So always
104 	 *  run this test on those platforms.
105 	 */
106 	e_machine = perf_env__e_machine(env, NULL);
107 
108 	if (!env->cpu && e_machine != EM_S390 && e_machine != EM_AARCH64)
109 		return TEST_SKIP;
110 
111 	/*
112 	 * In powerpc pSeries platform, not all the topology information
113 	 * are exposed via sysfs. Due to restriction, detail like
114 	 * physical_package_id will be set to -1. Hence skip this
115 	 * test if physical_package_id returns -1 for cpu from perf_cpu_map.
116 	 */
117 	if (e_machine == EM_PPC64) {
118 		if (cpu__get_socket_id(perf_cpu_map__cpu(map, 0)) == -1)
119 			return TEST_SKIP;
120 	}
121 
122 	TEST_ASSERT_VAL("Session header CPU map not set", env->cpu);
123 
124 	for (i = 0; i < (unsigned int)env->nr_cpus_avail; i++) {
125 		cpu.cpu = i;
126 		if (!perf_cpu_map__has(map, cpu))
127 			continue;
128 		pr_debug("CPU %d, core %d, socket %d\n", i,
129 			 env->cpu[i].core_id,
130 			 env->cpu[i].socket_id);
131 	}
132 
133 	// Test that CPU ID contains socket, die, core and CPU
134 	perf_cpu_map__for_each_cpu(cpu, i, map) {
135 		id = aggr_cpu_id__cpu(cpu, NULL);
136 		TEST_ASSERT_VAL("Cpu map - CPU ID doesn't match",
137 				cpu.cpu == id.cpu.cpu);
138 
139 		TEST_ASSERT_VAL("Cpu map - Core ID doesn't match",
140 			env->cpu[cpu.cpu].core_id == id.core);
141 		TEST_ASSERT_VAL("Cpu map - Socket ID doesn't match",
142 			env->cpu[cpu.cpu].socket_id == id.socket);
143 
144 		TEST_ASSERT_VAL("Cpu map - Die ID doesn't match",
145 			env->cpu[cpu.cpu].die_id == id.die);
146 		TEST_ASSERT_VAL("Cpu map - Node ID is set", id.node == -1);
147 		TEST_ASSERT_VAL("Cpu map - Thread IDX is set", id.thread_idx == -1);
148 	}
149 
150 	// Test that core ID contains socket, die and core
151 	perf_cpu_map__for_each_cpu(cpu, i, map) {
152 		id = aggr_cpu_id__core(cpu, NULL);
153 		TEST_ASSERT_VAL("Core map - Core ID doesn't match",
154 			env->cpu[cpu.cpu].core_id == id.core);
155 
156 		TEST_ASSERT_VAL("Core map - Socket ID doesn't match",
157 			env->cpu[cpu.cpu].socket_id == id.socket);
158 
159 		TEST_ASSERT_VAL("Core map - Die ID doesn't match",
160 			env->cpu[cpu.cpu].die_id == id.die);
161 		TEST_ASSERT_VAL("Core map - Node ID is set", id.node == -1);
162 		TEST_ASSERT_VAL("Core map - Thread IDX is set", id.thread_idx == -1);
163 	}
164 
165 	// Test that die ID contains socket and die
166 	perf_cpu_map__for_each_cpu(cpu, i, map) {
167 		id = aggr_cpu_id__die(cpu, NULL);
168 		TEST_ASSERT_VAL("Die map - Socket ID doesn't match",
169 			env->cpu[cpu.cpu].socket_id == id.socket);
170 
171 		TEST_ASSERT_VAL("Die map - Die ID doesn't match",
172 			env->cpu[cpu.cpu].die_id == id.die);
173 
174 		TEST_ASSERT_VAL("Die map - Node ID is set", id.node == -1);
175 		TEST_ASSERT_VAL("Die map - Core is set", id.core == -1);
176 		TEST_ASSERT_VAL("Die map - CPU is set", id.cpu.cpu == -1);
177 		TEST_ASSERT_VAL("Die map - Thread IDX is set", id.thread_idx == -1);
178 	}
179 
180 	// Test that socket ID contains only socket
181 	perf_cpu_map__for_each_cpu(cpu, i, map) {
182 		id = aggr_cpu_id__socket(cpu, NULL);
183 		TEST_ASSERT_VAL("Socket map - Socket ID doesn't match",
184 			env->cpu[cpu.cpu].socket_id == id.socket);
185 
186 		TEST_ASSERT_VAL("Socket map - Node ID is set", id.node == -1);
187 		TEST_ASSERT_VAL("Socket map - Die ID is set", id.die == -1);
188 		TEST_ASSERT_VAL("Socket map - Core is set", id.core == -1);
189 		TEST_ASSERT_VAL("Socket map - CPU is set", id.cpu.cpu == -1);
190 		TEST_ASSERT_VAL("Socket map - Thread IDX is set", id.thread_idx == -1);
191 	}
192 
193 	// Test that node ID contains only node
194 	perf_cpu_map__for_each_cpu(cpu, i, map) {
195 		id = aggr_cpu_id__node(cpu, NULL);
196 		TEST_ASSERT_VAL("Node map - Node ID doesn't match",
197 				cpu__get_node(cpu) == id.node);
198 		TEST_ASSERT_VAL("Node map - Socket is set", id.socket == -1);
199 		TEST_ASSERT_VAL("Node map - Die ID is set", id.die == -1);
200 		TEST_ASSERT_VAL("Node map - Core is set", id.core == -1);
201 		TEST_ASSERT_VAL("Node map - CPU is set", id.cpu.cpu == -1);
202 		TEST_ASSERT_VAL("Node map - Thread IDX is set", id.thread_idx == -1);
203 	}
204 	perf_session__delete(session);
205 
206 	return 0;
207 }
208 
209 static int test__session_topology(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
210 {
211 	char path[PATH_MAX];
212 	struct perf_cpu_map *map;
213 	int ret = TEST_FAIL;
214 
215 	TEST_ASSERT_VAL("can't get templ file", !get_temp(path));
216 
217 	pr_debug("templ file: %s\n", path);
218 
219 	if (session_write_header(path))
220 		goto free_path;
221 
222 	map = perf_cpu_map__new_online_cpus();
223 	if (map == NULL) {
224 		pr_debug("failed to get system cpumap\n");
225 		goto free_path;
226 	}
227 
228 	ret = check_cpu_topology(path, map);
229 	perf_cpu_map__put(map);
230 
231 free_path:
232 	unlink(path);
233 	return ret;
234 }
235 
236 DEFINE_SUITE("Session topology", session_topology);
237