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