]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - tools/perf/tests/topology.c
License cleanup: add SPDX GPL-2.0 license identifier to files with no license
[mirror_ubuntu-bionic-kernel.git] / tools / perf / tests / topology.c
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"
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;
33 struct perf_data_file file = {
34 .path = path,
35 .mode = PERF_DATA_MODE_WRITE,
36 };
37
38 session = perf_session__new(&file, false, NULL);
39 TEST_ASSERT_VAL("can't get session", session);
40
41 session->evlist = perf_evlist__new_default();
42 TEST_ASSERT_VAL("can't get evlist", session->evlist);
43
44 perf_header__set_feat(&session->header, HEADER_CPU_TOPOLOGY);
45 perf_header__set_feat(&session->header, HEADER_NRCPUS);
46
47 session->header.data_size += DATA_SIZE;
48
49 TEST_ASSERT_VAL("failed to write header",
50 !perf_session__write_header(session, session->evlist, file.fd, true));
51
52 perf_session__delete(session);
53
54 return 0;
55 }
56
57 static int check_cpu_topology(char *path, struct cpu_map *map)
58 {
59 struct perf_session *session;
60 struct perf_data_file file = {
61 .path = path,
62 .mode = PERF_DATA_MODE_READ,
63 };
64 int i;
65
66 session = perf_session__new(&file, false, NULL);
67 TEST_ASSERT_VAL("can't get session", session);
68
69 for (i = 0; i < session->header.env.nr_cpus_avail; i++) {
70 if (!cpu_map__has(map, i))
71 continue;
72 pr_debug("CPU %d, core %d, socket %d\n", i,
73 session->header.env.cpu[i].core_id,
74 session->header.env.cpu[i].socket_id);
75 }
76
77 for (i = 0; i < map->nr; i++) {
78 TEST_ASSERT_VAL("Core ID doesn't match",
79 (session->header.env.cpu[map->map[i]].core_id == (cpu_map__get_core(map, i, NULL) & 0xffff)));
80
81 TEST_ASSERT_VAL("Socket ID doesn't match",
82 (session->header.env.cpu[map->map[i]].socket_id == cpu_map__get_socket(map, i, NULL)));
83 }
84
85 perf_session__delete(session);
86
87 return 0;
88 }
89
90 int test__session_topology(struct test *test __maybe_unused, int subtest __maybe_unused)
91 {
92 char path[PATH_MAX];
93 struct cpu_map *map;
94 int ret = -1;
95
96 TEST_ASSERT_VAL("can't get templ file", !get_temp(path));
97
98 pr_debug("templ file: %s\n", path);
99
100 if (session_write_header(path))
101 goto free_path;
102
103 map = cpu_map__new(NULL);
104 if (map == NULL) {
105 pr_debug("failed to get system cpumap\n");
106 goto free_path;
107 }
108
109 if (check_cpu_topology(path, map))
110 goto free_map;
111 ret = 0;
112
113 free_map:
114 cpu_map__put(map);
115 free_path:
116 unlink(path);
117 return ret;
118 }