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