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