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