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> |
| 5 | #include "tests.h" |
| 6 | #include "util.h" |
| 7 | #include "session.h" |
| 8 | #include "evlist.h" |
| 9 | #include "debug.h" |
| 10 | |
| 11 | #define TEMPL "/tmp/perf-test-XXXXXX" |
| 12 | #define DATA_SIZE 10 |
| 13 | |
| 14 | static int get_temp(char *path) |
| 15 | { |
| 16 | int fd; |
| 17 | |
| 18 | strcpy(path, TEMPL); |
| 19 | |
| 20 | fd = mkstemp(path); |
| 21 | if (fd < 0) { |
| 22 | perror("mkstemp failed"); |
| 23 | return -1; |
| 24 | } |
| 25 | |
| 26 | close(fd); |
| 27 | return 0; |
| 28 | } |
| 29 | |
| 30 | static int session_write_header(char *path) |
| 31 | { |
| 32 | struct perf_session *session; |
Jiri Olsa | 8ceb41d | 2017-01-23 22:07:59 +0100 | [diff] [blame] | 33 | struct perf_data data = { |
Jiri Olsa | eae8ad8 | 2017-01-23 22:25:41 +0100 | [diff] [blame] | 34 | .file = { |
| 35 | .path = path, |
| 36 | }, |
| 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); |
Kan Liang | c84974e | 2015-09-04 04:58:31 -0400 | [diff] [blame] | 41 | TEST_ASSERT_VAL("can't get session", session); |
| 42 | |
| 43 | session->evlist = perf_evlist__new_default(); |
| 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); |
| 48 | |
| 49 | session->header.data_size += DATA_SIZE; |
| 50 | |
| 51 | TEST_ASSERT_VAL("failed to write header", |
Jiri Olsa | eae8ad8 | 2017-01-23 22:25:41 +0100 | [diff] [blame] | 52 | !perf_session__write_header(session, session->evlist, data.file.fd, true)); |
Kan Liang | c84974e | 2015-09-04 04:58:31 -0400 | [diff] [blame] | 53 | |
| 54 | perf_session__delete(session); |
| 55 | |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | static int check_cpu_topology(char *path, struct cpu_map *map) |
| 60 | { |
| 61 | struct perf_session *session; |
Jiri Olsa | 8ceb41d | 2017-01-23 22:07:59 +0100 | [diff] [blame] | 62 | struct perf_data data = { |
Jiri Olsa | eae8ad8 | 2017-01-23 22:25:41 +0100 | [diff] [blame] | 63 | .file = { |
| 64 | .path = path, |
| 65 | }, |
| 66 | .mode = PERF_DATA_MODE_READ, |
Kan Liang | c84974e | 2015-09-04 04:58:31 -0400 | [diff] [blame] | 67 | }; |
| 68 | int i; |
| 69 | |
Jiri Olsa | 8ceb41d | 2017-01-23 22:07:59 +0100 | [diff] [blame] | 70 | session = perf_session__new(&data, false, NULL); |
Kan Liang | c84974e | 2015-09-04 04:58:31 -0400 | [diff] [blame] | 71 | TEST_ASSERT_VAL("can't get session", session); |
| 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 | * |
| 89 | * This is the reason why this test might be skipped. |
| 90 | */ |
| 91 | if (!session->header.env.cpu) |
| 92 | return TEST_SKIP; |
| 93 | |
Jan Stancek | da8a58b | 2017-02-17 12:10:26 +0100 | [diff] [blame] | 94 | for (i = 0; i < session->header.env.nr_cpus_avail; i++) { |
| 95 | if (!cpu_map__has(map, i)) |
| 96 | continue; |
Kan Liang | c84974e | 2015-09-04 04:58:31 -0400 | [diff] [blame] | 97 | pr_debug("CPU %d, core %d, socket %d\n", i, |
| 98 | session->header.env.cpu[i].core_id, |
| 99 | session->header.env.cpu[i].socket_id); |
| 100 | } |
| 101 | |
| 102 | for (i = 0; i < map->nr; i++) { |
| 103 | TEST_ASSERT_VAL("Core ID doesn't match", |
Jiri Olsa | 1fe7a30 | 2015-10-16 12:41:15 +0200 | [diff] [blame] | 104 | (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] | 105 | |
| 106 | TEST_ASSERT_VAL("Socket ID doesn't match", |
Jiri Olsa | 1fe7a30 | 2015-10-16 12:41:15 +0200 | [diff] [blame] | 107 | (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] | 108 | } |
| 109 | |
| 110 | perf_session__delete(session); |
| 111 | |
| 112 | return 0; |
| 113 | } |
| 114 | |
Arnaldo Carvalho de Melo | 81f17c9 | 2017-08-03 15:16:31 -0300 | [diff] [blame] | 115 | 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] | 116 | { |
| 117 | char path[PATH_MAX]; |
| 118 | struct cpu_map *map; |
Thomas Richter | d121109 | 2018-05-28 09:36:57 +0200 | [diff] [blame^] | 119 | int ret = TEST_FAIL; |
Kan Liang | c84974e | 2015-09-04 04:58:31 -0400 | [diff] [blame] | 120 | |
| 121 | TEST_ASSERT_VAL("can't get templ file", !get_temp(path)); |
| 122 | |
| 123 | pr_debug("templ file: %s\n", path); |
| 124 | |
| 125 | if (session_write_header(path)) |
| 126 | goto free_path; |
| 127 | |
| 128 | map = cpu_map__new(NULL); |
| 129 | if (map == NULL) { |
| 130 | pr_debug("failed to get system cpumap\n"); |
| 131 | goto free_path; |
| 132 | } |
| 133 | |
Thomas Richter | d121109 | 2018-05-28 09:36:57 +0200 | [diff] [blame^] | 134 | ret = check_cpu_topology(path, map); |
Kan Liang | c84974e | 2015-09-04 04:58:31 -0400 | [diff] [blame] | 135 | cpu_map__put(map); |
Thomas Richter | d121109 | 2018-05-28 09:36:57 +0200 | [diff] [blame^] | 136 | |
Kan Liang | c84974e | 2015-09-04 04:58:31 -0400 | [diff] [blame] | 137 | free_path: |
| 138 | unlink(path); |
| 139 | return ret; |
| 140 | } |