107e228a42178069a087378484af1b9bdc1c459a
[lttngtop.git] / src / lttngtop.c
1 /*
2 * Copyright (C) 2011-2012 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 along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #define _GNU_SOURCE
19 #include <config.h>
20 #include <stdio.h>
21 #include <stdint.h>
22 #include <babeltrace/babeltrace.h>
23 #include <babeltrace/ctf/events.h>
24 #include <babeltrace/ctf/callbacks.h>
25 #include <babeltrace/ctf/iterator.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>
39 #include <assert.h>
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) {
77 if (quit)
78 return NULL;
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 /*
92 * Prevent the 1 second delay when we hit ESC
93 */
94 ESCDELAY = 0;
95 init_ncurses();
96
97 while (1) {
98 sem_wait(&timer);
99 sem_wait(&goodtodisplay);
100 sem_wait(&pause_sem);
101
102 copy = g_ptr_array_index(copies, current_display_index);
103 assert(copy);
104 display(current_display_index++);
105
106 sem_post(&goodtoupdate);
107 sem_post(&pause_sem);
108
109 if (quit) {
110 reset_ncurses();
111 pthread_exit(0);
112 }
113 }
114 }
115
116 /*
117 * hook on each event to check the timestamp and refresh the display if
118 * necessary
119 */
120 enum bt_cb_ret check_timestamp(struct bt_ctf_event *call_data, void *private_data)
121 {
122 unsigned long timestamp;
123
124 timestamp = bt_ctf_get_timestamp(call_data);
125 if (timestamp == -1ULL)
126 goto error;
127
128 if (last_display_update == 0)
129 last_display_update = timestamp;
130
131 if (timestamp - last_display_update >= refresh_display) {
132 sem_wait(&goodtoupdate);
133 g_ptr_array_add(copies, get_copy_lttngtop(last_display_update,
134 timestamp));
135 sem_post(&goodtodisplay);
136 sem_post(&bootstrap);
137 last_display_update = timestamp;
138 }
139 return BT_CB_OK;
140
141 error:
142 fprintf(stderr, "check_timestamp callback error\n");
143 return BT_CB_ERROR_STOP;
144 }
145
146 /*
147 * get_perf_counter : get or create and return a perf_counter struct for
148 * either a process or a cpu (only one of the 2 parameters mandatory)
149 */
150 struct perfcounter *get_perf_counter(const char *name, struct processtop *proc,
151 struct cputime *cpu)
152 {
153 struct perfcounter *ret, *global;
154 GHashTable *table;
155
156 if (proc)
157 table = proc->perf;
158 else if (cpu)
159 table = cpu->perf;
160 else
161 goto error;
162
163 ret = g_hash_table_lookup(table, (gpointer) name);
164 if (ret)
165 goto end;
166
167 ret = g_new0(struct perfcounter, 1);
168 /* by default, make it visible in the UI */
169 ret->visible = 1;
170 g_hash_table_insert(table, (gpointer) strdup(name), ret);
171
172 global = g_hash_table_lookup(global_perf_liszt, (gpointer) name);
173 if (!global) {
174 global = g_new0(struct perfcounter, 1);
175 memcpy(global, ret, sizeof(struct perfcounter));
176 /* by default, sort on the first perf context */
177 if (g_hash_table_size(global_perf_liszt) == 0)
178 global->sort = 1;
179 g_hash_table_insert(global_perf_liszt, (gpointer) strdup(name), global);
180 }
181
182 end:
183 return ret;
184
185 error:
186 return NULL;
187 }
188
189 void update_perf_value(struct processtop *proc, struct cputime *cpu,
190 const char *name, int value)
191 {
192 struct perfcounter *cpu_perf, *process_perf;
193
194 cpu_perf = get_perf_counter(name, NULL, cpu);
195 if (cpu_perf->count < value) {
196 process_perf = get_perf_counter(name, proc, NULL);
197 process_perf->count += value - cpu_perf->count;
198 cpu_perf->count = value;
199 }
200 }
201
202 void extract_perf_counter_scope(const struct bt_ctf_event *event,
203 const struct definition *scope,
204 struct processtop *proc,
205 struct cputime *cpu)
206 {
207 struct definition const * const *list = NULL;
208 unsigned int count;
209 int i, ret;
210
211 if (!scope)
212 goto end;
213
214 ret = bt_ctf_get_field_list(event, scope, &list, &count);
215 if (ret < 0)
216 goto end;
217
218 for (i = 0; i < count; i++) {
219 const char *name = bt_ctf_field_name(list[i]);
220 if (strncmp(name, "perf_", 5) == 0) {
221 int value = bt_ctf_get_uint64(list[i]);
222 if (bt_ctf_field_get_error())
223 continue;
224 update_perf_value(proc, cpu, name, value);
225 }
226 }
227
228 end:
229 return;
230 }
231
232 void update_perf_counter(struct processtop *proc, const struct bt_ctf_event *event)
233 {
234 struct cputime *cpu;
235 const struct definition *scope;
236
237 cpu = get_cpu(get_cpu_id(event));
238
239 scope = bt_ctf_get_top_level_scope(event, BT_STREAM_EVENT_CONTEXT);
240 extract_perf_counter_scope(event, scope, proc, cpu);
241
242 scope = bt_ctf_get_top_level_scope(event, BT_STREAM_PACKET_CONTEXT);
243 extract_perf_counter_scope(event, scope, proc, cpu);
244
245 scope = bt_ctf_get_top_level_scope(event, BT_EVENT_CONTEXT);
246 extract_perf_counter_scope(event, scope, proc, cpu);
247 }
248
249 enum bt_cb_ret fix_process_table(struct bt_ctf_event *call_data,
250 void *private_data)
251 {
252 int pid, tid, ppid;
253 char *comm;
254 struct processtop *parent, *child;
255 unsigned long timestamp;
256
257 timestamp = bt_ctf_get_timestamp(call_data);
258 if (timestamp == -1ULL)
259 goto error;
260
261 pid = get_context_pid(call_data);
262 if (pid == -1ULL) {
263 goto error;
264 }
265 tid = get_context_tid(call_data);
266 if (tid == -1ULL) {
267 goto error;
268 }
269 ppid = get_context_ppid(call_data);
270 if (ppid == -1ULL) {
271 goto error;
272 }
273 comm = get_context_comm(call_data);
274 if (!comm) {
275 goto error;
276 }
277
278 /* find or create the current process */
279 child = find_process_tid(&lttngtop, tid, comm);
280 if (!child)
281 child = add_proc(&lttngtop, tid, comm, timestamp);
282 update_proc(child, pid, tid, ppid, comm);
283
284 if (pid != tid) {
285 /* find or create the parent */
286 parent = find_process_tid(&lttngtop, pid, comm);
287 if (!parent) {
288 parent = add_proc(&lttngtop, pid, comm, timestamp);
289 parent->pid = pid;
290 }
291
292 /* attach the parent to the current process */
293 child->threadparent = parent;
294 add_thread(parent, child);
295 }
296
297 update_perf_counter(child, call_data);
298
299 return BT_CB_OK;
300
301 error:
302 return BT_CB_ERROR_STOP;
303 }
304
305 void init_lttngtop()
306 {
307 copies = g_ptr_array_new();
308 global_perf_liszt = g_hash_table_new(g_str_hash, g_str_equal);
309
310 sem_init(&goodtodisplay, 0, 0);
311 sem_init(&goodtoupdate, 0, 1);
312 sem_init(&timer, 0, 1);
313 sem_init(&bootstrap, 0, 0);
314 sem_init(&pause_sem, 0, 1);
315 sem_init(&end_trace_sem, 0, 0);
316
317 reset_global_counters();
318 lttngtop.nbproc = 0;
319 lttngtop.nbthreads = 0;
320 lttngtop.nbfiles = 0;
321
322 lttngtop.process_table = g_ptr_array_new();
323 lttngtop.files_table = g_ptr_array_new();
324 lttngtop.cpu_table = g_ptr_array_new();
325 }
326
327 void usage(FILE *fp)
328 {
329 fprintf(fp, "LTTngTop %s\n\n", VERSION);
330 fprintf(fp, "Usage : lttngtop /path/to/trace\n");
331 }
332
333 /*
334 * Return 0 if caller should continue, < 0 if caller should return
335 * error, > 0 if caller should exit without reporting error.
336 */
337 static int parse_options(int argc, char **argv)
338 {
339 poptContext pc;
340 int opt, ret = 0;
341
342 if (argc == 1) {
343 usage(stdout);
344 return 1; /* exit cleanly */
345 }
346
347 pc = poptGetContext(NULL, argc, (const char **) argv, long_options, 0);
348 poptReadDefaultConfig(pc, 0);
349
350 while ((opt = poptGetNextOpt(pc)) != -1) {
351 switch (opt) {
352 case OPT_HELP:
353 usage(stdout);
354 ret = 1; /* exit cleanly */
355 goto end;
356 default:
357 ret = -EINVAL;
358 goto end;
359 }
360 }
361
362 opt_input_path = poptGetArg(pc);
363 if (!opt_input_path) {
364 ret = -EINVAL;
365 goto end;
366 }
367 end:
368 if (pc) {
369 poptFreeContext(pc);
370 }
371 return ret;
372 }
373
374 void iter_trace(struct bt_context *bt_ctx)
375 {
376 struct bt_ctf_iter *iter;
377 struct bt_iter_pos begin_pos;
378 const struct bt_ctf_event *event;
379 int ret = 0;
380
381 begin_pos.type = BT_SEEK_BEGIN;
382 iter = bt_ctf_iter_create(bt_ctx, &begin_pos, NULL);
383
384 /* at each event check if we need to refresh */
385 bt_ctf_iter_add_callback(iter, 0, NULL, 0,
386 check_timestamp,
387 NULL, NULL, NULL);
388 /* at each event, verify the status of the process table */
389 bt_ctf_iter_add_callback(iter, 0, NULL, 0,
390 fix_process_table,
391 NULL, NULL, NULL);
392 /* to handle the scheduling events */
393 bt_ctf_iter_add_callback(iter,
394 g_quark_from_static_string("sched_switch"),
395 NULL, 0, handle_sched_switch, NULL, NULL, NULL);
396 /* to clean up the process table */
397 bt_ctf_iter_add_callback(iter,
398 g_quark_from_static_string("sched_process_free"),
399 NULL, 0, handle_sched_process_free, NULL, NULL, NULL);
400 /* to get all the process from the statedumps */
401 bt_ctf_iter_add_callback(iter,
402 g_quark_from_static_string(
403 "lttng_statedump_process_state"),
404 NULL, 0, handle_statedump_process_state,
405 NULL, NULL, NULL);
406
407 /* for IO top */
408 bt_ctf_iter_add_callback(iter,
409 g_quark_from_static_string("exit_syscall"),
410 NULL, 0, handle_exit_syscall, NULL, NULL, NULL);
411 bt_ctf_iter_add_callback(iter,
412 g_quark_from_static_string("sys_write"),
413 NULL, 0, handle_sys_write, NULL, NULL, NULL);
414 bt_ctf_iter_add_callback(iter,
415 g_quark_from_static_string("sys_read"),
416 NULL, 0, handle_sys_read, NULL, NULL, NULL);
417 bt_ctf_iter_add_callback(iter,
418 g_quark_from_static_string("sys_open"),
419 NULL, 0, handle_sys_open, NULL, NULL, NULL);
420 bt_ctf_iter_add_callback(iter,
421 g_quark_from_static_string("sys_close"),
422 NULL, 0, handle_sys_close, NULL, NULL, NULL);
423 bt_ctf_iter_add_callback(iter,
424 g_quark_from_static_string(
425 "lttng_statedump_file_descriptor"),
426 NULL, 0, handle_statedump_file_descriptor,
427 NULL, NULL, NULL);
428
429 while ((event = bt_ctf_iter_read_event(iter)) != NULL) {
430 ret = bt_iter_next(bt_ctf_get_iter(iter));
431 if (ret < 0)
432 goto end_iter;
433 }
434
435 /* block until quit, we reached the end of the trace */
436 sem_wait(&end_trace_sem);
437
438 end_iter:
439 bt_ctf_iter_destroy(iter);
440 }
441
442 /*
443 * bt_context_add_traces_recursive: Open a trace recursively
444 * (copied from BSD code in converter/babeltrace.c)
445 *
446 * Find each trace present in the subdirectory starting from the given
447 * path, and add them to the context. The packet_seek parameter can be
448 * NULL: this specify to use the default format packet_seek.
449 *
450 * Return: 0 on success, nonzero on failure.
451 * Unable to open toplevel: failure.
452 * Unable to open some subdirectory or file: warn and continue;
453 */
454 int bt_context_add_traces_recursive(struct bt_context *ctx, const char *path,
455 const char *format_str,
456 void (*packet_seek)(struct stream_pos *pos,
457 size_t offset, int whence))
458 {
459 FTS *tree;
460 FTSENT *node;
461 GArray *trace_ids;
462 char lpath[PATH_MAX];
463 char * const paths[2] = { lpath, NULL };
464 int ret = -1;
465
466 /*
467 * Need to copy path, because fts_open can change it.
468 * It is the pointer array, not the strings, that are constant.
469 */
470 strncpy(lpath, path, PATH_MAX);
471 lpath[PATH_MAX - 1] = '\0';
472
473 tree = fts_open(paths, FTS_NOCHDIR | FTS_LOGICAL, 0);
474 if (tree == NULL) {
475 fprintf(stderr, "[error] [Context] Cannot traverse \"%s\" for reading.\n",
476 path);
477 return -EINVAL;
478 }
479
480 trace_ids = g_array_new(FALSE, TRUE, sizeof(int));
481
482 while ((node = fts_read(tree))) {
483 int dirfd, metafd;
484
485 if (!(node->fts_info & FTS_D))
486 continue;
487
488 dirfd = open(node->fts_accpath, 0);
489 if (dirfd < 0) {
490 fprintf(stderr, "[error] [Context] Unable to open trace "
491 "directory file descriptor.\n");
492 ret = dirfd;
493 goto error;
494 }
495 metafd = openat(dirfd, "metadata", O_RDONLY);
496 if (metafd < 0) {
497 close(dirfd);
498 ret = -1;
499 continue;
500 } else {
501 int trace_id;
502
503 ret = close(metafd);
504 if (ret < 0) {
505 perror("close");
506 goto error;
507 }
508 ret = close(dirfd);
509 if (ret < 0) {
510 perror("close");
511 goto error;
512 }
513
514 trace_id = bt_context_add_trace(ctx,
515 node->fts_accpath, format_str,
516 packet_seek, NULL, NULL);
517 if (trace_id < 0) {
518 fprintf(stderr, "[warning] [Context] opening trace \"%s\" from %s "
519 "for reading.\n", node->fts_accpath, path);
520 /* Allow to skip erroneous traces. */
521 continue;
522 }
523 g_array_append_val(trace_ids, trace_id);
524 }
525 }
526
527 g_array_free(trace_ids, TRUE);
528 return ret;
529
530 error:
531 return ret;
532 }
533
534 static int check_field_requirements(const struct bt_ctf_field_decl *const * field_list,
535 int field_cnt, int *tid_check, int *pid_check,
536 int *procname_check, int *ppid_check)
537 {
538 int j;
539
540 for (j = 0; j < field_cnt; j++) {
541 if (*tid_check == 0) {
542 if (strncmp(bt_ctf_get_decl_field_name(field_list[j]), "tid", 3) == 0) {
543 (*tid_check)++;
544 }
545 }
546 if (*pid_check == 0) {
547 if (strncmp(bt_ctf_get_decl_field_name(field_list[j]), "pid", 3) == 0)
548 (*pid_check)++;
549 }
550 if (*ppid_check == 0) {
551 if (strncmp(bt_ctf_get_decl_field_name(field_list[j]), "ppid", 4) == 0)
552 (*ppid_check)++;
553 }
554 if (*procname_check == 0) {
555 if (strncmp(bt_ctf_get_decl_field_name(field_list[j]), "procname", 8) == 0)
556 (*procname_check)++;
557 }
558 }
559 /* if all checks are OK, no need to continue the checks */
560 if (*tid_check == 1 && *pid_check == 1 && *ppid_check == 1 &&
561 *procname_check == 1)
562 return 0;
563
564 return -1;
565 }
566
567 /*
568 * check_requirements: check if the required context informations are available
569 *
570 * If each mandatory context information is available for at least in one
571 * event, return 0 otherwise return -1.
572 */
573 int check_requirements(struct bt_context *ctx)
574 {
575 unsigned int i, evt_cnt, field_cnt;
576 struct bt_ctf_event_decl *const * evt_list;
577 const struct bt_ctf_field_decl *const * field_list;
578 int tid_check = 0;
579 int pid_check = 0;
580 int procname_check = 0;
581 int ppid_check = 0;
582 int ret = 0;
583
584 bt_ctf_get_event_decl_list(0, ctx, &evt_list, &evt_cnt);
585 for (i = 0; i < evt_cnt; i++) {
586 bt_ctf_get_decl_fields(evt_list[i], BT_STREAM_EVENT_CONTEXT,
587 &field_list, &field_cnt);
588 ret = check_field_requirements(field_list, field_cnt,
589 &tid_check, &pid_check, &procname_check,
590 &ppid_check);
591 if (ret == 0)
592 goto end;
593
594 bt_ctf_get_decl_fields(evt_list[i], BT_EVENT_CONTEXT,
595 &field_list, &field_cnt);
596 ret = check_field_requirements(field_list, field_cnt,
597 &tid_check, &pid_check, &procname_check,
598 &ppid_check);
599 if (ret == 0)
600 goto end;
601
602 bt_ctf_get_decl_fields(evt_list[i], BT_STREAM_PACKET_CONTEXT,
603 &field_list, &field_cnt);
604 ret = check_field_requirements(field_list, field_cnt,
605 &tid_check, &pid_check, &procname_check,
606 &ppid_check);
607 if (ret == 0)
608 goto end;
609 }
610
611 if (tid_check == 0) {
612 ret = -1;
613 fprintf(stderr, "[error] missing tid context information\n");
614 }
615 if (pid_check == 0) {
616 ret = -1;
617 fprintf(stderr, "[error] missing pid context information\n");
618 }
619 if (ppid_check == 0) {
620 ret = -1;
621 fprintf(stderr, "[error] missing ppid context information\n");
622 }
623 if (procname_check == 0) {
624 ret = -1;
625 fprintf(stderr, "[error] missing procname context information\n");
626 }
627
628 end:
629 return ret;
630 }
631
632 int main(int argc, char **argv)
633 {
634 int ret;
635 struct bt_context *bt_ctx = NULL;
636
637 ret = parse_options(argc, argv);
638 if (ret < 0) {
639 fprintf(stdout, "Error parsing options.\n\n");
640 usage(stdout);
641 exit(EXIT_FAILURE);
642 } else if (ret > 0) {
643 exit(EXIT_SUCCESS);
644 }
645
646 init_lttngtop();
647
648 bt_ctx = bt_context_create();
649 ret = bt_context_add_traces_recursive(bt_ctx, opt_input_path, "ctf", NULL);
650 if (ret < 0) {
651 fprintf(stderr, "[error] Opening the trace\n");
652 goto end;
653 }
654
655 ret = check_requirements(bt_ctx);
656 if (ret < 0) {
657 fprintf(stderr, "[error] some mandatory contexts were missing, exiting.\n");
658 goto end;
659 }
660
661 pthread_create(&display_thread, NULL, ncurses_display, (void *) NULL);
662 pthread_create(&timer_thread, NULL, refresh_thread, (void *) NULL);
663
664 iter_trace(bt_ctx);
665
666 quit = 1;
667 pthread_join(display_thread, NULL);
668 pthread_join(timer_thread, NULL);
669
670 end:
671 bt_context_put(bt_ctx);
672 return 0;
673 }
This page took 0.042031 seconds and 4 git commands to generate.