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