1 // SPDX-License-Identifier: GPL-2.0
2 #include <string.h>
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include "tests.h"
6 #include "util.h"
7 #include "session.h"
8 #include "evlist.h"
9 #include "debug.h"
10
11 #define TEMPL "/tmp/perf-test-XXXXXX"
12 #define DATA_SIZE 10
13
get_temp(char * path)14 static int get_temp(char *path)
15 {
16 int fd;
17
18 strcpy(path, TEMPL);
19
20 fd = mkstemp(path);
21 if (fd < 0) {
22 perror("mkstemp failed");
23 return -1;
24 }
25
26 close(fd);
27 return 0;
28 }
29
session_write_header(char * path)30 static int session_write_header(char *path)
31 {
32 struct perf_session *session;
33 struct perf_data_file file = {
34 .path = path,
35 .mode = PERF_DATA_MODE_WRITE,
36 };
37
38 session = perf_session__new(&file, false, NULL);
39 TEST_ASSERT_VAL("can't get session", session);
40
41 session->evlist = perf_evlist__new_default();
42 TEST_ASSERT_VAL("can't get evlist", session->evlist);
43
44 perf_header__set_feat(&session->header, HEADER_CPU_TOPOLOGY);
45 perf_header__set_feat(&session->header, HEADER_NRCPUS);
46 perf_header__set_feat(&session->header, HEADER_ARCH);
47
48 session->header.data_size += DATA_SIZE;
49
50 TEST_ASSERT_VAL("failed to write header",
51 !perf_session__write_header(session, session->evlist, file.fd, true));
52
53 perf_session__delete(session);
54
55 return 0;
56 }
57
check_cpu_topology(char * path,struct cpu_map * map)58 static int check_cpu_topology(char *path, struct cpu_map *map)
59 {
60 struct perf_session *session;
61 struct perf_data_file file = {
62 .path = path,
63 .mode = PERF_DATA_MODE_READ,
64 };
65 int i;
66
67 session = perf_session__new(&file, false, NULL);
68 TEST_ASSERT_VAL("can't get session", session);
69
70 /* On platforms with large numbers of CPUs process_cpu_topology()
71 * might issue an error while reading the perf.data file section
72 * HEADER_CPU_TOPOLOGY and the cpu_topology_map pointed to by member
73 * cpu is a NULL pointer.
74 * Example: On s390
75 * CPU 0 is on core_id 0 and physical_package_id 6
76 * CPU 1 is on core_id 1 and physical_package_id 3
77 *
78 * Core_id and physical_package_id are platform and architecture
79 * dependend and might have higher numbers than the CPU id.
80 * This actually depends on the configuration.
81 *
82 * In this case process_cpu_topology() prints error message:
83 * "socket_id number is too big. You may need to upgrade the
84 * perf tool."
85 *
86 * This is the reason why this test might be skipped.
87 */
88 if (!session->header.env.cpu)
89 return TEST_SKIP;
90
91 for (i = 0; i < session->header.env.nr_cpus_avail; i++) {
92 if (!cpu_map__has(map, i))
93 continue;
94 pr_debug("CPU %d, core %d, socket %d\n", i,
95 session->header.env.cpu[i].core_id,
96 session->header.env.cpu[i].socket_id);
97 }
98
99 for (i = 0; i < map->nr; i++) {
100 TEST_ASSERT_VAL("Core ID doesn't match",
101 (session->header.env.cpu[map->map[i]].core_id == (cpu_map__get_core(map, i, NULL) & 0xffff)));
102
103 TEST_ASSERT_VAL("Socket ID doesn't match",
104 (session->header.env.cpu[map->map[i]].socket_id == cpu_map__get_socket(map, i, NULL)));
105 }
106
107 perf_session__delete(session);
108
109 return 0;
110 }
111
test__session_topology(struct test * test __maybe_unused,int subtest __maybe_unused)112 int test__session_topology(struct test *test __maybe_unused, int subtest __maybe_unused)
113 {
114 char path[PATH_MAX];
115 struct cpu_map *map;
116 int ret = TEST_FAIL;
117
118 TEST_ASSERT_VAL("can't get templ file", !get_temp(path));
119
120 pr_debug("templ file: %s\n", path);
121
122 if (session_write_header(path))
123 goto free_path;
124
125 map = cpu_map__new(NULL);
126 if (map == NULL) {
127 pr_debug("failed to get system cpumap\n");
128 goto free_path;
129 }
130
131 ret = check_cpu_topology(path, map);
132 cpu_map__put(map);
133
134 free_path:
135 unlink(path);
136 return ret;
137 }
138