strace like working
[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 #include <sys/mman.h>
41 #include <lttng/lttng.h>
42 #include <lttng/lttngtop-helper.h>
43 #include <babeltrace/lttngtopmmappacketseek.h>
44
45 #include "lttngtoptypes.h"
46 #include "cputop.h"
47 #include "iostreamtop.h"
48 #include "cursesdisplay.h"
49 #include "common.h"
50
51 #define DEFAULT_FILE_ARRAY_SIZE 1
52
53 const char *opt_input_path;
54 static int opt_textdump;
55 static int opt_pid;
56 static int opt_child;
57
58 int quit = 0;
59
60 struct lttngtop *copy;
61 pthread_t display_thread;
62 pthread_t timer_thread;
63
64 unsigned long refresh_display = 1 * NSEC_PER_SEC;
65 unsigned long last_display_update = 0;
66
67 /* list of FDs available for being read with snapshots */
68 struct mmap_stream_list mmap_list;
69 GPtrArray *lttng_consumer_stream_array;
70 int sessiond_metadata, consumerd_metadata;
71 struct lttng_consumer_local_data *ctx = NULL;
72 /* list of snapshots currently not consumed */
73 GPtrArray *available_snapshots;
74 sem_t metadata_available;
75 int reload_trace = 0;
76
77 enum {
78 OPT_NONE = 0,
79 OPT_HELP,
80 OPT_TEXTDUMP,
81 OPT_PID,
82 OPT_CHILD,
83 };
84
85 static struct poptOption long_options[] = {
86 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
87 { "help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL },
88 { "textdump", 't', POPT_ARG_NONE, NULL, OPT_TEXTDUMP, NULL, NULL },
89 { "child", 'f', POPT_ARG_NONE, NULL, OPT_CHILD, NULL, NULL },
90 { "pid", 'p', POPT_ARG_INT, &opt_pid, OPT_PID, NULL, NULL },
91 { NULL, 0, 0, NULL, 0, NULL, NULL },
92 };
93
94 void *refresh_thread(void *p)
95 {
96 struct mmap_stream *mmap_info;
97
98 while (1) {
99 if (quit) {
100 sem_post(&pause_sem);
101 sem_post(&timer);
102 sem_post(&end_trace_sem);
103 sem_post(&goodtodisplay);
104 sem_post(&goodtoupdate);
105 pthread_exit(0);
106 }
107 if (!opt_input_path) {
108 bt_list_for_each_entry(mmap_info, &mmap_list.head, list)
109 helper_kernctl_buffer_flush(mmap_info->fd);
110 }
111 sem_wait(&pause_sem);
112 sem_post(&pause_sem);
113 sem_post(&timer);
114 sleep(refresh_display/NSEC_PER_SEC);
115 }
116 }
117
118 void *ncurses_display(void *p)
119 {
120 unsigned int current_display_index = 0;
121
122 sem_wait(&bootstrap);
123 /*
124 * Prevent the 1 second delay when we hit ESC
125 */
126 ESCDELAY = 0;
127 init_ncurses();
128
129 while (1) {
130 sem_wait(&timer);
131 sem_wait(&goodtodisplay);
132 sem_wait(&pause_sem);
133
134 if (quit) {
135 sem_post(&pause_sem);
136 sem_post(&timer);
137 reset_ncurses();
138 pthread_exit(0);
139 }
140
141 copy = g_ptr_array_index(copies, current_display_index);
142 assert(copy);
143 display(current_display_index++);
144
145 sem_post(&goodtoupdate);
146 sem_post(&pause_sem);
147 }
148 }
149
150 /*
151 * hook on each event to check the timestamp and refresh the display if
152 * necessary
153 */
154 enum bt_cb_ret print_timestamp(struct bt_ctf_event *call_data, void *private_data)
155 {
156 unsigned long timestamp;
157 struct tm start;
158 uint64_t ts_nsec_start;
159 int pid;
160 int64_t syscall_ret;
161 const struct definition *scope;
162
163 timestamp = bt_ctf_get_timestamp(call_data);
164
165 start = format_timestamp(timestamp);
166 ts_nsec_start = timestamp % NSEC_PER_SEC;
167
168 pid = get_context_pid(call_data);
169 if (pid == -1ULL && opt_pid) {
170 goto error;
171 }
172
173 if (opt_pid && opt_pid != pid)
174 goto end;
175
176 if (strcmp(bt_ctf_event_name(call_data), "exit_syscall") == 0) {
177 scope = bt_ctf_get_top_level_scope(call_data,
178 BT_EVENT_FIELDS);
179 syscall_ret = bt_ctf_get_int64(bt_ctf_get_field(call_data,
180 scope, "_ret"));
181 printf("= %ld\n", syscall_ret);
182 } else {
183 printf("%02d:%02d:%02d.%09" PRIu64 " %d : %s ", start.tm_hour,
184 start.tm_min, start.tm_sec, ts_nsec_start,
185 pid, bt_ctf_event_name(call_data));
186 }
187
188 end:
189 return BT_CB_OK;
190 error:
191 return BT_CB_ERROR_STOP;
192 }
193
194 /*
195 * hook on each event to check the timestamp and refresh the display if
196 * necessary
197 */
198 enum bt_cb_ret check_timestamp(struct bt_ctf_event *call_data, void *private_data)
199 {
200 unsigned long timestamp;
201
202 timestamp = bt_ctf_get_timestamp(call_data);
203 if (timestamp == -1ULL)
204 goto error;
205
206 if (last_display_update == 0)
207 last_display_update = timestamp;
208
209 if (timestamp - last_display_update >= refresh_display) {
210 sem_wait(&goodtoupdate);
211 g_ptr_array_add(copies, get_copy_lttngtop(last_display_update,
212 timestamp));
213 sem_post(&goodtodisplay);
214 sem_post(&bootstrap);
215 last_display_update = timestamp;
216 }
217 return BT_CB_OK;
218
219 error:
220 fprintf(stderr, "check_timestamp callback error\n");
221 return BT_CB_ERROR_STOP;
222 }
223
224 /*
225 * get_perf_counter : get or create and return a perf_counter struct for
226 * either a process or a cpu (only one of the 2 parameters mandatory)
227 */
228 struct perfcounter *get_perf_counter(const char *name, struct processtop *proc,
229 struct cputime *cpu)
230 {
231 struct perfcounter *ret;
232 GHashTable *table;
233
234 if (proc)
235 table = proc->perf;
236 else if (cpu)
237 table = cpu->perf;
238 else
239 goto error;
240
241 ret = g_hash_table_lookup(table, (gpointer) name);
242 if (ret)
243 goto end;
244
245 ret = g_new0(struct perfcounter, 1);
246 /* by default, make it visible in the UI */
247 ret->visible = 1;
248 g_hash_table_insert(table, (gpointer) strdup(name), ret);
249
250 end:
251 return ret;
252
253 error:
254 return NULL;
255 }
256
257 void update_perf_value(struct processtop *proc, struct cputime *cpu,
258 const char *name, int value)
259 {
260 struct perfcounter *cpu_perf, *process_perf;
261
262 cpu_perf = get_perf_counter(name, NULL, cpu);
263 if (cpu_perf->count < value) {
264 process_perf = get_perf_counter(name, proc, NULL);
265 process_perf->count += value - cpu_perf->count;
266 cpu_perf->count = value;
267 }
268 }
269
270 void extract_perf_counter_scope(const struct bt_ctf_event *event,
271 const struct bt_definition *scope,
272 struct processtop *proc,
273 struct cputime *cpu)
274 {
275 struct bt_definition const * const *list = NULL;
276 const struct bt_definition *field;
277 unsigned int count;
278 struct perfcounter *perfcounter;
279 GHashTableIter iter;
280 gpointer key;
281 int ret;
282
283 if (!scope)
284 goto end;
285
286 ret = bt_ctf_get_field_list(event, scope, &list, &count);
287 if (ret < 0)
288 goto end;
289
290 if (count == 0)
291 goto end;
292
293 g_hash_table_iter_init(&iter, global_perf_liszt);
294 while (g_hash_table_iter_next (&iter, &key, (gpointer) &perfcounter)) {
295 field = bt_ctf_get_field(event, scope, (char *) key);
296 if (field) {
297 int value = bt_ctf_get_uint64(field);
298 if (bt_ctf_field_get_error())
299 continue;
300 update_perf_value(proc, cpu, (char *) key, value);
301 }
302 }
303
304 end:
305 return;
306 }
307
308 void update_perf_counter(struct processtop *proc, const struct bt_ctf_event *event)
309 {
310 struct cputime *cpu;
311 const struct bt_definition *scope;
312
313 cpu = get_cpu(get_cpu_id(event));
314
315 scope = bt_ctf_get_top_level_scope(event, BT_STREAM_EVENT_CONTEXT);
316 extract_perf_counter_scope(event, scope, proc, cpu);
317
318 scope = bt_ctf_get_top_level_scope(event, BT_STREAM_PACKET_CONTEXT);
319 extract_perf_counter_scope(event, scope, proc, cpu);
320
321 scope = bt_ctf_get_top_level_scope(event, BT_EVENT_CONTEXT);
322 extract_perf_counter_scope(event, scope, proc, cpu);
323 }
324
325 enum bt_cb_ret fix_process_table(struct bt_ctf_event *call_data,
326 void *private_data)
327 {
328 int pid, tid, ppid, vpid, vtid, vppid;
329 char *comm;
330 struct processtop *parent, *child;
331 unsigned long timestamp;
332
333 timestamp = bt_ctf_get_timestamp(call_data);
334 if (timestamp == -1ULL)
335 goto error;
336
337 pid = get_context_pid(call_data);
338 if (pid == -1ULL) {
339 goto error;
340 }
341 tid = get_context_tid(call_data);
342 if (tid == -1ULL) {
343 goto error;
344 }
345 ppid = get_context_ppid(call_data);
346 if (ppid == -1ULL) {
347 goto error;
348 }
349 vpid = get_context_vpid(call_data);
350 if (pid == -1ULL) {
351 vpid = -1;
352 }
353 vtid = get_context_vtid(call_data);
354 if (tid == -1ULL) {
355 vtid = -1;
356 }
357 vppid = get_context_vppid(call_data);
358 if (ppid == -1ULL) {
359 vppid = -1;
360 }
361 comm = get_context_comm(call_data);
362 if (!comm) {
363 goto error;
364 }
365
366 /* find or create the current process */
367 child = find_process_tid(&lttngtop, tid, comm);
368 if (!child)
369 child = add_proc(&lttngtop, tid, comm, timestamp);
370 update_proc(child, pid, tid, ppid, vpid, vtid, vppid, comm);
371
372 if (pid != tid) {
373 /* find or create the parent */
374 parent = find_process_tid(&lttngtop, pid, comm);
375 if (!parent) {
376 parent = add_proc(&lttngtop, pid, comm, timestamp);
377 parent->pid = pid;
378 }
379
380 /* attach the parent to the current process */
381 child->threadparent = parent;
382 add_thread(parent, child);
383 }
384
385 update_perf_counter(child, call_data);
386
387 return BT_CB_OK;
388
389 error:
390 return BT_CB_ERROR_STOP;
391 }
392
393 void init_lttngtop()
394 {
395 copies = g_ptr_array_new();
396 global_perf_liszt = g_hash_table_new(g_str_hash, g_str_equal);
397
398 sem_init(&goodtodisplay, 0, 0);
399 sem_init(&goodtoupdate, 0, 1);
400 sem_init(&timer, 0, 1);
401 sem_init(&bootstrap, 0, 0);
402 sem_init(&pause_sem, 0, 1);
403 sem_init(&end_trace_sem, 0, 0);
404
405 reset_global_counters();
406 lttngtop.nbproc = 0;
407 lttngtop.nbthreads = 0;
408 lttngtop.nbfiles = 0;
409
410 lttngtop.process_hash_table = g_hash_table_new(g_direct_hash,
411 g_direct_equal);
412 lttngtop.process_table = g_ptr_array_new();
413 lttngtop.files_table = g_ptr_array_new();
414 lttngtop.cpu_table = g_ptr_array_new();
415 }
416
417 void usage(FILE *fp)
418 {
419 fprintf(fp, "LTTngTop %s\n\n", VERSION);
420 fprintf(fp, "Usage : lttngtop /path/to/trace\n");
421 }
422
423 /*
424 * Return 0 if caller should continue, < 0 if caller should return
425 * error, > 0 if caller should exit without reporting error.
426 */
427 static int parse_options(int argc, char **argv)
428 {
429 poptContext pc;
430 int opt, ret = 0;
431
432 pc = poptGetContext(NULL, argc, (const char **) argv, long_options, 0);
433 poptReadDefaultConfig(pc, 0);
434
435 while ((opt = poptGetNextOpt(pc)) != -1) {
436 switch (opt) {
437 case OPT_HELP:
438 usage(stdout);
439 ret = 1; /* exit cleanly */
440 goto end;
441 case OPT_TEXTDUMP:
442 opt_textdump = 1;
443 break;
444 case OPT_CHILD:
445 opt_textdump = 1;
446 opt_child = 1;
447 break;
448 case OPT_PID:
449 refresh_display = 0.1 * NSEC_PER_SEC;
450 opt_textdump = 1;
451 break;
452 default:
453 ret = -EINVAL;
454 goto end;
455 }
456 }
457
458 opt_input_path = poptGetArg(pc);
459
460 end:
461 if (pc) {
462 poptFreeContext(pc);
463 }
464 return ret;
465 }
466
467 void iter_trace(struct bt_context *bt_ctx)
468 {
469 struct bt_ctf_iter *iter;
470 struct bt_iter_pos begin_pos;
471 const struct bt_ctf_event *event;
472 int ret = 0;
473
474 begin_pos.type = BT_SEEK_BEGIN;
475 iter = bt_ctf_iter_create(bt_ctx, &begin_pos, NULL);
476
477 if (opt_textdump) {
478 if (opt_pid) {
479 printf("PID : %d, Child : %d\n", opt_pid, opt_child);
480 }
481 bt_ctf_iter_add_callback(iter, 0, NULL, 0,
482 print_timestamp,
483 NULL, NULL, NULL);
484 } else {
485 /* at each event check if we need to refresh */
486 bt_ctf_iter_add_callback(iter, 0, NULL, 0,
487 check_timestamp,
488 NULL, NULL, NULL);
489 /* at each event, verify the status of the process table */
490 bt_ctf_iter_add_callback(iter, 0, NULL, 0,
491 fix_process_table,
492 NULL, NULL, NULL);
493 /* to handle the scheduling events */
494 bt_ctf_iter_add_callback(iter,
495 g_quark_from_static_string("sched_switch"),
496 NULL, 0, handle_sched_switch, NULL, NULL, NULL);
497 /* to clean up the process table */
498 bt_ctf_iter_add_callback(iter,
499 g_quark_from_static_string("sched_process_free"),
500 NULL, 0, handle_sched_process_free, NULL, NULL, NULL);
501 /* to get all the process from the statedumps */
502 bt_ctf_iter_add_callback(iter,
503 g_quark_from_static_string(
504 "lttng_statedump_process_state"),
505 NULL, 0, handle_statedump_process_state,
506 NULL, NULL, NULL);
507
508 /* for IO top */
509 bt_ctf_iter_add_callback(iter,
510 g_quark_from_static_string("exit_syscall"),
511 NULL, 0, handle_exit_syscall, NULL, NULL, NULL);
512 bt_ctf_iter_add_callback(iter,
513 g_quark_from_static_string("sys_write"),
514 NULL, 0, handle_sys_write, NULL, NULL, NULL);
515 bt_ctf_iter_add_callback(iter,
516 g_quark_from_static_string("sys_read"),
517 NULL, 0, handle_sys_read, NULL, NULL, NULL);
518 bt_ctf_iter_add_callback(iter,
519 g_quark_from_static_string("sys_open"),
520 NULL, 0, handle_sys_open, NULL, NULL, NULL);
521 bt_ctf_iter_add_callback(iter,
522 g_quark_from_static_string("sys_close"),
523 NULL, 0, handle_sys_close, NULL, NULL, NULL);
524 bt_ctf_iter_add_callback(iter,
525 g_quark_from_static_string(
526 "lttng_statedump_file_descriptor"),
527 NULL, 0, handle_statedump_file_descriptor,
528 NULL, NULL, NULL);
529 }
530
531 while ((event = bt_ctf_iter_read_event(iter, NULL)) != NULL) {
532 if (quit || reload_trace)
533 goto end_iter;
534 ret = bt_iter_next(bt_ctf_get_iter(iter));
535 if (ret < 0)
536 goto end_iter;
537 }
538
539 /* block until quit, we reached the end of the trace */
540 sem_wait(&end_trace_sem);
541
542 end_iter:
543 bt_ctf_iter_destroy(iter);
544 }
545
546 /*
547 * bt_context_add_traces_recursive: Open a trace recursively
548 * (copied from BSD code in converter/babeltrace.c)
549 *
550 * Find each trace present in the subdirectory starting from the given
551 * path, and add them to the context. The packet_seek parameter can be
552 * NULL: this specify to use the default format packet_seek.
553 *
554 * Return: 0 on success, nonzero on failure.
555 * Unable to open toplevel: failure.
556 * Unable to open some subdirectory or file: warn and continue;
557 */
558 int bt_context_add_traces_recursive(struct bt_context *ctx, const char *path,
559 const char *format_str,
560 void (*packet_seek)(struct bt_stream_pos *pos,
561 size_t offset, int whence))
562 {
563 FTS *tree;
564 FTSENT *node;
565 GArray *trace_ids;
566 char lpath[PATH_MAX];
567 char * const paths[2] = { lpath, NULL };
568 int ret = -1;
569
570 /*
571 * Need to copy path, because fts_open can change it.
572 * It is the pointer array, not the strings, that are constant.
573 */
574 strncpy(lpath, path, PATH_MAX);
575 lpath[PATH_MAX - 1] = '\0';
576
577 tree = fts_open(paths, FTS_NOCHDIR | FTS_LOGICAL, 0);
578 if (tree == NULL) {
579 fprintf(stderr, "[error] [Context] Cannot traverse \"%s\" for reading.\n",
580 path);
581 return -EINVAL;
582 }
583
584 trace_ids = g_array_new(FALSE, TRUE, sizeof(int));
585
586 while ((node = fts_read(tree))) {
587 int dirfd, metafd;
588
589 if (!(node->fts_info & FTS_D))
590 continue;
591
592 dirfd = open(node->fts_accpath, 0);
593 if (dirfd < 0) {
594 fprintf(stderr, "[error] [Context] Unable to open trace "
595 "directory file descriptor.\n");
596 ret = dirfd;
597 goto error;
598 }
599 metafd = openat(dirfd, "metadata", O_RDONLY);
600 if (metafd < 0) {
601 close(dirfd);
602 ret = -1;
603 continue;
604 } else {
605 int trace_id;
606
607 ret = close(metafd);
608 if (ret < 0) {
609 perror("close");
610 goto error;
611 }
612 ret = close(dirfd);
613 if (ret < 0) {
614 perror("close");
615 goto error;
616 }
617
618 trace_id = bt_context_add_trace(ctx,
619 node->fts_accpath, format_str,
620 packet_seek, NULL, NULL);
621 if (trace_id < 0) {
622 fprintf(stderr, "[warning] [Context] opening trace \"%s\" from %s "
623 "for reading.\n", node->fts_accpath, path);
624 /* Allow to skip erroneous traces. */
625 continue;
626 }
627 g_array_append_val(trace_ids, trace_id);
628 }
629 }
630
631 g_array_free(trace_ids, TRUE);
632 return ret;
633
634 error:
635 return ret;
636 }
637
638 static int check_field_requirements(const struct bt_ctf_field_decl *const * field_list,
639 int field_cnt, int *tid_check, int *pid_check,
640 int *procname_check, int *ppid_check)
641 {
642 int j;
643 struct perfcounter *global;
644 const char *name;
645
646 for (j = 0; j < field_cnt; j++) {
647 name = bt_ctf_get_decl_field_name(field_list[j]);
648 if (*tid_check == 0) {
649 if (strncmp(name, "tid", 3) == 0)
650 (*tid_check)++;
651 }
652 if (*pid_check == 0) {
653 if (strncmp(name, "pid", 3) == 0)
654 (*pid_check)++;
655 }
656 if (*ppid_check == 0) {
657 if (strncmp(name, "ppid", 4) == 0)
658 (*ppid_check)++;
659 }
660 if (*procname_check == 0) {
661 if (strncmp(name, "procname", 8) == 0)
662 (*procname_check)++;
663 }
664 if (strncmp(name, "perf_", 5) == 0) {
665 global = g_hash_table_lookup(global_perf_liszt, (gpointer) name);
666 if (!global) {
667 global = g_new0(struct perfcounter, 1);
668 /* by default, sort on the first perf context */
669 if (g_hash_table_size(global_perf_liszt) == 0)
670 global->sort = 1;
671 global->visible = 1;
672 g_hash_table_insert(global_perf_liszt, (gpointer) strdup(name), global);
673 }
674 }
675 }
676
677 if (*tid_check == 1 && *pid_check == 1 && *ppid_check == 1 &&
678 *procname_check == 1)
679 return 0;
680
681 return -1;
682 }
683
684 /*
685 * check_requirements: check if the required context informations are available
686 *
687 * If each mandatory context information is available for at least in one
688 * event, return 0 otherwise return -1.
689 */
690 int check_requirements(struct bt_context *ctx)
691 {
692 unsigned int i, evt_cnt, field_cnt;
693 struct bt_ctf_event_decl *const * evt_list;
694 const struct bt_ctf_field_decl *const * field_list;
695 int tid_check = 0;
696 int pid_check = 0;
697 int procname_check = 0;
698 int ppid_check = 0;
699 int ret = 0;
700
701 bt_ctf_get_event_decl_list(0, ctx, &evt_list, &evt_cnt);
702 for (i = 0; i < evt_cnt; i++) {
703 bt_ctf_get_decl_fields(evt_list[i], BT_STREAM_EVENT_CONTEXT,
704 &field_list, &field_cnt);
705 ret = check_field_requirements(field_list, field_cnt,
706 &tid_check, &pid_check, &procname_check,
707 &ppid_check);
708
709 bt_ctf_get_decl_fields(evt_list[i], BT_EVENT_CONTEXT,
710 &field_list, &field_cnt);
711 ret = check_field_requirements(field_list, field_cnt,
712 &tid_check, &pid_check, &procname_check,
713 &ppid_check);
714
715 bt_ctf_get_decl_fields(evt_list[i], BT_STREAM_PACKET_CONTEXT,
716 &field_list, &field_cnt);
717 ret = check_field_requirements(field_list, field_cnt,
718 &tid_check, &pid_check, &procname_check,
719 &ppid_check);
720 }
721
722 if (tid_check == 0) {
723 ret = -1;
724 fprintf(stderr, "[error] missing tid context information\n");
725 }
726 if (pid_check == 0) {
727 ret = -1;
728 fprintf(stderr, "[error] missing pid context information\n");
729 }
730 if (ppid_check == 0) {
731 ret = -1;
732 fprintf(stderr, "[error] missing ppid context information\n");
733 }
734 if (procname_check == 0) {
735 ret = -1;
736 fprintf(stderr, "[error] missing procname context information\n");
737 }
738
739 return ret;
740 }
741
742 ssize_t read_subbuffer(struct lttng_consumer_stream *kconsumerd_fd,
743 struct lttng_consumer_local_data *ctx)
744 {
745 unsigned long len;
746 int err;
747 long ret = 0;
748 int infd = helper_get_lttng_consumer_stream_wait_fd(kconsumerd_fd);
749
750 if (helper_get_lttng_consumer_stream_output(kconsumerd_fd) == LTTNG_EVENT_SPLICE) {
751 /* Get the next subbuffer */
752 err = helper_kernctl_get_next_subbuf(infd);
753 if (err != 0) {
754 ret = errno;
755 perror("Reserving sub buffer failed (everything is normal, "
756 "it is due to concurrency)");
757 goto end;
758 }
759 /* read the whole subbuffer */
760 err = helper_kernctl_get_padded_subbuf_size(infd, &len);
761 if (err != 0) {
762 ret = errno;
763 perror("Getting sub-buffer len failed.");
764 goto end;
765 }
766
767 /* splice the subbuffer to the tracefile */
768 ret = helper_lttng_consumer_on_read_subbuffer_splice(ctx, kconsumerd_fd, len);
769 if (ret < 0) {
770 /*
771 * display the error but continue processing to try
772 * to release the subbuffer
773 */
774 fprintf(stderr,"Error splicing to tracefile\n");
775 }
776 err = helper_kernctl_put_next_subbuf(infd);
777 if (err != 0) {
778 ret = errno;
779 perror("Reserving sub buffer failed (everything is normal, "
780 "it is due to concurrency)");
781 goto end;
782 }
783 sem_post(&metadata_available);
784 }
785
786 end:
787 return 0;
788 }
789
790 int on_update_fd(int key, uint32_t state)
791 {
792 /* let the lib handle the metadata FD */
793 if (key == sessiond_metadata)
794 return 0;
795 return 1;
796 }
797
798 int on_recv_fd(struct lttng_consumer_stream *kconsumerd_fd)
799 {
800 int ret;
801 struct mmap_stream *new_mmap_stream;
802
803 /* Opening the tracefile in write mode */
804 if (helper_get_lttng_consumer_stream_path_name(kconsumerd_fd) != NULL) {
805 ret = open(helper_get_lttng_consumer_stream_path_name(kconsumerd_fd),
806 O_WRONLY|O_CREAT|O_TRUNC, S_IRWXU|S_IRWXG|S_IRWXO);
807 if (ret < 0) {
808 perror("open");
809 goto end;
810 }
811 helper_set_lttng_consumer_stream_out_fd(kconsumerd_fd, ret);
812 }
813
814 if (helper_get_lttng_consumer_stream_output(kconsumerd_fd) == LTTNG_EVENT_MMAP) {
815 new_mmap_stream = malloc(sizeof(struct mmap_stream));
816 new_mmap_stream->fd = helper_get_lttng_consumer_stream_wait_fd(
817 kconsumerd_fd);
818 bt_list_add(&new_mmap_stream->list, &mmap_list.head);
819
820 g_ptr_array_add(lttng_consumer_stream_array, kconsumerd_fd);
821 /* keep mmap FDs internally */
822 ret = 1;
823 } else {
824 consumerd_metadata = helper_get_lttng_consumer_stream_wait_fd(kconsumerd_fd);
825 sessiond_metadata = helper_get_lttng_consumer_stream_key(kconsumerd_fd);
826 ret = 0;
827 }
828
829 reload_trace = 1;
830
831 end:
832 return ret;
833 }
834
835 void live_consume(struct bt_context **bt_ctx)
836 {
837 int ret;
838 FILE *metadata_fp;
839
840 sem_wait(&metadata_available);
841 if (access("/tmp/livesession/kernel/metadata", F_OK) != 0) {
842 fprintf(stderr,"no metadata\n");
843 goto end;
844 }
845 metadata_fp = fopen("/tmp/livesession/kernel/metadata", "r");
846
847 *bt_ctx = bt_context_create();
848 ret = bt_context_add_trace(*bt_ctx, NULL, "ctf",
849 lttngtop_ctf_packet_seek, &mmap_list, metadata_fp);
850 if (ret < 0) {
851 printf("Error adding trace\n");
852 goto end;
853 }
854
855 end:
856 return;
857 }
858
859 int setup_consumer(char *command_sock_path, pthread_t *threads,
860 struct lttng_consumer_local_data *ctx)
861 {
862 int ret = 0;
863
864 ctx = helper_lttng_consumer_create(HELPER_LTTNG_CONSUMER_KERNEL,
865 read_subbuffer, NULL, on_recv_fd, on_update_fd);
866 if (!ctx)
867 goto end;
868
869 unlink(command_sock_path);
870 helper_lttng_consumer_set_command_sock_path(ctx, command_sock_path);
871 helper_lttng_consumer_init();
872
873 /* Create the thread to manage the receive of fd */
874 ret = pthread_create(&threads[0], NULL, helper_lttng_consumer_thread_receive_fds,
875 (void *) ctx);
876 if (ret != 0) {
877 perror("pthread_create receive fd");
878 goto end;
879 }
880 /* Create thread to manage the polling/writing of traces */
881 ret = pthread_create(&threads[1], NULL, helper_lttng_consumer_thread_poll_fds,
882 (void *) ctx);
883 if (ret != 0) {
884 perror("pthread_create poll fd");
885 goto end;
886 }
887
888 end:
889 return ret;
890 }
891
892 int setup_live_tracing()
893 {
894 struct lttng_domain dom;
895 struct lttng_channel chan;
896 char *channel_name = "mmapchan";
897 struct lttng_event ev;
898 int ret = 0;
899 char *command_sock_path = "/tmp/consumerd_sock";
900 static pthread_t threads[2]; /* recv_fd, poll */
901 struct lttng_event_context kctxpid, kctxcomm, kctxppid, kctxtid;
902
903 struct lttng_handle *handle;
904
905 BT_INIT_LIST_HEAD(&mmap_list.head);
906
907 lttng_consumer_stream_array = g_ptr_array_new();
908
909 if ((ret = setup_consumer(command_sock_path, threads, ctx)) < 0) {
910 fprintf(stderr,"error setting up consumer\n");
911 goto error;
912 }
913
914 available_snapshots = g_ptr_array_new();
915
916 /* setup the session */
917 dom.type = LTTNG_DOMAIN_KERNEL;
918
919 ret = unlink("/tmp/livesession");
920
921 lttng_destroy_session("test");
922 if ((ret = lttng_create_session("test", "/tmp/livesession")) < 0) {
923 fprintf(stderr,"error creating the session : %s\n",
924 helper_lttcomm_get_readable_code(ret));
925 goto error;
926 }
927
928 if ((handle = lttng_create_handle("test", &dom)) == NULL) {
929 fprintf(stderr,"error creating handle\n");
930 goto error_session;
931 }
932
933 /*
934 * FIXME : need to let the
935 * helper_lttng_consumer_thread_receive_fds create the
936 * socket.
937 * Cleaner solution ?
938 */
939 while (access(command_sock_path, F_OK)) {
940 sleep(0.1);
941 }
942
943 if ((ret = lttng_register_consumer(handle, command_sock_path)) < 0) {
944 fprintf(stderr,"error registering consumer : %s\n",
945 helper_lttcomm_get_readable_code(ret));
946 goto error_session;
947 }
948
949 strcpy(chan.name, channel_name);
950 chan.attr.overwrite = 0;
951 if (opt_pid) {
952 chan.attr.subbuf_size = 32768;
953 chan.attr.num_subbuf = 8;
954 } else {
955 chan.attr.subbuf_size = 1048576; /* 1MB */
956 chan.attr.num_subbuf = 4;
957 }
958 chan.attr.switch_timer_interval = 0;
959 chan.attr.read_timer_interval = 200;
960 chan.attr.output = LTTNG_EVENT_MMAP;
961
962 if ((ret = lttng_enable_channel(handle, &chan)) < 0) {
963 fprintf(stderr,"error creating channel : %s\n",
964 helper_lttcomm_get_readable_code(ret));
965 goto error_session;
966 }
967
968 memset(&ev, '\0', sizeof(struct lttng_event));
969 //sprintf(ev.name, "sched_switch");
970 if (!opt_pid) {
971 ev.type = LTTNG_EVENT_TRACEPOINT;
972 if ((ret = lttng_enable_event(handle, &ev, channel_name)) < 0) {
973 fprintf(stderr,"error enabling event : %s\n",
974 helper_lttcomm_get_readable_code(ret));
975 goto error_session;
976 }
977 }
978
979 ev.type = LTTNG_EVENT_SYSCALL;
980 if ((ret = lttng_enable_event(handle, &ev, channel_name)) < 0) {
981 fprintf(stderr,"error enabling syscalls : %s\n",
982 helper_lttcomm_get_readable_code(ret));
983 goto error_session;
984 }
985
986 kctxpid.ctx = LTTNG_EVENT_CONTEXT_PID;
987 lttng_add_context(handle, &kctxpid, NULL, NULL);
988 kctxtid.ctx = LTTNG_EVENT_CONTEXT_TID;
989 lttng_add_context(handle, &kctxtid, NULL, NULL);
990 if (!opt_pid) {
991 kctxppid.ctx = LTTNG_EVENT_CONTEXT_PPID;
992 lttng_add_context(handle, &kctxppid, NULL, NULL);
993 kctxcomm.ctx = LTTNG_EVENT_CONTEXT_PROCNAME;
994 lttng_add_context(handle, &kctxcomm, NULL, NULL);
995 kctxpid.ctx = LTTNG_EVENT_CONTEXT_VPID;
996 lttng_add_context(handle, &kctxpid, NULL, NULL);
997 kctxtid.ctx = LTTNG_EVENT_CONTEXT_VTID;
998 lttng_add_context(handle, &kctxtid, NULL, NULL);
999 }
1000
1001 if ((ret = lttng_start_tracing("test")) < 0) {
1002 fprintf(stderr,"error starting tracing : %s\n",
1003 helper_lttcomm_get_readable_code(ret));
1004 goto error_session;
1005 }
1006
1007 helper_kernctl_buffer_flush(consumerd_metadata);
1008
1009 /* block until metadata is ready */
1010 sem_init(&metadata_available, 0, 0);
1011
1012 return 0;
1013
1014 error_session:
1015 lttng_destroy_session("test");
1016 error:
1017 return -1;
1018 }
1019
1020 int main(int argc, char **argv)
1021 {
1022 int ret;
1023 struct bt_context *bt_ctx = NULL;
1024 struct mmap_stream *mmap_info;
1025 unsigned long mmap_len;
1026
1027 ret = parse_options(argc, argv);
1028 if (ret < 0) {
1029 fprintf(stdout, "Error parsing options.\n\n");
1030 usage(stdout);
1031 exit(EXIT_FAILURE);
1032 } else if (ret > 0) {
1033 exit(EXIT_SUCCESS);
1034 }
1035
1036 if (!opt_input_path) {
1037 ret = setup_live_tracing();
1038 if (ret < 0) {
1039 goto end;
1040 }
1041 init_lttngtop();
1042 if (!opt_textdump) {
1043 pthread_create(&display_thread, NULL, ncurses_display, (void *) NULL);
1044 pthread_create(&timer_thread, NULL, refresh_thread, (void *) NULL);
1045 }
1046 while (!quit) {
1047 reload_trace = 0;
1048 live_consume(&bt_ctx);
1049 iter_trace(bt_ctx);
1050 ret = bt_context_remove_trace(bt_ctx, 0);
1051 if (ret != 0)
1052 fprintf(stderr, "error removing trace\n");
1053 if (bt_ctx) {
1054 bt_context_put(bt_ctx);
1055 }
1056
1057 /*
1058 * since we receive all FDs every time there is an
1059 * update and the FD number is different every time,
1060 * we don't know which one are valid.
1061 * so we check if all FDs are usable with a simple
1062 * ioctl call.
1063 */
1064 bt_list_for_each_entry(mmap_info, &mmap_list.head, list) {
1065 ret = helper_kernctl_get_mmap_len(mmap_info->fd, &mmap_len);
1066 if (ret != 0) {
1067 bt_list_del(&mmap_info->list);
1068 }
1069 }
1070 sem_post(&metadata_available);
1071 }
1072
1073 pthread_join(timer_thread, NULL);
1074 quit = 1;
1075 pthread_join(display_thread, NULL);
1076
1077 lttng_stop_tracing("test");
1078 lttng_destroy_session("test");
1079
1080 goto end;
1081 } else {
1082 init_lttngtop();
1083
1084 bt_ctx = bt_context_create();
1085 ret = bt_context_add_traces_recursive(bt_ctx, opt_input_path, "ctf", NULL);
1086 if (ret < 0) {
1087 fprintf(stderr, "[error] Opening the trace\n");
1088 goto end;
1089 }
1090
1091 ret = check_requirements(bt_ctx);
1092 if (ret < 0) {
1093 fprintf(stderr, "[error] some mandatory contexts were missing, exiting.\n");
1094 goto end;
1095 }
1096 pthread_create(&display_thread, NULL, ncurses_display, (void *) NULL);
1097 pthread_create(&timer_thread, NULL, refresh_thread, (void *) NULL);
1098
1099 iter_trace(bt_ctx);
1100
1101 pthread_join(display_thread, NULL);
1102 quit = 1;
1103 pthread_join(timer_thread, NULL);
1104 }
1105
1106 end:
1107 if (bt_ctx)
1108 bt_context_put(bt_ctx);
1109
1110 return 0;
1111 }
This page took 0.057634 seconds and 4 git commands to generate.