Update copyright years
[lttngtop.git] / src / lttngtop.c
CommitLineData
1fc22eb4 1/*
aa15ac1c 2 * Copyright (C) 2011-2012 Julien Desfossez
1fc22eb4
JD
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 *
71bd7ce1
AM
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.
1fc22eb4
JD
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>
3ba84bed 25#include <babeltrace/ctf/iterator.h>
1fc22eb4
JD
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
49const char *opt_input_path;
50
51struct lttngtop *copy;
52pthread_t display_thread;
53pthread_t timer_thread;
54
55unsigned long refresh_display = 1 * NSEC_PER_SEC;
56unsigned long last_display_update = 0;
57int quit = 0;
58
59enum {
60 OPT_NONE = 0,
61 OPT_HELP,
62 OPT_LIST,
63 OPT_VERBOSE,
64 OPT_DEBUG,
65 OPT_NAMES,
66};
67
68static 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
74void *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
86void *ncurses_display(void *p)
87{
88 unsigned int current_display_index = 0;
89
90 sem_wait(&bootstrap);
b332d28f
JD
91 /*
92 * Prevent the 1 second delay when we hit ESC
93 */
94 ESCDELAY = 0;
1fc22eb4
JD
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);
85db4618
JD
103 assert(copy);
104 display(current_display_index++);
1fc22eb4
JD
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 */
4adc8274 120enum bt_cb_ret check_timestamp(struct bt_ctf_event *call_data, void *private_data)
1fc22eb4
JD
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
141error:
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 */
150struct 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
559c9f86 167 ret = g_new0(struct perfcounter, 1);
1fc22eb4
JD
168 /* by default, make it visible in the UI */
169 ret->visible = 1;
85db4618 170 g_hash_table_insert(table, (gpointer) strdup(name), ret);
1fc22eb4 171
0d91c12a 172 global = g_hash_table_lookup(global_perf_liszt, (gpointer) name);
1fc22eb4 173 if (!global) {
559c9f86 174 global = g_new0(struct perfcounter, 1);
1fc22eb4
JD
175 memcpy(global, ret, sizeof(struct perfcounter));
176 /* by default, sort on the first perf context */
0d91c12a 177 if (g_hash_table_size(global_perf_liszt) == 0)
1fc22eb4 178 global->sort = 1;
0d91c12a 179 g_hash_table_insert(global_perf_liszt, (gpointer) strdup(name), global);
1fc22eb4
JD
180 }
181
182end:
183 return ret;
184
185error:
186 return NULL;
187}
188
189void 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
4adc8274 202void extract_perf_counter_scope(const struct bt_ctf_event *event,
3ba84bed 203 const struct definition *scope,
1fc22eb4
JD
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]);
6419078d 220 if (strncmp(name, "perf_", 5) == 0) {
1fc22eb4
JD
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
228end:
229 return;
230}
231
4adc8274 232void update_perf_counter(struct processtop *proc, const struct bt_ctf_event *event)
1fc22eb4 233{
1fc22eb4 234 struct cputime *cpu;
3ba84bed 235 const struct definition *scope;
1fc22eb4 236
d67167cd 237 cpu = get_cpu(get_cpu_id(event));
1fc22eb4
JD
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);
1fc22eb4
JD
247}
248
4adc8274 249enum bt_cb_ret fix_process_table(struct bt_ctf_event *call_data,
1fc22eb4
JD
250 void *private_data)
251{
252 int pid, tid, ppid;
253 char *comm;
254 struct processtop *parent, *child;
1fc22eb4
JD
255 unsigned long timestamp;
256
85db4618 257 /* FIXME : display nice error when missing context pid, tid, ppid and comm */
1fc22eb4
JD
258
259 timestamp = bt_ctf_get_timestamp(call_data);
260 if (timestamp == -1ULL)
261 goto error;
262
1dec520a
JD
263 pid = get_context_pid(call_data);
264 if (pid == -1ULL) {
1fc22eb4
JD
265// fprintf(stderr, "Missing pid context info\n");
266 goto error;
267 }
1dec520a
JD
268 tid = get_context_tid(call_data);
269 if (tid == -1ULL) {
1fc22eb4
JD
270// fprintf(stderr, "Missing tid context info\n");
271 goto error;
272 }
1dec520a
JD
273 ppid = get_context_ppid(call_data);
274 if (ppid == -1ULL) {
1fc22eb4
JD
275// fprintf(stderr, "Missing ppid context info\n");
276 goto error;
277 }
1dec520a
JD
278 comm = get_context_comm(call_data);
279 if (!comm) {
1fc22eb4
JD
280// fprintf(stderr, "Missing procname context info\n");
281 goto error;
282 }
283
284 /* find or create the current process */
285 child = find_process_tid(&lttngtop, tid, comm);
286 if (!child)
287 child = add_proc(&lttngtop, tid, comm, timestamp);
288 update_proc(child, pid, tid, ppid, comm);
289
290 if (pid != tid) {
291 /* find or create the parent */
292 parent = find_process_tid(&lttngtop, pid, comm);
293 if (!parent) {
294 parent = add_proc(&lttngtop, pid, comm, timestamp);
295 parent->pid = pid;
296 }
297
298 /* attach the parent to the current process */
299 child->threadparent = parent;
300 add_thread(parent, child);
301 }
302
303 update_perf_counter(child, call_data);
304
305 return BT_CB_OK;
306
307error:
308 return BT_CB_ERROR_STOP;
309}
310
311void init_lttngtop()
312{
313 copies = g_ptr_array_new();
0d91c12a 314 global_perf_liszt = g_hash_table_new(g_str_hash, g_str_equal);
1fc22eb4
JD
315
316 sem_init(&goodtodisplay, 0, 0);
317 sem_init(&goodtoupdate, 0, 1);
318 sem_init(&timer, 0, 1);
319 sem_init(&bootstrap, 0, 0);
320 sem_init(&pause_sem, 0, 1);
321 sem_init(&end_trace_sem, 0, 0);
322
e05a35a6
JD
323 reset_global_counters();
324 lttngtop.nbproc = 0;
325 lttngtop.nbthreads = 0;
326 lttngtop.nbfiles = 0;
327
1fc22eb4
JD
328 lttngtop.process_table = g_ptr_array_new();
329 lttngtop.files_table = g_ptr_array_new();
330 lttngtop.cpu_table = g_ptr_array_new();
331}
332
c263c4eb 333void usage(FILE *fp)
1fc22eb4 334{
c263c4eb
JD
335 fprintf(fp, "LTTngTop %s\n\n", VERSION);
336 fprintf(fp, "Usage : lttngtop /path/to/trace\n");
1fc22eb4
JD
337}
338
339/*
340 * Return 0 if caller should continue, < 0 if caller should return
341 * error, > 0 if caller should exit without reporting error.
342 */
343static int parse_options(int argc, char **argv)
344{
345 poptContext pc;
346 int opt, ret = 0;
347
348 if (argc == 1) {
349 usage(stdout);
350 return 1; /* exit cleanly */
351 }
352
353 pc = poptGetContext(NULL, argc, (const char **) argv, long_options, 0);
354 poptReadDefaultConfig(pc, 0);
355
356 while ((opt = poptGetNextOpt(pc)) != -1) {
357 switch (opt) {
358 case OPT_HELP:
359 usage(stdout);
360 ret = 1; /* exit cleanly */
361 goto end;
362 case OPT_LIST:
363 // list_formats(stdout);
364 ret = 1;
365 goto end;
366 case OPT_VERBOSE:
367// babeltrace_verbose = 1;
368 break;
369 case OPT_DEBUG:
370// babeltrace_debug = 1;
371 break;
372 case OPT_NAMES:
373// opt_field_names = 1;
374 break;
375 default:
376 ret = -EINVAL;
377 goto end;
378 }
379 }
380
381 opt_input_path = poptGetArg(pc);
382 if (!opt_input_path) {
383 ret = -EINVAL;
384 goto end;
385 }
386end:
387 if (pc) {
388 poptFreeContext(pc);
389 }
390 return ret;
391}
392
393void iter_trace(struct bt_context *bt_ctx)
394{
395 struct bt_ctf_iter *iter;
396 struct bt_iter_pos begin_pos;
4adc8274 397 const struct bt_ctf_event *event;
1fc22eb4
JD
398 int ret = 0;
399
400 begin_pos.type = BT_SEEK_BEGIN;
401 iter = bt_ctf_iter_create(bt_ctx, &begin_pos, NULL);
402
403 /* at each event check if we need to refresh */
404 bt_ctf_iter_add_callback(iter, 0, NULL, 0,
405 check_timestamp,
406 NULL, NULL, NULL);
407 /* at each event, verify the status of the process table */
408 bt_ctf_iter_add_callback(iter, 0, NULL, 0,
409 fix_process_table,
410 NULL, NULL, NULL);
411 /* to handle the scheduling events */
412 bt_ctf_iter_add_callback(iter,
413 g_quark_from_static_string("sched_switch"),
414 NULL, 0, handle_sched_switch, NULL, NULL, NULL);
415 /* to clean up the process table */
416 bt_ctf_iter_add_callback(iter,
417 g_quark_from_static_string("sched_process_free"),
418 NULL, 0, handle_sched_process_free, NULL, NULL, NULL);
928f18a6
MB
419 /* to get all the process from the statedumps */
420 bt_ctf_iter_add_callback(iter,
421 g_quark_from_static_string(
422 "lttng_statedump_process_state"),
423 NULL, 0, handle_statedump_process_state,
424 NULL, NULL, NULL);
1fc22eb4
JD
425
426 /* for IO top */
427 bt_ctf_iter_add_callback(iter,
428 g_quark_from_static_string("exit_syscall"),
429 NULL, 0, handle_exit_syscall, NULL, NULL, NULL);
430 bt_ctf_iter_add_callback(iter,
431 g_quark_from_static_string("sys_write"),
432 NULL, 0, handle_sys_write, NULL, NULL, NULL);
433 bt_ctf_iter_add_callback(iter,
434 g_quark_from_static_string("sys_read"),
435 NULL, 0, handle_sys_read, NULL, NULL, NULL);
b093de8a
MB
436 bt_ctf_iter_add_callback(iter,
437 g_quark_from_static_string("sys_open"),
438 NULL, 0, handle_sys_open, NULL, NULL, NULL);
b093de8a
MB
439 bt_ctf_iter_add_callback(iter,
440 g_quark_from_static_string("sys_close"),
441 NULL, 0, handle_sys_close, NULL, NULL, NULL);
ceb3a221
MB
442 bt_ctf_iter_add_callback(iter,
443 g_quark_from_static_string(
444 "lttng_statedump_file_descriptor"),
445 NULL, 0, handle_statedump_file_descriptor,
446 NULL, NULL, NULL);
59288610 447
1fc22eb4
JD
448 while ((event = bt_ctf_iter_read_event(iter)) != NULL) {
449 ret = bt_iter_next(bt_ctf_get_iter(iter));
450 if (ret < 0)
451 goto end_iter;
452 }
453
454 /* block until quit, we reached the end of the trace */
455 sem_wait(&end_trace_sem);
456
457end_iter:
06570214 458 bt_ctf_iter_destroy(iter);
1fc22eb4
JD
459}
460
461/*
462 * bt_context_add_traces_recursive: Open a trace recursively
463 * (copied from BSD code in converter/babeltrace.c)
464 *
465 * Find each trace present in the subdirectory starting from the given
466 * path, and add them to the context. The packet_seek parameter can be
467 * NULL: this specify to use the default format packet_seek.
468 *
469 * Return: 0 on success, nonzero on failure.
470 * Unable to open toplevel: failure.
471 * Unable to open some subdirectory or file: warn and continue;
472 */
473int bt_context_add_traces_recursive(struct bt_context *ctx, const char *path,
474 const char *format_str,
475 void (*packet_seek)(struct stream_pos *pos,
476 size_t offset, int whence))
477{
478 FTS *tree;
479 FTSENT *node;
480 GArray *trace_ids;
481 char lpath[PATH_MAX];
482 char * const paths[2] = { lpath, NULL };
0d567cf1 483 int ret = -1;
1fc22eb4
JD
484
485 /*
486 * Need to copy path, because fts_open can change it.
487 * It is the pointer array, not the strings, that are constant.
488 */
489 strncpy(lpath, path, PATH_MAX);
490 lpath[PATH_MAX - 1] = '\0';
491
492 tree = fts_open(paths, FTS_NOCHDIR | FTS_LOGICAL, 0);
493 if (tree == NULL) {
494 fprintf(stderr, "[error] [Context] Cannot traverse \"%s\" for reading.\n",
495 path);
496 return -EINVAL;
497 }
498
499 trace_ids = g_array_new(FALSE, TRUE, sizeof(int));
500
501 while ((node = fts_read(tree))) {
502 int dirfd, metafd;
503
504 if (!(node->fts_info & FTS_D))
505 continue;
506
507 dirfd = open(node->fts_accpath, 0);
508 if (dirfd < 0) {
509 fprintf(stderr, "[error] [Context] Unable to open trace "
510 "directory file descriptor.\n");
511 ret = dirfd;
512 goto error;
513 }
514 metafd = openat(dirfd, "metadata", O_RDONLY);
515 if (metafd < 0) {
7314c855
JD
516 close(dirfd);
517 ret = -1;
518 continue;
1fc22eb4
JD
519 } else {
520 int trace_id;
521
522 ret = close(metafd);
523 if (ret < 0) {
524 perror("close");
525 goto error;
526 }
527 ret = close(dirfd);
528 if (ret < 0) {
529 perror("close");
530 goto error;
531 }
532
533 trace_id = bt_context_add_trace(ctx,
534 node->fts_accpath, format_str,
535 packet_seek, NULL, NULL);
536 if (trace_id < 0) {
0d567cf1 537 fprintf(stderr, "[warning] [Context] opening trace \"%s\" from %s "
1fc22eb4 538 "for reading.\n", node->fts_accpath, path);
0d567cf1
JD
539 /* Allow to skip erroneous traces. */
540 continue;
1fc22eb4
JD
541 }
542 g_array_append_val(trace_ids, trace_id);
543 }
544 }
545
546 g_array_free(trace_ids, TRUE);
0d567cf1 547 return ret;
1fc22eb4
JD
548
549error:
550 return ret;
551}
552
4553b4d1 553static int check_field_requirements(const struct bt_ctf_field_decl *const * field_list,
a54d0d2c
JD
554 int field_cnt, int *tid_check, int *pid_check,
555 int *procname_check, int *ppid_check)
556{
557 int j;
558
559 for (j = 0; j < field_cnt; j++) {
560 if (*tid_check == 0) {
561 if (strncmp(bt_ctf_get_decl_field_name(field_list[j]), "tid", 3) == 0) {
562 (*tid_check)++;
563 }
564 }
565 if (*pid_check == 0) {
566 if (strncmp(bt_ctf_get_decl_field_name(field_list[j]), "pid", 3) == 0)
567 (*pid_check)++;
568 }
569 if (*ppid_check == 0) {
570 if (strncmp(bt_ctf_get_decl_field_name(field_list[j]), "ppid", 4) == 0)
571 (*ppid_check)++;
572 }
573 if (*procname_check == 0) {
574 if (strncmp(bt_ctf_get_decl_field_name(field_list[j]), "procname", 8) == 0)
575 (*procname_check)++;
576 }
577 }
578 /* if all checks are OK, no need to continue the checks */
579 if (*tid_check == 1 && *pid_check == 1 && *ppid_check == 1 &&
580 *procname_check == 1)
581 return 0;
582
583 return -1;
584}
585
586/*
587 * check_requirements: check if the required context informations are available
588 *
589 * If each mandatory context information is available for at least in one
590 * event, return 0 otherwise return -1.
a54d0d2c
JD
591 */
592int check_requirements(struct bt_context *ctx)
593{
594 unsigned int i, evt_cnt, field_cnt;
595 struct bt_ctf_event_decl *const * evt_list;
596 const struct bt_ctf_field_decl *const * field_list;
597 int tid_check = 0;
598 int pid_check = 0;
599 int procname_check = 0;
600 int ppid_check = 0;
601 int ret = 0;
602
603 bt_ctf_get_event_decl_list(0, ctx, &evt_list, &evt_cnt);
604 for (i = 0; i < evt_cnt; i++) {
605 bt_ctf_get_decl_fields(evt_list[i], BT_STREAM_EVENT_CONTEXT,
606 &field_list, &field_cnt);
607 ret = check_field_requirements(field_list, field_cnt,
608 &tid_check, &pid_check, &procname_check,
609 &ppid_check);
610 if (ret == 0)
611 goto end;
612
613 bt_ctf_get_decl_fields(evt_list[i], BT_EVENT_CONTEXT,
614 &field_list, &field_cnt);
615 ret = check_field_requirements(field_list, field_cnt,
616 &tid_check, &pid_check, &procname_check,
617 &ppid_check);
618 if (ret == 0)
619 goto end;
620
621 bt_ctf_get_decl_fields(evt_list[i], BT_STREAM_PACKET_CONTEXT,
622 &field_list, &field_cnt);
623 ret = check_field_requirements(field_list, field_cnt,
624 &tid_check, &pid_check, &procname_check,
625 &ppid_check);
626 if (ret == 0)
627 goto end;
628 }
629
630 if (tid_check == 0) {
631 ret = -1;
632 fprintf(stderr, "[error] missing tid context information\n");
633 }
634 if (pid_check == 0) {
635 ret = -1;
636 fprintf(stderr, "[error] missing pid context information\n");
637 }
638 if (ppid_check == 0) {
639 ret = -1;
640 fprintf(stderr, "[error] missing ppid context information\n");
641 }
642 if (procname_check == 0) {
643 ret = -1;
644 fprintf(stderr, "[error] missing procname context information\n");
645 }
646
647end:
648 return ret;
649}
650
1fc22eb4
JD
651int main(int argc, char **argv)
652{
653 int ret;
654 struct bt_context *bt_ctx = NULL;
655
656 ret = parse_options(argc, argv);
657 if (ret < 0) {
658 fprintf(stdout, "Error parsing options.\n\n");
659 usage(stdout);
660 exit(EXIT_FAILURE);
661 } else if (ret > 0) {
662 exit(EXIT_SUCCESS);
663 }
664
665 init_lttngtop();
666
667 bt_ctx = bt_context_create();
668 ret = bt_context_add_traces_recursive(bt_ctx, opt_input_path, "ctf", NULL);
669 if (ret < 0) {
a54d0d2c
JD
670 fprintf(stderr, "[error] Opening the trace\n");
671 goto end;
672 }
673
674 ret = check_requirements(bt_ctx);
675 if (ret < 0) {
6ff9c173 676 fprintf(stderr, "[error] some mandatory contexts were missing, exiting.\n");
1fc22eb4
JD
677 goto end;
678 }
679
680 pthread_create(&display_thread, NULL, ncurses_display, (void *) NULL);
681 pthread_create(&timer_thread, NULL, refresh_thread, (void *) NULL);
682
683 iter_trace(bt_ctx);
684
685 quit = 1;
686 pthread_join(display_thread, NULL);
85db4618 687 pthread_join(timer_thread, NULL);
1fc22eb4
JD
688
689end:
85db4618 690 bt_context_put(bt_ctx);
1fc22eb4
JD
691 return 0;
692}
This page took 0.085137 seconds and 4 git commands to generate.