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
;
60 struct lttngtop
*copy
;
61 pthread_t display_thread
;
62 pthread_t timer_thread
;
64 unsigned long refresh_display
= 1 * NSEC_PER_SEC
;
65 unsigned long last_display_update
= 0;
67 /* list of FDs available for being read with snapshots */
68 struct mmap_stream_list mmap_list
;
69 GPtrArray
*lttng_consumer_stream_array
;
70 int sessiond_metadata
, consumerd_metadata
;
71 struct lttng_consumer_local_data
*ctx
= NULL
;
72 /* list of snapshots currently not consumed */
73 GPtrArray
*available_snapshots
;
74 sem_t metadata_available
;
85 static struct poptOption long_options
[] = {
86 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
87 { "help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
88 { "textdump", 't', POPT_ARG_NONE
, NULL
, OPT_TEXTDUMP
, NULL
, NULL
},
89 { "child", 'f', POPT_ARG_NONE
, NULL
, OPT_CHILD
, NULL
, NULL
},
90 { "pid", 'p', POPT_ARG_INT
, &opt_pid
, OPT_PID
, NULL
, NULL
},
91 { NULL
, 0, 0, NULL
, 0, NULL
, NULL
},
94 void *refresh_thread(void *p
)
96 struct mmap_stream
*mmap_info
;
100 sem_post(&pause_sem
);
102 sem_post(&end_trace_sem
);
103 sem_post(&goodtodisplay
);
104 sem_post(&goodtoupdate
);
107 if (!opt_input_path
) {
108 bt_list_for_each_entry(mmap_info
, &mmap_list
.head
, list
)
109 helper_kernctl_buffer_flush(mmap_info
->fd
);
111 sem_wait(&pause_sem
);
112 sem_post(&pause_sem
);
114 sleep(refresh_display
/NSEC_PER_SEC
);
118 void *ncurses_display(void *p
)
120 unsigned int current_display_index
= 0;
122 sem_wait(&bootstrap
);
124 * Prevent the 1 second delay when we hit ESC
131 sem_wait(&goodtodisplay
);
132 sem_wait(&pause_sem
);
135 sem_post(&pause_sem
);
141 copy
= g_ptr_array_index(copies
, current_display_index
);
143 display(current_display_index
++);
145 sem_post(&goodtoupdate
);
146 sem_post(&pause_sem
);
151 * hook on each event to check the timestamp and refresh the display if
154 enum bt_cb_ret
print_timestamp(struct bt_ctf_event
*call_data
, void *private_data
)
156 unsigned long timestamp
;
158 uint64_t ts_nsec_start
;
161 const struct definition
*scope
;
163 timestamp
= bt_ctf_get_timestamp(call_data
);
165 start
= format_timestamp(timestamp
);
166 ts_nsec_start
= timestamp
% NSEC_PER_SEC
;
168 pid
= get_context_pid(call_data
);
169 if (pid
== -1ULL && opt_pid
) {
173 if (opt_pid
&& opt_pid
!= pid
)
176 if (strcmp(bt_ctf_event_name(call_data
), "exit_syscall") == 0) {
177 scope
= bt_ctf_get_top_level_scope(call_data
,
179 syscall_ret
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
181 printf("= %ld\n", syscall_ret
);
183 printf("%02d:%02d:%02d.%09" PRIu64
" %d : %s ", start
.tm_hour
,
184 start
.tm_min
, start
.tm_sec
, ts_nsec_start
,
185 pid
, bt_ctf_event_name(call_data
));
191 return BT_CB_ERROR_STOP
;
195 * hook on each event to check the timestamp and refresh the display if
198 enum bt_cb_ret
check_timestamp(struct bt_ctf_event
*call_data
, void *private_data
)
200 unsigned long timestamp
;
202 timestamp
= bt_ctf_get_timestamp(call_data
);
203 if (timestamp
== -1ULL)
206 if (last_display_update
== 0)
207 last_display_update
= timestamp
;
209 if (timestamp
- last_display_update
>= refresh_display
) {
210 sem_wait(&goodtoupdate
);
211 g_ptr_array_add(copies
, get_copy_lttngtop(last_display_update
,
213 sem_post(&goodtodisplay
);
214 sem_post(&bootstrap
);
215 last_display_update
= timestamp
;
220 fprintf(stderr
, "check_timestamp callback error\n");
221 return BT_CB_ERROR_STOP
;
225 * get_perf_counter : get or create and return a perf_counter struct for
226 * either a process or a cpu (only one of the 2 parameters mandatory)
228 struct perfcounter
*get_perf_counter(const char *name
, struct processtop
*proc
,
231 struct perfcounter
*ret
;
241 ret
= g_hash_table_lookup(table
, (gpointer
) name
);
245 ret
= g_new0(struct perfcounter
, 1);
246 /* by default, make it visible in the UI */
248 g_hash_table_insert(table
, (gpointer
) strdup(name
), ret
);
257 void update_perf_value(struct processtop
*proc
, struct cputime
*cpu
,
258 const char *name
, int value
)
260 struct perfcounter
*cpu_perf
, *process_perf
;
262 cpu_perf
= get_perf_counter(name
, NULL
, cpu
);
263 if (cpu_perf
->count
< value
) {
264 process_perf
= get_perf_counter(name
, proc
, NULL
);
265 process_perf
->count
+= value
- cpu_perf
->count
;
266 cpu_perf
->count
= value
;
270 void extract_perf_counter_scope(const struct bt_ctf_event
*event
,
271 const struct bt_definition
*scope
,
272 struct processtop
*proc
,
275 struct bt_definition
const * const *list
= NULL
;
276 const struct bt_definition
*field
;
278 struct perfcounter
*perfcounter
;
286 ret
= bt_ctf_get_field_list(event
, scope
, &list
, &count
);
293 g_hash_table_iter_init(&iter
, global_perf_liszt
);
294 while (g_hash_table_iter_next (&iter
, &key
, (gpointer
) &perfcounter
)) {
295 field
= bt_ctf_get_field(event
, scope
, (char *) key
);
297 int value
= bt_ctf_get_uint64(field
);
298 if (bt_ctf_field_get_error())
300 update_perf_value(proc
, cpu
, (char *) key
, value
);
308 void update_perf_counter(struct processtop
*proc
, const struct bt_ctf_event
*event
)
311 const struct bt_definition
*scope
;
313 cpu
= get_cpu(get_cpu_id(event
));
315 scope
= bt_ctf_get_top_level_scope(event
, BT_STREAM_EVENT_CONTEXT
);
316 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
318 scope
= bt_ctf_get_top_level_scope(event
, BT_STREAM_PACKET_CONTEXT
);
319 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
321 scope
= bt_ctf_get_top_level_scope(event
, BT_EVENT_CONTEXT
);
322 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
325 enum bt_cb_ret
fix_process_table(struct bt_ctf_event
*call_data
,
328 int pid
, tid
, ppid
, vpid
, vtid
, vppid
;
330 struct processtop
*parent
, *child
;
331 unsigned long timestamp
;
333 timestamp
= bt_ctf_get_timestamp(call_data
);
334 if (timestamp
== -1ULL)
337 pid
= get_context_pid(call_data
);
341 tid
= get_context_tid(call_data
);
345 ppid
= get_context_ppid(call_data
);
349 vpid
= get_context_vpid(call_data
);
353 vtid
= get_context_vtid(call_data
);
357 vppid
= get_context_vppid(call_data
);
361 comm
= get_context_comm(call_data
);
366 /* find or create the current process */
367 child
= find_process_tid(<tngtop
, tid
, comm
);
369 child
= add_proc(<tngtop
, tid
, comm
, timestamp
);
370 update_proc(child
, pid
, tid
, ppid
, vpid
, vtid
, vppid
, comm
);
373 /* find or create the parent */
374 parent
= find_process_tid(<tngtop
, pid
, comm
);
376 parent
= add_proc(<tngtop
, pid
, comm
, timestamp
);
380 /* attach the parent to the current process */
381 child
->threadparent
= parent
;
382 add_thread(parent
, child
);
385 update_perf_counter(child
, call_data
);
390 return BT_CB_ERROR_STOP
;
395 copies
= g_ptr_array_new();
396 global_perf_liszt
= g_hash_table_new(g_str_hash
, g_str_equal
);
398 sem_init(&goodtodisplay
, 0, 0);
399 sem_init(&goodtoupdate
, 0, 1);
400 sem_init(&timer
, 0, 1);
401 sem_init(&bootstrap
, 0, 0);
402 sem_init(&pause_sem
, 0, 1);
403 sem_init(&end_trace_sem
, 0, 0);
405 reset_global_counters();
407 lttngtop
.nbthreads
= 0;
408 lttngtop
.nbfiles
= 0;
410 lttngtop
.process_hash_table
= g_hash_table_new(g_direct_hash
,
412 lttngtop
.process_table
= g_ptr_array_new();
413 lttngtop
.files_table
= g_ptr_array_new();
414 lttngtop
.cpu_table
= g_ptr_array_new();
419 fprintf(fp
, "LTTngTop %s\n\n", VERSION
);
420 fprintf(fp
, "Usage : lttngtop /path/to/trace\n");
424 * Return 0 if caller should continue, < 0 if caller should return
425 * error, > 0 if caller should exit without reporting error.
427 static int parse_options(int argc
, char **argv
)
432 pc
= poptGetContext(NULL
, argc
, (const char **) argv
, long_options
, 0);
433 poptReadDefaultConfig(pc
, 0);
435 while ((opt
= poptGetNextOpt(pc
)) != -1) {
439 ret
= 1; /* exit cleanly */
449 refresh_display
= 0.1 * NSEC_PER_SEC
;
458 opt_input_path
= poptGetArg(pc
);
467 void iter_trace(struct bt_context
*bt_ctx
)
469 struct bt_ctf_iter
*iter
;
470 struct bt_iter_pos begin_pos
;
471 const struct bt_ctf_event
*event
;
474 begin_pos
.type
= BT_SEEK_BEGIN
;
475 iter
= bt_ctf_iter_create(bt_ctx
, &begin_pos
, NULL
);
479 printf("PID : %d, Child : %d\n", opt_pid
, opt_child
);
481 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
485 /* at each event check if we need to refresh */
486 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
489 /* at each event, verify the status of the process table */
490 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
493 /* to handle the scheduling events */
494 bt_ctf_iter_add_callback(iter
,
495 g_quark_from_static_string("sched_switch"),
496 NULL
, 0, handle_sched_switch
, NULL
, NULL
, NULL
);
497 /* to clean up the process table */
498 bt_ctf_iter_add_callback(iter
,
499 g_quark_from_static_string("sched_process_free"),
500 NULL
, 0, handle_sched_process_free
, NULL
, NULL
, NULL
);
501 /* to get all the process from the statedumps */
502 bt_ctf_iter_add_callback(iter
,
503 g_quark_from_static_string(
504 "lttng_statedump_process_state"),
505 NULL
, 0, handle_statedump_process_state
,
509 bt_ctf_iter_add_callback(iter
,
510 g_quark_from_static_string("exit_syscall"),
511 NULL
, 0, handle_exit_syscall
, NULL
, NULL
, NULL
);
512 bt_ctf_iter_add_callback(iter
,
513 g_quark_from_static_string("sys_write"),
514 NULL
, 0, handle_sys_write
, NULL
, NULL
, NULL
);
515 bt_ctf_iter_add_callback(iter
,
516 g_quark_from_static_string("sys_read"),
517 NULL
, 0, handle_sys_read
, NULL
, NULL
, NULL
);
518 bt_ctf_iter_add_callback(iter
,
519 g_quark_from_static_string("sys_open"),
520 NULL
, 0, handle_sys_open
, NULL
, NULL
, NULL
);
521 bt_ctf_iter_add_callback(iter
,
522 g_quark_from_static_string("sys_close"),
523 NULL
, 0, handle_sys_close
, NULL
, NULL
, NULL
);
524 bt_ctf_iter_add_callback(iter
,
525 g_quark_from_static_string(
526 "lttng_statedump_file_descriptor"),
527 NULL
, 0, handle_statedump_file_descriptor
,
531 while ((event
= bt_ctf_iter_read_event(iter
, NULL
)) != NULL
) {
532 if (quit
|| reload_trace
)
534 ret
= bt_iter_next(bt_ctf_get_iter(iter
));
539 /* block until quit, we reached the end of the trace */
540 sem_wait(&end_trace_sem
);
543 bt_ctf_iter_destroy(iter
);
547 * bt_context_add_traces_recursive: Open a trace recursively
548 * (copied from BSD code in converter/babeltrace.c)
550 * Find each trace present in the subdirectory starting from the given
551 * path, and add them to the context. The packet_seek parameter can be
552 * NULL: this specify to use the default format packet_seek.
554 * Return: 0 on success, nonzero on failure.
555 * Unable to open toplevel: failure.
556 * Unable to open some subdirectory or file: warn and continue;
558 int bt_context_add_traces_recursive(struct bt_context
*ctx
, const char *path
,
559 const char *format_str
,
560 void (*packet_seek
)(struct bt_stream_pos
*pos
,
561 size_t offset
, int whence
))
566 char lpath
[PATH_MAX
];
567 char * const paths
[2] = { lpath
, NULL
};
571 * Need to copy path, because fts_open can change it.
572 * It is the pointer array, not the strings, that are constant.
574 strncpy(lpath
, path
, PATH_MAX
);
575 lpath
[PATH_MAX
- 1] = '\0';
577 tree
= fts_open(paths
, FTS_NOCHDIR
| FTS_LOGICAL
, 0);
579 fprintf(stderr
, "[error] [Context] Cannot traverse \"%s\" for reading.\n",
584 trace_ids
= g_array_new(FALSE
, TRUE
, sizeof(int));
586 while ((node
= fts_read(tree
))) {
589 if (!(node
->fts_info
& FTS_D
))
592 dirfd
= open(node
->fts_accpath
, 0);
594 fprintf(stderr
, "[error] [Context] Unable to open trace "
595 "directory file descriptor.\n");
599 metafd
= openat(dirfd
, "metadata", O_RDONLY
);
618 trace_id
= bt_context_add_trace(ctx
,
619 node
->fts_accpath
, format_str
,
620 packet_seek
, NULL
, NULL
);
622 fprintf(stderr
, "[warning] [Context] opening trace \"%s\" from %s "
623 "for reading.\n", node
->fts_accpath
, path
);
624 /* Allow to skip erroneous traces. */
627 g_array_append_val(trace_ids
, trace_id
);
631 g_array_free(trace_ids
, TRUE
);
638 static int check_field_requirements(const struct bt_ctf_field_decl
*const * field_list
,
639 int field_cnt
, int *tid_check
, int *pid_check
,
640 int *procname_check
, int *ppid_check
)
643 struct perfcounter
*global
;
646 for (j
= 0; j
< field_cnt
; j
++) {
647 name
= bt_ctf_get_decl_field_name(field_list
[j
]);
648 if (*tid_check
== 0) {
649 if (strncmp(name
, "tid", 3) == 0)
652 if (*pid_check
== 0) {
653 if (strncmp(name
, "pid", 3) == 0)
656 if (*ppid_check
== 0) {
657 if (strncmp(name
, "ppid", 4) == 0)
660 if (*procname_check
== 0) {
661 if (strncmp(name
, "procname", 8) == 0)
664 if (strncmp(name
, "perf_", 5) == 0) {
665 global
= g_hash_table_lookup(global_perf_liszt
, (gpointer
) name
);
667 global
= g_new0(struct perfcounter
, 1);
668 /* by default, sort on the first perf context */
669 if (g_hash_table_size(global_perf_liszt
) == 0)
672 g_hash_table_insert(global_perf_liszt
, (gpointer
) strdup(name
), global
);
677 if (*tid_check
== 1 && *pid_check
== 1 && *ppid_check
== 1 &&
678 *procname_check
== 1)
685 * check_requirements: check if the required context informations are available
687 * If each mandatory context information is available for at least in one
688 * event, return 0 otherwise return -1.
690 int check_requirements(struct bt_context
*ctx
)
692 unsigned int i
, evt_cnt
, field_cnt
;
693 struct bt_ctf_event_decl
*const * evt_list
;
694 const struct bt_ctf_field_decl
*const * field_list
;
697 int procname_check
= 0;
701 bt_ctf_get_event_decl_list(0, ctx
, &evt_list
, &evt_cnt
);
702 for (i
= 0; i
< evt_cnt
; i
++) {
703 bt_ctf_get_decl_fields(evt_list
[i
], BT_STREAM_EVENT_CONTEXT
,
704 &field_list
, &field_cnt
);
705 ret
= check_field_requirements(field_list
, field_cnt
,
706 &tid_check
, &pid_check
, &procname_check
,
709 bt_ctf_get_decl_fields(evt_list
[i
], BT_EVENT_CONTEXT
,
710 &field_list
, &field_cnt
);
711 ret
= check_field_requirements(field_list
, field_cnt
,
712 &tid_check
, &pid_check
, &procname_check
,
715 bt_ctf_get_decl_fields(evt_list
[i
], BT_STREAM_PACKET_CONTEXT
,
716 &field_list
, &field_cnt
);
717 ret
= check_field_requirements(field_list
, field_cnt
,
718 &tid_check
, &pid_check
, &procname_check
,
722 if (tid_check
== 0) {
724 fprintf(stderr
, "[error] missing tid context information\n");
726 if (pid_check
== 0) {
728 fprintf(stderr
, "[error] missing pid context information\n");
730 if (ppid_check
== 0) {
732 fprintf(stderr
, "[error] missing ppid context information\n");
734 if (procname_check
== 0) {
736 fprintf(stderr
, "[error] missing procname context information\n");
742 ssize_t
read_subbuffer(struct lttng_consumer_stream
*kconsumerd_fd
,
743 struct lttng_consumer_local_data
*ctx
)
748 int infd
= helper_get_lttng_consumer_stream_wait_fd(kconsumerd_fd
);
750 if (helper_get_lttng_consumer_stream_output(kconsumerd_fd
) == LTTNG_EVENT_SPLICE
) {
751 /* Get the next subbuffer */
752 err
= helper_kernctl_get_next_subbuf(infd
);
755 perror("Reserving sub buffer failed (everything is normal, "
756 "it is due to concurrency)");
759 /* read the whole subbuffer */
760 err
= helper_kernctl_get_padded_subbuf_size(infd
, &len
);
763 perror("Getting sub-buffer len failed.");
767 /* splice the subbuffer to the tracefile */
768 ret
= helper_lttng_consumer_on_read_subbuffer_splice(ctx
, kconsumerd_fd
, len
);
771 * display the error but continue processing to try
772 * to release the subbuffer
774 fprintf(stderr
,"Error splicing to tracefile\n");
776 err
= helper_kernctl_put_next_subbuf(infd
);
779 perror("Reserving sub buffer failed (everything is normal, "
780 "it is due to concurrency)");
783 sem_post(&metadata_available
);
790 int on_update_fd(int key
, uint32_t state
)
792 /* let the lib handle the metadata FD */
793 if (key
== sessiond_metadata
)
798 int on_recv_fd(struct lttng_consumer_stream
*kconsumerd_fd
)
801 struct mmap_stream
*new_mmap_stream
;
803 /* Opening the tracefile in write mode */
804 if (helper_get_lttng_consumer_stream_path_name(kconsumerd_fd
) != NULL
) {
805 ret
= open(helper_get_lttng_consumer_stream_path_name(kconsumerd_fd
),
806 O_WRONLY
|O_CREAT
|O_TRUNC
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
811 helper_set_lttng_consumer_stream_out_fd(kconsumerd_fd
, ret
);
814 if (helper_get_lttng_consumer_stream_output(kconsumerd_fd
) == LTTNG_EVENT_MMAP
) {
815 new_mmap_stream
= malloc(sizeof(struct mmap_stream
));
816 new_mmap_stream
->fd
= helper_get_lttng_consumer_stream_wait_fd(
818 bt_list_add(&new_mmap_stream
->list
, &mmap_list
.head
);
820 g_ptr_array_add(lttng_consumer_stream_array
, kconsumerd_fd
);
821 /* keep mmap FDs internally */
824 consumerd_metadata
= helper_get_lttng_consumer_stream_wait_fd(kconsumerd_fd
);
825 sessiond_metadata
= helper_get_lttng_consumer_stream_key(kconsumerd_fd
);
835 void live_consume(struct bt_context
**bt_ctx
)
840 sem_wait(&metadata_available
);
841 if (access("/tmp/livesession/kernel/metadata", F_OK
) != 0) {
842 fprintf(stderr
,"no metadata\n");
845 metadata_fp
= fopen("/tmp/livesession/kernel/metadata", "r");
847 *bt_ctx
= bt_context_create();
848 ret
= bt_context_add_trace(*bt_ctx
, NULL
, "ctf",
849 lttngtop_ctf_packet_seek
, &mmap_list
, metadata_fp
);
851 printf("Error adding trace\n");
859 int setup_consumer(char *command_sock_path
, pthread_t
*threads
,
860 struct lttng_consumer_local_data
*ctx
)
864 ctx
= helper_lttng_consumer_create(HELPER_LTTNG_CONSUMER_KERNEL
,
865 read_subbuffer
, NULL
, on_recv_fd
, on_update_fd
);
869 unlink(command_sock_path
);
870 helper_lttng_consumer_set_command_sock_path(ctx
, command_sock_path
);
871 helper_lttng_consumer_init();
873 /* Create the thread to manage the receive of fd */
874 ret
= pthread_create(&threads
[0], NULL
, helper_lttng_consumer_thread_receive_fds
,
877 perror("pthread_create receive fd");
880 /* Create thread to manage the polling/writing of traces */
881 ret
= pthread_create(&threads
[1], NULL
, helper_lttng_consumer_thread_poll_fds
,
884 perror("pthread_create poll fd");
892 int setup_live_tracing()
894 struct lttng_domain dom
;
895 struct lttng_channel chan
;
896 char *channel_name
= "mmapchan";
897 struct lttng_event ev
;
899 char *command_sock_path
= "/tmp/consumerd_sock";
900 static pthread_t threads
[2]; /* recv_fd, poll */
901 struct lttng_event_context kctxpid
, kctxcomm
, kctxppid
, kctxtid
;
903 struct lttng_handle
*handle
;
905 BT_INIT_LIST_HEAD(&mmap_list
.head
);
907 lttng_consumer_stream_array
= g_ptr_array_new();
909 if ((ret
= setup_consumer(command_sock_path
, threads
, ctx
)) < 0) {
910 fprintf(stderr
,"error setting up consumer\n");
914 available_snapshots
= g_ptr_array_new();
916 /* setup the session */
917 dom
.type
= LTTNG_DOMAIN_KERNEL
;
919 ret
= unlink("/tmp/livesession");
921 lttng_destroy_session("test");
922 if ((ret
= lttng_create_session("test", "/tmp/livesession")) < 0) {
923 fprintf(stderr
,"error creating the session : %s\n",
924 helper_lttcomm_get_readable_code(ret
));
928 if ((handle
= lttng_create_handle("test", &dom
)) == NULL
) {
929 fprintf(stderr
,"error creating handle\n");
934 * FIXME : need to let the
935 * helper_lttng_consumer_thread_receive_fds create the
939 while (access(command_sock_path
, F_OK
)) {
943 if ((ret
= lttng_register_consumer(handle
, command_sock_path
)) < 0) {
944 fprintf(stderr
,"error registering consumer : %s\n",
945 helper_lttcomm_get_readable_code(ret
));
949 strcpy(chan
.name
, channel_name
);
950 chan
.attr
.overwrite
= 0;
952 chan
.attr
.subbuf_size
= 32768;
953 chan
.attr
.num_subbuf
= 8;
955 chan
.attr
.subbuf_size
= 1048576; /* 1MB */
956 chan
.attr
.num_subbuf
= 4;
958 chan
.attr
.switch_timer_interval
= 0;
959 chan
.attr
.read_timer_interval
= 200;
960 chan
.attr
.output
= LTTNG_EVENT_MMAP
;
962 if ((ret
= lttng_enable_channel(handle
, &chan
)) < 0) {
963 fprintf(stderr
,"error creating channel : %s\n",
964 helper_lttcomm_get_readable_code(ret
));
968 memset(&ev
, '\0', sizeof(struct lttng_event
));
969 //sprintf(ev.name, "sched_switch");
971 ev
.type
= LTTNG_EVENT_TRACEPOINT
;
972 if ((ret
= lttng_enable_event(handle
, &ev
, channel_name
)) < 0) {
973 fprintf(stderr
,"error enabling event : %s\n",
974 helper_lttcomm_get_readable_code(ret
));
979 ev
.type
= LTTNG_EVENT_SYSCALL
;
980 if ((ret
= lttng_enable_event(handle
, &ev
, channel_name
)) < 0) {
981 fprintf(stderr
,"error enabling syscalls : %s\n",
982 helper_lttcomm_get_readable_code(ret
));
986 kctxpid
.ctx
= LTTNG_EVENT_CONTEXT_PID
;
987 lttng_add_context(handle
, &kctxpid
, NULL
, NULL
);
988 kctxtid
.ctx
= LTTNG_EVENT_CONTEXT_TID
;
989 lttng_add_context(handle
, &kctxtid
, NULL
, NULL
);
991 kctxppid
.ctx
= LTTNG_EVENT_CONTEXT_PPID
;
992 lttng_add_context(handle
, &kctxppid
, NULL
, NULL
);
993 kctxcomm
.ctx
= LTTNG_EVENT_CONTEXT_PROCNAME
;
994 lttng_add_context(handle
, &kctxcomm
, NULL
, NULL
);
995 kctxpid
.ctx
= LTTNG_EVENT_CONTEXT_VPID
;
996 lttng_add_context(handle
, &kctxpid
, NULL
, NULL
);
997 kctxtid
.ctx
= LTTNG_EVENT_CONTEXT_VTID
;
998 lttng_add_context(handle
, &kctxtid
, NULL
, NULL
);
1001 if ((ret
= lttng_start_tracing("test")) < 0) {
1002 fprintf(stderr
,"error starting tracing : %s\n",
1003 helper_lttcomm_get_readable_code(ret
));
1007 helper_kernctl_buffer_flush(consumerd_metadata
);
1009 /* block until metadata is ready */
1010 sem_init(&metadata_available
, 0, 0);
1015 lttng_destroy_session("test");
1020 int main(int argc
, char **argv
)
1023 struct bt_context
*bt_ctx
= NULL
;
1024 struct mmap_stream
*mmap_info
;
1025 unsigned long mmap_len
;
1027 ret
= parse_options(argc
, argv
);
1029 fprintf(stdout
, "Error parsing options.\n\n");
1032 } else if (ret
> 0) {
1036 if (!opt_input_path
) {
1037 ret
= setup_live_tracing();
1042 if (!opt_textdump
) {
1043 pthread_create(&display_thread
, NULL
, ncurses_display
, (void *) NULL
);
1044 pthread_create(&timer_thread
, NULL
, refresh_thread
, (void *) NULL
);
1048 live_consume(&bt_ctx
);
1050 ret
= bt_context_remove_trace(bt_ctx
, 0);
1052 fprintf(stderr
, "error removing trace\n");
1054 bt_context_put(bt_ctx
);
1058 * since we receive all FDs every time there is an
1059 * update and the FD number is different every time,
1060 * we don't know which one are valid.
1061 * so we check if all FDs are usable with a simple
1064 bt_list_for_each_entry(mmap_info
, &mmap_list
.head
, list
) {
1065 ret
= helper_kernctl_get_mmap_len(mmap_info
->fd
, &mmap_len
);
1067 bt_list_del(&mmap_info
->list
);
1070 sem_post(&metadata_available
);
1073 pthread_join(timer_thread
, NULL
);
1075 pthread_join(display_thread
, NULL
);
1077 lttng_stop_tracing("test");
1078 lttng_destroy_session("test");
1084 bt_ctx
= bt_context_create();
1085 ret
= bt_context_add_traces_recursive(bt_ctx
, opt_input_path
, "ctf", NULL
);
1087 fprintf(stderr
, "[error] Opening the trace\n");
1091 ret
= check_requirements(bt_ctx
);
1093 fprintf(stderr
, "[error] some mandatory contexts were missing, exiting.\n");
1096 pthread_create(&display_thread
, NULL
, ncurses_display
, (void *) NULL
);
1097 pthread_create(&timer_thread
, NULL
, refresh_thread
, (void *) NULL
);
1101 pthread_join(display_thread
, NULL
);
1103 pthread_join(timer_thread
, NULL
);
1108 bt_context_put(bt_ctx
);