blob: b5efe675b321746e978ae56453390cde85e5f607 [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"
Jin Yaoc1020382021-04-27 15:01:36 +080011#include "pmu.h"
Mamatha Inamdar6ef81c52019-08-22 12:50:49 +053012#include <linux/err.h>
Kan Liangc84974e2015-09-04 04:58:31 -040013
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;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +010036 struct perf_data data = {
Tommi Rantaladbd660e2020-04-23 14:53:40 +030037 .path = path,
38 .mode = PERF_DATA_MODE_WRITE,
Kan Liangc84974e2015-09-04 04:58:31 -040039 };
40
Jiri Olsa8ceb41d2017-01-23 22:07:59 +010041 session = perf_session__new(&data, false, NULL);
Mamatha Inamdar6ef81c52019-08-22 12:50:49 +053042 TEST_ASSERT_VAL("can't get session", !IS_ERR(session));
Kan Liangc84974e2015-09-04 04:58:31 -040043
Jin Yaoc1020382021-04-27 15:01:36 +080044 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 }
Kan Liangc84974e2015-09-04 04:58:31 -040054
55 perf_header__set_feat(&session->header, HEADER_CPU_TOPOLOGY);
56 perf_header__set_feat(&session->header, HEADER_NRCPUS);
Thomas Richterb930e622018-06-11 09:31:53 +020057 perf_header__set_feat(&session->header, HEADER_ARCH);
Kan Liangc84974e2015-09-04 04:58:31 -040058
59 session->header.data_size += DATA_SIZE;
60
61 TEST_ASSERT_VAL("failed to write header",
Jiri Olsaeae8ad82017-01-23 22:25:41 +010062 !perf_session__write_header(session, session->evlist, data.file.fd, true));
Kan Liangc84974e2015-09-04 04:58:31 -040063
Riccardo Mancini233f2dc2021-07-15 18:07:08 +020064 evlist__delete(session->evlist);
Kan Liangc84974e2015-09-04 04:58:31 -040065 perf_session__delete(session);
66
67 return 0;
68}
69
Jiri Olsaf8548392019-07-21 13:23:49 +020070static int check_cpu_topology(char *path, struct perf_cpu_map *map)
Kan Liangc84974e2015-09-04 04:58:31 -040071{
72 struct perf_session *session;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +010073 struct perf_data data = {
Tommi Rantaladbd660e2020-04-23 14:53:40 +030074 .path = path,
75 .mode = PERF_DATA_MODE_READ,
Kan Liangc84974e2015-09-04 04:58:31 -040076 };
James Clark2760f5a12020-11-26 16:13:20 +020077 int i;
78 struct aggr_cpu_id id;
Kan Liangc84974e2015-09-04 04:58:31 -040079
Jiri Olsa8ceb41d2017-01-23 22:07:59 +010080 session = perf_session__new(&data, false, NULL);
Mamatha Inamdar6ef81c52019-08-22 12:50:49 +053081 TEST_ASSERT_VAL("can't get session", !IS_ERR(session));
James Clark23331ee2020-11-26 16:13:17 +020082 cpu__setup_cpunode_map();
Kan Liangc84974e2015-09-04 04:58:31 -040083
Thomas Richterd1211092018-05-28 09:36:57 +020084 /* 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
Ingo Molnar4d39c892021-03-23 17:09:15 +010093 * dependent and might have higher numbers than the CPU id.
Thomas Richterd1211092018-05-28 09:36:57 +020094 * 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 *
James Clark23331ee2020-11-26 16:13:17 +0200100 * 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.
Thomas Richterd1211092018-05-28 09:36:57 +0200104 */
James Clark23331ee2020-11-26 16:13:17 +0200105 if (!session->header.env.cpu
106 && strncmp(session->header.env.arch, "s390", 4)
107 && strncmp(session->header.env.arch, "aarch64", 7))
Thomas Richterd1211092018-05-28 09:36:57 +0200108 return TEST_SKIP;
109
James Clark23331ee2020-11-26 16:13:17 +0200110 TEST_ASSERT_VAL("Session header CPU map not set", session->header.env.cpu);
111
Jan Stancekda8a58b2017-02-17 12:10:26 +0100112 for (i = 0; i < session->header.env.nr_cpus_avail; i++) {
113 if (!cpu_map__has(map, i))
114 continue;
Kan Liangc84974e2015-09-04 04:58:31 -0400115 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
James Clark23331ee2020-11-26 16:13:17 +0200120 // Test that core ID contains socket, die and core
Kan Liangc84974e2015-09-04 04:58:31 -0400121 for (i = 0; i < map->nr; i++) {
James Clark23331ee2020-11-26 16:13:17 +0200122 id = cpu_map__get_core(map, i, NULL);
123 TEST_ASSERT_VAL("Core map - Core ID doesn't match",
James Clarkb9933812020-11-26 16:13:27 +0200124 session->header.env.cpu[map->map[i]].core_id == id.core);
Kan Liangc84974e2015-09-04 04:58:31 -0400125
James Clark23331ee2020-11-26 16:13:17 +0200126 TEST_ASSERT_VAL("Core map - Socket ID doesn't match",
James Clark1a270cb2020-11-26 16:13:25 +0200127 session->header.env.cpu[map->map[i]].socket_id == id.socket);
James Clark23331ee2020-11-26 16:13:17 +0200128
129 TEST_ASSERT_VAL("Core map - Die ID doesn't match",
James Clarkba2ee162020-11-26 16:13:26 +0200130 session->header.env.cpu[map->map[i]].die_id == id.die);
James Clarkfcd83a32020-11-26 16:13:24 +0200131 TEST_ASSERT_VAL("Core map - Node ID is set", id.node == -1);
James Clark8d4852b2020-11-26 16:13:28 +0200132 TEST_ASSERT_VAL("Core map - Thread is set", id.thread == -1);
Kan Liangc84974e2015-09-04 04:58:31 -0400133 }
134
James Clark23331ee2020-11-26 16:13:17 +0200135 // 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",
James Clark1a270cb2020-11-26 16:13:25 +0200139 session->header.env.cpu[map->map[i]].socket_id == id.socket);
James Clark23331ee2020-11-26 16:13:17 +0200140
141 TEST_ASSERT_VAL("Die map - Die ID doesn't match",
James Clarkba2ee162020-11-26 16:13:26 +0200142 session->header.env.cpu[map->map[i]].die_id == id.die);
James Clarkfcd83a32020-11-26 16:13:24 +0200143
144 TEST_ASSERT_VAL("Die map - Node ID is set", id.node == -1);
James Clarkb9933812020-11-26 16:13:27 +0200145 TEST_ASSERT_VAL("Die map - Core is set", id.core == -1);
James Clark8d4852b2020-11-26 16:13:28 +0200146 TEST_ASSERT_VAL("Die map - Thread is set", id.thread == -1);
James Clark23331ee2020-11-26 16:13:17 +0200147 }
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",
James Clark1a270cb2020-11-26 16:13:25 +0200153 session->header.env.cpu[map->map[i]].socket_id == id.socket);
James Clarkfcd83a32020-11-26 16:13:24 +0200154
155 TEST_ASSERT_VAL("Socket map - Node ID is set", id.node == -1);
James Clarkba2ee162020-11-26 16:13:26 +0200156 TEST_ASSERT_VAL("Socket map - Die ID is set", id.die == -1);
James Clarkb9933812020-11-26 16:13:27 +0200157 TEST_ASSERT_VAL("Socket map - Core is set", id.core == -1);
James Clark8d4852b2020-11-26 16:13:28 +0200158 TEST_ASSERT_VAL("Socket map - Thread is set", id.thread == -1);
James Clark23331ee2020-11-26 16:13:17 +0200159 }
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",
James Clarkfcd83a32020-11-26 16:13:24 +0200165 cpu__get_node(map->map[i]) == id.node);
James Clark1a270cb2020-11-26 16:13:25 +0200166 TEST_ASSERT_VAL("Node map - Socket is set", id.socket == -1);
James Clarkba2ee162020-11-26 16:13:26 +0200167 TEST_ASSERT_VAL("Node map - Die ID is set", id.die == -1);
James Clarkb9933812020-11-26 16:13:27 +0200168 TEST_ASSERT_VAL("Node map - Core is set", id.core == -1);
James Clark8d4852b2020-11-26 16:13:28 +0200169 TEST_ASSERT_VAL("Node map - Thread is set", id.thread == -1);
James Clark23331ee2020-11-26 16:13:17 +0200170 }
Kan Liangc84974e2015-09-04 04:58:31 -0400171 perf_session__delete(session);
172
173 return 0;
174}
175
Arnaldo Carvalho de Melo81f17c92017-08-03 15:16:31 -0300176int test__session_topology(struct test *test __maybe_unused, int subtest __maybe_unused)
Kan Liangc84974e2015-09-04 04:58:31 -0400177{
178 char path[PATH_MAX];
Jiri Olsaf8548392019-07-21 13:23:49 +0200179 struct perf_cpu_map *map;
Thomas Richterd1211092018-05-28 09:36:57 +0200180 int ret = TEST_FAIL;
Kan Liangc84974e2015-09-04 04:58:31 -0400181
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
Jiri Olsa9c3516d2019-07-21 13:24:30 +0200189 map = perf_cpu_map__new(NULL);
Kan Liangc84974e2015-09-04 04:58:31 -0400190 if (map == NULL) {
191 pr_debug("failed to get system cpumap\n");
192 goto free_path;
193 }
194
Thomas Richterd1211092018-05-28 09:36:57 +0200195 ret = check_cpu_topology(path, map);
Jiri Olsa38f01d82019-07-21 13:24:17 +0200196 perf_cpu_map__put(map);
Thomas Richterd1211092018-05-28 09:36:57 +0200197
Kan Liangc84974e2015-09-04 04:58:31 -0400198free_path:
199 unlink(path);
200 return ret;
201}