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