perf session: Event statistics also are per session
[deliverable/linux.git] / tools / perf / builtin-trace.c
CommitLineData
5f9c39dc
FW
1#include "builtin.h"
2
3#include "util/util.h"
4#include "util/cache.h"
5#include "util/symbol.h"
6#include "util/thread.h"
7#include "util/header.h"
cf72344d
IM
8#include "util/exec_cmd.h"
9#include "util/trace-event.h"
94c744b6 10#include "util/session.h"
5f9c39dc 11
956ffd02
TZ
12static char const *script_name;
13static char const *generate_script_lang;
14
15static int default_start_script(const char *script __attribute((unused)))
16{
17 return 0;
18}
19
20static int default_stop_script(void)
21{
22 return 0;
23}
24
25static int default_generate_script(const char *outfile __attribute ((unused)))
26{
27 return 0;
28}
29
30static struct scripting_ops default_scripting_ops = {
31 .start_script = default_start_script,
32 .stop_script = default_stop_script,
33 .process_event = print_event,
34 .generate_script = default_generate_script,
35};
36
37static struct scripting_ops *scripting_ops;
38
39static void setup_scripting(void)
40{
41 /* make sure PERF_EXEC_PATH is set for scripts */
42 perf_set_argv_exec_path(perf_exec_path());
43
16c632de
TZ
44 setup_perl_scripting();
45
956ffd02
TZ
46 scripting_ops = &default_scripting_ops;
47}
48
49static int cleanup_scripting(void)
50{
51 return scripting_ops->stop_script();
52}
53
5f9c39dc
FW
54#include "util/parse-options.h"
55
56#include "perf.h"
57#include "util/debug.h"
58
59#include "util/trace-event.h"
956ffd02 60#include "util/exec_cmd.h"
5f9c39dc 61
956ffd02 62static char const *input_name = "perf.data";
5f9c39dc 63
b3165f41 64static int process_sample_event(event_t *event, struct perf_session *session)
5f9c39dc 65{
180f95e2
OH
66 struct sample_data data;
67 struct thread *thread;
6ddf259d 68
180f95e2
OH
69 memset(&data, 0, sizeof(data));
70 data.time = -1;
71 data.cpu = -1;
72 data.period = 1;
cd6feeea 73
c019879b 74 event__parse_sample(event, session->sample_type, &data);
5f9c39dc 75
62daacb5 76 dump_printf("(IP, %d): %d/%d: %p period: %Ld\n",
5f9c39dc 77 event->header.misc,
180f95e2
OH
78 data.pid, data.tid,
79 (void *)(long)data.ip,
80 (long long)data.period);
5f9c39dc 81
b3165f41 82 thread = perf_session__findnew(session, event->ip.pid);
5f9c39dc 83 if (thread == NULL) {
6beba7ad
ACM
84 pr_debug("problem processing %d event, skipping it.\n",
85 event->header.type);
5f9c39dc
FW
86 return -1;
87 }
88
c019879b 89 if (session->sample_type & PERF_SAMPLE_RAW) {
5f9c39dc
FW
90 /*
91 * FIXME: better resolve from pid from the struct trace_entry
92 * field, although it should be the same than this perf
93 * event pid
94 */
180f95e2
OH
95 scripting_ops->process_event(data.cpu, data.raw_data,
96 data.raw_size,
97 data.time, thread->comm);
5f9c39dc 98 }
5f9c39dc 99
f823e441 100 session->events_stats.total += data.period;
5f9c39dc
FW
101 return 0;
102}
103
c019879b 104static int sample_type_check(struct perf_session *session)
5f9c39dc 105{
c019879b 106 if (!(session->sample_type & PERF_SAMPLE_RAW)) {
016e92fb
FW
107 fprintf(stderr,
108 "No trace sample to read. Did you call perf record "
109 "without -R?");
5f9c39dc
FW
110 return -1;
111 }
112
113 return 0;
114}
115
301a0b02 116static struct perf_event_ops event_ops = {
016e92fb 117 .process_sample_event = process_sample_event,
62daacb5 118 .process_comm_event = event__process_comm,
016e92fb
FW
119 .sample_type_check = sample_type_check,
120};
121
d8f66248 122static int __cmd_trace(struct perf_session *session)
5f9c39dc 123{
ec913369 124 return perf_session__process_events(session, &event_ops);
5f9c39dc
FW
125}
126
956ffd02
TZ
127struct script_spec {
128 struct list_head node;
129 struct scripting_ops *ops;
130 char spec[0];
131};
132
133LIST_HEAD(script_specs);
134
135static struct script_spec *script_spec__new(const char *spec,
136 struct scripting_ops *ops)
137{
138 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
139
140 if (s != NULL) {
141 strcpy(s->spec, spec);
142 s->ops = ops;
143 }
144
145 return s;
146}
147
148static void script_spec__delete(struct script_spec *s)
149{
150 free(s->spec);
151 free(s);
152}
153
154static void script_spec__add(struct script_spec *s)
155{
156 list_add_tail(&s->node, &script_specs);
157}
158
159static struct script_spec *script_spec__find(const char *spec)
160{
161 struct script_spec *s;
162
163 list_for_each_entry(s, &script_specs, node)
164 if (strcasecmp(s->spec, spec) == 0)
165 return s;
166 return NULL;
167}
168
169static struct script_spec *script_spec__findnew(const char *spec,
170 struct scripting_ops *ops)
171{
172 struct script_spec *s = script_spec__find(spec);
173
174 if (s)
175 return s;
176
177 s = script_spec__new(spec, ops);
178 if (!s)
179 goto out_delete_spec;
180
181 script_spec__add(s);
182
183 return s;
184
185out_delete_spec:
186 script_spec__delete(s);
187
188 return NULL;
189}
190
191int script_spec_register(const char *spec, struct scripting_ops *ops)
192{
193 struct script_spec *s;
194
195 s = script_spec__find(spec);
196 if (s)
197 return -1;
198
199 s = script_spec__findnew(spec, ops);
200 if (!s)
201 return -1;
202
203 return 0;
204}
205
206static struct scripting_ops *script_spec__lookup(const char *spec)
207{
208 struct script_spec *s = script_spec__find(spec);
209 if (!s)
210 return NULL;
211
212 return s->ops;
213}
214
215static void list_available_languages(void)
216{
217 struct script_spec *s;
218
219 fprintf(stderr, "\n");
220 fprintf(stderr, "Scripting language extensions (used in "
221 "perf trace -s [spec:]script.[spec]):\n\n");
222
223 list_for_each_entry(s, &script_specs, node)
224 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
225
226 fprintf(stderr, "\n");
227}
228
229static int parse_scriptname(const struct option *opt __used,
230 const char *str, int unset __used)
231{
232 char spec[PATH_MAX];
233 const char *script, *ext;
234 int len;
235
236 if (strcmp(str, "list") == 0) {
237 list_available_languages();
238 return 0;
239 }
240
241 script = strchr(str, ':');
242 if (script) {
243 len = script - str;
244 if (len >= PATH_MAX) {
245 fprintf(stderr, "invalid language specifier");
246 return -1;
247 }
248 strncpy(spec, str, len);
249 spec[len] = '\0';
250 scripting_ops = script_spec__lookup(spec);
251 if (!scripting_ops) {
252 fprintf(stderr, "invalid language specifier");
253 return -1;
254 }
255 script++;
256 } else {
257 script = str;
258 ext = strchr(script, '.');
259 if (!ext) {
260 fprintf(stderr, "invalid script extension");
261 return -1;
262 }
263 scripting_ops = script_spec__lookup(++ext);
264 if (!scripting_ops) {
265 fprintf(stderr, "invalid script extension");
266 return -1;
267 }
268 }
269
270 script_name = strdup(script);
271
272 return 0;
273}
274
5f9c39dc
FW
275static const char * const annotate_usage[] = {
276 "perf trace [<options>] <command>",
277 NULL
278};
279
280static const struct option options[] = {
281 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
282 "dump raw trace in ASCII"),
283 OPT_BOOLEAN('v', "verbose", &verbose,
284 "be more verbose (show symbol address, etc)"),
cda48461
SR
285 OPT_BOOLEAN('l', "latency", &latency_format,
286 "show latency attributes (irqs/preemption disabled, etc)"),
956ffd02
TZ
287 OPT_CALLBACK('s', "script", NULL, "name",
288 "script file name (lang:script name, script name, or *)",
289 parse_scriptname),
290 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
291 "generate perf-trace.xx script in specified language"),
292
1909629f 293 OPT_END()
5f9c39dc
FW
294};
295
296int cmd_trace(int argc, const char **argv, const char *prefix __used)
297{
956ffd02 298 int err;
d8f66248 299 struct perf_session *session;
956ffd02 300
00a192b3 301 symbol__init(0);
5f9c39dc 302
956ffd02
TZ
303 setup_scripting();
304
5f9c39dc
FW
305 argc = parse_options(argc, argv, options, annotate_usage, 0);
306 if (argc) {
307 /*
308 * Special case: if there's an argument left then assume tha
309 * it's a symbol filter:
310 */
311 if (argc > 1)
312 usage_with_options(annotate_usage, options);
313 }
314
5f9c39dc
FW
315 setup_pager();
316
4aa65636 317 session = perf_session__new(input_name, O_RDONLY, 0, NULL);
d8f66248
ACM
318 if (session == NULL)
319 return -ENOMEM;
320
956ffd02
TZ
321 if (generate_script_lang) {
322 struct stat perf_stat;
323
324 int input = open(input_name, O_RDONLY);
325 if (input < 0) {
326 perror("failed to open file");
327 exit(-1);
328 }
329
330 err = fstat(input, &perf_stat);
331 if (err < 0) {
332 perror("failed to stat file");
333 exit(-1);
334 }
335
336 if (!perf_stat.st_size) {
337 fprintf(stderr, "zero-sized file, nothing to do!\n");
338 exit(0);
339 }
340
341 scripting_ops = script_spec__lookup(generate_script_lang);
342 if (!scripting_ops) {
343 fprintf(stderr, "invalid language specifier");
344 return -1;
345 }
346
94c744b6 347 perf_header__read(&session->header, input);
956ffd02
TZ
348 err = scripting_ops->generate_script("perf-trace");
349 goto out;
350 }
351
352 if (script_name) {
353 err = scripting_ops->start_script(script_name);
354 if (err)
355 goto out;
356 }
357
d8f66248 358 err = __cmd_trace(session);
956ffd02 359
d8f66248 360 perf_session__delete(session);
956ffd02
TZ
361 cleanup_scripting();
362out:
363 return err;
5f9c39dc 364}
This page took 0.053792 seconds and 5 git commands to generate.