blob: 17cb1bb3448c842930df519d1bad7077777a098d [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>
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
14static 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
30static int session_write_header(char *path)
31{
32 struct perf_session *session;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +010033 struct perf_data data = {
Jiri Olsaeae8ad82017-01-23 22:25:41 +010034 .file = {
35 .path = path,
36 },
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);
Kan Liangc84974e2015-09-04 04:58:31 -040041 TEST_ASSERT_VAL("can't get session", 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
49 session->header.data_size += DATA_SIZE;
50
51 TEST_ASSERT_VAL("failed to write header",
Jiri Olsaeae8ad82017-01-23 22:25:41 +010052 !perf_session__write_header(session, session->evlist, data.file.fd, true));
Kan Liangc84974e2015-09-04 04:58:31 -040053
54 perf_session__delete(session);
55
56 return 0;
57}
58
59static int check_cpu_topology(char *path, struct cpu_map *map)
60{
61 struct perf_session *session;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +010062 struct perf_data data = {
Jiri Olsaeae8ad82017-01-23 22:25:41 +010063 .file = {
64 .path = path,
65 },
66 .mode = PERF_DATA_MODE_READ,
Kan Liangc84974e2015-09-04 04:58:31 -040067 };
68 int i;
69
Jiri Olsa8ceb41d2017-01-23 22:07:59 +010070 session = perf_session__new(&data, false, NULL);
Kan Liangc84974e2015-09-04 04:58:31 -040071 TEST_ASSERT_VAL("can't get session", session);
72
Jan Stancekda8a58b2017-02-17 12:10:26 +010073 for (i = 0; i < session->header.env.nr_cpus_avail; i++) {
74 if (!cpu_map__has(map, i))
75 continue;
Kan Liangc84974e2015-09-04 04:58:31 -040076 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);
79 }
80
81 for (i = 0; i < map->nr; i++) {
82 TEST_ASSERT_VAL("Core ID doesn't match",
Jiri Olsa1fe7a302015-10-16 12:41:15 +020083 (session->header.env.cpu[map->map[i]].core_id == (cpu_map__get_core(map, i, NULL) & 0xffff)));
Kan Liangc84974e2015-09-04 04:58:31 -040084
85 TEST_ASSERT_VAL("Socket ID doesn't match",
Jiri Olsa1fe7a302015-10-16 12:41:15 +020086 (session->header.env.cpu[map->map[i]].socket_id == cpu_map__get_socket(map, i, NULL)));
Kan Liangc84974e2015-09-04 04:58:31 -040087 }
88
89 perf_session__delete(session);
90
91 return 0;
92}
93
Arnaldo Carvalho de Melo81f17c92017-08-03 15:16:31 -030094int test__session_topology(struct test *test __maybe_unused, int subtest __maybe_unused)
Kan Liangc84974e2015-09-04 04:58:31 -040095{
96 char path[PATH_MAX];
97 struct cpu_map *map;
98 int ret = -1;
99
100 TEST_ASSERT_VAL("can't get templ file", !get_temp(path));
101
102 pr_debug("templ file: %s\n", path);
103
104 if (session_write_header(path))
105 goto free_path;
106
107 map = cpu_map__new(NULL);
108 if (map == NULL) {
109 pr_debug("failed to get system cpumap\n");
110 goto free_path;
111 }
112
113 if (check_cpu_topology(path, map))
114 goto free_map;
115 ret = 0;
116
117free_map:
118 cpu_map__put(map);
119free_path:
120 unlink(path);
121 return ret;
122}