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