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" |
| 11 | |
| 12 | #define TEMPL "/tmp/perf-test-XXXXXX" |
| 13 | #define DATA_SIZE 10 |
| 14 | |
| 15 | static int get_temp(char *path) |
| 16 | { |
| 17 | int fd; |
| 18 | |
| 19 | strcpy(path, TEMPL); |
| 20 | |
| 21 | fd = mkstemp(path); |
| 22 | if (fd < 0) { |
| 23 | perror("mkstemp failed"); |
| 24 | return -1; |
| 25 | } |
| 26 | |
| 27 | close(fd); |
| 28 | return 0; |
| 29 | } |
| 30 | |
| 31 | static int session_write_header(char *path) |
| 32 | { |
| 33 | struct perf_session *session; |
Jiri Olsa | 8ceb41d | 2017-01-23 22:07:59 +0100 | [diff] [blame] | 34 | struct perf_data data = { |
Jiri Olsa | eae8ad8 | 2017-01-23 22:25:41 +0100 | [diff] [blame] | 35 | .file = { |
| 36 | .path = path, |
| 37 | }, |
| 38 | .mode = PERF_DATA_MODE_WRITE, |
Kan Liang | c84974e | 2015-09-04 04:58:31 -0400 | [diff] [blame] | 39 | }; |
| 40 | |
Jiri Olsa | 8ceb41d | 2017-01-23 22:07:59 +0100 | [diff] [blame] | 41 | session = perf_session__new(&data, false, NULL); |
Kan Liang | c84974e | 2015-09-04 04:58:31 -0400 | [diff] [blame] | 42 | TEST_ASSERT_VAL("can't get session", session); |
| 43 | |
| 44 | session->evlist = perf_evlist__new_default(); |
| 45 | TEST_ASSERT_VAL("can't get evlist", session->evlist); |
| 46 | |
| 47 | perf_header__set_feat(&session->header, HEADER_CPU_TOPOLOGY); |
| 48 | perf_header__set_feat(&session->header, HEADER_NRCPUS); |
Thomas Richter | b930e62 | 2018-06-11 09:31:53 +0200 | [diff] [blame] | 49 | perf_header__set_feat(&session->header, HEADER_ARCH); |
Kan Liang | c84974e | 2015-09-04 04:58:31 -0400 | [diff] [blame] | 50 | |
| 51 | session->header.data_size += DATA_SIZE; |
| 52 | |
| 53 | TEST_ASSERT_VAL("failed to write header", |
Jiri Olsa | eae8ad8 | 2017-01-23 22:25:41 +0100 | [diff] [blame] | 54 | !perf_session__write_header(session, session->evlist, data.file.fd, true)); |
Kan Liang | c84974e | 2015-09-04 04:58:31 -0400 | [diff] [blame] | 55 | |
| 56 | perf_session__delete(session); |
| 57 | |
| 58 | return 0; |
| 59 | } |
| 60 | |
Jiri Olsa | f854839 | 2019-07-21 13:23:49 +0200 | [diff] [blame] | 61 | static int check_cpu_topology(char *path, struct perf_cpu_map *map) |
Kan Liang | c84974e | 2015-09-04 04:58:31 -0400 | [diff] [blame] | 62 | { |
| 63 | struct perf_session *session; |
Jiri Olsa | 8ceb41d | 2017-01-23 22:07:59 +0100 | [diff] [blame] | 64 | struct perf_data data = { |
Jiri Olsa | eae8ad8 | 2017-01-23 22:25:41 +0100 | [diff] [blame] | 65 | .file = { |
| 66 | .path = path, |
| 67 | }, |
| 68 | .mode = PERF_DATA_MODE_READ, |
Kan Liang | c84974e | 2015-09-04 04:58:31 -0400 | [diff] [blame] | 69 | }; |
| 70 | int i; |
| 71 | |
Jiri Olsa | 8ceb41d | 2017-01-23 22:07:59 +0100 | [diff] [blame] | 72 | session = perf_session__new(&data, false, NULL); |
Kan Liang | c84974e | 2015-09-04 04:58:31 -0400 | [diff] [blame] | 73 | TEST_ASSERT_VAL("can't get session", session); |
| 74 | |
Thomas Richter | d121109 | 2018-05-28 09:36:57 +0200 | [diff] [blame] | 75 | /* On platforms with large numbers of CPUs process_cpu_topology() |
| 76 | * might issue an error while reading the perf.data file section |
| 77 | * HEADER_CPU_TOPOLOGY and the cpu_topology_map pointed to by member |
| 78 | * cpu is a NULL pointer. |
| 79 | * Example: On s390 |
| 80 | * CPU 0 is on core_id 0 and physical_package_id 6 |
| 81 | * CPU 1 is on core_id 1 and physical_package_id 3 |
| 82 | * |
| 83 | * Core_id and physical_package_id are platform and architecture |
| 84 | * dependend and might have higher numbers than the CPU id. |
| 85 | * This actually depends on the configuration. |
| 86 | * |
| 87 | * In this case process_cpu_topology() prints error message: |
| 88 | * "socket_id number is too big. You may need to upgrade the |
| 89 | * perf tool." |
| 90 | * |
| 91 | * This is the reason why this test might be skipped. |
| 92 | */ |
| 93 | if (!session->header.env.cpu) |
| 94 | return TEST_SKIP; |
| 95 | |
Jan Stancek | da8a58b | 2017-02-17 12:10:26 +0100 | [diff] [blame] | 96 | for (i = 0; i < session->header.env.nr_cpus_avail; i++) { |
| 97 | if (!cpu_map__has(map, i)) |
| 98 | continue; |
Kan Liang | c84974e | 2015-09-04 04:58:31 -0400 | [diff] [blame] | 99 | pr_debug("CPU %d, core %d, socket %d\n", i, |
| 100 | session->header.env.cpu[i].core_id, |
| 101 | session->header.env.cpu[i].socket_id); |
| 102 | } |
| 103 | |
| 104 | for (i = 0; i < map->nr; i++) { |
| 105 | TEST_ASSERT_VAL("Core ID doesn't match", |
Jiri Olsa | 1fe7a30 | 2015-10-16 12:41:15 +0200 | [diff] [blame] | 106 | (session->header.env.cpu[map->map[i]].core_id == (cpu_map__get_core(map, i, NULL) & 0xffff))); |
Kan Liang | c84974e | 2015-09-04 04:58:31 -0400 | [diff] [blame] | 107 | |
| 108 | TEST_ASSERT_VAL("Socket ID doesn't match", |
Jiri Olsa | 1fe7a30 | 2015-10-16 12:41:15 +0200 | [diff] [blame] | 109 | (session->header.env.cpu[map->map[i]].socket_id == cpu_map__get_socket(map, i, NULL))); |
Kan Liang | c84974e | 2015-09-04 04:58:31 -0400 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | perf_session__delete(session); |
| 113 | |
| 114 | return 0; |
| 115 | } |
| 116 | |
Arnaldo Carvalho de Melo | 81f17c9 | 2017-08-03 15:16:31 -0300 | [diff] [blame] | 117 | 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] | 118 | { |
| 119 | char path[PATH_MAX]; |
Jiri Olsa | f854839 | 2019-07-21 13:23:49 +0200 | [diff] [blame] | 120 | struct perf_cpu_map *map; |
Thomas Richter | d121109 | 2018-05-28 09:36:57 +0200 | [diff] [blame] | 121 | int ret = TEST_FAIL; |
Kan Liang | c84974e | 2015-09-04 04:58:31 -0400 | [diff] [blame] | 122 | |
| 123 | TEST_ASSERT_VAL("can't get templ file", !get_temp(path)); |
| 124 | |
| 125 | pr_debug("templ file: %s\n", path); |
| 126 | |
| 127 | if (session_write_header(path)) |
| 128 | goto free_path; |
| 129 | |
Jiri Olsa | 9c3516d | 2019-07-21 13:24:30 +0200 | [diff] [blame] | 130 | map = perf_cpu_map__new(NULL); |
Kan Liang | c84974e | 2015-09-04 04:58:31 -0400 | [diff] [blame] | 131 | if (map == NULL) { |
| 132 | pr_debug("failed to get system cpumap\n"); |
| 133 | goto free_path; |
| 134 | } |
| 135 | |
Thomas Richter | d121109 | 2018-05-28 09:36:57 +0200 | [diff] [blame] | 136 | ret = check_cpu_topology(path, map); |
Jiri Olsa | 38f01d8 | 2019-07-21 13:24:17 +0200 | [diff] [blame] | 137 | perf_cpu_map__put(map); |
Thomas Richter | d121109 | 2018-05-28 09:36:57 +0200 | [diff] [blame] | 138 | |
Kan Liang | c84974e | 2015-09-04 04:58:31 -0400 | [diff] [blame] | 139 | free_path: |
| 140 | unlink(path); |
| 141 | return ret; |
| 142 | } |