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