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 * hook on each event to check the timestamp and refresh the display if
139 enum bt_cb_ret
check_timestamp(struct bt_ctf_event
*call_data
, void *private_data
)
141 unsigned long timestamp
;
143 timestamp
= bt_ctf_get_timestamp(call_data
);
144 if (timestamp
== -1ULL)
147 if (last_display_update
== 0)
148 last_display_update
= timestamp
;
150 if (timestamp
- last_display_update
>= refresh_display
) {
151 sem_wait(&goodtoupdate
);
152 g_ptr_array_add(copies
, get_copy_lttngtop(last_display_update
,
154 sem_post(&goodtodisplay
);
155 sem_post(&bootstrap
);
156 last_display_update
= timestamp
;
161 fprintf(stderr
, "check_timestamp callback error\n");
162 return BT_CB_ERROR_STOP
;
166 * get_perf_counter : get or create and return a perf_counter struct for
167 * either a process or a cpu (only one of the 2 parameters mandatory)
169 struct perfcounter
*get_perf_counter(const char *name
, struct processtop
*proc
,
172 struct perfcounter
*ret
;
182 ret
= g_hash_table_lookup(table
, (gpointer
) name
);
186 ret
= g_new0(struct perfcounter
, 1);
187 /* by default, make it visible in the UI */
189 g_hash_table_insert(table
, (gpointer
) strdup(name
), ret
);
198 void update_perf_value(struct processtop
*proc
, struct cputime
*cpu
,
199 const char *name
, int value
)
201 struct perfcounter
*cpu_perf
, *process_perf
;
203 cpu_perf
= get_perf_counter(name
, NULL
, cpu
);
204 if (cpu_perf
->count
< value
) {
205 process_perf
= get_perf_counter(name
, proc
, NULL
);
206 process_perf
->count
+= value
- cpu_perf
->count
;
207 cpu_perf
->count
= value
;
211 void extract_perf_counter_scope(const struct bt_ctf_event
*event
,
212 const struct bt_definition
*scope
,
213 struct processtop
*proc
,
216 struct bt_definition
const * const *list
= NULL
;
217 const struct bt_definition
*field
;
219 struct perfcounter
*perfcounter
;
227 ret
= bt_ctf_get_field_list(event
, scope
, &list
, &count
);
234 g_hash_table_iter_init(&iter
, global_perf_liszt
);
235 while (g_hash_table_iter_next (&iter
, &key
, (gpointer
) &perfcounter
)) {
236 field
= bt_ctf_get_field(event
, scope
, (char *) key
);
238 int value
= bt_ctf_get_uint64(field
);
239 if (bt_ctf_field_get_error())
241 update_perf_value(proc
, cpu
, (char *) key
, value
);
249 void update_perf_counter(struct processtop
*proc
, const struct bt_ctf_event
*event
)
252 const struct bt_definition
*scope
;
254 cpu
= get_cpu(get_cpu_id(event
));
256 scope
= bt_ctf_get_top_level_scope(event
, BT_STREAM_EVENT_CONTEXT
);
257 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
259 scope
= bt_ctf_get_top_level_scope(event
, BT_STREAM_PACKET_CONTEXT
);
260 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
262 scope
= bt_ctf_get_top_level_scope(event
, BT_EVENT_CONTEXT
);
263 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
266 enum bt_cb_ret
fix_process_table(struct bt_ctf_event
*call_data
,
271 struct processtop
*parent
, *child
;
272 unsigned long timestamp
;
274 timestamp
= bt_ctf_get_timestamp(call_data
);
275 if (timestamp
== -1ULL)
278 pid
= get_context_pid(call_data
);
282 tid
= get_context_tid(call_data
);
286 ppid
= get_context_ppid(call_data
);
290 comm
= get_context_comm(call_data
);
295 /* find or create the current process */
296 child
= find_process_tid(<tngtop
, tid
, comm
);
298 child
= add_proc(<tngtop
, tid
, comm
, timestamp
);
299 update_proc(child
, pid
, tid
, ppid
, comm
);
302 /* find or create the parent */
303 parent
= find_process_tid(<tngtop
, pid
, comm
);
305 parent
= add_proc(<tngtop
, pid
, comm
, timestamp
);
309 /* attach the parent to the current process */
310 child
->threadparent
= parent
;
311 add_thread(parent
, child
);
314 update_perf_counter(child
, call_data
);
319 return BT_CB_ERROR_STOP
;
324 copies
= g_ptr_array_new();
325 global_perf_liszt
= g_hash_table_new(g_str_hash
, g_str_equal
);
327 sem_init(&goodtodisplay
, 0, 0);
328 sem_init(&goodtoupdate
, 0, 1);
329 sem_init(&timer
, 0, 1);
330 sem_init(&bootstrap
, 0, 0);
331 sem_init(&pause_sem
, 0, 1);
332 sem_init(&end_trace_sem
, 0, 0);
334 reset_global_counters();
336 lttngtop
.nbthreads
= 0;
337 lttngtop
.nbfiles
= 0;
339 lttngtop
.process_table
= g_ptr_array_new();
340 lttngtop
.files_table
= g_ptr_array_new();
341 lttngtop
.cpu_table
= g_ptr_array_new();
346 fprintf(fp
, "LTTngTop %s\n\n", VERSION
);
347 fprintf(fp
, "Usage : lttngtop /path/to/trace\n");
351 * Return 0 if caller should continue, < 0 if caller should return
352 * error, > 0 if caller should exit without reporting error.
354 static int parse_options(int argc
, char **argv
)
359 pc
= poptGetContext(NULL
, argc
, (const char **) argv
, long_options
, 0);
360 poptReadDefaultConfig(pc
, 0);
362 while ((opt
= poptGetNextOpt(pc
)) != -1) {
366 ret
= 1; /* exit cleanly */
374 opt_input_path
= poptGetArg(pc
);
383 void iter_trace(struct bt_context
*bt_ctx
)
385 struct bt_ctf_iter
*iter
;
386 struct bt_iter_pos begin_pos
;
387 const struct bt_ctf_event
*event
;
390 begin_pos
.type
= BT_SEEK_BEGIN
;
391 iter
= bt_ctf_iter_create(bt_ctx
, &begin_pos
, NULL
);
393 /* at each event check if we need to refresh */
394 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
397 /* at each event, verify the status of the process table */
398 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
401 /* to handle the scheduling events */
402 bt_ctf_iter_add_callback(iter
,
403 g_quark_from_static_string("sched_switch"),
404 NULL
, 0, handle_sched_switch
, NULL
, NULL
, NULL
);
405 /* to clean up the process table */
406 bt_ctf_iter_add_callback(iter
,
407 g_quark_from_static_string("sched_process_free"),
408 NULL
, 0, handle_sched_process_free
, NULL
, NULL
, NULL
);
409 /* to get all the process from the statedumps */
410 bt_ctf_iter_add_callback(iter
,
411 g_quark_from_static_string(
412 "lttng_statedump_process_state"),
413 NULL
, 0, handle_statedump_process_state
,
417 bt_ctf_iter_add_callback(iter
,
418 g_quark_from_static_string("exit_syscall"),
419 NULL
, 0, handle_exit_syscall
, NULL
, NULL
, NULL
);
420 bt_ctf_iter_add_callback(iter
,
421 g_quark_from_static_string("sys_write"),
422 NULL
, 0, handle_sys_write
, NULL
, NULL
, NULL
);
423 bt_ctf_iter_add_callback(iter
,
424 g_quark_from_static_string("sys_read"),
425 NULL
, 0, handle_sys_read
, NULL
, NULL
, NULL
);
426 bt_ctf_iter_add_callback(iter
,
427 g_quark_from_static_string("sys_open"),
428 NULL
, 0, handle_sys_open
, NULL
, NULL
, NULL
);
429 bt_ctf_iter_add_callback(iter
,
430 g_quark_from_static_string("sys_close"),
431 NULL
, 0, handle_sys_close
, NULL
, NULL
, NULL
);
432 bt_ctf_iter_add_callback(iter
,
433 g_quark_from_static_string(
434 "lttng_statedump_file_descriptor"),
435 NULL
, 0, handle_statedump_file_descriptor
,
438 while ((event
= bt_ctf_iter_read_event(iter
)) != NULL
) {
439 ret
= bt_iter_next(bt_ctf_get_iter(iter
));
444 /* block until quit, we reached the end of the trace */
445 sem_wait(&end_trace_sem
);
448 bt_ctf_iter_destroy(iter
);
452 * bt_context_add_traces_recursive: Open a trace recursively
453 * (copied from BSD code in converter/babeltrace.c)
455 * Find each trace present in the subdirectory starting from the given
456 * path, and add them to the context. The packet_seek parameter can be
457 * NULL: this specify to use the default format packet_seek.
459 * Return: 0 on success, nonzero on failure.
460 * Unable to open toplevel: failure.
461 * Unable to open some subdirectory or file: warn and continue;
463 int bt_context_add_traces_recursive(struct bt_context
*ctx
, const char *path
,
464 const char *format_str
,
465 void (*packet_seek
)(struct bt_stream_pos
*pos
,
466 size_t offset
, int whence
))
471 char lpath
[PATH_MAX
];
472 char * const paths
[2] = { lpath
, NULL
};
476 * Need to copy path, because fts_open can change it.
477 * It is the pointer array, not the strings, that are constant.
479 strncpy(lpath
, path
, PATH_MAX
);
480 lpath
[PATH_MAX
- 1] = '\0';
482 tree
= fts_open(paths
, FTS_NOCHDIR
| FTS_LOGICAL
, 0);
484 fprintf(stderr
, "[error] [Context] Cannot traverse \"%s\" for reading.\n",
489 trace_ids
= g_array_new(FALSE
, TRUE
, sizeof(int));
491 while ((node
= fts_read(tree
))) {
494 if (!(node
->fts_info
& FTS_D
))
497 dirfd
= open(node
->fts_accpath
, 0);
499 fprintf(stderr
, "[error] [Context] Unable to open trace "
500 "directory file descriptor.\n");
504 metafd
= openat(dirfd
, "metadata", O_RDONLY
);
523 trace_id
= bt_context_add_trace(ctx
,
524 node
->fts_accpath
, format_str
,
525 packet_seek
, NULL
, NULL
);
527 fprintf(stderr
, "[warning] [Context] opening trace \"%s\" from %s "
528 "for reading.\n", node
->fts_accpath
, path
);
529 /* Allow to skip erroneous traces. */
532 g_array_append_val(trace_ids
, trace_id
);
536 g_array_free(trace_ids
, TRUE
);
543 static int check_field_requirements(const struct bt_ctf_field_decl
*const * field_list
,
544 int field_cnt
, int *tid_check
, int *pid_check
,
545 int *procname_check
, int *ppid_check
)
548 struct perfcounter
*global
;
551 for (j
= 0; j
< field_cnt
; j
++) {
552 name
= bt_ctf_get_decl_field_name(field_list
[j
]);
553 if (*tid_check
== 0) {
554 if (strncmp(name
, "tid", 3) == 0)
557 if (*pid_check
== 0) {
558 if (strncmp(name
, "pid", 3) == 0)
561 if (*ppid_check
== 0) {
562 if (strncmp(name
, "ppid", 4) == 0)
565 if (*procname_check
== 0) {
566 if (strncmp(name
, "procname", 8) == 0)
569 if (strncmp(name
, "perf_", 5) == 0) {
570 global
= g_hash_table_lookup(global_perf_liszt
, (gpointer
) name
);
572 global
= g_new0(struct perfcounter
, 1);
573 /* by default, sort on the first perf context */
574 if (g_hash_table_size(global_perf_liszt
) == 0)
577 g_hash_table_insert(global_perf_liszt
, (gpointer
) strdup(name
), global
);
582 if (*tid_check
== 1 && *pid_check
== 1 && *ppid_check
== 1 &&
583 *procname_check
== 1)
590 * check_requirements: check if the required context informations are available
592 * If each mandatory context information is available for at least in one
593 * event, return 0 otherwise return -1.
595 int check_requirements(struct bt_context
*ctx
)
597 unsigned int i
, evt_cnt
, field_cnt
;
598 struct bt_ctf_event_decl
*const * evt_list
;
599 const struct bt_ctf_field_decl
*const * field_list
;
602 int procname_check
= 0;
606 bt_ctf_get_event_decl_list(0, ctx
, &evt_list
, &evt_cnt
);
607 for (i
= 0; i
< evt_cnt
; i
++) {
608 bt_ctf_get_decl_fields(evt_list
[i
], BT_STREAM_EVENT_CONTEXT
,
609 &field_list
, &field_cnt
);
610 ret
= check_field_requirements(field_list
, field_cnt
,
611 &tid_check
, &pid_check
, &procname_check
,
614 bt_ctf_get_decl_fields(evt_list
[i
], BT_EVENT_CONTEXT
,
615 &field_list
, &field_cnt
);
616 ret
= check_field_requirements(field_list
, field_cnt
,
617 &tid_check
, &pid_check
, &procname_check
,
620 bt_ctf_get_decl_fields(evt_list
[i
], BT_STREAM_PACKET_CONTEXT
,
621 &field_list
, &field_cnt
);
622 ret
= check_field_requirements(field_list
, field_cnt
,
623 &tid_check
, &pid_check
, &procname_check
,
627 if (tid_check
== 0) {
629 fprintf(stderr
, "[error] missing tid context information\n");
631 if (pid_check
== 0) {
633 fprintf(stderr
, "[error] missing pid context information\n");
635 if (ppid_check
== 0) {
637 fprintf(stderr
, "[error] missing ppid context information\n");
639 if (procname_check
== 0) {
641 fprintf(stderr
, "[error] missing procname context information\n");
650 struct lttng_consumer_stream
*iter
;
652 struct mmap_stream
*new_snapshot
;
657 * try lock mutex ressource courante (overrun)
659 * stop trace (flush implicite avant stop)
660 * lttng_consumer_take_snapshot
661 * read timestamp packet end (use time as end pos)
662 * - stream_packet_context
663 * - reculer de 1 subbuf : pos - max_subbuff_size
665 * - position de fin (take_snapshot)
666 * - mov_pos_slow ( fin - max_subbuff_size) lire timestamp packet end
667 * - prend min(end) (activité sur tous les streams)
673 helper_kernctl_buffer_flush(consumerd_metadata
);
674 for (i
= 0; i
< lttng_consumer_stream_array
->len
; i
++) {
675 iter
= g_ptr_array_index(lttng_consumer_stream_array
, i
);
676 helper_kernctl_buffer_flush(helper_get_lttng_consumer_stream_wait_fd(iter
));
677 printf("Taking snapshot of fd : %d\n", helper_get_lttng_consumer_stream_wait_fd(iter
));
678 ret
= helper_lttng_consumer_take_snapshot(ctx
, iter
);
681 perror("lttng_consumer_take_snapshots");
685 for (i
= 0; i
< lttng_consumer_stream_array
->len
; i
++) {
686 iter
= g_ptr_array_index(lttng_consumer_stream_array
, i
);
687 ret
= helper_lttng_consumer_get_produced_snapshot(ctx
, iter
, &spos
);
690 perror("helper_lttng_consumer_get_produced_snapshot");
693 while (helper_get_lttng_consumer_stream_wait_last_pos(iter
) < spos
) {
694 new_snapshot
= g_new0(struct mmap_stream
, 1);
695 new_snapshot
->fd
= helper_get_lttng_consumer_stream_wait_fd(iter
);
696 new_snapshot
->last_pos
= helper_get_lttng_consumer_stream_wait_last_pos(iter
);
697 fprintf(stderr
,"ADDING AVAILABLE SNAPSHOT ON FD %d AT POSITION %lu\n",
699 new_snapshot
->last_pos
);
700 g_ptr_array_add(available_snapshots
, new_snapshot
);
701 helper_set_lttng_consumer_stream_wait_last_pos(iter
,
702 helper_get_lttng_consumer_stream_wait_last_pos(iter
) +
703 helper_get_lttng_consumer_stream_chan_max_sb_size(iter
));
707 if (!metadata_ready
) {
708 fprintf(stderr
, "BLOCKING BEFORE METADATA\n");
709 sem_wait(&metadata_available
);
710 fprintf(stderr
,"OPENING TRACE\n");
711 if (access("/tmp/livesession/kernel/metadata", F_OK
) != 0) {
712 fprintf(stderr
,"NO METADATA FILE, SKIPPING\n");
716 metadata_fp
= fopen("/tmp/livesession/kernel/metadata", "r");
725 ssize_t
read_subbuffer(struct lttng_consumer_stream
*kconsumerd_fd
,
726 struct lttng_consumer_local_data
*ctx
)
731 int infd
= helper_get_lttng_consumer_stream_wait_fd(kconsumerd_fd
);
733 if (helper_get_lttng_consumer_stream_output(kconsumerd_fd
) == LTTNG_EVENT_SPLICE
) {
734 /* Get the next subbuffer */
735 printf("get_next : %d\n", infd
);
736 err
= helper_kernctl_get_next_subbuf(infd
);
739 perror("Reserving sub buffer failed (everything is normal, "
740 "it is due to concurrency)");
743 /* read the whole subbuffer */
744 err
= helper_kernctl_get_padded_subbuf_size(infd
, &len
);
747 perror("Getting sub-buffer len failed.");
750 printf("len : %ld\n", len
);
752 /* splice the subbuffer to the tracefile */
753 ret
= helper_lttng_consumer_on_read_subbuffer_splice(ctx
, kconsumerd_fd
, len
);
756 * display the error but continue processing to try
757 * to release the subbuffer
759 fprintf(stderr
,"Error splicing to tracefile\n");
761 printf("ret : %ld\n", ret
);
762 printf("put_next : %d\n", infd
);
763 err
= helper_kernctl_put_next_subbuf(infd
);
766 perror("Reserving sub buffer failed (everything is normal, "
767 "it is due to concurrency)");
770 sem_post(&metadata_available
);
777 int on_update_fd(int key
, uint32_t state
)
779 /* let the lib handle the metadata FD */
780 if (key
== sessiond_metadata
)
785 int on_recv_fd(struct lttng_consumer_stream
*kconsumerd_fd
)
788 struct mmap_stream
*new_info
;
791 /* Opening the tracefile in write mode */
792 if (helper_get_lttng_consumer_stream_path_name(kconsumerd_fd
) != NULL
) {
793 ret
= open(helper_get_lttng_consumer_stream_path_name(kconsumerd_fd
),
794 O_WRONLY
|O_CREAT
|O_TRUNC
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
799 helper_set_lttng_consumer_stream_out_fd(kconsumerd_fd
, ret
);
802 if (helper_get_lttng_consumer_stream_output(kconsumerd_fd
) == LTTNG_EVENT_MMAP
) {
803 new_info
= malloc(sizeof(struct mmap_stream
));
804 new_info
->fd
= helper_get_lttng_consumer_stream_wait_fd(kconsumerd_fd
);
805 bt_list_add(&new_info
->list
, &mmap_list
.head
);
807 /* get the len of the mmap region */
808 ret
= helper_kernctl_get_mmap_len(helper_get_lttng_consumer_stream_wait_fd(kconsumerd_fd
),
812 perror("helper_kernctl_get_mmap_len");
815 helper_set_lttng_consumer_stream_mmap_len(kconsumerd_fd
, tmp_mmap_len
);
817 helper_set_lttng_consumer_stream_mmap_base(kconsumerd_fd
,
818 mmap(NULL
, helper_get_lttng_consumer_stream_mmap_len(kconsumerd_fd
),
819 PROT_READ
, MAP_PRIVATE
, helper_get_lttng_consumer_stream_wait_fd(kconsumerd_fd
), 0));
820 if (helper_get_lttng_consumer_stream_mmap_base(kconsumerd_fd
) == MAP_FAILED
) {
821 perror("Error mmaping");
826 g_ptr_array_add(lttng_consumer_stream_array
, kconsumerd_fd
);
829 consumerd_metadata
= helper_get_lttng_consumer_stream_wait_fd(kconsumerd_fd
);
830 sessiond_metadata
= helper_get_lttng_consumer_stream_key(kconsumerd_fd
);
840 struct bt_context
*bt_ctx
= NULL
;
846 if (!metadata_ready
) {
847 fprintf(stderr
, "BLOCKING BEFORE METADATA\n");
848 sem_wait(&metadata_available
);
849 fprintf(stderr
,"OPENING TRACE\n");
850 if (access("/tmp/livesession/kernel/metadata", F_OK
) != 0) {
851 fprintf(stderr
,"NO METADATA FILE, SKIPPING\n");
855 metadata_fp
= fopen("/tmp/livesession/kernel/metadata", "r");
859 bt_ctx
= bt_context_create();
860 ret
= bt_context_add_trace(bt_ctx
, NULL
, "ctf",
861 lttngtop_ctf_packet_seek
, &mmap_list
, metadata_fp
);
863 printf("Error adding trace\n");
873 int setup_consumer(char *command_sock_path
, pthread_t
*threads
,
874 struct lttng_consumer_local_data
*ctx
)
878 ctx
= helper_lttng_consumer_create(HELPER_LTTNG_CONSUMER_KERNEL
,
879 read_subbuffer
, NULL
, on_recv_fd
, on_update_fd
);
883 unlink(command_sock_path
);
884 helper_lttng_consumer_set_command_sock_path(ctx
, command_sock_path
);
885 helper_lttng_consumer_init();
887 /* Create the thread to manage the receive of fd */
888 ret
= pthread_create(&threads
[0], NULL
, helper_lttng_consumer_thread_receive_fds
,
891 perror("pthread_create receive fd");
894 /* Create thread to manage the polling/writing of traces */
895 ret
= pthread_create(&threads
[1], NULL
, helper_lttng_consumer_thread_poll_fds
,
898 perror("pthread_create poll fd");
906 void *setup_live_tracing()
908 struct lttng_domain dom
;
909 struct lttng_channel chan
;
910 char *channel_name
= "mmapchan";
911 struct lttng_event ev
;
913 char *command_sock_path
= "/tmp/consumerd_sock";
914 static pthread_t threads
[2]; /* recv_fd, poll */
915 struct lttng_event_context kctxpid
, kctxcomm
, kctxppid
, kctxtid
;
917 struct lttng_handle
*handle
;
919 BT_INIT_LIST_HEAD(&mmap_list
.head
);
921 lttng_consumer_stream_array
= g_ptr_array_new();
923 if ((ret
= setup_consumer(command_sock_path
, threads
, ctx
)) < 0) {
924 fprintf(stderr
,"error setting up consumer\n");
928 available_snapshots
= g_ptr_array_new();
930 /* setup the session */
931 dom
.type
= LTTNG_DOMAIN_KERNEL
;
933 ret
= system("rm -rf /tmp/livesession");
935 if ((ret
= lttng_create_session("test", "/tmp/livesession")) < 0) {
936 fprintf(stderr
,"error creating the session : %s\n",
937 helper_lttcomm_get_readable_code(ret
));
941 if ((handle
= lttng_create_handle("test", &dom
)) == NULL
) {
942 fprintf(stderr
,"error creating handle\n");
946 if ((ret
= lttng_register_consumer(handle
, command_sock_path
)) < 0) {
947 fprintf(stderr
,"error registering consumer : %s\n",
948 helper_lttcomm_get_readable_code(ret
));
952 strcpy(chan
.name
, channel_name
);
953 chan
.attr
.overwrite
= 0;
954 chan
.attr
.subbuf_size
= 32768;
955 // chan.attr.subbuf_size = 1048576; /* 1MB */
956 chan
.attr
.num_subbuf
= 4;
957 chan
.attr
.switch_timer_interval
= 0;
958 chan
.attr
.read_timer_interval
= 200;
959 chan
.attr
.output
= LTTNG_EVENT_MMAP
;
961 if ((ret
= lttng_enable_channel(handle
, &chan
)) < 0) {
962 fprintf(stderr
,"error creating channel : %s\n", helper_lttcomm_get_readable_code(ret
));
966 sprintf(ev
.name
, "sched_switch");
967 ev
.type
= LTTNG_EVENT_TRACEPOINT
;
969 //if ((ret = lttng_enable_event(handle, NULL, channel_name)) < 0) {
970 if ((ret
= lttng_enable_event(handle
, &ev
, channel_name
)) < 0) {
971 fprintf(stderr
,"error enabling event : %s\n", helper_lttcomm_get_readable_code(ret
));
975 kctxpid
.ctx
= LTTNG_EVENT_CONTEXT_PID
;
976 lttng_add_context(handle
, &kctxpid
, NULL
, NULL
);
977 kctxppid
.ctx
= LTTNG_EVENT_CONTEXT_PPID
;
978 lttng_add_context(handle
, &kctxppid
, NULL
, NULL
);
979 kctxcomm
.ctx
= LTTNG_EVENT_CONTEXT_PROCNAME
;
980 lttng_add_context(handle
, &kctxcomm
, NULL
, NULL
);
981 kctxtid
.ctx
= LTTNG_EVENT_CONTEXT_TID
;
982 lttng_add_context(handle
, &kctxtid
, NULL
, NULL
);
984 if ((ret
= lttng_start_tracing("test")) < 0) {
985 fprintf(stderr
,"error starting tracing : %s\n", helper_lttcomm_get_readable_code(ret
));
989 helper_kernctl_buffer_flush(consumerd_metadata
);
991 /* Create thread to manage the polling/writing of traces */
992 ret
= pthread_create(&thread_live_consume
, NULL
, live_consume
, NULL
);
994 perror("pthread_create");
998 // pthread_cancel(live_trace_thread);
1000 /* block until metadata is ready */
1001 sem_init(&metadata_available
, 0, 0);
1009 int main(int argc
, char **argv
)
1012 struct bt_context
*bt_ctx
= NULL
;
1014 ret
= parse_options(argc
, argv
);
1016 fprintf(stdout
, "Error parsing options.\n\n");
1019 } else if (ret
> 0) {
1023 if (!opt_input_path
) {
1024 printf("live tracing enabled\n");
1025 pthread_create(&live_trace_thread
, NULL
, setup_live_tracing
, (void *) NULL
);
1027 printf("STOPPING\n");
1028 lttng_stop_tracing("test");
1029 printf("DESTROYING\n");
1030 lttng_destroy_session("test");
1032 printf("CANCELLING\n");
1033 pthread_cancel(live_trace_thread
);
1038 bt_ctx
= bt_context_create();
1039 ret
= bt_context_add_traces_recursive(bt_ctx
, opt_input_path
, "ctf", NULL
);
1041 fprintf(stderr
, "[error] Opening the trace\n");
1045 ret
= check_requirements(bt_ctx
);
1047 fprintf(stderr
, "[error] some mandatory contexts were missing, exiting.\n");
1050 pthread_create(&display_thread
, NULL
, ncurses_display
, (void *) NULL
);
1051 pthread_create(&timer_thread
, NULL
, refresh_thread
, (void *) NULL
);
1056 pthread_join(display_thread
, NULL
);
1057 pthread_join(timer_thread
, NULL
);
1062 bt_context_put(bt_ctx
);