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" |
Mamatha Inamdar | 6ef81c5 | 2019-08-22 12:50:49 +0530 | [diff] [blame] | 11 | #include <linux/err.h> |
Kan Liang | c84974e | 2015-09-04 04:58:31 -0400 | [diff] [blame] | 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; |
Jiri Olsa | 8ceb41d | 2017-01-23 22:07:59 +0100 | [diff] [blame] | 35 | struct perf_data data = { |
Tommi Rantala | dbd660e | 2020-04-23 14:53:40 +0300 | [diff] [blame] | 36 | .path = path, |
| 37 | .mode = PERF_DATA_MODE_WRITE, |
Kan Liang | c84974e | 2015-09-04 04:58:31 -0400 | [diff] [blame] | 38 | }; |
| 39 | |
Jiri Olsa | 8ceb41d | 2017-01-23 22:07:59 +0100 | [diff] [blame] | 40 | session = perf_session__new(&data, false, NULL); |
Mamatha Inamdar | 6ef81c5 | 2019-08-22 12:50:49 +0530 | [diff] [blame] | 41 | TEST_ASSERT_VAL("can't get session", !IS_ERR(session)); |
Kan Liang | c84974e | 2015-09-04 04:58:31 -0400 | [diff] [blame] | 42 | |
Arnaldo Carvalho de Melo | 606e2c2 | 2020-11-30 15:04:05 -0300 | [diff] [blame] | 43 | session->evlist = evlist__new_default(); |
Kan Liang | c84974e | 2015-09-04 04:58:31 -0400 | [diff] [blame] | 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); |
Thomas Richter | b930e62 | 2018-06-11 09:31:53 +0200 | [diff] [blame] | 48 | perf_header__set_feat(&session->header, HEADER_ARCH); |
Kan Liang | c84974e | 2015-09-04 04:58:31 -0400 | [diff] [blame] | 49 | |
| 50 | session->header.data_size += DATA_SIZE; |
| 51 | |
| 52 | TEST_ASSERT_VAL("failed to write header", |
Jiri Olsa | eae8ad8 | 2017-01-23 22:25:41 +0100 | [diff] [blame] | 53 | !perf_session__write_header(session, session->evlist, data.file.fd, true)); |
Kan Liang | c84974e | 2015-09-04 04:58:31 -0400 | [diff] [blame] | 54 | |
| 55 | perf_session__delete(session); |
| 56 | |
| 57 | return 0; |
| 58 | } |
| 59 | |
Jiri Olsa | f854839 | 2019-07-21 13:23:49 +0200 | [diff] [blame] | 60 | static int check_cpu_topology(char *path, struct perf_cpu_map *map) |
Kan Liang | c84974e | 2015-09-04 04:58:31 -0400 | [diff] [blame] | 61 | { |
| 62 | struct perf_session *session; |
Jiri Olsa | 8ceb41d | 2017-01-23 22:07:59 +0100 | [diff] [blame] | 63 | struct perf_data data = { |
Tommi Rantala | dbd660e | 2020-04-23 14:53:40 +0300 | [diff] [blame] | 64 | .path = path, |
| 65 | .mode = PERF_DATA_MODE_READ, |
Kan Liang | c84974e | 2015-09-04 04:58:31 -0400 | [diff] [blame] | 66 | }; |
James Clark | 23331ee | 2020-11-26 16:13:17 +0200 | [diff] [blame^] | 67 | int i, id; |
Kan Liang | c84974e | 2015-09-04 04:58:31 -0400 | [diff] [blame] | 68 | |
Jiri Olsa | 8ceb41d | 2017-01-23 22:07:59 +0100 | [diff] [blame] | 69 | session = perf_session__new(&data, false, NULL); |
Mamatha Inamdar | 6ef81c5 | 2019-08-22 12:50:49 +0530 | [diff] [blame] | 70 | TEST_ASSERT_VAL("can't get session", !IS_ERR(session)); |
James Clark | 23331ee | 2020-11-26 16:13:17 +0200 | [diff] [blame^] | 71 | cpu__setup_cpunode_map(); |
Kan Liang | c84974e | 2015-09-04 04:58:31 -0400 | [diff] [blame] | 72 | |
Thomas Richter | d121109 | 2018-05-28 09:36:57 +0200 | [diff] [blame] | 73 | /* On platforms with large numbers of CPUs process_cpu_topology() |
| 74 | * might issue an error while reading the perf.data file section |
| 75 | * HEADER_CPU_TOPOLOGY and the cpu_topology_map pointed to by member |
| 76 | * cpu is a NULL pointer. |
| 77 | * Example: On s390 |
| 78 | * CPU 0 is on core_id 0 and physical_package_id 6 |
| 79 | * CPU 1 is on core_id 1 and physical_package_id 3 |
| 80 | * |
| 81 | * Core_id and physical_package_id are platform and architecture |
| 82 | * dependend and might have higher numbers than the CPU id. |
| 83 | * This actually depends on the configuration. |
| 84 | * |
| 85 | * In this case process_cpu_topology() prints error message: |
| 86 | * "socket_id number is too big. You may need to upgrade the |
| 87 | * perf tool." |
| 88 | * |
James Clark | 23331ee | 2020-11-26 16:13:17 +0200 | [diff] [blame^] | 89 | * This is the reason why this test might be skipped. aarch64 and |
| 90 | * s390 always write this part of the header, even when the above |
| 91 | * condition is true (see do_core_id_test in header.c). So always |
| 92 | * run this test on those platforms. |
Thomas Richter | d121109 | 2018-05-28 09:36:57 +0200 | [diff] [blame] | 93 | */ |
James Clark | 23331ee | 2020-11-26 16:13:17 +0200 | [diff] [blame^] | 94 | if (!session->header.env.cpu |
| 95 | && strncmp(session->header.env.arch, "s390", 4) |
| 96 | && strncmp(session->header.env.arch, "aarch64", 7)) |
Thomas Richter | d121109 | 2018-05-28 09:36:57 +0200 | [diff] [blame] | 97 | return TEST_SKIP; |
| 98 | |
James Clark | 23331ee | 2020-11-26 16:13:17 +0200 | [diff] [blame^] | 99 | TEST_ASSERT_VAL("Session header CPU map not set", session->header.env.cpu); |
| 100 | |
Jan Stancek | da8a58b | 2017-02-17 12:10:26 +0100 | [diff] [blame] | 101 | for (i = 0; i < session->header.env.nr_cpus_avail; i++) { |
| 102 | if (!cpu_map__has(map, i)) |
| 103 | continue; |
Kan Liang | c84974e | 2015-09-04 04:58:31 -0400 | [diff] [blame] | 104 | pr_debug("CPU %d, core %d, socket %d\n", i, |
| 105 | session->header.env.cpu[i].core_id, |
| 106 | session->header.env.cpu[i].socket_id); |
| 107 | } |
| 108 | |
James Clark | 23331ee | 2020-11-26 16:13:17 +0200 | [diff] [blame^] | 109 | // Test that core ID contains socket, die and core |
Kan Liang | c84974e | 2015-09-04 04:58:31 -0400 | [diff] [blame] | 110 | for (i = 0; i < map->nr; i++) { |
James Clark | 23331ee | 2020-11-26 16:13:17 +0200 | [diff] [blame^] | 111 | id = cpu_map__get_core(map, i, NULL); |
| 112 | TEST_ASSERT_VAL("Core map - Core ID doesn't match", |
| 113 | session->header.env.cpu[map->map[i]].core_id == cpu_map__id_to_cpu(id)); |
Kan Liang | c84974e | 2015-09-04 04:58:31 -0400 | [diff] [blame] | 114 | |
James Clark | 23331ee | 2020-11-26 16:13:17 +0200 | [diff] [blame^] | 115 | TEST_ASSERT_VAL("Core map - Socket ID doesn't match", |
| 116 | session->header.env.cpu[map->map[i]].socket_id == |
| 117 | cpu_map__id_to_socket(id)); |
| 118 | |
| 119 | TEST_ASSERT_VAL("Core map - Die ID doesn't match", |
| 120 | session->header.env.cpu[map->map[i]].die_id == cpu_map__id_to_die(id)); |
Kan Liang | c84974e | 2015-09-04 04:58:31 -0400 | [diff] [blame] | 121 | } |
| 122 | |
James Clark | 23331ee | 2020-11-26 16:13:17 +0200 | [diff] [blame^] | 123 | // Test that die ID contains socket and die |
| 124 | for (i = 0; i < map->nr; i++) { |
| 125 | id = cpu_map__get_die(map, i, NULL); |
| 126 | TEST_ASSERT_VAL("Die map - Socket ID doesn't match", |
| 127 | session->header.env.cpu[map->map[i]].socket_id == |
| 128 | cpu_map__id_to_socket(id << 16)); |
| 129 | |
| 130 | TEST_ASSERT_VAL("Die map - Die ID doesn't match", |
| 131 | session->header.env.cpu[map->map[i]].die_id == |
| 132 | cpu_map__id_to_die(id << 16)); |
| 133 | } |
| 134 | |
| 135 | // Test that socket ID contains only socket |
| 136 | for (i = 0; i < map->nr; i++) { |
| 137 | id = cpu_map__get_socket(map, i, NULL); |
| 138 | TEST_ASSERT_VAL("Socket map - Socket ID doesn't match", |
| 139 | session->header.env.cpu[map->map[i]].socket_id == id); |
| 140 | } |
| 141 | |
| 142 | // Test that node ID contains only node |
| 143 | for (i = 0; i < map->nr; i++) { |
| 144 | id = cpu_map__get_node(map, i, NULL); |
| 145 | TEST_ASSERT_VAL("Node map - Node ID doesn't match", |
| 146 | cpu__get_node(map->map[i]) == id); |
| 147 | } |
Kan Liang | c84974e | 2015-09-04 04:58:31 -0400 | [diff] [blame] | 148 | perf_session__delete(session); |
| 149 | |
| 150 | return 0; |
| 151 | } |
| 152 | |
Arnaldo Carvalho de Melo | 81f17c9 | 2017-08-03 15:16:31 -0300 | [diff] [blame] | 153 | int test__session_topology(struct test *test __maybe_unused, int subtest __maybe_unused) |
Kan Liang | c84974e | 2015-09-04 04:58:31 -0400 | [diff] [blame] | 154 | { |
| 155 | char path[PATH_MAX]; |
Jiri Olsa | f854839 | 2019-07-21 13:23:49 +0200 | [diff] [blame] | 156 | struct perf_cpu_map *map; |
Thomas Richter | d121109 | 2018-05-28 09:36:57 +0200 | [diff] [blame] | 157 | int ret = TEST_FAIL; |
Kan Liang | c84974e | 2015-09-04 04:58:31 -0400 | [diff] [blame] | 158 | |
| 159 | TEST_ASSERT_VAL("can't get templ file", !get_temp(path)); |
| 160 | |
| 161 | pr_debug("templ file: %s\n", path); |
| 162 | |
| 163 | if (session_write_header(path)) |
| 164 | goto free_path; |
| 165 | |
Jiri Olsa | 9c3516d | 2019-07-21 13:24:30 +0200 | [diff] [blame] | 166 | map = perf_cpu_map__new(NULL); |
Kan Liang | c84974e | 2015-09-04 04:58:31 -0400 | [diff] [blame] | 167 | if (map == NULL) { |
| 168 | pr_debug("failed to get system cpumap\n"); |
| 169 | goto free_path; |
| 170 | } |
| 171 | |
Thomas Richter | d121109 | 2018-05-28 09:36:57 +0200 | [diff] [blame] | 172 | ret = check_cpu_topology(path, map); |
Jiri Olsa | 38f01d8 | 2019-07-21 13:24:17 +0200 | [diff] [blame] | 173 | perf_cpu_map__put(map); |
Thomas Richter | d121109 | 2018-05-28 09:36:57 +0200 | [diff] [blame] | 174 | |
Kan Liang | c84974e | 2015-09-04 04:58:31 -0400 | [diff] [blame] | 175 | free_path: |
| 176 | unlink(path); |
| 177 | return ret; |
| 178 | } |