Merge branch 'locking-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[cris-mirror.git] / tools / perf / tests / topology.c
blob17cb1bb3448c842930df519d1bad7077777a098d
1 // SPDX-License-Identifier: GPL-2.0
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"
11 #define TEMPL "/tmp/perf-test-XXXXXX"
12 #define DATA_SIZE 10
14 static int get_temp(char *path)
16 int fd;
18 strcpy(path, TEMPL);
20 fd = mkstemp(path);
21 if (fd < 0) {
22 perror("mkstemp failed");
23 return -1;
26 close(fd);
27 return 0;
30 static int session_write_header(char *path)
32 struct perf_session *session;
33 struct perf_data data = {
34 .file = {
35 .path = path,
37 .mode = PERF_DATA_MODE_WRITE,
40 session = perf_session__new(&data, false, NULL);
41 TEST_ASSERT_VAL("can't get session", session);
43 session->evlist = perf_evlist__new_default();
44 TEST_ASSERT_VAL("can't get evlist", session->evlist);
46 perf_header__set_feat(&session->header, HEADER_CPU_TOPOLOGY);
47 perf_header__set_feat(&session->header, HEADER_NRCPUS);
49 session->header.data_size += DATA_SIZE;
51 TEST_ASSERT_VAL("failed to write header",
52 !perf_session__write_header(session, session->evlist, data.file.fd, true));
54 perf_session__delete(session);
56 return 0;
59 static int check_cpu_topology(char *path, struct cpu_map *map)
61 struct perf_session *session;
62 struct perf_data data = {
63 .file = {
64 .path = path,
66 .mode = PERF_DATA_MODE_READ,
68 int i;
70 session = perf_session__new(&data, false, NULL);
71 TEST_ASSERT_VAL("can't get session", session);
73 for (i = 0; i < session->header.env.nr_cpus_avail; i++) {
74 if (!cpu_map__has(map, i))
75 continue;
76 pr_debug("CPU %d, core %d, socket %d\n", i,
77 session->header.env.cpu[i].core_id,
78 session->header.env.cpu[i].socket_id);
81 for (i = 0; i < map->nr; i++) {
82 TEST_ASSERT_VAL("Core ID doesn't match",
83 (session->header.env.cpu[map->map[i]].core_id == (cpu_map__get_core(map, i, NULL) & 0xffff)));
85 TEST_ASSERT_VAL("Socket ID doesn't match",
86 (session->header.env.cpu[map->map[i]].socket_id == cpu_map__get_socket(map, i, NULL)));
89 perf_session__delete(session);
91 return 0;
94 int test__session_topology(struct test *test __maybe_unused, int subtest __maybe_unused)
96 char path[PATH_MAX];
97 struct cpu_map *map;
98 int ret = -1;
100 TEST_ASSERT_VAL("can't get templ file", !get_temp(path));
102 pr_debug("templ file: %s\n", path);
104 if (session_write_header(path))
105 goto free_path;
107 map = cpu_map__new(NULL);
108 if (map == NULL) {
109 pr_debug("failed to get system cpumap\n");
110 goto free_path;
113 if (check_cpu_topology(path, map))
114 goto free_map;
115 ret = 0;
117 free_map:
118 cpu_map__put(map);
119 free_path:
120 unlink(path);
121 return ret;