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