• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <string.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include "tests.h"
5 #include "util.h"
6 #include "session.h"
7 #include "evlist.h"
8 #include "debug.h"
9 
10 #define TEMPL "/tmp/perf-test-XXXXXX"
11 #define DATA_SIZE	10
12 
get_temp(char * path)13 static int get_temp(char *path)
14 {
15 	int fd;
16 
17 	strcpy(path, TEMPL);
18 
19 	fd = mkstemp(path);
20 	if (fd < 0) {
21 		perror("mkstemp failed");
22 		return -1;
23 	}
24 
25 	close(fd);
26 	return 0;
27 }
28 
session_write_header(char * path)29 static int session_write_header(char *path)
30 {
31 	struct perf_session *session;
32 	struct perf_data_file file = {
33 		.path = path,
34 		.mode = PERF_DATA_MODE_WRITE,
35 	};
36 
37 	session = perf_session__new(&file, false, NULL);
38 	TEST_ASSERT_VAL("can't get session", session);
39 
40 	session->evlist = perf_evlist__new_default();
41 	TEST_ASSERT_VAL("can't get evlist", session->evlist);
42 
43 	perf_header__set_feat(&session->header, HEADER_CPU_TOPOLOGY);
44 	perf_header__set_feat(&session->header, HEADER_NRCPUS);
45 	perf_header__set_feat(&session->header, HEADER_ARCH);
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 
check_cpu_topology(char * path,struct cpu_map * map)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_online; i++) {
70 		pr_debug("CPU %d, core %d, socket %d\n", i,
71 			 session->header.env.cpu[i].core_id,
72 			 session->header.env.cpu[i].socket_id);
73 	}
74 
75 	for (i = 0; i < map->nr; i++) {
76 		TEST_ASSERT_VAL("Core ID doesn't match",
77 			(session->header.env.cpu[map->map[i]].core_id == (cpu_map__get_core(map, i, NULL) & 0xffff)));
78 
79 		TEST_ASSERT_VAL("Socket ID doesn't match",
80 			(session->header.env.cpu[map->map[i]].socket_id == cpu_map__get_socket(map, i, NULL)));
81 	}
82 
83 	perf_session__delete(session);
84 
85 	return 0;
86 }
87 
test_session_topology(void)88 int test_session_topology(void)
89 {
90 	char path[PATH_MAX];
91 	struct cpu_map *map;
92 	int ret = -1;
93 
94 	TEST_ASSERT_VAL("can't get templ file", !get_temp(path));
95 
96 	pr_debug("templ file: %s\n", path);
97 
98 	if (session_write_header(path))
99 		goto free_path;
100 
101 	map = cpu_map__new(NULL);
102 	if (map == NULL) {
103 		pr_debug("failed to get system cpumap\n");
104 		goto free_path;
105 	}
106 
107 	if (check_cpu_topology(path, map))
108 		goto free_map;
109 	ret = 0;
110 
111 free_map:
112 	cpu_map__put(map);
113 free_path:
114 	unlink(path);
115 	return ret;
116 }
117