cleanup, live textdump 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
55 struct lttngtop *copy;
56 pthread_t display_thread;
57 pthread_t timer_thread;
58 pthread_t live_trace_thread;
59
60 unsigned long refresh_display = 1 * NSEC_PER_SEC;
61 unsigned long last_display_update = 0;
62 int quit = 0;
63
64 /* LIVE */
65 pthread_t thread_live_consume;
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 FILE *metadata_fp;
75 int trace_opened = 0;
76 int metadata_ready = 0;
77
78 enum {
79 OPT_NONE = 0,
80 OPT_HELP,
81 OPT_LIST,
82 OPT_VERBOSE,
83 OPT_DEBUG,
84 OPT_NAMES,
85 };
86
87 static struct poptOption long_options[] = {
88 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
89 { "help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL },
90 { NULL, 0, 0, NULL, 0, NULL, NULL },
91 };
92
93 void *refresh_thread(void *p)
94 {
95 while (1) {
96 if (quit)
97 return NULL;
98 sem_wait(&pause_sem);
99 sem_post(&pause_sem);
100 sem_post(&timer);
101 sleep(refresh_display/NSEC_PER_SEC);
102 }
103 }
104
105 void *ncurses_display(void *p)
106 {
107 unsigned int current_display_index = 0;
108
109 sem_wait(&bootstrap);
110 /*
111 * Prevent the 1 second delay when we hit ESC
112 */
113 ESCDELAY = 0;
114 init_ncurses();
115
116 while (1) {
117 sem_wait(&timer);
118 sem_wait(&goodtodisplay);
119 sem_wait(&pause_sem);
120
121 copy = g_ptr_array_index(copies, current_display_index);
122 assert(copy);
123 display(current_display_index++);
124
125 sem_post(&goodtoupdate);
126 sem_post(&pause_sem);
127
128 if (quit) {
129 reset_ncurses();
130 pthread_exit(0);
131 }
132 }
133 }
134
135 /*
136 * hook on each event to check the timestamp and refresh the display if
137 * necessary
138 */
139 enum bt_cb_ret print_timestamp(struct bt_ctf_event *call_data, void *private_data)
140 {
141 unsigned long timestamp;
142 struct tm start;
143 uint64_t ts_nsec_start;
144
145
146 timestamp = bt_ctf_get_timestamp(call_data);
147
148 start = format_timestamp(timestamp);
149 ts_nsec_start = timestamp % NSEC_PER_SEC;
150
151 printf("%02d:%02d:%02d.%09" PRIu64 " %s\n", start.tm_hour,
152 start.tm_min, start.tm_sec, ts_nsec_start,
153 bt_ctf_event_name(call_data));
154
155 return BT_CB_OK;
156 }
157
158 /*
159 * hook on each event to check the timestamp and refresh the display if
160 * necessary
161 */
162 enum bt_cb_ret check_timestamp(struct bt_ctf_event *call_data, void *private_data)
163 {
164 unsigned long timestamp;
165
166 timestamp = bt_ctf_get_timestamp(call_data);
167 if (timestamp == -1ULL)
168 goto error;
169
170 if (last_display_update == 0)
171 last_display_update = timestamp;
172
173 if (timestamp - last_display_update >= refresh_display) {
174 sem_wait(&goodtoupdate);
175 g_ptr_array_add(copies, get_copy_lttngtop(last_display_update,
176 timestamp));
177 sem_post(&goodtodisplay);
178 sem_post(&bootstrap);
179 last_display_update = timestamp;
180 }
181 return BT_CB_OK;
182
183 error:
184 fprintf(stderr, "check_timestamp callback error\n");
185 return BT_CB_ERROR_STOP;
186 }
187
188 /*
189 * get_perf_counter : get or create and return a perf_counter struct for
190 * either a process or a cpu (only one of the 2 parameters mandatory)
191 */
192 struct perfcounter *get_perf_counter(const char *name, struct processtop *proc,
193 struct cputime *cpu)
194 {
195 struct perfcounter *ret;
196 GHashTable *table;
197
198 if (proc)
199 table = proc->perf;
200 else if (cpu)
201 table = cpu->perf;
202 else
203 goto error;
204
205 ret = g_hash_table_lookup(table, (gpointer) name);
206 if (ret)
207 goto end;
208
209 ret = g_new0(struct perfcounter, 1);
210 /* by default, make it visible in the UI */
211 ret->visible = 1;
212 g_hash_table_insert(table, (gpointer) strdup(name), ret);
213
214 end:
215 return ret;
216
217 error:
218 return NULL;
219 }
220
221 void update_perf_value(struct processtop *proc, struct cputime *cpu,
222 const char *name, int value)
223 {
224 struct perfcounter *cpu_perf, *process_perf;
225
226 cpu_perf = get_perf_counter(name, NULL, cpu);
227 if (cpu_perf->count < value) {
228 process_perf = get_perf_counter(name, proc, NULL);
229 process_perf->count += value - cpu_perf->count;
230 cpu_perf->count = value;
231 }
232 }
233
234 void extract_perf_counter_scope(const struct bt_ctf_event *event,
235 const struct bt_definition *scope,
236 struct processtop *proc,
237 struct cputime *cpu)
238 {
239 struct bt_definition const * const *list = NULL;
240 const struct bt_definition *field;
241 unsigned int count;
242 struct perfcounter *perfcounter;
243 GHashTableIter iter;
244 gpointer key;
245 int ret;
246
247 if (!scope)
248 goto end;
249
250 ret = bt_ctf_get_field_list(event, scope, &list, &count);
251 if (ret < 0)
252 goto end;
253
254 if (count == 0)
255 goto end;
256
257 g_hash_table_iter_init(&iter, global_perf_liszt);
258 while (g_hash_table_iter_next (&iter, &key, (gpointer) &perfcounter)) {
259 field = bt_ctf_get_field(event, scope, (char *) key);
260 if (field) {
261 int value = bt_ctf_get_uint64(field);
262 if (bt_ctf_field_get_error())
263 continue;
264 update_perf_value(proc, cpu, (char *) key, value);
265 }
266 }
267
268 end:
269 return;
270 }
271
272 void update_perf_counter(struct processtop *proc, const struct bt_ctf_event *event)
273 {
274 struct cputime *cpu;
275 const struct bt_definition *scope;
276
277 cpu = get_cpu(get_cpu_id(event));
278
279 scope = bt_ctf_get_top_level_scope(event, BT_STREAM_EVENT_CONTEXT);
280 extract_perf_counter_scope(event, scope, proc, cpu);
281
282 scope = bt_ctf_get_top_level_scope(event, BT_STREAM_PACKET_CONTEXT);
283 extract_perf_counter_scope(event, scope, proc, cpu);
284
285 scope = bt_ctf_get_top_level_scope(event, BT_EVENT_CONTEXT);
286 extract_perf_counter_scope(event, scope, proc, cpu);
287 }
288
289 enum bt_cb_ret fix_process_table(struct bt_ctf_event *call_data,
290 void *private_data)
291 {
292 int pid, tid, ppid;
293 char *comm;
294 struct processtop *parent, *child;
295 unsigned long timestamp;
296
297 timestamp = bt_ctf_get_timestamp(call_data);
298 if (timestamp == -1ULL)
299 goto error;
300
301 pid = get_context_pid(call_data);
302 if (pid == -1ULL) {
303 goto error;
304 }
305 tid = get_context_tid(call_data);
306 if (tid == -1ULL) {
307 goto error;
308 }
309 ppid = get_context_ppid(call_data);
310 if (ppid == -1ULL) {
311 goto error;
312 }
313 comm = get_context_comm(call_data);
314 if (!comm) {
315 goto error;
316 }
317
318 /* find or create the current process */
319 child = find_process_tid(&lttngtop, tid, comm);
320 if (!child)
321 child = add_proc(&lttngtop, tid, comm, timestamp);
322 update_proc(child, pid, tid, ppid, comm);
323
324 if (pid != tid) {
325 /* find or create the parent */
326 parent = find_process_tid(&lttngtop, pid, comm);
327 if (!parent) {
328 parent = add_proc(&lttngtop, pid, comm, timestamp);
329 parent->pid = pid;
330 }
331
332 /* attach the parent to the current process */
333 child->threadparent = parent;
334 add_thread(parent, child);
335 }
336
337 update_perf_counter(child, call_data);
338
339 return BT_CB_OK;
340
341 error:
342 return BT_CB_ERROR_STOP;
343 }
344
345 void init_lttngtop()
346 {
347 copies = g_ptr_array_new();
348 global_perf_liszt = g_hash_table_new(g_str_hash, g_str_equal);
349
350 sem_init(&goodtodisplay, 0, 0);
351 sem_init(&goodtoupdate, 0, 1);
352 sem_init(&timer, 0, 1);
353 sem_init(&bootstrap, 0, 0);
354 sem_init(&pause_sem, 0, 1);
355 sem_init(&end_trace_sem, 0, 0);
356
357 reset_global_counters();
358 lttngtop.nbproc = 0;
359 lttngtop.nbthreads = 0;
360 lttngtop.nbfiles = 0;
361
362 lttngtop.process_table = g_ptr_array_new();
363 lttngtop.files_table = g_ptr_array_new();
364 lttngtop.cpu_table = g_ptr_array_new();
365 }
366
367 void usage(FILE *fp)
368 {
369 fprintf(fp, "LTTngTop %s\n\n", VERSION);
370 fprintf(fp, "Usage : lttngtop /path/to/trace\n");
371 }
372
373 /*
374 * Return 0 if caller should continue, < 0 if caller should return
375 * error, > 0 if caller should exit without reporting error.
376 */
377 static int parse_options(int argc, char **argv)
378 {
379 poptContext pc;
380 int opt, ret = 0;
381
382 pc = poptGetContext(NULL, argc, (const char **) argv, long_options, 0);
383 poptReadDefaultConfig(pc, 0);
384
385 while ((opt = poptGetNextOpt(pc)) != -1) {
386 switch (opt) {
387 case OPT_HELP:
388 usage(stdout);
389 ret = 1; /* exit cleanly */
390 goto end;
391 default:
392 ret = -EINVAL;
393 goto end;
394 }
395 }
396
397 opt_input_path = poptGetArg(pc);
398
399 end:
400 if (pc) {
401 poptFreeContext(pc);
402 }
403 return ret;
404 }
405
406 void iter_trace(struct bt_context *bt_ctx)
407 {
408 struct bt_ctf_iter *iter;
409 struct bt_iter_pos begin_pos;
410 const struct bt_ctf_event *event;
411 int ret = 0;
412
413 begin_pos.type = BT_SEEK_BEGIN;
414 iter = bt_ctf_iter_create(bt_ctx, &begin_pos, NULL);
415
416 bt_ctf_iter_add_callback(iter, 0, NULL, 0,
417 print_timestamp,
418 NULL, NULL, NULL);
419
420 #if 0
421 /* at each event check if we need to refresh */
422 bt_ctf_iter_add_callback(iter, 0, NULL, 0,
423 check_timestamp,
424 NULL, NULL, NULL);
425 /* at each event, verify the status of the process table */
426 bt_ctf_iter_add_callback(iter, 0, NULL, 0,
427 fix_process_table,
428 NULL, NULL, NULL);
429 /* to handle the scheduling events */
430 bt_ctf_iter_add_callback(iter,
431 g_quark_from_static_string("sched_switch"),
432 NULL, 0, handle_sched_switch, NULL, NULL, NULL);
433 /* to clean up the process table */
434 bt_ctf_iter_add_callback(iter,
435 g_quark_from_static_string("sched_process_free"),
436 NULL, 0, handle_sched_process_free, NULL, NULL, NULL);
437 /* to get all the process from the statedumps */
438 bt_ctf_iter_add_callback(iter,
439 g_quark_from_static_string(
440 "lttng_statedump_process_state"),
441 NULL, 0, handle_statedump_process_state,
442 NULL, NULL, NULL);
443
444 /* for IO top */
445 bt_ctf_iter_add_callback(iter,
446 g_quark_from_static_string("exit_syscall"),
447 NULL, 0, handle_exit_syscall, NULL, NULL, NULL);
448 bt_ctf_iter_add_callback(iter,
449 g_quark_from_static_string("sys_write"),
450 NULL, 0, handle_sys_write, NULL, NULL, NULL);
451 bt_ctf_iter_add_callback(iter,
452 g_quark_from_static_string("sys_read"),
453 NULL, 0, handle_sys_read, NULL, NULL, NULL);
454 bt_ctf_iter_add_callback(iter,
455 g_quark_from_static_string("sys_open"),
456 NULL, 0, handle_sys_open, NULL, NULL, NULL);
457 bt_ctf_iter_add_callback(iter,
458 g_quark_from_static_string("sys_close"),
459 NULL, 0, handle_sys_close, NULL, NULL, NULL);
460 bt_ctf_iter_add_callback(iter,
461 g_quark_from_static_string(
462 "lttng_statedump_file_descriptor"),
463 NULL, 0, handle_statedump_file_descriptor,
464 NULL, NULL, NULL);
465 #endif
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", helper_lttcomm_get_readable_code(ret));
887 goto end;
888 }
889
890 sprintf(ev.name, "sched_switch");
891 ev.type = LTTNG_EVENT_TRACEPOINT;
892
893 //if ((ret = lttng_enable_event(handle, NULL, channel_name)) < 0) {
894 if ((ret = lttng_enable_event(handle, &ev, channel_name)) < 0) {
895 fprintf(stderr,"error enabling event : %s\n", helper_lttcomm_get_readable_code(ret));
896 goto end;
897 }
898
899 kctxpid.ctx = LTTNG_EVENT_CONTEXT_PID;
900 lttng_add_context(handle, &kctxpid, NULL, NULL);
901 kctxppid.ctx = LTTNG_EVENT_CONTEXT_PPID;
902 lttng_add_context(handle, &kctxppid, NULL, NULL);
903 kctxcomm.ctx = LTTNG_EVENT_CONTEXT_PROCNAME;
904 lttng_add_context(handle, &kctxcomm, NULL, NULL);
905 kctxtid.ctx = LTTNG_EVENT_CONTEXT_TID;
906 lttng_add_context(handle, &kctxtid, NULL, NULL);
907
908 if ((ret = lttng_start_tracing("test")) < 0) {
909 fprintf(stderr,"error starting tracing : %s\n", helper_lttcomm_get_readable_code(ret));
910 goto end;
911 }
912
913 helper_kernctl_buffer_flush(consumerd_metadata);
914
915 /* Create thread to manage the polling/writing of traces */
916 ret = pthread_create(&thread_live_consume, NULL, live_consume, NULL);
917 if (ret != 0) {
918 perror("pthread_create");
919 goto end;
920 }
921
922 /* block until metadata is ready */
923 sem_init(&metadata_available, 0, 0);
924
925 //init_lttngtop();
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 pthread_create(&live_trace_thread, NULL, setup_live_tracing, (void *) NULL);
947 sleep(2000);
948 printf("STOPPING\n");
949 lttng_stop_tracing("test");
950 printf("DESTROYING\n");
951 lttng_destroy_session("test");
952
953 printf("CANCELLING\n");
954 pthread_cancel(live_trace_thread);
955 goto end;
956 } else {
957 init_lttngtop();
958
959 bt_ctx = bt_context_create();
960 ret = bt_context_add_traces_recursive(bt_ctx, opt_input_path, "ctf", NULL);
961 if (ret < 0) {
962 fprintf(stderr, "[error] Opening the trace\n");
963 goto end;
964 }
965
966 ret = check_requirements(bt_ctx);
967 if (ret < 0) {
968 fprintf(stderr, "[error] some mandatory contexts were missing, exiting.\n");
969 goto end;
970 }
971 pthread_create(&display_thread, NULL, ncurses_display, (void *) NULL);
972 pthread_create(&timer_thread, NULL, refresh_thread, (void *) NULL);
973
974 iter_trace(bt_ctx);
975
976 quit = 1;
977 pthread_join(display_thread, NULL);
978 pthread_join(timer_thread, NULL);
979 }
980
981 end:
982 if (bt_ctx)
983 bt_context_put(bt_ctx);
984
985 return 0;
986 }
This page took 0.047211 seconds and 4 git commands to generate.