File Index
Files and directories
Sign in
FILE

topology.c

File preview with syntax highlighting.

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