]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
c84974ed KL |
2 | #include <string.h> |
3 | #include <stdlib.h> | |
4 | #include <stdio.h> | |
9c3516d1 | 5 | #include <perf/cpumap.h> |
87ffb6c6 | 6 | #include "cpumap.h" |
c84974ed | 7 | #include "tests.h" |
c84974ed KL |
8 | #include "session.h" |
9 | #include "evlist.h" | |
10 | #include "debug.h" | |
6ef81c55 | 11 | #include <linux/err.h> |
c84974ed KL |
12 | |
13 | #define TEMPL "/tmp/perf-test-XXXXXX" | |
14 | #define DATA_SIZE 10 | |
15 | ||
16 | static 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 | ||
32 | static int session_write_header(char *path) | |
33 | { | |
34 | struct perf_session *session; | |
8ceb41d7 | 35 | struct perf_data data = { |
dbd660e6 TR |
36 | .path = path, |
37 | .mode = PERF_DATA_MODE_WRITE, | |
c84974ed KL |
38 | }; |
39 | ||
8ceb41d7 | 40 | session = perf_session__new(&data, false, NULL); |
6ef81c55 | 41 | TEST_ASSERT_VAL("can't get session", !IS_ERR(session)); |
c84974ed | 42 | |
606e2c29 | 43 | session->evlist = evlist__new_default(); |
c84974ed KL |
44 | 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); | |
b930e62e | 48 | perf_header__set_feat(&session->header, HEADER_ARCH); |
c84974ed KL |
49 | |
50 | session->header.data_size += DATA_SIZE; | |
51 | ||
52 | TEST_ASSERT_VAL("failed to write header", | |
eae8ad80 | 53 | !perf_session__write_header(session, session->evlist, data.file.fd, true)); |
c84974ed KL |
54 | |
55 | perf_session__delete(session); | |
56 | ||
57 | return 0; | |
58 | } | |
59 | ||
f854839b | 60 | static int check_cpu_topology(char *path, struct perf_cpu_map *map) |
c84974ed KL |
61 | { |
62 | struct perf_session *session; | |
8ceb41d7 | 63 | struct perf_data data = { |
dbd660e6 TR |
64 | .path = path, |
65 | .mode = PERF_DATA_MODE_READ, | |
c84974ed | 66 | }; |
2760f5a1 JC |
67 | int i; |
68 | struct aggr_cpu_id id; | |
c84974ed | 69 | |
8ceb41d7 | 70 | session = perf_session__new(&data, false, NULL); |
6ef81c55 | 71 | TEST_ASSERT_VAL("can't get session", !IS_ERR(session)); |
23331eeb | 72 | cpu__setup_cpunode_map(); |
c84974ed | 73 | |
d1211091 TR |
74 | /* 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 | * | |
23331eeb JC |
90 | * 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. | |
d1211091 | 94 | */ |
23331eeb JC |
95 | if (!session->header.env.cpu |
96 | && strncmp(session->header.env.arch, "s390", 4) | |
97 | && strncmp(session->header.env.arch, "aarch64", 7)) | |
d1211091 TR |
98 | return TEST_SKIP; |
99 | ||
23331eeb JC |
100 | TEST_ASSERT_VAL("Session header CPU map not set", session->header.env.cpu); |
101 | ||
da8a58b5 JS |
102 | for (i = 0; i < session->header.env.nr_cpus_avail; i++) { |
103 | if (!cpu_map__has(map, i)) | |
104 | continue; | |
c84974ed KL |
105 | 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 | ||
23331eeb JC |
110 | // Test that core ID contains socket, die and core |
111 | for (i = 0; i < map->nr; i++) { | |
112 | id = cpu_map__get_core(map, i, NULL); | |
113 | TEST_ASSERT_VAL("Core map - Core ID doesn't match", | |
b9933817 | 114 | session->header.env.cpu[map->map[i]].core_id == id.core); |
23331eeb JC |
115 | |
116 | TEST_ASSERT_VAL("Core map - Socket ID doesn't match", | |
1a270cb6 | 117 | session->header.env.cpu[map->map[i]].socket_id == id.socket); |
23331eeb JC |
118 | |
119 | TEST_ASSERT_VAL("Core map - Die ID doesn't match", | |
ba2ee166 | 120 | session->header.env.cpu[map->map[i]].die_id == id.die); |
fcd83a35 | 121 | TEST_ASSERT_VAL("Core map - Node ID is set", id.node == -1); |
8d4852b4 | 122 | TEST_ASSERT_VAL("Core map - Thread is set", id.thread == -1); |
23331eeb JC |
123 | } |
124 | ||
125 | // Test that die ID contains socket and die | |
c84974ed | 126 | for (i = 0; i < map->nr; i++) { |
23331eeb JC |
127 | id = cpu_map__get_die(map, i, NULL); |
128 | TEST_ASSERT_VAL("Die map - Socket ID doesn't match", | |
1a270cb6 | 129 | session->header.env.cpu[map->map[i]].socket_id == id.socket); |
23331eeb JC |
130 | |
131 | TEST_ASSERT_VAL("Die map - Die ID doesn't match", | |
ba2ee166 | 132 | session->header.env.cpu[map->map[i]].die_id == id.die); |
fcd83a35 JC |
133 | |
134 | TEST_ASSERT_VAL("Die map - Node ID is set", id.node == -1); | |
b9933817 | 135 | TEST_ASSERT_VAL("Die map - Core is set", id.core == -1); |
8d4852b4 | 136 | TEST_ASSERT_VAL("Die map - Thread is set", id.thread == -1); |
23331eeb | 137 | } |
c84974ed | 138 | |
23331eeb JC |
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", | |
1a270cb6 | 143 | session->header.env.cpu[map->map[i]].socket_id == id.socket); |
fcd83a35 JC |
144 | |
145 | TEST_ASSERT_VAL("Socket map - Node ID is set", id.node == -1); | |
ba2ee166 | 146 | TEST_ASSERT_VAL("Socket map - Die ID is set", id.die == -1); |
b9933817 | 147 | TEST_ASSERT_VAL("Socket map - Core is set", id.core == -1); |
8d4852b4 | 148 | TEST_ASSERT_VAL("Socket map - Thread is set", id.thread == -1); |
c84974ed KL |
149 | } |
150 | ||
23331eeb JC |
151 | // Test that node ID contains only node |
152 | for (i = 0; i < map->nr; i++) { | |
153 | id = cpu_map__get_node(map, i, NULL); | |
154 | TEST_ASSERT_VAL("Node map - Node ID doesn't match", | |
fcd83a35 | 155 | cpu__get_node(map->map[i]) == id.node); |
1a270cb6 | 156 | TEST_ASSERT_VAL("Node map - Socket is set", id.socket == -1); |
ba2ee166 | 157 | TEST_ASSERT_VAL("Node map - Die ID is set", id.die == -1); |
b9933817 | 158 | TEST_ASSERT_VAL("Node map - Core is set", id.core == -1); |
8d4852b4 | 159 | TEST_ASSERT_VAL("Node map - Thread is set", id.thread == -1); |
23331eeb | 160 | } |
c84974ed KL |
161 | perf_session__delete(session); |
162 | ||
163 | return 0; | |
164 | } | |
165 | ||
81f17c90 | 166 | int test__session_topology(struct test *test __maybe_unused, int subtest __maybe_unused) |
c84974ed KL |
167 | { |
168 | char path[PATH_MAX]; | |
f854839b | 169 | struct perf_cpu_map *map; |
d1211091 | 170 | int ret = TEST_FAIL; |
c84974ed KL |
171 | |
172 | TEST_ASSERT_VAL("can't get templ file", !get_temp(path)); | |
173 | ||
174 | pr_debug("templ file: %s\n", path); | |
175 | ||
176 | if (session_write_header(path)) | |
177 | goto free_path; | |
178 | ||
9c3516d1 | 179 | map = perf_cpu_map__new(NULL); |
c84974ed KL |
180 | if (map == NULL) { |
181 | pr_debug("failed to get system cpumap\n"); | |
182 | goto free_path; | |
183 | } | |
184 | ||
d1211091 | 185 | ret = check_cpu_topology(path, map); |
38f01d8d | 186 | perf_cpu_map__put(map); |
d1211091 | 187 | |
c84974ed KL |
188 | free_path: |
189 | unlink(path); | |
190 | return ret; | |
191 | } |