2 * Copyright (C) 2011-2012 Julien Desfossez
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;
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.
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.
22 #include <babeltrace/babeltrace.h>
23 #include <babeltrace/ctf/events.h>
24 #include <babeltrace/ctf/callbacks.h>
25 #include <babeltrace/ctf/iterator.h>
37 #include <sys/types.h>
41 #include <lttng/lttng.h>
42 #include <lttng/lttngtop-helper.h>
43 #include <babeltrace/lttngtopmmappacketseek.h>
45 #include "lttngtoptypes.h"
47 #include "iostreamtop.h"
48 #include "cursesdisplay.h"
51 #define DEFAULT_FILE_ARRAY_SIZE 1
53 const char *opt_input_path
;
55 struct lttngtop
*copy
;
56 pthread_t display_thread
;
57 pthread_t timer_thread
;
58 pthread_t live_trace_thread
;
60 unsigned long refresh_display
= 1 * NSEC_PER_SEC
;
61 unsigned long last_display_update
= 0;
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
;
76 int metadata_ready
= 0;
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
},
93 void *refresh_thread(void *p
)
101 sleep(refresh_display
/NSEC_PER_SEC
);
105 void *ncurses_display(void *p
)
107 unsigned int current_display_index
= 0;
109 sem_wait(&bootstrap
);
111 * Prevent the 1 second delay when we hit ESC
118 sem_wait(&goodtodisplay
);
119 sem_wait(&pause_sem
);
121 copy
= g_ptr_array_index(copies
, current_display_index
);
123 display(current_display_index
++);
125 sem_post(&goodtoupdate
);
126 sem_post(&pause_sem
);
136 struct tm
ts_format_timestamp(uint64_t timestamp
)
139 uint64_t ts_sec
= 0, ts_nsec
;
143 ts_sec
+= ts_nsec
/ NSEC_PER_SEC
;
144 ts_nsec
= ts_nsec
% NSEC_PER_SEC
;
146 time_s
= (time_t) ts_sec
;
148 localtime_r(&time_s
, &tm
);
154 * hook on each event to check the timestamp and refresh the display if
157 enum bt_cb_ret
print_timestamp(struct bt_ctf_event
*call_data
, void *private_data
)
159 unsigned long timestamp
;
161 uint64_t ts_nsec_start
, ts_nsec_end
;
164 timestamp
= bt_ctf_get_timestamp(call_data
);
166 start
= ts_format_timestamp(timestamp
);
167 ts_nsec_start
= timestamp
% NSEC_PER_SEC
;
169 // printf("%02d:%02d:%02d.%09" PRIu64 "\n", start.tm_hour, start.tm_min, start.tm_sec, ts_nsec_start);
175 * hook on each event to check the timestamp and refresh the display if
178 enum bt_cb_ret
check_timestamp(struct bt_ctf_event
*call_data
, void *private_data
)
180 unsigned long timestamp
;
182 timestamp
= bt_ctf_get_timestamp(call_data
);
183 if (timestamp
== -1ULL)
186 if (last_display_update
== 0)
187 last_display_update
= timestamp
;
189 if (timestamp
- last_display_update
>= refresh_display
) {
190 sem_wait(&goodtoupdate
);
191 g_ptr_array_add(copies
, get_copy_lttngtop(last_display_update
,
193 sem_post(&goodtodisplay
);
194 sem_post(&bootstrap
);
195 last_display_update
= timestamp
;
200 fprintf(stderr
, "check_timestamp callback error\n");
201 return BT_CB_ERROR_STOP
;
205 * get_perf_counter : get or create and return a perf_counter struct for
206 * either a process or a cpu (only one of the 2 parameters mandatory)
208 struct perfcounter
*get_perf_counter(const char *name
, struct processtop
*proc
,
211 struct perfcounter
*ret
;
221 ret
= g_hash_table_lookup(table
, (gpointer
) name
);
225 ret
= g_new0(struct perfcounter
, 1);
226 /* by default, make it visible in the UI */
228 g_hash_table_insert(table
, (gpointer
) strdup(name
), ret
);
237 void update_perf_value(struct processtop
*proc
, struct cputime
*cpu
,
238 const char *name
, int value
)
240 struct perfcounter
*cpu_perf
, *process_perf
;
242 cpu_perf
= get_perf_counter(name
, NULL
, cpu
);
243 if (cpu_perf
->count
< value
) {
244 process_perf
= get_perf_counter(name
, proc
, NULL
);
245 process_perf
->count
+= value
- cpu_perf
->count
;
246 cpu_perf
->count
= value
;
250 void extract_perf_counter_scope(const struct bt_ctf_event
*event
,
251 const struct definition
*scope
,
252 struct processtop
*proc
,
255 struct definition
const * const *list
= NULL
;
256 const struct definition
*field
;
258 struct perfcounter
*perfcounter
;
266 ret
= bt_ctf_get_field_list(event
, scope
, &list
, &count
);
273 g_hash_table_iter_init(&iter
, global_perf_liszt
);
274 while (g_hash_table_iter_next (&iter
, &key
, (gpointer
) &perfcounter
)) {
275 field
= bt_ctf_get_field(event
, scope
, (char *) key
);
277 int value
= bt_ctf_get_uint64(field
);
278 if (bt_ctf_field_get_error())
280 update_perf_value(proc
, cpu
, (char *) key
, value
);
288 void update_perf_counter(struct processtop
*proc
, const struct bt_ctf_event
*event
)
291 const struct definition
*scope
;
293 cpu
= get_cpu(get_cpu_id(event
));
295 scope
= bt_ctf_get_top_level_scope(event
, BT_STREAM_EVENT_CONTEXT
);
296 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
298 scope
= bt_ctf_get_top_level_scope(event
, BT_STREAM_PACKET_CONTEXT
);
299 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
301 scope
= bt_ctf_get_top_level_scope(event
, BT_EVENT_CONTEXT
);
302 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
305 enum bt_cb_ret
fix_process_table(struct bt_ctf_event
*call_data
,
310 struct processtop
*parent
, *child
;
311 unsigned long timestamp
;
313 timestamp
= bt_ctf_get_timestamp(call_data
);
314 if (timestamp
== -1ULL)
317 pid
= get_context_pid(call_data
);
321 tid
= get_context_tid(call_data
);
325 ppid
= get_context_ppid(call_data
);
329 comm
= get_context_comm(call_data
);
334 /* find or create the current process */
335 child
= find_process_tid(<tngtop
, tid
, comm
);
337 child
= add_proc(<tngtop
, tid
, comm
, timestamp
);
338 update_proc(child
, pid
, tid
, ppid
, comm
);
341 /* find or create the parent */
342 parent
= find_process_tid(<tngtop
, pid
, comm
);
344 parent
= add_proc(<tngtop
, pid
, comm
, timestamp
);
348 /* attach the parent to the current process */
349 child
->threadparent
= parent
;
350 add_thread(parent
, child
);
353 update_perf_counter(child
, call_data
);
358 return BT_CB_ERROR_STOP
;
363 copies
= g_ptr_array_new();
364 global_perf_liszt
= g_hash_table_new(g_str_hash
, g_str_equal
);
366 sem_init(&goodtodisplay
, 0, 0);
367 sem_init(&goodtoupdate
, 0, 1);
368 sem_init(&timer
, 0, 1);
369 sem_init(&bootstrap
, 0, 0);
370 sem_init(&pause_sem
, 0, 1);
371 sem_init(&end_trace_sem
, 0, 0);
373 reset_global_counters();
375 lttngtop
.nbthreads
= 0;
376 lttngtop
.nbfiles
= 0;
378 lttngtop
.process_table
= g_ptr_array_new();
379 lttngtop
.files_table
= g_ptr_array_new();
380 lttngtop
.cpu_table
= g_ptr_array_new();
385 fprintf(fp
, "LTTngTop %s\n\n", VERSION
);
386 fprintf(fp
, "Usage : lttngtop /path/to/trace\n");
390 * Return 0 if caller should continue, < 0 if caller should return
391 * error, > 0 if caller should exit without reporting error.
393 static int parse_options(int argc
, char **argv
)
398 pc
= poptGetContext(NULL
, argc
, (const char **) argv
, long_options
, 0);
399 poptReadDefaultConfig(pc
, 0);
401 while ((opt
= poptGetNextOpt(pc
)) != -1) {
405 ret
= 1; /* exit cleanly */
413 opt_input_path
= poptGetArg(pc
);
422 void iter_trace(struct bt_context
*bt_ctx
)
424 struct bt_ctf_iter
*iter
;
425 struct bt_iter_pos begin_pos
;
426 const struct bt_ctf_event
*event
;
429 begin_pos
.type
= BT_SEEK_BEGIN
;
430 iter
= bt_ctf_iter_create(bt_ctx
, &begin_pos
, NULL
);
432 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
437 /* at each event check if we need to refresh */
438 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
441 /* at each event, verify the status of the process table */
442 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
445 /* to handle the scheduling events */
446 bt_ctf_iter_add_callback(iter
,
447 g_quark_from_static_string("sched_switch"),
448 NULL
, 0, handle_sched_switch
, NULL
, NULL
, NULL
);
449 /* to clean up the process table */
450 bt_ctf_iter_add_callback(iter
,
451 g_quark_from_static_string("sched_process_free"),
452 NULL
, 0, handle_sched_process_free
, NULL
, NULL
, NULL
);
453 /* to get all the process from the statedumps */
454 bt_ctf_iter_add_callback(iter
,
455 g_quark_from_static_string(
456 "lttng_statedump_process_state"),
457 NULL
, 0, handle_statedump_process_state
,
461 bt_ctf_iter_add_callback(iter
,
462 g_quark_from_static_string("exit_syscall"),
463 NULL
, 0, handle_exit_syscall
, NULL
, NULL
, NULL
);
464 bt_ctf_iter_add_callback(iter
,
465 g_quark_from_static_string("sys_write"),
466 NULL
, 0, handle_sys_write
, NULL
, NULL
, NULL
);
467 bt_ctf_iter_add_callback(iter
,
468 g_quark_from_static_string("sys_read"),
469 NULL
, 0, handle_sys_read
, NULL
, NULL
, NULL
);
470 bt_ctf_iter_add_callback(iter
,
471 g_quark_from_static_string("sys_open"),
472 NULL
, 0, handle_sys_open
, NULL
, NULL
, NULL
);
473 bt_ctf_iter_add_callback(iter
,
474 g_quark_from_static_string("sys_close"),
475 NULL
, 0, handle_sys_close
, NULL
, NULL
, NULL
);
476 bt_ctf_iter_add_callback(iter
,
477 g_quark_from_static_string(
478 "lttng_statedump_file_descriptor"),
479 NULL
, 0, handle_statedump_file_descriptor
,
482 while ((event
= bt_ctf_iter_read_event(iter
)) != NULL
) {
483 ret
= bt_iter_next(bt_ctf_get_iter(iter
));
488 /* block until quit, we reached the end of the trace */
489 sem_wait(&end_trace_sem
);
492 bt_ctf_iter_destroy(iter
);
496 * bt_context_add_traces_recursive: Open a trace recursively
497 * (copied from BSD code in converter/babeltrace.c)
499 * Find each trace present in the subdirectory starting from the given
500 * path, and add them to the context. The packet_seek parameter can be
501 * NULL: this specify to use the default format packet_seek.
503 * Return: 0 on success, nonzero on failure.
504 * Unable to open toplevel: failure.
505 * Unable to open some subdirectory or file: warn and continue;
507 int bt_context_add_traces_recursive(struct bt_context
*ctx
, const char *path
,
508 const char *format_str
,
509 void (*packet_seek
)(struct stream_pos
*pos
,
510 size_t offset
, int whence
))
515 char lpath
[PATH_MAX
];
516 char * const paths
[2] = { lpath
, NULL
};
520 * Need to copy path, because fts_open can change it.
521 * It is the pointer array, not the strings, that are constant.
523 strncpy(lpath
, path
, PATH_MAX
);
524 lpath
[PATH_MAX
- 1] = '\0';
526 tree
= fts_open(paths
, FTS_NOCHDIR
| FTS_LOGICAL
, 0);
528 fprintf(stderr
, "[error] [Context] Cannot traverse \"%s\" for reading.\n",
533 trace_ids
= g_array_new(FALSE
, TRUE
, sizeof(int));
535 while ((node
= fts_read(tree
))) {
538 if (!(node
->fts_info
& FTS_D
))
541 dirfd
= open(node
->fts_accpath
, 0);
543 fprintf(stderr
, "[error] [Context] Unable to open trace "
544 "directory file descriptor.\n");
548 metafd
= openat(dirfd
, "metadata", O_RDONLY
);
567 trace_id
= bt_context_add_trace(ctx
,
568 node
->fts_accpath
, format_str
,
569 packet_seek
, NULL
, NULL
);
571 fprintf(stderr
, "[warning] [Context] opening trace \"%s\" from %s "
572 "for reading.\n", node
->fts_accpath
, path
);
573 /* Allow to skip erroneous traces. */
576 g_array_append_val(trace_ids
, trace_id
);
580 g_array_free(trace_ids
, TRUE
);
587 static int check_field_requirements(const struct bt_ctf_field_decl
*const * field_list
,
588 int field_cnt
, int *tid_check
, int *pid_check
,
589 int *procname_check
, int *ppid_check
)
592 struct perfcounter
*global
;
595 for (j
= 0; j
< field_cnt
; j
++) {
596 name
= bt_ctf_get_decl_field_name(field_list
[j
]);
597 if (*tid_check
== 0) {
598 if (strncmp(name
, "tid", 3) == 0)
601 if (*pid_check
== 0) {
602 if (strncmp(name
, "tid", 3) == 0)
605 if (*ppid_check
== 0) {
606 if (strncmp(name
, "ppid", 4) == 0)
609 if (*procname_check
== 0) {
610 if (strncmp(name
, "procname", 8) == 0)
613 if (strncmp(name
, "perf_", 5) == 0) {
614 global
= g_hash_table_lookup(global_perf_liszt
, (gpointer
) name
);
616 global
= g_new0(struct perfcounter
, 1);
617 /* by default, sort on the first perf context */
618 if (g_hash_table_size(global_perf_liszt
) == 0)
621 g_hash_table_insert(global_perf_liszt
, (gpointer
) strdup(name
), global
);
626 if (*tid_check
== 1 && *pid_check
== 1 && *ppid_check
== 1 &&
627 *procname_check
== 1)
634 * check_requirements: check if the required context informations are available
636 * If each mandatory context information is available for at least in one
637 * event, return 0 otherwise return -1.
639 int check_requirements(struct bt_context
*ctx
)
641 unsigned int i
, evt_cnt
, field_cnt
;
642 struct bt_ctf_event_decl
*const * evt_list
;
643 const struct bt_ctf_field_decl
*const * field_list
;
646 int procname_check
= 0;
650 bt_ctf_get_event_decl_list(0, ctx
, &evt_list
, &evt_cnt
);
651 for (i
= 0; i
< evt_cnt
; i
++) {
652 bt_ctf_get_decl_fields(evt_list
[i
], BT_STREAM_EVENT_CONTEXT
,
653 &field_list
, &field_cnt
);
654 ret
= check_field_requirements(field_list
, field_cnt
,
655 &tid_check
, &pid_check
, &procname_check
,
658 bt_ctf_get_decl_fields(evt_list
[i
], BT_EVENT_CONTEXT
,
659 &field_list
, &field_cnt
);
660 ret
= check_field_requirements(field_list
, field_cnt
,
661 &tid_check
, &pid_check
, &procname_check
,
664 bt_ctf_get_decl_fields(evt_list
[i
], BT_STREAM_PACKET_CONTEXT
,
665 &field_list
, &field_cnt
);
666 ret
= check_field_requirements(field_list
, field_cnt
,
667 &tid_check
, &pid_check
, &procname_check
,
671 if (tid_check
== 0) {
673 fprintf(stderr
, "[error] missing tid context information\n");
675 if (pid_check
== 0) {
677 fprintf(stderr
, "[error] missing pid context information\n");
679 if (ppid_check
== 0) {
681 fprintf(stderr
, "[error] missing ppid context information\n");
683 if (procname_check
== 0) {
685 fprintf(stderr
, "[error] missing procname context information\n");
694 struct lttng_consumer_stream
*iter
;
696 struct mmap_stream
*new_snapshot
;
701 * try lock mutex ressource courante (overrun)
703 * stop trace (flush implicite avant stop)
704 * lttng_consumer_take_snapshot
705 * read timestamp packet end (use time as end pos)
706 * - stream_packet_context
707 * - reculer de 1 subbuf : pos - max_subbuff_size
709 * - position de fin (take_snapshot)
710 * - mov_pos_slow ( fin - max_subbuff_size) lire timestamp packet end
711 * - prend min(end) (activité sur tous les streams)
717 helper_kernctl_buffer_flush(consumerd_metadata
);
718 for (i
= 0; i
< lttng_consumer_stream_array
->len
; i
++) {
719 iter
= g_ptr_array_index(lttng_consumer_stream_array
, i
);
720 helper_kernctl_buffer_flush(helper_get_lttng_consumer_stream_wait_fd(iter
));
721 printf("Taking snapshot of fd : %d\n", helper_get_lttng_consumer_stream_wait_fd(iter
));
722 ret
= helper_lttng_consumer_take_snapshot(ctx
, iter
);
725 perror("lttng_consumer_take_snapshots");
729 for (i
= 0; i
< lttng_consumer_stream_array
->len
; i
++) {
730 iter
= g_ptr_array_index(lttng_consumer_stream_array
, i
);
731 ret
= helper_lttng_consumer_get_produced_snapshot(ctx
, iter
, &spos
);
734 perror("helper_lttng_consumer_get_produced_snapshot");
737 while (helper_get_lttng_consumer_stream_wait_last_pos(iter
) < spos
) {
738 new_snapshot
= g_new0(struct mmap_stream
, 1);
739 new_snapshot
->fd
= helper_get_lttng_consumer_stream_wait_fd(iter
);
740 new_snapshot
->last_pos
= helper_get_lttng_consumer_stream_wait_last_pos(iter
);
741 fprintf(stderr
,"ADDING AVAILABLE SNAPSHOT ON FD %d AT POSITION %lu\n",
743 new_snapshot
->last_pos
);
744 g_ptr_array_add(available_snapshots
, new_snapshot
);
745 helper_set_lttng_consumer_stream_wait_last_pos(iter
,
746 helper_get_lttng_consumer_stream_wait_last_pos(iter
) +
747 helper_get_lttng_consumer_stream_chan_max_sb_size(iter
));
751 if (!metadata_ready
) {
752 fprintf(stderr
, "BLOCKING BEFORE METADATA\n");
753 sem_wait(&metadata_available
);
754 fprintf(stderr
,"OPENING TRACE\n");
755 if (access("/tmp/livesession/kernel/metadata", F_OK
) != 0) {
756 fprintf(stderr
,"NO METADATA FILE, SKIPPING\n");
760 metadata_fp
= fopen("/tmp/livesession/kernel/metadata", "r");
769 ssize_t
read_subbuffer(struct lttng_consumer_stream
*kconsumerd_fd
,
770 struct lttng_consumer_local_data
*ctx
)
775 int infd
= helper_get_lttng_consumer_stream_wait_fd(kconsumerd_fd
);
777 if (helper_get_lttng_consumer_stream_output(kconsumerd_fd
) == LTTNG_EVENT_SPLICE
) {
778 /* Get the next subbuffer */
779 printf("get_next : %d\n", infd
);
780 err
= helper_kernctl_get_next_subbuf(infd
);
783 perror("Reserving sub buffer failed (everything is normal, "
784 "it is due to concurrency)");
787 /* read the whole subbuffer */
788 err
= helper_kernctl_get_padded_subbuf_size(infd
, &len
);
791 perror("Getting sub-buffer len failed.");
794 printf("len : %ld\n", len
);
796 /* splice the subbuffer to the tracefile */
797 ret
= helper_lttng_consumer_on_read_subbuffer_splice(ctx
, kconsumerd_fd
, len
);
800 * display the error but continue processing to try
801 * to release the subbuffer
803 fprintf(stderr
,"Error splicing to tracefile\n");
805 printf("ret : %ld\n", ret
);
806 printf("put_next : %d\n", infd
);
807 err
= helper_kernctl_put_next_subbuf(infd
);
810 perror("Reserving sub buffer failed (everything is normal, "
811 "it is due to concurrency)");
814 sem_post(&metadata_available
);
821 int on_update_fd(int key
, uint32_t state
)
823 /* let the lib handle the metadata FD */
824 if (key
== sessiond_metadata
)
829 int on_recv_fd(struct lttng_consumer_stream
*kconsumerd_fd
)
832 struct mmap_stream
*new_info
;
835 /* Opening the tracefile in write mode */
836 if (helper_get_lttng_consumer_stream_path_name(kconsumerd_fd
) != NULL
) {
837 ret
= open(helper_get_lttng_consumer_stream_path_name(kconsumerd_fd
),
838 O_WRONLY
|O_CREAT
|O_TRUNC
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
843 helper_set_lttng_consumer_stream_out_fd(kconsumerd_fd
, ret
);
846 if (helper_get_lttng_consumer_stream_output(kconsumerd_fd
) == LTTNG_EVENT_MMAP
) {
847 new_info
= malloc(sizeof(struct mmap_stream
));
848 new_info
->fd
= helper_get_lttng_consumer_stream_wait_fd(kconsumerd_fd
);
849 bt_list_add(&new_info
->list
, &mmap_list
.head
);
851 /* get the len of the mmap region */
852 ret
= helper_kernctl_get_mmap_len(helper_get_lttng_consumer_stream_wait_fd(kconsumerd_fd
),
856 perror("helper_kernctl_get_mmap_len");
859 helper_set_lttng_consumer_stream_mmap_len(kconsumerd_fd
, tmp_mmap_len
);
861 helper_set_lttng_consumer_stream_mmap_base(kconsumerd_fd
,
862 mmap(NULL
, helper_get_lttng_consumer_stream_mmap_len(kconsumerd_fd
),
863 PROT_READ
, MAP_PRIVATE
, helper_get_lttng_consumer_stream_wait_fd(kconsumerd_fd
), 0));
864 if (helper_get_lttng_consumer_stream_mmap_base(kconsumerd_fd
) == MAP_FAILED
) {
865 perror("Error mmaping");
870 g_ptr_array_add(lttng_consumer_stream_array
, kconsumerd_fd
);
871 /* keep mmap FDs internally */
874 consumerd_metadata
= helper_get_lttng_consumer_stream_wait_fd(kconsumerd_fd
);
875 sessiond_metadata
= helper_get_lttng_consumer_stream_key(kconsumerd_fd
);
885 struct bt_context
*bt_ctx
= NULL
;
891 if (!metadata_ready
) {
892 fprintf(stderr
, "BLOCKING BEFORE METADATA\n");
893 sem_wait(&metadata_available
);
894 fprintf(stderr
,"OPENING TRACE\n");
895 if (access("/tmp/livesession/kernel/metadata", F_OK
) != 0) {
896 fprintf(stderr
,"NO METADATA FILE, SKIPPING\n");
900 metadata_fp
= fopen("/tmp/livesession/kernel/metadata", "r");
904 bt_ctx
= bt_context_create();
905 ret
= bt_context_add_trace(bt_ctx
, NULL
, "ctf",
906 lttngtop_ctf_packet_seek
, &mmap_list
, metadata_fp
);
908 printf("Error adding trace\n");
918 int setup_consumer(char *command_sock_path
, pthread_t
*threads
,
919 struct lttng_consumer_local_data
*ctx
)
923 ctx
= helper_lttng_consumer_create(HELPER_LTTNG_CONSUMER_KERNEL
,
924 read_subbuffer
, NULL
, on_recv_fd
, on_update_fd
);
928 unlink(command_sock_path
);
929 helper_lttng_consumer_set_command_sock_path(ctx
, command_sock_path
);
930 helper_lttng_consumer_init();
932 /* Create the thread to manage the receive of fd */
933 ret
= pthread_create(&threads
[0], NULL
, helper_lttng_consumer_thread_receive_fds
,
936 perror("pthread_create receive fd");
939 /* Create thread to manage the polling/writing of traces */
940 ret
= pthread_create(&threads
[1], NULL
, helper_lttng_consumer_thread_poll_fds
,
943 perror("pthread_create poll fd");
951 void *setup_live_tracing()
953 struct lttng_domain dom
;
954 struct lttng_channel chan
;
955 char *channel_name
= "mmapchan";
956 struct lttng_event ev
;
958 char *command_sock_path
= "/tmp/consumerd_sock";
959 static pthread_t threads
[2]; /* recv_fd, poll */
960 struct lttng_event_context kctxpid
, kctxcomm
, kctxppid
, kctxtid
;
962 struct lttng_handle
*handle
;
964 BT_INIT_LIST_HEAD(&mmap_list
.head
);
966 lttng_consumer_stream_array
= g_ptr_array_new();
968 if ((ret
= setup_consumer(command_sock_path
, threads
, ctx
)) < 0) {
969 fprintf(stderr
,"error setting up consumer\n");
973 available_snapshots
= g_ptr_array_new();
975 /* setup the session */
976 dom
.type
= LTTNG_DOMAIN_KERNEL
;
978 ret
= system("rm -rf /tmp/livesession");
980 if ((ret
= lttng_create_session("test", "/tmp/livesession")) < 0) {
981 fprintf(stderr
,"error creating the session : %s\n",
982 helper_lttcomm_get_readable_code(ret
));
986 if ((handle
= lttng_create_handle("test", &dom
)) == NULL
) {
987 fprintf(stderr
,"error creating handle\n");
991 if ((ret
= lttng_register_consumer(handle
, command_sock_path
)) < 0) {
992 fprintf(stderr
,"error registering consumer : %s\n",
993 helper_lttcomm_get_readable_code(ret
));
997 strcpy(chan
.name
, channel_name
);
998 chan
.attr
.overwrite
= 0;
999 chan
.attr
.subbuf_size
= 32768;
1000 // chan.attr.subbuf_size = 1048576; /* 1MB */
1001 chan
.attr
.num_subbuf
= 4;
1002 chan
.attr
.switch_timer_interval
= 0;
1003 chan
.attr
.read_timer_interval
= 200;
1004 chan
.attr
.output
= LTTNG_EVENT_MMAP
;
1006 if ((ret
= lttng_enable_channel(handle
, &chan
)) < 0) {
1007 fprintf(stderr
,"error creating channel : %s\n", helper_lttcomm_get_readable_code(ret
));
1011 sprintf(ev
.name
, "sched_switch");
1012 ev
.type
= LTTNG_EVENT_TRACEPOINT
;
1014 //if ((ret = lttng_enable_event(handle, NULL, channel_name)) < 0) {
1015 if ((ret
= lttng_enable_event(handle
, &ev
, channel_name
)) < 0) {
1016 fprintf(stderr
,"error enabling event : %s\n", helper_lttcomm_get_readable_code(ret
));
1020 kctxpid
.ctx
= LTTNG_EVENT_CONTEXT_PID
;
1021 lttng_add_context(handle
, &kctxpid
, NULL
, NULL
);
1022 kctxppid
.ctx
= LTTNG_EVENT_CONTEXT_PPID
;
1023 lttng_add_context(handle
, &kctxppid
, NULL
, NULL
);
1024 kctxcomm
.ctx
= LTTNG_EVENT_CONTEXT_PROCNAME
;
1025 lttng_add_context(handle
, &kctxcomm
, NULL
, NULL
);
1026 kctxtid
.ctx
= LTTNG_EVENT_CONTEXT_TID
;
1027 lttng_add_context(handle
, &kctxtid
, NULL
, NULL
);
1029 if ((ret
= lttng_start_tracing("test")) < 0) {
1030 fprintf(stderr
,"error starting tracing : %s\n", helper_lttcomm_get_readable_code(ret
));
1034 helper_kernctl_buffer_flush(consumerd_metadata
);
1036 /* Create thread to manage the polling/writing of traces */
1037 ret
= pthread_create(&thread_live_consume
, NULL
, live_consume
, NULL
);
1039 perror("pthread_create");
1043 // pthread_cancel(live_trace_thread);
1045 /* block until metadata is ready */
1046 sem_init(&metadata_available
, 0, 0);
1054 int main(int argc
, char **argv
)
1057 struct bt_context
*bt_ctx
= NULL
;
1059 ret
= parse_options(argc
, argv
);
1061 fprintf(stdout
, "Error parsing options.\n\n");
1064 } else if (ret
> 0) {
1068 if (!opt_input_path
) {
1069 printf("live tracing enabled\n");
1070 pthread_create(&live_trace_thread
, NULL
, setup_live_tracing
, (void *) NULL
);
1072 printf("STOPPING\n");
1073 lttng_stop_tracing("test");
1074 printf("DESTROYING\n");
1075 lttng_destroy_session("test");
1077 printf("CANCELLING\n");
1078 pthread_cancel(live_trace_thread
);
1083 bt_ctx
= bt_context_create();
1084 ret
= bt_context_add_traces_recursive(bt_ctx
, opt_input_path
, "ctf", NULL
);
1086 fprintf(stderr
, "[error] Opening the trace\n");
1090 ret
= check_requirements(bt_ctx
);
1092 fprintf(stderr
, "[error] some mandatory contexts were missing, exiting.\n");
1095 pthread_create(&display_thread
, NULL
, ncurses_display
, (void *) NULL
);
1096 pthread_create(&timer_thread
, NULL
, refresh_thread
, (void *) NULL
);
1101 pthread_join(display_thread
, NULL
);
1102 pthread_join(timer_thread
, NULL
);
1107 bt_context_put(bt_ctx
);