perf stat aggregation: Add separate socket member
[linux-2.6-microblaze.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 <perf/cpumap.h>
6 #include "cpumap.h"
7 #include "tests.h"
8 #include "session.h"
9 #include "evlist.h"
10 #include "debug.h"
11 #include <linux/err.h>
12
13 #define TEMPL "/tmp/perf-test-XXXXXX"
14 #define DATA_SIZE       10
15
16 static int get_temp(char *path)
17 {
18         int fd;
19
20         strcpy(path, TEMPL);
21
22         fd = mkstemp(path);
23         if (fd < 0) {
24                 perror("mkstemp failed");
25                 return -1;
26         }
27
28         close(fd);
29         return 0;
30 }
31
32 static int session_write_header(char *path)
33 {
34         struct perf_session *session;
35         struct perf_data data = {
36                 .path = path,
37                 .mode = PERF_DATA_MODE_WRITE,
38         };
39
40         session = perf_session__new(&data, false, NULL);
41         TEST_ASSERT_VAL("can't get session", !IS_ERR(session));
42
43         session->evlist = 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         perf_header__set_feat(&session->header, HEADER_ARCH);
49
50         session->header.data_size += DATA_SIZE;
51
52         TEST_ASSERT_VAL("failed to write header",
53                         !perf_session__write_header(session, session->evlist, data.file.fd, true));
54
55         perf_session__delete(session);
56
57         return 0;
58 }
59
60 static int check_cpu_topology(char *path, struct perf_cpu_map *map)
61 {
62         struct perf_session *session;
63         struct perf_data data = {
64                 .path = path,
65                 .mode = PERF_DATA_MODE_READ,
66         };
67         int i;
68         struct aggr_cpu_id id;
69
70         session = perf_session__new(&data, false, NULL);
71         TEST_ASSERT_VAL("can't get session", !IS_ERR(session));
72         cpu__setup_cpunode_map();
73
74         /* On platforms with large numbers of CPUs process_cpu_topology()
75          * might issue an error while reading the perf.data file section
76          * HEADER_CPU_TOPOLOGY and the cpu_topology_map pointed to by member
77          * cpu is a NULL pointer.
78          * Example: On s390
79          *   CPU 0 is on core_id 0 and physical_package_id 6
80          *   CPU 1 is on core_id 1 and physical_package_id 3
81          *
82          *   Core_id and physical_package_id are platform and architecture
83          *   dependend and might have higher numbers than the CPU id.
84          *   This actually depends on the configuration.
85          *
86          *  In this case process_cpu_topology() prints error message:
87          *  "socket_id number is too big. You may need to upgrade the
88          *  perf tool."
89          *
90          *  This is the reason why this test might be skipped. aarch64 and
91          *  s390 always write this part of the header, even when the above
92          *  condition is true (see do_core_id_test in header.c). So always
93          *  run this test on those platforms.
94          */
95         if (!session->header.env.cpu
96                         && strncmp(session->header.env.arch, "s390", 4)
97                         && strncmp(session->header.env.arch, "aarch64", 7))
98                 return TEST_SKIP;
99
100         TEST_ASSERT_VAL("Session header CPU map not set", session->header.env.cpu);
101
102         for (i = 0; i < session->header.env.nr_cpus_avail; i++) {
103                 if (!cpu_map__has(map, i))
104                         continue;
105                 pr_debug("CPU %d, core %d, socket %d\n", i,
106                          session->header.env.cpu[i].core_id,
107                          session->header.env.cpu[i].socket_id);
108         }
109
110         // Test that core ID contains socket, die and core
111         for (i = 0; i < map->nr; i++) {
112                 id = cpu_map__get_core(map, i, NULL);
113                 TEST_ASSERT_VAL("Core map - Core ID doesn't match",
114                         session->header.env.cpu[map->map[i]].core_id == cpu_map__id_to_cpu(id.id));
115
116                 TEST_ASSERT_VAL("Core map - Socket ID doesn't match",
117                         session->header.env.cpu[map->map[i]].socket_id == id.socket);
118
119                 TEST_ASSERT_VAL("Core map - Die ID doesn't match",
120                         session->header.env.cpu[map->map[i]].die_id == cpu_map__id_to_die(id.id));
121                 TEST_ASSERT_VAL("Core map - Node ID is set", id.node == -1);
122         }
123
124         // Test that die ID contains socket and die
125         for (i = 0; i < map->nr; i++) {
126                 id = cpu_map__get_die(map, i, NULL);
127                 TEST_ASSERT_VAL("Die map - Socket ID doesn't match",
128                         session->header.env.cpu[map->map[i]].socket_id == id.socket);
129
130                 TEST_ASSERT_VAL("Die map - Die ID doesn't match",
131                         session->header.env.cpu[map->map[i]].die_id ==
132                                 cpu_map__id_to_die(id.id << 16));
133
134                 TEST_ASSERT_VAL("Die map - Node ID is set", id.node == -1);
135         }
136
137         // Test that socket ID contains only socket
138         for (i = 0; i < map->nr; i++) {
139                 id = cpu_map__get_socket(map, i, NULL);
140                 TEST_ASSERT_VAL("Socket map - Socket ID doesn't match",
141                         session->header.env.cpu[map->map[i]].socket_id == id.socket);
142
143                 TEST_ASSERT_VAL("Socket map - Node ID is set", id.node == -1);
144                 TEST_ASSERT_VAL("Socket map - ID is set", id.id == -1);
145         }
146
147         // Test that node ID contains only node
148         for (i = 0; i < map->nr; i++) {
149                 id = cpu_map__get_node(map, i, NULL);
150                 TEST_ASSERT_VAL("Node map - Node ID doesn't match",
151                         cpu__get_node(map->map[i]) == id.node);
152                 TEST_ASSERT_VAL("Node map - ID is set", id.id == -1);
153                 TEST_ASSERT_VAL("Node map - Socket is set", id.socket == -1);
154         }
155         perf_session__delete(session);
156
157         return 0;
158 }
159
160 int test__session_topology(struct test *test __maybe_unused, int subtest __maybe_unused)
161 {
162         char path[PATH_MAX];
163         struct perf_cpu_map *map;
164         int ret = TEST_FAIL;
165
166         TEST_ASSERT_VAL("can't get templ file", !get_temp(path));
167
168         pr_debug("templ file: %s\n", path);
169
170         if (session_write_header(path))
171                 goto free_path;
172
173         map = perf_cpu_map__new(NULL);
174         if (map == NULL) {
175                 pr_debug("failed to get system cpumap\n");
176                 goto free_path;
177         }
178
179         ret = check_cpu_topology(path, map);
180         perf_cpu_map__put(map);
181
182 free_path:
183         unlink(path);
184         return ret;
185 }