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
;
54 static int opt_textdump
;
59 struct lttngtop
*copy
;
60 pthread_t display_thread
;
61 pthread_t timer_thread
;
63 unsigned long refresh_display
= 1 * NSEC_PER_SEC
;
64 unsigned long last_display_update
= 0;
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 last_textdump_print_newline
= 1;
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 { "textdump", 't', POPT_ARG_NONE
, NULL
, OPT_TEXTDUMP
, NULL
, NULL
},
91 { "child", 'f', POPT_ARG_NONE
, NULL
, OPT_CHILD
, NULL
, NULL
},
92 { "pid", 'p', POPT_ARG_STRING
, &opt_tid
, OPT_PID
, NULL
, NULL
},
93 { "hostname", 'n', POPT_ARG_STRING
, &opt_hostname
, OPT_HOSTNAME
, NULL
, NULL
},
94 { NULL
, 0, 0, NULL
, 0, NULL
, NULL
},
97 static void handle_textdump_sigterm(int signal
)
100 lttng_destroy_session("test");
103 void *refresh_thread(void *p
)
105 struct mmap_stream
*mmap_info
;
109 sem_post(&pause_sem
);
111 sem_post(&end_trace_sem
);
112 sem_post(&goodtodisplay
);
113 sem_post(&goodtoupdate
);
116 if (!opt_input_path
) {
117 bt_list_for_each_entry(mmap_info
, &mmap_list
.head
, list
)
118 helper_kernctl_buffer_flush(mmap_info
->fd
);
120 sem_wait(&pause_sem
);
121 sem_post(&pause_sem
);
123 sleep(refresh_display
/NSEC_PER_SEC
);
127 void *ncurses_display(void *p
)
129 unsigned int current_display_index
= 0;
131 sem_wait(&bootstrap
);
133 * Prevent the 1 second delay when we hit ESC
140 sem_wait(&goodtodisplay
);
141 sem_wait(&pause_sem
);
144 sem_post(&pause_sem
);
150 copy
= g_ptr_array_index(copies
, current_display_index
);
152 display(current_display_index
++);
154 sem_post(&goodtoupdate
);
155 sem_post(&pause_sem
);
160 * hook on each event to check the timestamp and refresh the display if
163 enum bt_cb_ret
print_timestamp(struct bt_ctf_event
*call_data
, void *private_data
)
165 unsigned long timestamp
;
167 uint64_t ts_nsec_start
;
170 const struct definition
*scope
;
171 const char *hostname
;
173 timestamp
= bt_ctf_get_timestamp(call_data
);
175 start
= format_timestamp(timestamp
);
176 ts_nsec_start
= timestamp
% NSEC_PER_SEC
;
178 pid
= get_context_pid(call_data
);
179 if (pid
== -1ULL && opt_tid
) {
183 hostname
= get_context_hostname(call_data
);
184 if (!opt_tid
&& (opt_hostname
&& !lookup_hostname_list(hostname
)))
186 if (!opt_hostname
&& (opt_tid
&& !lookup_tid_list(pid
)))
188 if ((opt_tid
&& !lookup_tid_list(pid
)) &&
189 (opt_hostname
&& !lookup_hostname_list(hostname
)))
192 if (strcmp(bt_ctf_event_name(call_data
), "exit_syscall") == 0) {
193 scope
= bt_ctf_get_top_level_scope(call_data
,
195 syscall_ret
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
197 printf("= %ld\n", syscall_ret
);
198 last_textdump_print_newline
= 1;
200 /* we might have lost the exit_syscall event, so need to
201 * print the newline in this case */
202 if (last_textdump_print_newline
== 0)
204 printf("%02d:%02d:%02d.%09" PRIu64
" %d : %s ", start
.tm_hour
,
205 start
.tm_min
, start
.tm_sec
, ts_nsec_start
,
206 pid
, bt_ctf_event_name(call_data
));
207 last_textdump_print_newline
= 0;
213 return BT_CB_ERROR_STOP
;
217 * hook on each event to check the timestamp and refresh the display if
220 enum bt_cb_ret
check_timestamp(struct bt_ctf_event
*call_data
, void *private_data
)
222 unsigned long timestamp
;
224 timestamp
= bt_ctf_get_timestamp(call_data
);
225 if (timestamp
== -1ULL)
228 if (last_display_update
== 0)
229 last_display_update
= timestamp
;
231 if (timestamp
- last_display_update
>= refresh_display
) {
232 sem_wait(&goodtoupdate
);
233 g_ptr_array_add(copies
, get_copy_lttngtop(last_display_update
,
235 sem_post(&goodtodisplay
);
236 sem_post(&bootstrap
);
237 last_display_update
= timestamp
;
242 fprintf(stderr
, "check_timestamp callback error\n");
243 return BT_CB_ERROR_STOP
;
247 * get_perf_counter : get or create and return a perf_counter struct for
248 * either a process or a cpu (only one of the 2 parameters mandatory)
250 struct perfcounter
*get_perf_counter(const char *name
, struct processtop
*proc
,
253 struct perfcounter
*ret
;
263 ret
= g_hash_table_lookup(table
, (gpointer
) name
);
267 ret
= g_new0(struct perfcounter
, 1);
268 /* by default, make it visible in the UI */
270 g_hash_table_insert(table
, (gpointer
) strdup(name
), ret
);
279 void update_perf_value(struct processtop
*proc
, struct cputime
*cpu
,
280 const char *name
, int value
)
282 struct perfcounter
*cpu_perf
, *process_perf
;
284 cpu_perf
= get_perf_counter(name
, NULL
, cpu
);
285 if (cpu_perf
->count
< value
) {
286 process_perf
= get_perf_counter(name
, proc
, NULL
);
287 process_perf
->count
+= value
- cpu_perf
->count
;
288 cpu_perf
->count
= value
;
292 void extract_perf_counter_scope(const struct bt_ctf_event
*event
,
293 const struct bt_definition
*scope
,
294 struct processtop
*proc
,
297 struct bt_definition
const * const *list
= NULL
;
298 const struct bt_definition
*field
;
300 struct perfcounter
*perfcounter
;
308 ret
= bt_ctf_get_field_list(event
, scope
, &list
, &count
);
315 g_hash_table_iter_init(&iter
, global_perf_liszt
);
316 while (g_hash_table_iter_next (&iter
, &key
, (gpointer
) &perfcounter
)) {
317 field
= bt_ctf_get_field(event
, scope
, (char *) key
);
319 int value
= bt_ctf_get_uint64(field
);
320 if (bt_ctf_field_get_error())
322 update_perf_value(proc
, cpu
, (char *) key
, value
);
330 void update_perf_counter(struct processtop
*proc
, const struct bt_ctf_event
*event
)
333 const struct bt_definition
*scope
;
335 cpu
= get_cpu(get_cpu_id(event
));
337 scope
= bt_ctf_get_top_level_scope(event
, BT_STREAM_EVENT_CONTEXT
);
338 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
340 scope
= bt_ctf_get_top_level_scope(event
, BT_STREAM_PACKET_CONTEXT
);
341 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
343 scope
= bt_ctf_get_top_level_scope(event
, BT_EVENT_CONTEXT
);
344 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
347 enum bt_cb_ret
fix_process_table(struct bt_ctf_event
*call_data
,
350 int pid
, tid
, ppid
, vpid
, vtid
, vppid
;
351 char *comm
, *hostname
;
352 struct processtop
*parent
, *child
;
353 unsigned long timestamp
;
355 timestamp
= bt_ctf_get_timestamp(call_data
);
356 if (timestamp
== -1ULL)
359 pid
= get_context_pid(call_data
);
364 tid
= get_context_tid(call_data
);
368 ppid
= get_context_ppid(call_data
);
372 vpid
= get_context_vpid(call_data
);
376 vtid
= get_context_vtid(call_data
);
380 vppid
= get_context_vppid(call_data
);
384 comm
= get_context_comm(call_data
);
389 hostname
= get_context_hostname(call_data
);
391 /* find or create the current process */
392 child
= find_process_tid(<tngtop
, tid
, comm
);
394 child
= add_proc(<tngtop
, tid
, comm
, timestamp
);
397 update_proc(child
, pid
, tid
, ppid
, vpid
, vtid
, vppid
, comm
, hostname
);
400 /* find or create the parent */
401 parent
= find_process_tid(<tngtop
, pid
, comm
);
403 parent
= add_proc(<tngtop
, pid
, comm
, timestamp
);
408 /* attach the parent to the current process */
409 child
->threadparent
= parent
;
410 add_thread(parent
, child
);
413 update_perf_counter(child
, call_data
);
419 return BT_CB_ERROR_STOP
;
424 copies
= g_ptr_array_new();
425 global_perf_liszt
= g_hash_table_new(g_str_hash
, g_str_equal
);
427 sem_init(&goodtodisplay
, 0, 0);
428 sem_init(&goodtoupdate
, 0, 1);
429 sem_init(&timer
, 0, 1);
430 sem_init(&bootstrap
, 0, 0);
431 sem_init(&pause_sem
, 0, 1);
432 sem_init(&end_trace_sem
, 0, 0);
434 reset_global_counters();
436 lttngtop
.nbthreads
= 0;
437 lttngtop
.nbfiles
= 0;
439 lttngtop
.process_hash_table
= g_hash_table_new(g_direct_hash
,
441 lttngtop
.process_table
= g_ptr_array_new();
442 lttngtop
.files_table
= g_ptr_array_new();
443 lttngtop
.cpu_table
= g_ptr_array_new();
448 fprintf(fp
, "LTTngTop %s\n\n", VERSION
);
449 fprintf(fp
, "Usage : lttngtop [OPTIONS] [TRACE]\n");
450 fprintf(fp
, " TRACE Path to the trace to analyse (no trace path for live tracing)\n");
451 fprintf(fp
, " -h, --help This help message\n");
452 fprintf(fp
, " -t, --textdump Display live events in text-only\n");
453 fprintf(fp
, " -p, --pid Comma-separated list of PIDs to display\n");
454 fprintf(fp
, " -f, --child Follow threads associated with selected PIDs\n");
455 fprintf(fp
, " -n, --hostname Comma-separated list of hostnames to display (require hostname context in trace)\n");
459 * Return 0 if caller should continue, < 0 if caller should return
460 * error, > 0 if caller should exit without reporting error.
462 static int parse_options(int argc
, char **argv
)
469 pc
= poptGetContext(NULL
, argc
, (const char **) argv
, long_options
, 0);
470 poptReadDefaultConfig(pc
, 0);
472 while ((opt
= poptGetNextOpt(pc
)) != -1) {
476 ret
= 1; /* exit cleanly */
486 tid_list
= g_hash_table_new(g_str_hash
,
488 tmp_str
= strtok(opt_tid
, ",");
490 tid
= malloc(sizeof(int));
491 *tid
= atoi(tmp_str
);
492 g_hash_table_insert(tid_list
,
493 (gpointer
) tid
, tid
);
494 tmp_str
= strtok(NULL
, ",");
498 hostname_list
= g_hash_table_new(g_str_hash
,
500 tmp_str
= strtok(opt_hostname
, ",");
502 char *new_str
= strdup(tmp_str
);
503 g_hash_table_insert(hostname_list
,
506 tmp_str
= strtok(NULL
, ",");
515 opt_input_path
= poptGetArg(pc
);
524 void iter_trace(struct bt_context
*bt_ctx
)
526 struct bt_ctf_iter
*iter
;
527 struct bt_iter_pos begin_pos
;
528 const struct bt_ctf_event
*event
;
531 begin_pos
.type
= BT_SEEK_BEGIN
;
532 iter
= bt_ctf_iter_create(bt_ctx
, &begin_pos
, NULL
);
535 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
539 /* at each event check if we need to refresh */
540 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
543 /* at each event, verify the status of the process table */
544 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
547 /* to handle the scheduling events */
548 bt_ctf_iter_add_callback(iter
,
549 g_quark_from_static_string("sched_switch"),
550 NULL
, 0, handle_sched_switch
, NULL
, NULL
, NULL
);
551 /* to clean up the process table */
552 bt_ctf_iter_add_callback(iter
,
553 g_quark_from_static_string("sched_process_free"),
554 NULL
, 0, handle_sched_process_free
, NULL
, NULL
, NULL
);
555 /* to get all the process from the statedumps */
556 bt_ctf_iter_add_callback(iter
,
557 g_quark_from_static_string(
558 "lttng_statedump_process_state"),
559 NULL
, 0, handle_statedump_process_state
,
563 bt_ctf_iter_add_callback(iter
,
564 g_quark_from_static_string("exit_syscall"),
565 NULL
, 0, handle_exit_syscall
, NULL
, NULL
, NULL
);
566 bt_ctf_iter_add_callback(iter
,
567 g_quark_from_static_string("sys_write"),
568 NULL
, 0, handle_sys_write
, NULL
, NULL
, NULL
);
569 bt_ctf_iter_add_callback(iter
,
570 g_quark_from_static_string("sys_read"),
571 NULL
, 0, handle_sys_read
, NULL
, NULL
, NULL
);
572 bt_ctf_iter_add_callback(iter
,
573 g_quark_from_static_string("sys_open"),
574 NULL
, 0, handle_sys_open
, NULL
, NULL
, NULL
);
575 bt_ctf_iter_add_callback(iter
,
576 g_quark_from_static_string("sys_close"),
577 NULL
, 0, handle_sys_close
, NULL
, NULL
, NULL
);
578 bt_ctf_iter_add_callback(iter
,
579 g_quark_from_static_string(
580 "lttng_statedump_file_descriptor"),
581 NULL
, 0, handle_statedump_file_descriptor
,
585 while ((event
= bt_ctf_iter_read_event(iter
, NULL
)) != NULL
) {
586 if (quit
|| reload_trace
)
588 ret
= bt_iter_next(bt_ctf_get_iter(iter
));
593 /* block until quit, we reached the end of the trace */
594 sem_wait(&end_trace_sem
);
597 bt_ctf_iter_destroy(iter
);
601 * bt_context_add_traces_recursive: Open a trace recursively
602 * (copied from BSD code in converter/babeltrace.c)
604 * Find each trace present in the subdirectory starting from the given
605 * path, and add them to the context. The packet_seek parameter can be
606 * NULL: this specify to use the default format packet_seek.
608 * Return: 0 on success, nonzero on failure.
609 * Unable to open toplevel: failure.
610 * Unable to open some subdirectory or file: warn and continue;
612 int bt_context_add_traces_recursive(struct bt_context
*ctx
, const char *path
,
613 const char *format_str
,
614 void (*packet_seek
)(struct bt_stream_pos
*pos
,
615 size_t offset
, int whence
))
620 char lpath
[PATH_MAX
];
621 char * const paths
[2] = { lpath
, NULL
};
625 * Need to copy path, because fts_open can change it.
626 * It is the pointer array, not the strings, that are constant.
628 strncpy(lpath
, path
, PATH_MAX
);
629 lpath
[PATH_MAX
- 1] = '\0';
631 tree
= fts_open(paths
, FTS_NOCHDIR
| FTS_LOGICAL
, 0);
633 fprintf(stderr
, "[error] [Context] Cannot traverse \"%s\" for reading.\n",
638 trace_ids
= g_array_new(FALSE
, TRUE
, sizeof(int));
640 while ((node
= fts_read(tree
))) {
643 if (!(node
->fts_info
& FTS_D
))
646 dirfd
= open(node
->fts_accpath
, 0);
648 fprintf(stderr
, "[error] [Context] Unable to open trace "
649 "directory file descriptor.\n");
653 metafd
= openat(dirfd
, "metadata", O_RDONLY
);
672 trace_id
= bt_context_add_trace(ctx
,
673 node
->fts_accpath
, format_str
,
674 packet_seek
, NULL
, NULL
);
676 fprintf(stderr
, "[warning] [Context] opening trace \"%s\" from %s "
677 "for reading.\n", node
->fts_accpath
, path
);
678 /* Allow to skip erroneous traces. */
681 g_array_append_val(trace_ids
, trace_id
);
685 g_array_free(trace_ids
, TRUE
);
692 static int check_field_requirements(const struct bt_ctf_field_decl
*const * field_list
,
693 int field_cnt
, int *tid_check
, int *pid_check
,
694 int *procname_check
, int *ppid_check
)
697 struct perfcounter
*global
;
700 for (j
= 0; j
< field_cnt
; j
++) {
701 name
= bt_ctf_get_decl_field_name(field_list
[j
]);
702 if (*tid_check
== 0) {
703 if (strncmp(name
, "tid", 3) == 0)
706 if (*pid_check
== 0) {
707 if (strncmp(name
, "pid", 3) == 0)
710 if (*ppid_check
== 0) {
711 if (strncmp(name
, "ppid", 4) == 0)
714 if (*procname_check
== 0) {
715 if (strncmp(name
, "procname", 8) == 0)
718 if (strncmp(name
, "perf_", 5) == 0) {
719 global
= g_hash_table_lookup(global_perf_liszt
, (gpointer
) name
);
721 global
= g_new0(struct perfcounter
, 1);
722 /* by default, sort on the first perf context */
723 if (g_hash_table_size(global_perf_liszt
) == 0)
726 g_hash_table_insert(global_perf_liszt
, (gpointer
) strdup(name
), global
);
731 if (*tid_check
== 1 && *pid_check
== 1 && *ppid_check
== 1 &&
732 *procname_check
== 1)
739 * check_requirements: check if the required context informations are available
741 * If each mandatory context information is available for at least in one
742 * event, return 0 otherwise return -1.
744 int check_requirements(struct bt_context
*ctx
)
746 unsigned int i
, evt_cnt
, field_cnt
;
747 struct bt_ctf_event_decl
*const * evt_list
;
748 const struct bt_ctf_field_decl
*const * field_list
;
751 int procname_check
= 0;
755 bt_ctf_get_event_decl_list(0, ctx
, &evt_list
, &evt_cnt
);
756 for (i
= 0; i
< evt_cnt
; i
++) {
757 bt_ctf_get_decl_fields(evt_list
[i
], BT_STREAM_EVENT_CONTEXT
,
758 &field_list
, &field_cnt
);
759 ret
= check_field_requirements(field_list
, field_cnt
,
760 &tid_check
, &pid_check
, &procname_check
,
763 bt_ctf_get_decl_fields(evt_list
[i
], BT_EVENT_CONTEXT
,
764 &field_list
, &field_cnt
);
765 ret
= check_field_requirements(field_list
, field_cnt
,
766 &tid_check
, &pid_check
, &procname_check
,
769 bt_ctf_get_decl_fields(evt_list
[i
], BT_STREAM_PACKET_CONTEXT
,
770 &field_list
, &field_cnt
);
771 ret
= check_field_requirements(field_list
, field_cnt
,
772 &tid_check
, &pid_check
, &procname_check
,
776 if (tid_check
== 0) {
778 fprintf(stderr
, "[error] missing tid context information\n");
780 if (pid_check
== 0) {
782 fprintf(stderr
, "[error] missing pid context information\n");
784 if (ppid_check
== 0) {
786 fprintf(stderr
, "[error] missing ppid context information\n");
788 if (procname_check
== 0) {
790 fprintf(stderr
, "[error] missing procname context information\n");
796 ssize_t
read_subbuffer(struct lttng_consumer_stream
*kconsumerd_fd
,
797 struct lttng_consumer_local_data
*ctx
)
802 int infd
= helper_get_lttng_consumer_stream_wait_fd(kconsumerd_fd
);
804 if (helper_get_lttng_consumer_stream_output(kconsumerd_fd
) == LTTNG_EVENT_SPLICE
) {
805 /* Get the next subbuffer */
806 err
= helper_kernctl_get_next_subbuf(infd
);
809 perror("Reserving sub buffer failed (everything is normal, "
810 "it is due to concurrency)");
813 /* read the whole subbuffer */
814 err
= helper_kernctl_get_padded_subbuf_size(infd
, &len
);
817 perror("Getting sub-buffer len failed.");
821 /* splice the subbuffer to the tracefile */
822 ret
= helper_lttng_consumer_on_read_subbuffer_splice(ctx
, kconsumerd_fd
, len
);
825 * display the error but continue processing to try
826 * to release the subbuffer
828 fprintf(stderr
,"Error splicing to tracefile\n");
830 err
= helper_kernctl_put_next_subbuf(infd
);
833 perror("Reserving sub buffer failed (everything is normal, "
834 "it is due to concurrency)");
837 sem_post(&metadata_available
);
844 int on_update_fd(int key
, uint32_t state
)
846 /* let the lib handle the metadata FD */
847 if (key
== sessiond_metadata
)
852 int on_recv_fd(struct lttng_consumer_stream
*kconsumerd_fd
)
855 struct mmap_stream
*new_mmap_stream
;
857 /* Opening the tracefile in write mode */
858 if (helper_get_lttng_consumer_stream_path_name(kconsumerd_fd
) != NULL
) {
859 ret
= open(helper_get_lttng_consumer_stream_path_name(kconsumerd_fd
),
860 O_WRONLY
|O_CREAT
|O_TRUNC
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
865 helper_set_lttng_consumer_stream_out_fd(kconsumerd_fd
, ret
);
868 if (helper_get_lttng_consumer_stream_output(kconsumerd_fd
) == LTTNG_EVENT_MMAP
) {
869 new_mmap_stream
= malloc(sizeof(struct mmap_stream
));
870 new_mmap_stream
->fd
= helper_get_lttng_consumer_stream_wait_fd(
872 bt_list_add(&new_mmap_stream
->list
, &mmap_list
.head
);
874 g_ptr_array_add(lttng_consumer_stream_array
, kconsumerd_fd
);
875 /* keep mmap FDs internally */
878 consumerd_metadata
= helper_get_lttng_consumer_stream_wait_fd(kconsumerd_fd
);
879 sessiond_metadata
= helper_get_lttng_consumer_stream_key(kconsumerd_fd
);
889 void live_consume(struct bt_context
**bt_ctx
)
894 sem_wait(&metadata_available
);
895 if (access("/tmp/livesession/kernel/metadata", F_OK
) != 0) {
896 fprintf(stderr
,"no metadata\n");
899 metadata_fp
= fopen("/tmp/livesession/kernel/metadata", "r");
901 *bt_ctx
= bt_context_create();
902 ret
= bt_context_add_trace(*bt_ctx
, NULL
, "ctf",
903 lttngtop_ctf_packet_seek
, &mmap_list
, metadata_fp
);
905 printf("Error adding trace\n");
913 int setup_consumer(char *command_sock_path
, pthread_t
*threads
,
914 struct lttng_consumer_local_data
*ctx
)
918 ctx
= helper_lttng_consumer_create(HELPER_LTTNG_CONSUMER_KERNEL
,
919 read_subbuffer
, NULL
, on_recv_fd
, on_update_fd
);
923 unlink(command_sock_path
);
924 helper_lttng_consumer_set_command_sock_path(ctx
, command_sock_path
);
925 helper_lttng_consumer_init();
927 /* Create the thread to manage the receive of fd */
928 ret
= pthread_create(&threads
[0], NULL
, helper_lttng_consumer_thread_receive_fds
,
931 perror("pthread_create receive fd");
934 /* Create thread to manage the polling/writing of traces */
935 ret
= pthread_create(&threads
[1], NULL
, helper_lttng_consumer_thread_poll_fds
,
938 perror("pthread_create poll fd");
946 int setup_live_tracing()
948 struct lttng_domain dom
;
949 struct lttng_channel chan
;
950 char *channel_name
= "mmapchan";
951 struct lttng_event ev
;
953 char *command_sock_path
= "/tmp/consumerd_sock";
954 static pthread_t threads
[2]; /* recv_fd, poll */
955 struct lttng_event_context kctxpid
, kctxcomm
, kctxppid
, kctxtid
;
957 struct lttng_handle
*handle
;
959 BT_INIT_LIST_HEAD(&mmap_list
.head
);
961 lttng_consumer_stream_array
= g_ptr_array_new();
963 if ((ret
= setup_consumer(command_sock_path
, threads
, ctx
)) < 0) {
964 fprintf(stderr
,"error setting up consumer\n");
968 available_snapshots
= g_ptr_array_new();
970 /* setup the session */
971 dom
.type
= LTTNG_DOMAIN_KERNEL
;
973 ret
= unlink("/tmp/livesession");
975 lttng_destroy_session("test");
976 if ((ret
= lttng_create_session("test", "/tmp/livesession")) < 0) {
977 fprintf(stderr
,"error creating the session : %s\n",
978 helper_lttcomm_get_readable_code(ret
));
982 if ((handle
= lttng_create_handle("test", &dom
)) == NULL
) {
983 fprintf(stderr
,"error creating handle\n");
988 * FIXME : need to let the
989 * helper_lttng_consumer_thread_receive_fds create the
993 while (access(command_sock_path
, F_OK
)) {
997 if ((ret
= lttng_register_consumer(handle
, command_sock_path
)) < 0) {
998 fprintf(stderr
,"error registering consumer : %s\n",
999 helper_lttcomm_get_readable_code(ret
));
1003 strcpy(chan
.name
, channel_name
);
1004 chan
.attr
.overwrite
= 0;
1005 if (opt_tid
&& opt_textdump
) {
1006 chan
.attr
.subbuf_size
= 32768;
1007 chan
.attr
.num_subbuf
= 8;
1009 chan
.attr
.subbuf_size
= 1048576; /* 1MB */
1010 chan
.attr
.num_subbuf
= 4;
1012 chan
.attr
.switch_timer_interval
= 0;
1013 chan
.attr
.read_timer_interval
= 200;
1014 chan
.attr
.output
= LTTNG_EVENT_MMAP
;
1016 if ((ret
= lttng_enable_channel(handle
, &chan
)) < 0) {
1017 fprintf(stderr
,"error creating channel : %s\n",
1018 helper_lttcomm_get_readable_code(ret
));
1022 memset(&ev
, '\0', sizeof(struct lttng_event
));
1023 //sprintf(ev.name, "sched_switch");
1024 ev
.type
= LTTNG_EVENT_TRACEPOINT
;
1025 if ((ret
= lttng_enable_event(handle
, &ev
, channel_name
)) < 0) {
1026 fprintf(stderr
,"error enabling event : %s\n",
1027 helper_lttcomm_get_readable_code(ret
));
1031 ev
.type
= LTTNG_EVENT_SYSCALL
;
1032 if ((ret
= lttng_enable_event(handle
, &ev
, channel_name
)) < 0) {
1033 fprintf(stderr
,"error enabling syscalls : %s\n",
1034 helper_lttcomm_get_readable_code(ret
));
1038 kctxpid
.ctx
= LTTNG_EVENT_CONTEXT_PID
;
1039 lttng_add_context(handle
, &kctxpid
, NULL
, NULL
);
1040 kctxtid
.ctx
= LTTNG_EVENT_CONTEXT_TID
;
1041 lttng_add_context(handle
, &kctxtid
, NULL
, NULL
);
1042 kctxppid
.ctx
= LTTNG_EVENT_CONTEXT_PPID
;
1043 lttng_add_context(handle
, &kctxppid
, NULL
, NULL
);
1044 kctxcomm
.ctx
= LTTNG_EVENT_CONTEXT_PROCNAME
;
1045 lttng_add_context(handle
, &kctxcomm
, NULL
, NULL
);
1046 kctxpid
.ctx
= LTTNG_EVENT_CONTEXT_VPID
;
1047 lttng_add_context(handle
, &kctxpid
, NULL
, NULL
);
1048 kctxtid
.ctx
= LTTNG_EVENT_CONTEXT_VTID
;
1049 lttng_add_context(handle
, &kctxtid
, NULL
, NULL
);
1050 kctxtid
.ctx
= LTTNG_EVENT_CONTEXT_HOSTNAME
;
1051 lttng_add_context(handle
, &kctxtid
, NULL
, NULL
);
1053 if ((ret
= lttng_start_tracing("test")) < 0) {
1054 fprintf(stderr
,"error starting tracing : %s\n",
1055 helper_lttcomm_get_readable_code(ret
));
1059 helper_kernctl_buffer_flush(consumerd_metadata
);
1061 /* block until metadata is ready */
1062 sem_init(&metadata_available
, 0, 0);
1067 lttng_destroy_session("test");
1072 int main(int argc
, char **argv
)
1075 struct bt_context
*bt_ctx
= NULL
;
1076 struct mmap_stream
*mmap_info
;
1077 unsigned long mmap_len
;
1079 ret
= parse_options(argc
, argv
);
1081 fprintf(stdout
, "Error parsing options.\n\n");
1084 } else if (ret
> 0) {
1088 if (!opt_input_path
) {
1090 signal(SIGTERM
, handle_textdump_sigterm
);
1091 signal(SIGINT
, handle_textdump_sigterm
);
1093 ret
= setup_live_tracing();
1098 if (!opt_textdump
) {
1099 pthread_create(&display_thread
, NULL
, ncurses_display
, (void *) NULL
);
1100 pthread_create(&timer_thread
, NULL
, refresh_thread
, (void *) NULL
);
1104 live_consume(&bt_ctx
);
1106 ret
= bt_context_remove_trace(bt_ctx
, 0);
1108 fprintf(stderr
, "error removing trace\n");
1110 bt_context_put(bt_ctx
);
1114 * since we receive all FDs every time there is an
1115 * update and the FD number is different every time,
1116 * we don't know which one are valid.
1117 * so we check if all FDs are usable with a simple
1120 bt_list_for_each_entry(mmap_info
, &mmap_list
.head
, list
) {
1121 ret
= helper_kernctl_get_mmap_len(mmap_info
->fd
, &mmap_len
);
1123 bt_list_del(&mmap_info
->list
);
1126 sem_post(&metadata_available
);
1129 pthread_join(timer_thread
, NULL
);
1131 pthread_join(display_thread
, NULL
);
1133 lttng_stop_tracing("test");
1134 lttng_destroy_session("test");
1140 bt_ctx
= bt_context_create();
1141 ret
= bt_context_add_traces_recursive(bt_ctx
, opt_input_path
, "ctf", NULL
);
1143 fprintf(stderr
, "[error] Opening the trace\n");
1147 ret
= check_requirements(bt_ctx
);
1149 fprintf(stderr
, "[error] some mandatory contexts were missing, exiting.\n");
1152 pthread_create(&display_thread
, NULL
, ncurses_display
, (void *) NULL
);
1153 pthread_create(&timer_thread
, NULL
, refresh_thread
, (void *) NULL
);
1157 pthread_join(display_thread
, NULL
);
1159 pthread_join(timer_thread
, NULL
);
1164 bt_context_put(bt_ctx
);