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 17 static 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 33 static 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 perf_session__delete(session); 65 66 return 0; 67 } 68 69 static int check_cpu_topology(char *path, struct perf_cpu_map *map) 70 { 71 struct perf_session *session; 72 struct perf_data data = { 73 .path = path, 74 .mode = PERF_DATA_MODE_READ, 75 }; 76 int i; 77 struct aggr_cpu_id id; 78 79 session = perf_session__new(&data, false, NULL); 80 TEST_ASSERT_VAL("can't get session", !IS_ERR(session)); 81 cpu__setup_cpunode_map(); 82 83 /* On platforms with large numbers of CPUs process_cpu_topology() 84 * might issue an error while reading the perf.data file section 85 * HEADER_CPU_TOPOLOGY and the cpu_topology_map pointed to by member 86 * cpu is a NULL pointer. 87 * Example: On s390 88 * CPU 0 is on core_id 0 and physical_package_id 6 89 * CPU 1 is on core_id 1 and physical_package_id 3 90 * 91 * Core_id and physical_package_id are platform and architecture 92 * dependent and might have higher numbers than the CPU id. 93 * This actually depends on the configuration. 94 * 95 * In this case process_cpu_topology() prints error message: 96 * "socket_id number is too big. You may need to upgrade the 97 * perf tool." 98 * 99 * This is the reason why this test might be skipped. aarch64 and 100 * s390 always write this part of the header, even when the above 101 * condition is true (see do_core_id_test in header.c). So always 102 * run this test on those platforms. 103 */ 104 if (!session->header.env.cpu 105 && strncmp(session->header.env.arch, "s390", 4) 106 && strncmp(session->header.env.arch, "aarch64", 7)) 107 return TEST_SKIP; 108 109 TEST_ASSERT_VAL("Session header CPU map not set", session->header.env.cpu); 110 111 for (i = 0; i < session->header.env.nr_cpus_avail; i++) { 112 if (!cpu_map__has(map, i)) 113 continue; 114 pr_debug("CPU %d, core %d, socket %d\n", i, 115 session->header.env.cpu[i].core_id, 116 session->header.env.cpu[i].socket_id); 117 } 118 119 // Test that core ID contains socket, die and core 120 for (i = 0; i < map->nr; i++) { 121 id = cpu_map__get_core(map, i, NULL); 122 TEST_ASSERT_VAL("Core map - Core ID doesn't match", 123 session->header.env.cpu[map->map[i]].core_id == id.core); 124 125 TEST_ASSERT_VAL("Core map - Socket ID doesn't match", 126 session->header.env.cpu[map->map[i]].socket_id == id.socket); 127 128 TEST_ASSERT_VAL("Core map - Die ID doesn't match", 129 session->header.env.cpu[map->map[i]].die_id == id.die); 130 TEST_ASSERT_VAL("Core map - Node ID is set", id.node == -1); 131 TEST_ASSERT_VAL("Core map - Thread is set", id.thread == -1); 132 } 133 134 // Test that die ID contains socket and die 135 for (i = 0; i < map->nr; i++) { 136 id = cpu_map__get_die(map, i, NULL); 137 TEST_ASSERT_VAL("Die map - Socket ID doesn't match", 138 session->header.env.cpu[map->map[i]].socket_id == id.socket); 139 140 TEST_ASSERT_VAL("Die map - Die ID doesn't match", 141 session->header.env.cpu[map->map[i]].die_id == id.die); 142 143 TEST_ASSERT_VAL("Die map - Node ID is set", id.node == -1); 144 TEST_ASSERT_VAL("Die map - Core is set", id.core == -1); 145 TEST_ASSERT_VAL("Die map - Thread is set", id.thread == -1); 146 } 147 148 // Test that socket ID contains only socket 149 for (i = 0; i < map->nr; i++) { 150 id = cpu_map__get_socket(map, i, NULL); 151 TEST_ASSERT_VAL("Socket map - Socket ID doesn't match", 152 session->header.env.cpu[map->map[i]].socket_id == id.socket); 153 154 TEST_ASSERT_VAL("Socket map - Node ID is set", id.node == -1); 155 TEST_ASSERT_VAL("Socket map - Die ID is set", id.die == -1); 156 TEST_ASSERT_VAL("Socket map - Core is set", id.core == -1); 157 TEST_ASSERT_VAL("Socket map - Thread is set", id.thread == -1); 158 } 159 160 // Test that node ID contains only node 161 for (i = 0; i < map->nr; i++) { 162 id = cpu_map__get_node(map, i, NULL); 163 TEST_ASSERT_VAL("Node map - Node ID doesn't match", 164 cpu__get_node(map->map[i]) == id.node); 165 TEST_ASSERT_VAL("Node map - Socket is set", id.socket == -1); 166 TEST_ASSERT_VAL("Node map - Die ID is set", id.die == -1); 167 TEST_ASSERT_VAL("Node map - Core is set", id.core == -1); 168 TEST_ASSERT_VAL("Node map - Thread is set", id.thread == -1); 169 } 170 perf_session__delete(session); 171 172 return 0; 173 } 174 175 int test__session_topology(struct test *test __maybe_unused, int subtest __maybe_unused) 176 { 177 char path[PATH_MAX]; 178 struct perf_cpu_map *map; 179 int ret = TEST_FAIL; 180 181 TEST_ASSERT_VAL("can't get templ file", !get_temp(path)); 182 183 pr_debug("templ file: %s\n", path); 184 185 if (session_write_header(path)) 186 goto free_path; 187 188 map = perf_cpu_map__new(NULL); 189 if (map == NULL) { 190 pr_debug("failed to get system cpumap\n"); 191 goto free_path; 192 } 193 194 ret = check_cpu_topology(path, map); 195 perf_cpu_map__put(map); 196 197 free_path: 198 unlink(path); 199 return ret; 200 } 201