Commit | Line | Data |
---|---|---|
1fc22eb4 JD |
1 | /* |
2 | * Copyright (C) 2011 Julien Desfossez | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License Version 2 as | |
6 | * published by the Free Software Foundation; | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, | |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 | * GNU General Public License for more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public License | |
14 | * along with this program; if not, write to the Free Software | |
15 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, | |
16 | * MA 02111-1307, USA. | |
17 | */ | |
18 | ||
19 | #define _GNU_SOURCE | |
20 | #include <config.h> | |
21 | #include <stdio.h> | |
22 | #include <stdint.h> | |
23 | #include <babeltrace/babeltrace.h> | |
24 | #include <babeltrace/ctf/events.h> | |
25 | #include <babeltrace/ctf/callbacks.h> | |
26 | #include <fcntl.h> | |
27 | #include <pthread.h> | |
28 | #include <popt.h> | |
29 | #include <stdlib.h> | |
30 | #include <ftw.h> | |
31 | #include <dirent.h> | |
32 | #include <ctype.h> | |
33 | #include <sys/stat.h> | |
34 | #include <unistd.h> | |
35 | #include <string.h> | |
36 | #include <errno.h> | |
37 | #include <sys/types.h> | |
38 | #include <fts.h> | |
85db4618 | 39 | #include <assert.h> |
1fc22eb4 JD |
40 | |
41 | #include "lttngtoptypes.h" | |
42 | #include "cputop.h" | |
43 | #include "iostreamtop.h" | |
44 | #include "cursesdisplay.h" | |
45 | #include "common.h" | |
46 | ||
47 | #define DEFAULT_FILE_ARRAY_SIZE 1 | |
48 | ||
49 | const char *opt_input_path; | |
50 | ||
51 | struct lttngtop *copy; | |
52 | pthread_t display_thread; | |
53 | pthread_t timer_thread; | |
54 | ||
55 | unsigned long refresh_display = 1 * NSEC_PER_SEC; | |
56 | unsigned long last_display_update = 0; | |
57 | int quit = 0; | |
58 | ||
59 | enum { | |
60 | OPT_NONE = 0, | |
61 | OPT_HELP, | |
62 | OPT_LIST, | |
63 | OPT_VERBOSE, | |
64 | OPT_DEBUG, | |
65 | OPT_NAMES, | |
66 | }; | |
67 | ||
68 | static struct poptOption long_options[] = { | |
69 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ | |
70 | { "help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL }, | |
71 | { NULL, 0, 0, NULL, 0, NULL, NULL }, | |
72 | }; | |
73 | ||
74 | void *refresh_thread(void *p) | |
75 | { | |
76 | while (1) { | |
85db4618 JD |
77 | if (quit) |
78 | return NULL; | |
1fc22eb4 JD |
79 | sem_wait(&pause_sem); |
80 | sem_post(&pause_sem); | |
81 | sem_post(&timer); | |
82 | sleep(refresh_display/NSEC_PER_SEC); | |
83 | } | |
84 | } | |
85 | ||
86 | void *ncurses_display(void *p) | |
87 | { | |
88 | unsigned int current_display_index = 0; | |
89 | ||
90 | sem_wait(&bootstrap); | |
91 | init_ncurses(); | |
92 | ||
93 | while (1) { | |
94 | sem_wait(&timer); | |
95 | sem_wait(&goodtodisplay); | |
96 | sem_wait(&pause_sem); | |
97 | ||
98 | copy = g_ptr_array_index(copies, current_display_index); | |
85db4618 JD |
99 | assert(copy); |
100 | display(current_display_index++); | |
1fc22eb4 JD |
101 | |
102 | sem_post(&goodtoupdate); | |
103 | sem_post(&pause_sem); | |
104 | ||
105 | if (quit) { | |
106 | reset_ncurses(); | |
107 | pthread_exit(0); | |
108 | } | |
109 | } | |
110 | } | |
111 | ||
112 | /* | |
113 | * hook on each event to check the timestamp and refresh the display if | |
114 | * necessary | |
115 | */ | |
116 | enum bt_cb_ret check_timestamp(struct bt_ctf_event *call_data, void *private_data) | |
117 | { | |
118 | unsigned long timestamp; | |
119 | ||
120 | timestamp = bt_ctf_get_timestamp(call_data); | |
121 | if (timestamp == -1ULL) | |
122 | goto error; | |
123 | ||
124 | if (last_display_update == 0) | |
125 | last_display_update = timestamp; | |
126 | ||
127 | if (timestamp - last_display_update >= refresh_display) { | |
128 | sem_wait(&goodtoupdate); | |
129 | g_ptr_array_add(copies, get_copy_lttngtop(last_display_update, | |
130 | timestamp)); | |
131 | sem_post(&goodtodisplay); | |
132 | sem_post(&bootstrap); | |
133 | last_display_update = timestamp; | |
134 | } | |
135 | return BT_CB_OK; | |
136 | ||
137 | error: | |
138 | fprintf(stderr, "check_timestamp callback error\n"); | |
139 | return BT_CB_ERROR_STOP; | |
140 | } | |
141 | ||
142 | /* | |
143 | * get_perf_counter : get or create and return a perf_counter struct for | |
144 | * either a process or a cpu (only one of the 2 parameters mandatory) | |
145 | */ | |
146 | struct perfcounter *get_perf_counter(const char *name, struct processtop *proc, | |
147 | struct cputime *cpu) | |
148 | { | |
149 | struct perfcounter *ret, *global; | |
150 | GHashTable *table; | |
151 | ||
152 | if (proc) | |
153 | table = proc->perf; | |
154 | else if (cpu) | |
155 | table = cpu->perf; | |
156 | else | |
157 | goto error; | |
158 | ||
159 | ret = g_hash_table_lookup(table, (gpointer) name); | |
160 | if (ret) | |
161 | goto end; | |
162 | ||
559c9f86 | 163 | ret = g_new0(struct perfcounter, 1); |
1fc22eb4 JD |
164 | /* by default, make it visible in the UI */ |
165 | ret->visible = 1; | |
85db4618 | 166 | g_hash_table_insert(table, (gpointer) strdup(name), ret); |
1fc22eb4 JD |
167 | |
168 | global = g_hash_table_lookup(lttngtop.perf_list, (gpointer) name); | |
169 | if (!global) { | |
559c9f86 | 170 | global = g_new0(struct perfcounter, 1); |
1fc22eb4 JD |
171 | memcpy(global, ret, sizeof(struct perfcounter)); |
172 | /* by default, sort on the first perf context */ | |
173 | if (g_hash_table_size(lttngtop.perf_list) == 0) | |
174 | global->sort = 1; | |
85db4618 | 175 | g_hash_table_insert(lttngtop.perf_list, (gpointer) strdup(name), global); |
1fc22eb4 JD |
176 | } |
177 | ||
178 | end: | |
179 | return ret; | |
180 | ||
181 | error: | |
182 | return NULL; | |
183 | } | |
184 | ||
185 | void update_perf_value(struct processtop *proc, struct cputime *cpu, | |
186 | const char *name, int value) | |
187 | { | |
188 | struct perfcounter *cpu_perf, *process_perf; | |
189 | ||
190 | cpu_perf = get_perf_counter(name, NULL, cpu); | |
191 | if (cpu_perf->count < value) { | |
192 | process_perf = get_perf_counter(name, proc, NULL); | |
193 | process_perf->count += value - cpu_perf->count; | |
194 | cpu_perf->count = value; | |
195 | } | |
196 | } | |
197 | ||
198 | void extract_perf_counter_scope(struct bt_ctf_event *event, | |
199 | struct definition *scope, | |
200 | struct processtop *proc, | |
201 | struct cputime *cpu) | |
202 | { | |
203 | struct definition const * const *list = NULL; | |
204 | unsigned int count; | |
205 | int i, ret; | |
206 | ||
207 | if (!scope) | |
208 | goto end; | |
209 | ||
210 | ret = bt_ctf_get_field_list(event, scope, &list, &count); | |
211 | if (ret < 0) | |
212 | goto end; | |
213 | ||
214 | for (i = 0; i < count; i++) { | |
215 | const char *name = bt_ctf_field_name(list[i]); | |
216 | if (strncmp(name, "_perf_", 6) == 0) { | |
217 | int value = bt_ctf_get_uint64(list[i]); | |
218 | if (bt_ctf_field_get_error()) | |
219 | continue; | |
220 | update_perf_value(proc, cpu, name, value); | |
221 | } | |
222 | } | |
223 | ||
224 | end: | |
225 | return; | |
226 | } | |
227 | ||
228 | void update_perf_counter(struct processtop *proc, struct bt_ctf_event *event) | |
229 | { | |
230 | struct definition *scope; | |
231 | uint64_t cpu_id; | |
232 | struct cputime *cpu; | |
233 | ||
234 | scope = bt_ctf_get_top_level_scope(event, BT_STREAM_PACKET_CONTEXT); | |
235 | cpu_id = bt_ctf_get_uint64(bt_ctf_get_field(event, scope, "cpu_id")); | |
236 | if (bt_ctf_field_get_error()) { | |
237 | fprintf(stderr, "[error] get cpu_id\n"); | |
238 | goto end; | |
239 | } | |
240 | cpu = get_cpu(cpu_id); | |
241 | ||
242 | scope = bt_ctf_get_top_level_scope(event, BT_STREAM_EVENT_CONTEXT); | |
243 | extract_perf_counter_scope(event, scope, proc, cpu); | |
244 | ||
245 | scope = bt_ctf_get_top_level_scope(event, BT_STREAM_PACKET_CONTEXT); | |
246 | extract_perf_counter_scope(event, scope, proc, cpu); | |
247 | ||
248 | scope = bt_ctf_get_top_level_scope(event, BT_EVENT_CONTEXT); | |
249 | extract_perf_counter_scope(event, scope, proc, cpu); | |
250 | ||
251 | end: | |
252 | return; | |
253 | } | |
254 | ||
255 | enum bt_cb_ret fix_process_table(struct bt_ctf_event *call_data, | |
256 | void *private_data) | |
257 | { | |
258 | int pid, tid, ppid; | |
259 | char *comm; | |
260 | struct processtop *parent, *child; | |
261 | struct definition *scope; | |
262 | unsigned long timestamp; | |
263 | ||
85db4618 | 264 | /* FIXME : display nice error when missing context pid, tid, ppid and comm */ |
1fc22eb4 JD |
265 | |
266 | timestamp = bt_ctf_get_timestamp(call_data); | |
267 | if (timestamp == -1ULL) | |
268 | goto error; | |
269 | ||
270 | scope = bt_ctf_get_top_level_scope(call_data, BT_STREAM_EVENT_CONTEXT); | |
271 | ||
272 | pid = bt_ctf_get_int64(bt_ctf_get_field(call_data, scope, "_pid")); | |
273 | if (bt_ctf_field_get_error()) { | |
274 | // fprintf(stderr, "Missing pid context info\n"); | |
275 | goto error; | |
276 | } | |
277 | tid = bt_ctf_get_int64(bt_ctf_get_field(call_data, scope, "_tid")); | |
278 | if (bt_ctf_field_get_error()) { | |
279 | // fprintf(stderr, "Missing tid context info\n"); | |
280 | goto error; | |
281 | } | |
282 | ppid = bt_ctf_get_int64(bt_ctf_get_field(call_data, scope, "_ppid")); | |
283 | if (bt_ctf_field_get_error()) { | |
284 | // fprintf(stderr, "Missing ppid context info\n"); | |
285 | goto error; | |
286 | } | |
287 | comm = bt_ctf_get_char_array(bt_ctf_get_field(call_data, scope, "_procname")); | |
288 | if (bt_ctf_field_get_error()) { | |
289 | // fprintf(stderr, "Missing procname context info\n"); | |
290 | goto error; | |
291 | } | |
292 | ||
293 | /* find or create the current process */ | |
294 | child = find_process_tid(<tngtop, tid, comm); | |
295 | if (!child) | |
296 | child = add_proc(<tngtop, tid, comm, timestamp); | |
297 | update_proc(child, pid, tid, ppid, comm); | |
298 | ||
299 | if (pid != tid) { | |
300 | /* find or create the parent */ | |
301 | parent = find_process_tid(<tngtop, pid, comm); | |
302 | if (!parent) { | |
303 | parent = add_proc(<tngtop, pid, comm, timestamp); | |
304 | parent->pid = pid; | |
305 | } | |
306 | ||
307 | /* attach the parent to the current process */ | |
308 | child->threadparent = parent; | |
309 | add_thread(parent, child); | |
310 | } | |
311 | ||
312 | update_perf_counter(child, call_data); | |
313 | ||
314 | return BT_CB_OK; | |
315 | ||
316 | error: | |
317 | return BT_CB_ERROR_STOP; | |
318 | } | |
319 | ||
320 | void init_lttngtop() | |
321 | { | |
322 | copies = g_ptr_array_new(); | |
85db4618 | 323 | lttngtop.perf_list = g_hash_table_new(g_str_hash, g_str_equal); |
1fc22eb4 JD |
324 | |
325 | sem_init(&goodtodisplay, 0, 0); | |
326 | sem_init(&goodtoupdate, 0, 1); | |
327 | sem_init(&timer, 0, 1); | |
328 | sem_init(&bootstrap, 0, 0); | |
329 | sem_init(&pause_sem, 0, 1); | |
330 | sem_init(&end_trace_sem, 0, 0); | |
331 | ||
332 | lttngtop.process_table = g_ptr_array_new(); | |
333 | lttngtop.files_table = g_ptr_array_new(); | |
334 | lttngtop.cpu_table = g_ptr_array_new(); | |
335 | } | |
336 | ||
c263c4eb | 337 | void usage(FILE *fp) |
1fc22eb4 | 338 | { |
c263c4eb JD |
339 | fprintf(fp, "LTTngTop %s\n\n", VERSION); |
340 | fprintf(fp, "Usage : lttngtop /path/to/trace\n"); | |
1fc22eb4 JD |
341 | } |
342 | ||
343 | /* | |
344 | * Return 0 if caller should continue, < 0 if caller should return | |
345 | * error, > 0 if caller should exit without reporting error. | |
346 | */ | |
347 | static int parse_options(int argc, char **argv) | |
348 | { | |
349 | poptContext pc; | |
350 | int opt, ret = 0; | |
351 | ||
352 | if (argc == 1) { | |
353 | usage(stdout); | |
354 | return 1; /* exit cleanly */ | |
355 | } | |
356 | ||
357 | pc = poptGetContext(NULL, argc, (const char **) argv, long_options, 0); | |
358 | poptReadDefaultConfig(pc, 0); | |
359 | ||
360 | while ((opt = poptGetNextOpt(pc)) != -1) { | |
361 | switch (opt) { | |
362 | case OPT_HELP: | |
363 | usage(stdout); | |
364 | ret = 1; /* exit cleanly */ | |
365 | goto end; | |
366 | case OPT_LIST: | |
367 | // list_formats(stdout); | |
368 | ret = 1; | |
369 | goto end; | |
370 | case OPT_VERBOSE: | |
371 | // babeltrace_verbose = 1; | |
372 | break; | |
373 | case OPT_DEBUG: | |
374 | // babeltrace_debug = 1; | |
375 | break; | |
376 | case OPT_NAMES: | |
377 | // opt_field_names = 1; | |
378 | break; | |
379 | default: | |
380 | ret = -EINVAL; | |
381 | goto end; | |
382 | } | |
383 | } | |
384 | ||
385 | opt_input_path = poptGetArg(pc); | |
386 | if (!opt_input_path) { | |
387 | ret = -EINVAL; | |
388 | goto end; | |
389 | } | |
390 | end: | |
391 | if (pc) { | |
392 | poptFreeContext(pc); | |
393 | } | |
394 | return ret; | |
395 | } | |
396 | ||
397 | void iter_trace(struct bt_context *bt_ctx) | |
398 | { | |
399 | struct bt_ctf_iter *iter; | |
400 | struct bt_iter_pos begin_pos; | |
401 | struct bt_ctf_event *event; | |
402 | int ret = 0; | |
403 | ||
404 | begin_pos.type = BT_SEEK_BEGIN; | |
405 | iter = bt_ctf_iter_create(bt_ctx, &begin_pos, NULL); | |
406 | ||
407 | /* at each event check if we need to refresh */ | |
408 | bt_ctf_iter_add_callback(iter, 0, NULL, 0, | |
409 | check_timestamp, | |
410 | NULL, NULL, NULL); | |
411 | /* at each event, verify the status of the process table */ | |
412 | bt_ctf_iter_add_callback(iter, 0, NULL, 0, | |
413 | fix_process_table, | |
414 | NULL, NULL, NULL); | |
415 | /* to handle the scheduling events */ | |
416 | bt_ctf_iter_add_callback(iter, | |
417 | g_quark_from_static_string("sched_switch"), | |
418 | NULL, 0, handle_sched_switch, NULL, NULL, NULL); | |
419 | /* to clean up the process table */ | |
420 | bt_ctf_iter_add_callback(iter, | |
421 | g_quark_from_static_string("sched_process_free"), | |
422 | NULL, 0, handle_sched_process_free, NULL, NULL, NULL); | |
423 | ||
424 | /* for IO top */ | |
425 | bt_ctf_iter_add_callback(iter, | |
426 | g_quark_from_static_string("exit_syscall"), | |
427 | NULL, 0, handle_exit_syscall, NULL, NULL, NULL); | |
428 | bt_ctf_iter_add_callback(iter, | |
429 | g_quark_from_static_string("sys_write"), | |
430 | NULL, 0, handle_sys_write, NULL, NULL, NULL); | |
431 | bt_ctf_iter_add_callback(iter, | |
432 | g_quark_from_static_string("sys_read"), | |
433 | NULL, 0, handle_sys_read, NULL, NULL, NULL); | |
434 | while ((event = bt_ctf_iter_read_event(iter)) != NULL) { | |
435 | ret = bt_iter_next(bt_ctf_get_iter(iter)); | |
436 | if (ret < 0) | |
437 | goto end_iter; | |
438 | } | |
439 | ||
440 | /* block until quit, we reached the end of the trace */ | |
441 | sem_wait(&end_trace_sem); | |
442 | ||
443 | end_iter: | |
444 | bt_iter_destroy(bt_ctf_get_iter(iter)); | |
445 | } | |
446 | ||
447 | /* | |
448 | * bt_context_add_traces_recursive: Open a trace recursively | |
449 | * (copied from BSD code in converter/babeltrace.c) | |
450 | * | |
451 | * Find each trace present in the subdirectory starting from the given | |
452 | * path, and add them to the context. The packet_seek parameter can be | |
453 | * NULL: this specify to use the default format packet_seek. | |
454 | * | |
455 | * Return: 0 on success, nonzero on failure. | |
456 | * Unable to open toplevel: failure. | |
457 | * Unable to open some subdirectory or file: warn and continue; | |
458 | */ | |
459 | int bt_context_add_traces_recursive(struct bt_context *ctx, const char *path, | |
460 | const char *format_str, | |
461 | void (*packet_seek)(struct stream_pos *pos, | |
462 | size_t offset, int whence)) | |
463 | { | |
464 | FTS *tree; | |
465 | FTSENT *node; | |
466 | GArray *trace_ids; | |
467 | char lpath[PATH_MAX]; | |
468 | char * const paths[2] = { lpath, NULL }; | |
469 | int ret; | |
470 | ||
471 | /* | |
472 | * Need to copy path, because fts_open can change it. | |
473 | * It is the pointer array, not the strings, that are constant. | |
474 | */ | |
475 | strncpy(lpath, path, PATH_MAX); | |
476 | lpath[PATH_MAX - 1] = '\0'; | |
477 | ||
478 | tree = fts_open(paths, FTS_NOCHDIR | FTS_LOGICAL, 0); | |
479 | if (tree == NULL) { | |
480 | fprintf(stderr, "[error] [Context] Cannot traverse \"%s\" for reading.\n", | |
481 | path); | |
482 | return -EINVAL; | |
483 | } | |
484 | ||
485 | trace_ids = g_array_new(FALSE, TRUE, sizeof(int)); | |
486 | ||
487 | while ((node = fts_read(tree))) { | |
488 | int dirfd, metafd; | |
489 | ||
490 | if (!(node->fts_info & FTS_D)) | |
491 | continue; | |
492 | ||
493 | dirfd = open(node->fts_accpath, 0); | |
494 | if (dirfd < 0) { | |
495 | fprintf(stderr, "[error] [Context] Unable to open trace " | |
496 | "directory file descriptor.\n"); | |
497 | ret = dirfd; | |
498 | goto error; | |
499 | } | |
500 | metafd = openat(dirfd, "metadata", O_RDONLY); | |
501 | if (metafd < 0) { | |
502 | ret = close(dirfd); | |
503 | if (ret < 0) { | |
504 | perror("close"); | |
505 | goto error; | |
506 | } | |
507 | } else { | |
508 | int trace_id; | |
509 | ||
510 | ret = close(metafd); | |
511 | if (ret < 0) { | |
512 | perror("close"); | |
513 | goto error; | |
514 | } | |
515 | ret = close(dirfd); | |
516 | if (ret < 0) { | |
517 | perror("close"); | |
518 | goto error; | |
519 | } | |
520 | ||
521 | trace_id = bt_context_add_trace(ctx, | |
522 | node->fts_accpath, format_str, | |
523 | packet_seek, NULL, NULL); | |
524 | if (trace_id < 0) { | |
525 | fprintf(stderr, "[error] [Context] opening trace \"%s\" from %s " | |
526 | "for reading.\n", node->fts_accpath, path); | |
527 | ret = trace_id; | |
528 | goto error; | |
529 | } | |
530 | g_array_append_val(trace_ids, trace_id); | |
531 | } | |
532 | } | |
533 | ||
534 | g_array_free(trace_ids, TRUE); | |
535 | return 0; | |
536 | ||
537 | error: | |
538 | return ret; | |
539 | } | |
540 | ||
541 | int main(int argc, char **argv) | |
542 | { | |
543 | int ret; | |
544 | struct bt_context *bt_ctx = NULL; | |
545 | ||
546 | ret = parse_options(argc, argv); | |
547 | if (ret < 0) { | |
548 | fprintf(stdout, "Error parsing options.\n\n"); | |
549 | usage(stdout); | |
550 | exit(EXIT_FAILURE); | |
551 | } else if (ret > 0) { | |
552 | exit(EXIT_SUCCESS); | |
553 | } | |
554 | ||
555 | init_lttngtop(); | |
556 | ||
557 | bt_ctx = bt_context_create(); | |
558 | ret = bt_context_add_traces_recursive(bt_ctx, opt_input_path, "ctf", NULL); | |
559 | if (ret < 0) { | |
560 | printf("[error] Opening the trace\n"); | |
561 | goto end; | |
562 | } | |
563 | ||
564 | pthread_create(&display_thread, NULL, ncurses_display, (void *) NULL); | |
565 | pthread_create(&timer_thread, NULL, refresh_thread, (void *) NULL); | |
566 | ||
567 | iter_trace(bt_ctx); | |
568 | ||
569 | quit = 1; | |
570 | pthread_join(display_thread, NULL); | |
85db4618 | 571 | pthread_join(timer_thread, NULL); |
1fc22eb4 JD |
572 | |
573 | end: | |
85db4618 | 574 | bt_context_put(bt_ctx); |
1fc22eb4 JD |
575 | return 0; |
576 | } |