Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Kan Liang | c84974e | 2015-09-04 04:58:31 -0400 | [diff] [blame] | 2 | #include <string.h> |
| 3 | #include <stdlib.h> |
| 4 | #include <stdio.h> |
Jiri Olsa | 9c3516d | 2019-07-21 13:24:30 +0200 | [diff] [blame] | 5 | #include <perf/cpumap.h> |
Arnaldo Carvalho de Melo | 87ffb6c | 2019-09-10 16:29:02 +0100 | [diff] [blame] | 6 | #include "cpumap.h" |
Kan Liang | c84974e | 2015-09-04 04:58:31 -0400 | [diff] [blame] | 7 | #include "tests.h" |
Kan Liang | c84974e | 2015-09-04 04:58:31 -0400 | [diff] [blame] | 8 | #include "session.h" |
| 9 | #include "evlist.h" |
| 10 | #include "debug.h" |
Jin Yao | c102038 | 2021-04-27 15:01:36 +0800 | [diff] [blame] | 11 | #include "pmu.h" |
Mamatha Inamdar | 6ef81c5 | 2019-08-22 12:50:49 +0530 | [diff] [blame] | 12 | #include <linux/err.h> |
Kan Liang | c84974e | 2015-09-04 04:58:31 -0400 | [diff] [blame] | 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; |
Jiri Olsa | 8ceb41d | 2017-01-23 22:07:59 +0100 | [diff] [blame] | 36 | struct perf_data data = { |
Tommi Rantala | dbd660e | 2020-04-23 14:53:40 +0300 | [diff] [blame] | 37 | .path = path, |
| 38 | .mode = PERF_DATA_MODE_WRITE, |
Kan Liang | c84974e | 2015-09-04 04:58:31 -0400 | [diff] [blame] | 39 | }; |
| 40 | |
Namhyung Kim | 2681bd8 | 2021-07-19 15:31:49 -0700 | [diff] [blame] | 41 | session = perf_session__new(&data, NULL); |
Mamatha Inamdar | 6ef81c5 | 2019-08-22 12:50:49 +0530 | [diff] [blame] | 42 | TEST_ASSERT_VAL("can't get session", !IS_ERR(session)); |
Kan Liang | c84974e | 2015-09-04 04:58:31 -0400 | [diff] [blame] | 43 | |
Jin Yao | c102038 | 2021-04-27 15:01:36 +0800 | [diff] [blame] | 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); |
Ian Rogers | 07eafd4 | 2021-11-07 01:00:01 -0800 | [diff] [blame] | 52 | parse_events_error__init(&err); |
Jin Yao | c102038 | 2021-04-27 15:01:36 +0800 | [diff] [blame] | 53 | parse_events(session->evlist, "cpu_core/cycles/", &err); |
Ian Rogers | 07eafd4 | 2021-11-07 01:00:01 -0800 | [diff] [blame] | 54 | parse_events_error__exit(&err); |
Jin Yao | c102038 | 2021-04-27 15:01:36 +0800 | [diff] [blame] | 55 | } |
Kan Liang | c84974e | 2015-09-04 04:58:31 -0400 | [diff] [blame] | 56 | |
| 57 | perf_header__set_feat(&session->header, HEADER_CPU_TOPOLOGY); |
| 58 | perf_header__set_feat(&session->header, HEADER_NRCPUS); |
Thomas Richter | b930e62 | 2018-06-11 09:31:53 +0200 | [diff] [blame] | 59 | perf_header__set_feat(&session->header, HEADER_ARCH); |
Kan Liang | c84974e | 2015-09-04 04:58:31 -0400 | [diff] [blame] | 60 | |
| 61 | session->header.data_size += DATA_SIZE; |
| 62 | |
| 63 | TEST_ASSERT_VAL("failed to write header", |
Jiri Olsa | eae8ad8 | 2017-01-23 22:25:41 +0100 | [diff] [blame] | 64 | !perf_session__write_header(session, session->evlist, data.file.fd, true)); |
Kan Liang | c84974e | 2015-09-04 04:58:31 -0400 | [diff] [blame] | 65 | |
Riccardo Mancini | 233f2dc | 2021-07-15 18:07:08 +0200 | [diff] [blame] | 66 | evlist__delete(session->evlist); |
Kan Liang | c84974e | 2015-09-04 04:58:31 -0400 | [diff] [blame] | 67 | perf_session__delete(session); |
| 68 | |
| 69 | return 0; |
| 70 | } |
| 71 | |
Jiri Olsa | f854839 | 2019-07-21 13:23:49 +0200 | [diff] [blame] | 72 | static int check_cpu_topology(char *path, struct perf_cpu_map *map) |
Kan Liang | c84974e | 2015-09-04 04:58:31 -0400 | [diff] [blame] | 73 | { |
| 74 | struct perf_session *session; |
Jiri Olsa | 8ceb41d | 2017-01-23 22:07:59 +0100 | [diff] [blame] | 75 | struct perf_data data = { |
Tommi Rantala | dbd660e | 2020-04-23 14:53:40 +0300 | [diff] [blame] | 76 | .path = path, |
| 77 | .mode = PERF_DATA_MODE_READ, |
Kan Liang | c84974e | 2015-09-04 04:58:31 -0400 | [diff] [blame] | 78 | }; |
James Clark | 2760f5a1 | 2020-11-26 16:13:20 +0200 | [diff] [blame] | 79 | int i; |
| 80 | struct aggr_cpu_id id; |
Kan Liang | c84974e | 2015-09-04 04:58:31 -0400 | [diff] [blame] | 81 | |
Namhyung Kim | 2681bd8 | 2021-07-19 15:31:49 -0700 | [diff] [blame] | 82 | session = perf_session__new(&data, NULL); |
Mamatha Inamdar | 6ef81c5 | 2019-08-22 12:50:49 +0530 | [diff] [blame] | 83 | TEST_ASSERT_VAL("can't get session", !IS_ERR(session)); |
James Clark | 23331ee | 2020-11-26 16:13:17 +0200 | [diff] [blame] | 84 | cpu__setup_cpunode_map(); |
Kan Liang | c84974e | 2015-09-04 04:58:31 -0400 | [diff] [blame] | 85 | |
Thomas Richter | d121109 | 2018-05-28 09:36:57 +0200 | [diff] [blame] | 86 | /* On platforms with large numbers of CPUs process_cpu_topology() |
| 87 | * might issue an error while reading the perf.data file section |
| 88 | * HEADER_CPU_TOPOLOGY and the cpu_topology_map pointed to by member |
| 89 | * cpu is a NULL pointer. |
| 90 | * Example: On s390 |
| 91 | * CPU 0 is on core_id 0 and physical_package_id 6 |
| 92 | * CPU 1 is on core_id 1 and physical_package_id 3 |
| 93 | * |
| 94 | * Core_id and physical_package_id are platform and architecture |
Ingo Molnar | 4d39c89 | 2021-03-23 17:09:15 +0100 | [diff] [blame] | 95 | * dependent and might have higher numbers than the CPU id. |
Thomas Richter | d121109 | 2018-05-28 09:36:57 +0200 | [diff] [blame] | 96 | * This actually depends on the configuration. |
| 97 | * |
| 98 | * In this case process_cpu_topology() prints error message: |
| 99 | * "socket_id number is too big. You may need to upgrade the |
| 100 | * perf tool." |
| 101 | * |
James Clark | 23331ee | 2020-11-26 16:13:17 +0200 | [diff] [blame] | 102 | * This is the reason why this test might be skipped. aarch64 and |
| 103 | * s390 always write this part of the header, even when the above |
| 104 | * condition is true (see do_core_id_test in header.c). So always |
| 105 | * run this test on those platforms. |
Thomas Richter | d121109 | 2018-05-28 09:36:57 +0200 | [diff] [blame] | 106 | */ |
James Clark | 23331ee | 2020-11-26 16:13:17 +0200 | [diff] [blame] | 107 | if (!session->header.env.cpu |
| 108 | && strncmp(session->header.env.arch, "s390", 4) |
| 109 | && strncmp(session->header.env.arch, "aarch64", 7)) |
Thomas Richter | d121109 | 2018-05-28 09:36:57 +0200 | [diff] [blame] | 110 | return TEST_SKIP; |
| 111 | |
James Clark | 23331ee | 2020-11-26 16:13:17 +0200 | [diff] [blame] | 112 | TEST_ASSERT_VAL("Session header CPU map not set", session->header.env.cpu); |
| 113 | |
Jan Stancek | da8a58b | 2017-02-17 12:10:26 +0100 | [diff] [blame] | 114 | for (i = 0; i < session->header.env.nr_cpus_avail; i++) { |
| 115 | if (!cpu_map__has(map, i)) |
| 116 | continue; |
Kan Liang | c84974e | 2015-09-04 04:58:31 -0400 | [diff] [blame] | 117 | pr_debug("CPU %d, core %d, socket %d\n", i, |
| 118 | session->header.env.cpu[i].core_id, |
| 119 | session->header.env.cpu[i].socket_id); |
| 120 | } |
| 121 | |
James Clark | 23331ee | 2020-11-26 16:13:17 +0200 | [diff] [blame] | 122 | // Test that core ID contains socket, die and core |
Kan Liang | c84974e | 2015-09-04 04:58:31 -0400 | [diff] [blame] | 123 | for (i = 0; i < map->nr; i++) { |
James Clark | 23331ee | 2020-11-26 16:13:17 +0200 | [diff] [blame] | 124 | id = cpu_map__get_core(map, i, NULL); |
| 125 | TEST_ASSERT_VAL("Core map - Core ID doesn't match", |
James Clark | b993381 | 2020-11-26 16:13:27 +0200 | [diff] [blame] | 126 | session->header.env.cpu[map->map[i]].core_id == id.core); |
Kan Liang | c84974e | 2015-09-04 04:58:31 -0400 | [diff] [blame] | 127 | |
James Clark | 23331ee | 2020-11-26 16:13:17 +0200 | [diff] [blame] | 128 | TEST_ASSERT_VAL("Core map - Socket ID doesn't match", |
James Clark | 1a270cb | 2020-11-26 16:13:25 +0200 | [diff] [blame] | 129 | session->header.env.cpu[map->map[i]].socket_id == id.socket); |
James Clark | 23331ee | 2020-11-26 16:13:17 +0200 | [diff] [blame] | 130 | |
| 131 | TEST_ASSERT_VAL("Core map - Die ID doesn't match", |
James Clark | ba2ee16 | 2020-11-26 16:13:26 +0200 | [diff] [blame] | 132 | session->header.env.cpu[map->map[i]].die_id == id.die); |
James Clark | fcd83a3 | 2020-11-26 16:13:24 +0200 | [diff] [blame] | 133 | TEST_ASSERT_VAL("Core map - Node ID is set", id.node == -1); |
James Clark | 8d4852b | 2020-11-26 16:13:28 +0200 | [diff] [blame] | 134 | TEST_ASSERT_VAL("Core map - Thread is set", id.thread == -1); |
Kan Liang | c84974e | 2015-09-04 04:58:31 -0400 | [diff] [blame] | 135 | } |
| 136 | |
James Clark | 23331ee | 2020-11-26 16:13:17 +0200 | [diff] [blame] | 137 | // Test that die ID contains socket and die |
| 138 | for (i = 0; i < map->nr; i++) { |
| 139 | id = cpu_map__get_die(map, i, NULL); |
| 140 | TEST_ASSERT_VAL("Die map - Socket ID doesn't match", |
James Clark | 1a270cb | 2020-11-26 16:13:25 +0200 | [diff] [blame] | 141 | session->header.env.cpu[map->map[i]].socket_id == id.socket); |
James Clark | 23331ee | 2020-11-26 16:13:17 +0200 | [diff] [blame] | 142 | |
| 143 | TEST_ASSERT_VAL("Die map - Die ID doesn't match", |
James Clark | ba2ee16 | 2020-11-26 16:13:26 +0200 | [diff] [blame] | 144 | session->header.env.cpu[map->map[i]].die_id == id.die); |
James Clark | fcd83a3 | 2020-11-26 16:13:24 +0200 | [diff] [blame] | 145 | |
| 146 | TEST_ASSERT_VAL("Die map - Node ID is set", id.node == -1); |
James Clark | b993381 | 2020-11-26 16:13:27 +0200 | [diff] [blame] | 147 | TEST_ASSERT_VAL("Die map - Core is set", id.core == -1); |
James Clark | 8d4852b | 2020-11-26 16:13:28 +0200 | [diff] [blame] | 148 | TEST_ASSERT_VAL("Die map - Thread is set", id.thread == -1); |
James Clark | 23331ee | 2020-11-26 16:13:17 +0200 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | // Test that socket ID contains only socket |
| 152 | for (i = 0; i < map->nr; i++) { |
| 153 | id = cpu_map__get_socket(map, i, NULL); |
| 154 | TEST_ASSERT_VAL("Socket map - Socket ID doesn't match", |
James Clark | 1a270cb | 2020-11-26 16:13:25 +0200 | [diff] [blame] | 155 | session->header.env.cpu[map->map[i]].socket_id == id.socket); |
James Clark | fcd83a3 | 2020-11-26 16:13:24 +0200 | [diff] [blame] | 156 | |
| 157 | TEST_ASSERT_VAL("Socket map - Node ID is set", id.node == -1); |
James Clark | ba2ee16 | 2020-11-26 16:13:26 +0200 | [diff] [blame] | 158 | TEST_ASSERT_VAL("Socket map - Die ID is set", id.die == -1); |
James Clark | b993381 | 2020-11-26 16:13:27 +0200 | [diff] [blame] | 159 | TEST_ASSERT_VAL("Socket map - Core is set", id.core == -1); |
James Clark | 8d4852b | 2020-11-26 16:13:28 +0200 | [diff] [blame] | 160 | TEST_ASSERT_VAL("Socket map - Thread is set", id.thread == -1); |
James Clark | 23331ee | 2020-11-26 16:13:17 +0200 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | // Test that node ID contains only node |
| 164 | for (i = 0; i < map->nr; i++) { |
| 165 | id = cpu_map__get_node(map, i, NULL); |
| 166 | TEST_ASSERT_VAL("Node map - Node ID doesn't match", |
James Clark | fcd83a3 | 2020-11-26 16:13:24 +0200 | [diff] [blame] | 167 | cpu__get_node(map->map[i]) == id.node); |
James Clark | 1a270cb | 2020-11-26 16:13:25 +0200 | [diff] [blame] | 168 | TEST_ASSERT_VAL("Node map - Socket is set", id.socket == -1); |
James Clark | ba2ee16 | 2020-11-26 16:13:26 +0200 | [diff] [blame] | 169 | TEST_ASSERT_VAL("Node map - Die ID is set", id.die == -1); |
James Clark | b993381 | 2020-11-26 16:13:27 +0200 | [diff] [blame] | 170 | TEST_ASSERT_VAL("Node map - Core is set", id.core == -1); |
James Clark | 8d4852b | 2020-11-26 16:13:28 +0200 | [diff] [blame] | 171 | TEST_ASSERT_VAL("Node map - Thread is set", id.thread == -1); |
James Clark | 23331ee | 2020-11-26 16:13:17 +0200 | [diff] [blame] | 172 | } |
Kan Liang | c84974e | 2015-09-04 04:58:31 -0400 | [diff] [blame] | 173 | perf_session__delete(session); |
| 174 | |
| 175 | return 0; |
| 176 | } |
| 177 | |
Ian Rogers | 33f44bf | 2021-11-03 23:41:51 -0700 | [diff] [blame] | 178 | static int test__session_topology(struct test_suite *test __maybe_unused, int subtest __maybe_unused) |
Kan Liang | c84974e | 2015-09-04 04:58:31 -0400 | [diff] [blame] | 179 | { |
| 180 | char path[PATH_MAX]; |
Jiri Olsa | f854839 | 2019-07-21 13:23:49 +0200 | [diff] [blame] | 181 | struct perf_cpu_map *map; |
Thomas Richter | d121109 | 2018-05-28 09:36:57 +0200 | [diff] [blame] | 182 | int ret = TEST_FAIL; |
Kan Liang | c84974e | 2015-09-04 04:58:31 -0400 | [diff] [blame] | 183 | |
| 184 | TEST_ASSERT_VAL("can't get templ file", !get_temp(path)); |
| 185 | |
| 186 | pr_debug("templ file: %s\n", path); |
| 187 | |
| 188 | if (session_write_header(path)) |
| 189 | goto free_path; |
| 190 | |
Jiri Olsa | 9c3516d | 2019-07-21 13:24:30 +0200 | [diff] [blame] | 191 | map = perf_cpu_map__new(NULL); |
Kan Liang | c84974e | 2015-09-04 04:58:31 -0400 | [diff] [blame] | 192 | if (map == NULL) { |
| 193 | pr_debug("failed to get system cpumap\n"); |
| 194 | goto free_path; |
| 195 | } |
| 196 | |
Thomas Richter | d121109 | 2018-05-28 09:36:57 +0200 | [diff] [blame] | 197 | ret = check_cpu_topology(path, map); |
Jiri Olsa | 38f01d8 | 2019-07-21 13:24:17 +0200 | [diff] [blame] | 198 | perf_cpu_map__put(map); |
Thomas Richter | d121109 | 2018-05-28 09:36:57 +0200 | [diff] [blame] | 199 | |
Kan Liang | c84974e | 2015-09-04 04:58:31 -0400 | [diff] [blame] | 200 | free_path: |
| 201 | unlink(path); |
| 202 | return ret; |
| 203 | } |
Ian Rogers | d68f036 | 2021-11-03 23:41:50 -0700 | [diff] [blame] | 204 | |
| 205 | DEFINE_SUITE("Session topology", session_topology); |