blob: 22daf2bdf5faf1b0e7a5ac85ec021c4af80b4371 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Kan Liangc84974e2015-09-04 04:58:31 -04002#include <string.h>
3#include <stdlib.h>
4#include <stdio.h>
Jiri Olsa9c3516d2019-07-21 13:24:30 +02005#include <perf/cpumap.h>
Arnaldo Carvalho de Melo87ffb6c2019-09-10 16:29:02 +01006#include "cpumap.h"
Kan Liangc84974e2015-09-04 04:58:31 -04007#include "tests.h"
Kan Liangc84974e2015-09-04 04:58:31 -04008#include "session.h"
9#include "evlist.h"
10#include "debug.h"
Mamatha Inamdar6ef81c52019-08-22 12:50:49 +053011#include <linux/err.h>
Kan Liangc84974e2015-09-04 04:58:31 -040012
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;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +010035 struct perf_data data = {
Tommi Rantaladbd660e2020-04-23 14:53:40 +030036 .path = path,
37 .mode = PERF_DATA_MODE_WRITE,
Kan Liangc84974e2015-09-04 04:58:31 -040038 };
39
Jiri Olsa8ceb41d2017-01-23 22:07:59 +010040 session = perf_session__new(&data, false, NULL);
Mamatha Inamdar6ef81c52019-08-22 12:50:49 +053041 TEST_ASSERT_VAL("can't get session", !IS_ERR(session));
Kan Liangc84974e2015-09-04 04:58:31 -040042
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);
Thomas Richterb930e622018-06-11 09:31:53 +020048 perf_header__set_feat(&session->header, HEADER_ARCH);
Kan Liangc84974e2015-09-04 04:58:31 -040049
50 session->header.data_size += DATA_SIZE;
51
52 TEST_ASSERT_VAL("failed to write header",
Jiri Olsaeae8ad82017-01-23 22:25:41 +010053 !perf_session__write_header(session, session->evlist, data.file.fd, true));
Kan Liangc84974e2015-09-04 04:58:31 -040054
55 perf_session__delete(session);
56
57 return 0;
58}
59
Jiri Olsaf8548392019-07-21 13:23:49 +020060static int check_cpu_topology(char *path, struct perf_cpu_map *map)
Kan Liangc84974e2015-09-04 04:58:31 -040061{
62 struct perf_session *session;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +010063 struct perf_data data = {
Tommi Rantaladbd660e2020-04-23 14:53:40 +030064 .path = path,
65 .mode = PERF_DATA_MODE_READ,
Kan Liangc84974e2015-09-04 04:58:31 -040066 };
67 int i;
68
Jiri Olsa8ceb41d2017-01-23 22:07:59 +010069 session = perf_session__new(&data, false, NULL);
Mamatha Inamdar6ef81c52019-08-22 12:50:49 +053070 TEST_ASSERT_VAL("can't get session", !IS_ERR(session));
Kan Liangc84974e2015-09-04 04:58:31 -040071
Thomas Richterd1211092018-05-28 09:36:57 +020072 /* 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
Jan Stancekda8a58b2017-02-17 12:10:26 +010093 for (i = 0; i < session->header.env.nr_cpus_avail; i++) {
94 if (!cpu_map__has(map, i))
95 continue;
Kan Liangc84974e2015-09-04 04:58:31 -040096 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",
Jiri Olsa1fe7a302015-10-16 12:41:15 +0200103 (session->header.env.cpu[map->map[i]].core_id == (cpu_map__get_core(map, i, NULL) & 0xffff)));
Kan Liangc84974e2015-09-04 04:58:31 -0400104
105 TEST_ASSERT_VAL("Socket ID doesn't match",
Jiri Olsa1fe7a302015-10-16 12:41:15 +0200106 (session->header.env.cpu[map->map[i]].socket_id == cpu_map__get_socket(map, i, NULL)));
Kan Liangc84974e2015-09-04 04:58:31 -0400107 }
108
109 perf_session__delete(session);
110
111 return 0;
112}
113
Arnaldo Carvalho de Melo81f17c92017-08-03 15:16:31 -0300114int test__session_topology(struct test *test __maybe_unused, int subtest __maybe_unused)
Kan Liangc84974e2015-09-04 04:58:31 -0400115{
116 char path[PATH_MAX];
Jiri Olsaf8548392019-07-21 13:23:49 +0200117 struct perf_cpu_map *map;
Thomas Richterd1211092018-05-28 09:36:57 +0200118 int ret = TEST_FAIL;
Kan Liangc84974e2015-09-04 04:58:31 -0400119
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
Jiri Olsa9c3516d2019-07-21 13:24:30 +0200127 map = perf_cpu_map__new(NULL);
Kan Liangc84974e2015-09-04 04:58:31 -0400128 if (map == NULL) {
129 pr_debug("failed to get system cpumap\n");
130 goto free_path;
131 }
132
Thomas Richterd1211092018-05-28 09:36:57 +0200133 ret = check_cpu_topology(path, map);
Jiri Olsa38f01d82019-07-21 13:24:17 +0200134 perf_cpu_map__put(map);
Thomas Richterd1211092018-05-28 09:36:57 +0200135
Kan Liangc84974e2015-09-04 04:58:31 -0400136free_path:
137 unlink(path);
138 return ret;
139}