xref: /openbmc/linux/tools/perf/builtin-kvm.c (revision 25985edc)
1 #include "builtin.h"
2 #include "perf.h"
3 
4 #include "util/util.h"
5 #include "util/cache.h"
6 #include "util/symbol.h"
7 #include "util/thread.h"
8 #include "util/header.h"
9 #include "util/session.h"
10 
11 #include "util/parse-options.h"
12 #include "util/trace-event.h"
13 
14 #include "util/debug.h"
15 
16 #include <sys/prctl.h>
17 
18 #include <semaphore.h>
19 #include <pthread.h>
20 #include <math.h>
21 
22 static const char		*file_name;
23 static char			name_buffer[256];
24 
25 bool				perf_host = 1;
26 bool				perf_guest;
27 
28 static const char * const kvm_usage[] = {
29 	"perf kvm [<options>] {top|record|report|diff|buildid-list}",
30 	NULL
31 };
32 
33 static const struct option kvm_options[] = {
34 	OPT_STRING('i', "input", &file_name, "file",
35 		   "Input file name"),
36 	OPT_STRING('o', "output", &file_name, "file",
37 		   "Output file name"),
38 	OPT_BOOLEAN(0, "guest", &perf_guest,
39 		    "Collect guest os data"),
40 	OPT_BOOLEAN(0, "host", &perf_host,
41 		    "Collect guest os data"),
42 	OPT_STRING(0, "guestmount", &symbol_conf.guestmount, "directory",
43 		   "guest mount directory under which every guest os"
44 		   " instance has a subdir"),
45 	OPT_STRING(0, "guestvmlinux", &symbol_conf.default_guest_vmlinux_name,
46 		   "file", "file saving guest os vmlinux"),
47 	OPT_STRING(0, "guestkallsyms", &symbol_conf.default_guest_kallsyms,
48 		   "file", "file saving guest os /proc/kallsyms"),
49 	OPT_STRING(0, "guestmodules", &symbol_conf.default_guest_modules,
50 		   "file", "file saving guest os /proc/modules"),
51 	OPT_END()
52 };
53 
54 static int __cmd_record(int argc, const char **argv)
55 {
56 	int rec_argc, i = 0, j;
57 	const char **rec_argv;
58 
59 	rec_argc = argc + 2;
60 	rec_argv = calloc(rec_argc + 1, sizeof(char *));
61 	rec_argv[i++] = strdup("record");
62 	rec_argv[i++] = strdup("-o");
63 	rec_argv[i++] = strdup(file_name);
64 	for (j = 1; j < argc; j++, i++)
65 		rec_argv[i] = argv[j];
66 
67 	BUG_ON(i != rec_argc);
68 
69 	return cmd_record(i, rec_argv, NULL);
70 }
71 
72 static int __cmd_report(int argc, const char **argv)
73 {
74 	int rec_argc, i = 0, j;
75 	const char **rec_argv;
76 
77 	rec_argc = argc + 2;
78 	rec_argv = calloc(rec_argc + 1, sizeof(char *));
79 	rec_argv[i++] = strdup("report");
80 	rec_argv[i++] = strdup("-i");
81 	rec_argv[i++] = strdup(file_name);
82 	for (j = 1; j < argc; j++, i++)
83 		rec_argv[i] = argv[j];
84 
85 	BUG_ON(i != rec_argc);
86 
87 	return cmd_report(i, rec_argv, NULL);
88 }
89 
90 static int __cmd_buildid_list(int argc, const char **argv)
91 {
92 	int rec_argc, i = 0, j;
93 	const char **rec_argv;
94 
95 	rec_argc = argc + 2;
96 	rec_argv = calloc(rec_argc + 1, sizeof(char *));
97 	rec_argv[i++] = strdup("buildid-list");
98 	rec_argv[i++] = strdup("-i");
99 	rec_argv[i++] = strdup(file_name);
100 	for (j = 1; j < argc; j++, i++)
101 		rec_argv[i] = argv[j];
102 
103 	BUG_ON(i != rec_argc);
104 
105 	return cmd_buildid_list(i, rec_argv, NULL);
106 }
107 
108 int cmd_kvm(int argc, const char **argv, const char *prefix __used)
109 {
110 	perf_host = perf_guest = 0;
111 
112 	argc = parse_options(argc, argv, kvm_options, kvm_usage,
113 			PARSE_OPT_STOP_AT_NON_OPTION);
114 	if (!argc)
115 		usage_with_options(kvm_usage, kvm_options);
116 
117 	if (!perf_host)
118 		perf_guest = 1;
119 
120 	if (!file_name) {
121 		if (perf_host && !perf_guest)
122 			sprintf(name_buffer, "perf.data.host");
123 		else if (!perf_host && perf_guest)
124 			sprintf(name_buffer, "perf.data.guest");
125 		else
126 			sprintf(name_buffer, "perf.data.kvm");
127 		file_name = name_buffer;
128 	}
129 
130 	if (!strncmp(argv[0], "rec", 3))
131 		return __cmd_record(argc, argv);
132 	else if (!strncmp(argv[0], "rep", 3))
133 		return __cmd_report(argc, argv);
134 	else if (!strncmp(argv[0], "diff", 4))
135 		return cmd_diff(argc, argv, NULL);
136 	else if (!strncmp(argv[0], "top", 3))
137 		return cmd_top(argc, argv, NULL);
138 	else if (!strncmp(argv[0], "buildid-list", 12))
139 		return __cmd_buildid_list(argc, argv);
140 	else
141 		usage_with_options(kvm_usage, kvm_options);
142 
143 	return 0;
144 }
145