blob: ee272d45a4459761a07655c88ad75c67a7865d7c [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Kan Liangc84974e2015-09-04 04:58:31 -04002#include <string.h>
3#include <stdlib.h>
4#include <stdio.h>
Jiri Olsa9c3516d2019-07-21 13:24:30 +02005#include <perf/cpumap.h>
Arnaldo Carvalho de Melo87ffb6c2019-09-10 16:29:02 +01006#include "cpumap.h"
Kan Liangc84974e2015-09-04 04:58:31 -04007#include "tests.h"
Kan Liangc84974e2015-09-04 04:58:31 -04008#include "session.h"
9#include "evlist.h"
10#include "debug.h"
Mamatha Inamdar6ef81c52019-08-22 12:50:49 +053011#include <linux/err.h>
Kan Liangc84974e2015-09-04 04:58:31 -040012
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;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +010035 struct perf_data data = {
Tommi Rantaladbd660e2020-04-23 14:53:40 +030036 .path = path,
37 .mode = PERF_DATA_MODE_WRITE,
Kan Liangc84974e2015-09-04 04:58:31 -040038 };
39
Jiri Olsa8ceb41d2017-01-23 22:07:59 +010040 session = perf_session__new(&data, false, NULL);
Mamatha Inamdar6ef81c52019-08-22 12:50:49 +053041 TEST_ASSERT_VAL("can't get session", !IS_ERR(session));
Kan Liangc84974e2015-09-04 04:58:31 -040042
Arnaldo Carvalho de Melo606e2c22020-11-30 15:04:05 -030043 session->evlist = evlist__new_default();
Kan Liangc84974e2015-09-04 04:58:31 -040044 TEST_ASSERT_VAL("can't get evlist", session->evlist);
45
46 perf_header__set_feat(&session->header, HEADER_CPU_TOPOLOGY);
47 perf_header__set_feat(&session->header, HEADER_NRCPUS);
Thomas Richterb930e622018-06-11 09:31:53 +020048 perf_header__set_feat(&session->header, HEADER_ARCH);
Kan Liangc84974e2015-09-04 04:58:31 -040049
50 session->header.data_size += DATA_SIZE;
51
52 TEST_ASSERT_VAL("failed to write header",
Jiri Olsaeae8ad82017-01-23 22:25:41 +010053 !perf_session__write_header(session, session->evlist, data.file.fd, true));
Kan Liangc84974e2015-09-04 04:58:31 -040054
55 perf_session__delete(session);
56
57 return 0;
58}
59
Jiri Olsaf8548392019-07-21 13:23:49 +020060static int check_cpu_topology(char *path, struct perf_cpu_map *map)
Kan Liangc84974e2015-09-04 04:58:31 -040061{
62 struct perf_session *session;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +010063 struct perf_data data = {
Tommi Rantaladbd660e2020-04-23 14:53:40 +030064 .path = path,
65 .mode = PERF_DATA_MODE_READ,
Kan Liangc84974e2015-09-04 04:58:31 -040066 };
James Clark2760f5a12020-11-26 16:13:20 +020067 int i;
68 struct aggr_cpu_id id;
Kan Liangc84974e2015-09-04 04:58:31 -040069
Jiri Olsa8ceb41d2017-01-23 22:07:59 +010070 session = perf_session__new(&data, false, NULL);
Mamatha Inamdar6ef81c52019-08-22 12:50:49 +053071 TEST_ASSERT_VAL("can't get session", !IS_ERR(session));
James Clark23331ee2020-11-26 16:13:17 +020072 cpu__setup_cpunode_map();
Kan Liangc84974e2015-09-04 04:58:31 -040073
Thomas Richterd1211092018-05-28 09:36:57 +020074 /* On platforms with large numbers of CPUs process_cpu_topology()
75 * might issue an error while reading the perf.data file section
76 * HEADER_CPU_TOPOLOGY and the cpu_topology_map pointed to by member
77 * cpu is a NULL pointer.
78 * Example: On s390
79 * CPU 0 is on core_id 0 and physical_package_id 6
80 * CPU 1 is on core_id 1 and physical_package_id 3
81 *
82 * Core_id and physical_package_id are platform and architecture
83 * dependend and might have higher numbers than the CPU id.
84 * This actually depends on the configuration.
85 *
86 * In this case process_cpu_topology() prints error message:
87 * "socket_id number is too big. You may need to upgrade the
88 * perf tool."
89 *
James Clark23331ee2020-11-26 16:13:17 +020090 * This is the reason why this test might be skipped. aarch64 and
91 * s390 always write this part of the header, even when the above
92 * condition is true (see do_core_id_test in header.c). So always
93 * run this test on those platforms.
Thomas Richterd1211092018-05-28 09:36:57 +020094 */
James Clark23331ee2020-11-26 16:13:17 +020095 if (!session->header.env.cpu
96 && strncmp(session->header.env.arch, "s390", 4)
97 && strncmp(session->header.env.arch, "aarch64", 7))
Thomas Richterd1211092018-05-28 09:36:57 +020098 return TEST_SKIP;
99
James Clark23331ee2020-11-26 16:13:17 +0200100 TEST_ASSERT_VAL("Session header CPU map not set", session->header.env.cpu);
101
Jan Stancekda8a58b2017-02-17 12:10:26 +0100102 for (i = 0; i < session->header.env.nr_cpus_avail; i++) {
103 if (!cpu_map__has(map, i))
104 continue;
Kan Liangc84974e2015-09-04 04:58:31 -0400105 pr_debug("CPU %d, core %d, socket %d\n", i,
106 session->header.env.cpu[i].core_id,
107 session->header.env.cpu[i].socket_id);
108 }
109
James Clark23331ee2020-11-26 16:13:17 +0200110 // Test that core ID contains socket, die and core
Kan Liangc84974e2015-09-04 04:58:31 -0400111 for (i = 0; i < map->nr; i++) {
James Clark23331ee2020-11-26 16:13:17 +0200112 id = cpu_map__get_core(map, i, NULL);
113 TEST_ASSERT_VAL("Core map - Core ID doesn't match",
James Clark2760f5a12020-11-26 16:13:20 +0200114 session->header.env.cpu[map->map[i]].core_id == cpu_map__id_to_cpu(id.id));
Kan Liangc84974e2015-09-04 04:58:31 -0400115
James Clark23331ee2020-11-26 16:13:17 +0200116 TEST_ASSERT_VAL("Core map - Socket ID doesn't match",
117 session->header.env.cpu[map->map[i]].socket_id ==
James Clark2760f5a12020-11-26 16:13:20 +0200118 cpu_map__id_to_socket(id.id));
James Clark23331ee2020-11-26 16:13:17 +0200119
120 TEST_ASSERT_VAL("Core map - Die ID doesn't match",
James Clark2760f5a12020-11-26 16:13:20 +0200121 session->header.env.cpu[map->map[i]].die_id == cpu_map__id_to_die(id.id));
James Clarkfcd83a32020-11-26 16:13:24 +0200122 TEST_ASSERT_VAL("Core map - Node ID is set", id.node == -1);
Kan Liangc84974e2015-09-04 04:58:31 -0400123 }
124
James Clark23331ee2020-11-26 16:13:17 +0200125 // Test that die ID contains socket and die
126 for (i = 0; i < map->nr; i++) {
127 id = cpu_map__get_die(map, i, NULL);
128 TEST_ASSERT_VAL("Die map - Socket ID doesn't match",
129 session->header.env.cpu[map->map[i]].socket_id ==
James Clark2760f5a12020-11-26 16:13:20 +0200130 cpu_map__id_to_socket(id.id << 16));
James Clark23331ee2020-11-26 16:13:17 +0200131
132 TEST_ASSERT_VAL("Die map - Die ID doesn't match",
133 session->header.env.cpu[map->map[i]].die_id ==
James Clark2760f5a12020-11-26 16:13:20 +0200134 cpu_map__id_to_die(id.id << 16));
James Clarkfcd83a32020-11-26 16:13:24 +0200135
136 TEST_ASSERT_VAL("Die map - Node ID is set", id.node == -1);
James Clark23331ee2020-11-26 16:13:17 +0200137 }
138
139 // Test that socket ID contains only socket
140 for (i = 0; i < map->nr; i++) {
141 id = cpu_map__get_socket(map, i, NULL);
142 TEST_ASSERT_VAL("Socket map - Socket ID doesn't match",
James Clark2760f5a12020-11-26 16:13:20 +0200143 session->header.env.cpu[map->map[i]].socket_id == id.id);
James Clarkfcd83a32020-11-26 16:13:24 +0200144
145 TEST_ASSERT_VAL("Socket map - Node ID is set", id.node == -1);
James Clark23331ee2020-11-26 16:13:17 +0200146 }
147
148 // Test that node ID contains only node
149 for (i = 0; i < map->nr; i++) {
150 id = cpu_map__get_node(map, i, NULL);
151 TEST_ASSERT_VAL("Node map - Node ID doesn't match",
James Clarkfcd83a32020-11-26 16:13:24 +0200152 cpu__get_node(map->map[i]) == id.node);
153 TEST_ASSERT_VAL("Node map - ID is set", id.id == -1);
James Clark23331ee2020-11-26 16:13:17 +0200154 }
Kan Liangc84974e2015-09-04 04:58:31 -0400155 perf_session__delete(session);
156
157 return 0;
158}
159
Arnaldo Carvalho de Melo81f17c92017-08-03 15:16:31 -0300160int test__session_topology(struct test *test __maybe_unused, int subtest __maybe_unused)
Kan Liangc84974e2015-09-04 04:58:31 -0400161{
162 char path[PATH_MAX];
Jiri Olsaf8548392019-07-21 13:23:49 +0200163 struct perf_cpu_map *map;
Thomas Richterd1211092018-05-28 09:36:57 +0200164 int ret = TEST_FAIL;
Kan Liangc84974e2015-09-04 04:58:31 -0400165
166 TEST_ASSERT_VAL("can't get templ file", !get_temp(path));
167
168 pr_debug("templ file: %s\n", path);
169
170 if (session_write_header(path))
171 goto free_path;
172
Jiri Olsa9c3516d2019-07-21 13:24:30 +0200173 map = perf_cpu_map__new(NULL);
Kan Liangc84974e2015-09-04 04:58:31 -0400174 if (map == NULL) {
175 pr_debug("failed to get system cpumap\n");
176 goto free_path;
177 }
178
Thomas Richterd1211092018-05-28 09:36:57 +0200179 ret = check_cpu_topology(path, map);
Jiri Olsa38f01d82019-07-21 13:24:17 +0200180 perf_cpu_map__put(map);
Thomas Richterd1211092018-05-28 09:36:57 +0200181
Kan Liangc84974e2015-09-04 04:58:31 -0400182free_path:
183 unlink(path);
184 return ret;
185}