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
);
159 void print_fields(struct bt_ctf_event
*event
)
162 const struct definition
*const * list
;
163 const struct definition
*scope
;
164 enum ctf_type_id type
;
167 scope
= bt_ctf_get_top_level_scope(event
, BT_EVENT_FIELDS
);
169 bt_ctf_get_field_list(event
, scope
, &list
, &cnt
);
170 for (i
= 0; i
< cnt
; i
++) {
173 printf("%s = ", bt_ctf_field_name(list
[i
]));
174 type
= bt_ctf_field_type(list
[i
]);
175 if (type
== CTF_TYPE_INTEGER
) {
176 if (bt_ctf_get_int_signedness(list
[i
]) == 0)
177 printf("%" PRIu64
"", bt_ctf_get_uint64(list
[i
]));
179 printf("%" PRId64
"", bt_ctf_get_int64(list
[i
]));
180 } else if (type
== CTF_TYPE_STRING
) {
181 printf("%s", bt_ctf_get_string(list
[i
]));
182 } else if (type
== CTF_TYPE_ARRAY
) {
183 str
= bt_ctf_get_char_array(list
[i
]);
191 * hook on each event to check the timestamp and refresh the display if
194 enum bt_cb_ret
print_timestamp(struct bt_ctf_event
*call_data
, void *private_data
)
196 unsigned long timestamp
;
198 uint64_t ts_nsec_start
;
201 const struct definition
*scope
;
202 const char *hostname
, *procname
;
204 timestamp
= bt_ctf_get_timestamp(call_data
);
206 start
= format_timestamp(timestamp
);
207 ts_nsec_start
= timestamp
% NSEC_PER_SEC
;
209 pid
= get_context_pid(call_data
);
210 if (pid
== -1ULL && opt_tid
) {
214 hostname
= get_context_hostname(call_data
);
215 if (!opt_tid
&& (opt_hostname
&& !lookup_hostname_list(hostname
)))
217 if (!opt_hostname
&& (opt_tid
&& !lookup_tid_list(pid
)))
219 if ((opt_tid
&& !lookup_tid_list(pid
)) &&
220 (opt_hostname
&& !lookup_hostname_list(hostname
)))
223 cpu_id
= get_cpu_id(call_data
);
224 procname
= get_context_comm(call_data
);
226 if (strcmp(bt_ctf_event_name(call_data
), "exit_syscall") == 0) {
227 scope
= bt_ctf_get_top_level_scope(call_data
,
229 syscall_ret
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
231 printf("= %ld\n", syscall_ret
);
232 last_textdump_print_newline
= 1;
234 /* we might have lost the exit_syscall event, so need to
235 * print the newline in this case */
236 if (last_textdump_print_newline
== 0)
238 printf("%02d:%02d:%02d.%09" PRIu64
" (%s) (cpu %d) [%s (%d)] %s (",
239 start
.tm_hour
, start
.tm_min
, start
.tm_sec
,
240 ts_nsec_start
, hostname
, cpu_id
, procname
, pid
,
241 bt_ctf_event_name(call_data
));
242 print_fields(call_data
);
245 if (strncmp(bt_ctf_event_name(call_data), "sys_", 4) == 0) {
250 last_textdump_print_newline
= 0;
256 return BT_CB_ERROR_STOP
;
260 * hook on each event to check the timestamp and refresh the display if
263 enum bt_cb_ret
check_timestamp(struct bt_ctf_event
*call_data
, void *private_data
)
265 unsigned long timestamp
;
267 timestamp
= bt_ctf_get_timestamp(call_data
);
268 if (timestamp
== -1ULL)
271 if (last_display_update
== 0)
272 last_display_update
= timestamp
;
274 if (timestamp
- last_display_update
>= refresh_display
) {
275 sem_wait(&goodtoupdate
);
276 g_ptr_array_add(copies
, get_copy_lttngtop(last_display_update
,
278 sem_post(&goodtodisplay
);
279 sem_post(&bootstrap
);
280 last_display_update
= timestamp
;
285 fprintf(stderr
, "check_timestamp callback error\n");
286 return BT_CB_ERROR_STOP
;
290 * get_perf_counter : get or create and return a perf_counter struct for
291 * either a process or a cpu (only one of the 2 parameters mandatory)
293 struct perfcounter
*get_perf_counter(const char *name
, struct processtop
*proc
,
296 struct perfcounter
*ret
;
306 ret
= g_hash_table_lookup(table
, (gpointer
) name
);
310 ret
= g_new0(struct perfcounter
, 1);
311 /* by default, make it visible in the UI */
313 g_hash_table_insert(table
, (gpointer
) strdup(name
), ret
);
322 void update_perf_value(struct processtop
*proc
, struct cputime
*cpu
,
323 const char *name
, int value
)
325 struct perfcounter
*cpu_perf
, *process_perf
;
327 cpu_perf
= get_perf_counter(name
, NULL
, cpu
);
328 if (cpu_perf
->count
< value
) {
329 process_perf
= get_perf_counter(name
, proc
, NULL
);
330 process_perf
->count
+= value
- cpu_perf
->count
;
331 cpu_perf
->count
= value
;
335 void extract_perf_counter_scope(const struct bt_ctf_event
*event
,
336 const struct definition
*scope
,
337 struct processtop
*proc
,
340 struct definition
const * const *list
= NULL
;
341 const struct definition
*field
;
343 struct perfcounter
*perfcounter
;
351 ret
= bt_ctf_get_field_list(event
, scope
, &list
, &count
);
358 g_hash_table_iter_init(&iter
, global_perf_liszt
);
359 while (g_hash_table_iter_next (&iter
, &key
, (gpointer
) &perfcounter
)) {
360 field
= bt_ctf_get_field(event
, scope
, (char *) key
);
362 int value
= bt_ctf_get_uint64(field
);
363 if (bt_ctf_field_get_error())
365 update_perf_value(proc
, cpu
, (char *) key
, value
);
373 void update_perf_counter(struct processtop
*proc
, const struct bt_ctf_event
*event
)
376 const struct definition
*scope
;
378 cpu
= get_cpu(get_cpu_id(event
));
380 scope
= bt_ctf_get_top_level_scope(event
, BT_STREAM_EVENT_CONTEXT
);
381 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
383 scope
= bt_ctf_get_top_level_scope(event
, BT_STREAM_PACKET_CONTEXT
);
384 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
386 scope
= bt_ctf_get_top_level_scope(event
, BT_EVENT_CONTEXT
);
387 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
390 enum bt_cb_ret
fix_process_table(struct bt_ctf_event
*call_data
,
393 int pid
, tid
, ppid
, vpid
, vtid
, vppid
;
394 char *comm
, *hostname
;
395 struct processtop
*parent
, *child
;
396 unsigned long timestamp
;
398 timestamp
= bt_ctf_get_timestamp(call_data
);
399 if (timestamp
== -1ULL)
402 pid
= get_context_pid(call_data
);
407 tid
= get_context_tid(call_data
);
411 ppid
= get_context_ppid(call_data
);
415 vpid
= get_context_vpid(call_data
);
419 vtid
= get_context_vtid(call_data
);
423 vppid
= get_context_vppid(call_data
);
427 comm
= get_context_comm(call_data
);
432 hostname
= get_context_hostname(call_data
);
434 /* find or create the current process */
435 child
= find_process_tid(<tngtop
, tid
, comm
);
437 child
= add_proc(<tngtop
, tid
, comm
, timestamp
);
440 update_proc(child
, pid
, tid
, ppid
, vpid
, vtid
, vppid
, comm
, hostname
);
443 /* find or create the parent */
444 parent
= find_process_tid(<tngtop
, pid
, comm
);
446 parent
= add_proc(<tngtop
, pid
, comm
, timestamp
);
451 /* attach the parent to the current process */
452 child
->threadparent
= parent
;
453 add_thread(parent
, child
);
456 update_perf_counter(child
, call_data
);
462 return BT_CB_ERROR_STOP
;
467 copies
= g_ptr_array_new();
468 global_perf_liszt
= g_hash_table_new(g_str_hash
, g_str_equal
);
470 sem_init(&goodtodisplay
, 0, 0);
471 sem_init(&goodtoupdate
, 0, 1);
472 sem_init(&timer
, 0, 1);
473 sem_init(&bootstrap
, 0, 0);
474 sem_init(&pause_sem
, 0, 1);
475 sem_init(&end_trace_sem
, 0, 0);
477 reset_global_counters();
479 lttngtop
.nbthreads
= 0;
480 lttngtop
.nbfiles
= 0;
482 lttngtop
.process_hash_table
= g_hash_table_new(g_direct_hash
,
484 lttngtop
.process_table
= g_ptr_array_new();
485 lttngtop
.files_table
= g_ptr_array_new();
486 lttngtop
.cpu_table
= g_ptr_array_new();
491 fprintf(fp
, "LTTngTop %s\n\n", VERSION
);
492 fprintf(fp
, "Usage : lttngtop [OPTIONS] [TRACE]\n");
493 fprintf(fp
, " TRACE Path to the trace to analyse (no trace path for live tracing)\n");
494 fprintf(fp
, " -h, --help This help message\n");
495 fprintf(fp
, " -t, --textdump Display live events in text-only\n");
496 fprintf(fp
, " -p, --pid Comma-separated list of PIDs to display\n");
497 fprintf(fp
, " -f, --child Follow threads associated with selected PIDs\n");
498 fprintf(fp
, " -n, --hostname Comma-separated list of hostnames to display (require hostname context in trace)\n");
502 * Return 0 if caller should continue, < 0 if caller should return
503 * error, > 0 if caller should exit without reporting error.
505 static int parse_options(int argc
, char **argv
)
512 pc
= poptGetContext(NULL
, argc
, (const char **) argv
, long_options
, 0);
513 poptReadDefaultConfig(pc
, 0);
515 while ((opt
= poptGetNextOpt(pc
)) != -1) {
519 ret
= 1; /* exit cleanly */
529 tid_list
= g_hash_table_new(g_str_hash
,
531 tmp_str
= strtok(opt_tid
, ",");
533 tid
= malloc(sizeof(int));
534 *tid
= atoi(tmp_str
);
535 g_hash_table_insert(tid_list
,
536 (gpointer
) tid
, tid
);
537 tmp_str
= strtok(NULL
, ",");
541 hostname_list
= g_hash_table_new(g_str_hash
,
543 tmp_str
= strtok(opt_hostname
, ",");
545 char *new_str
= strdup(tmp_str
);
546 g_hash_table_insert(hostname_list
,
549 tmp_str
= strtok(NULL
, ",");
558 opt_input_path
= poptGetArg(pc
);
567 void iter_trace(struct bt_context
*bt_ctx
)
569 struct bt_ctf_iter
*iter
;
570 struct bt_iter_pos begin_pos
;
571 const struct bt_ctf_event
*event
;
574 begin_pos
.type
= BT_SEEK_BEGIN
;
575 iter
= bt_ctf_iter_create(bt_ctx
, &begin_pos
, NULL
);
578 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
582 /* at each event check if we need to refresh */
583 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
586 /* at each event, verify the status of the process table */
587 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
590 /* to handle the scheduling events */
591 bt_ctf_iter_add_callback(iter
,
592 g_quark_from_static_string("sched_switch"),
593 NULL
, 0, handle_sched_switch
, NULL
, NULL
, NULL
);
594 /* to clean up the process table */
595 bt_ctf_iter_add_callback(iter
,
596 g_quark_from_static_string("sched_process_free"),
597 NULL
, 0, handle_sched_process_free
, NULL
, NULL
, NULL
);
598 /* to get all the process from the statedumps */
599 bt_ctf_iter_add_callback(iter
,
600 g_quark_from_static_string(
601 "lttng_statedump_process_state"),
602 NULL
, 0, handle_statedump_process_state
,
606 bt_ctf_iter_add_callback(iter
,
607 g_quark_from_static_string("exit_syscall"),
608 NULL
, 0, handle_exit_syscall
, NULL
, NULL
, NULL
);
609 bt_ctf_iter_add_callback(iter
,
610 g_quark_from_static_string("sys_write"),
611 NULL
, 0, handle_sys_write
, NULL
, NULL
, NULL
);
612 bt_ctf_iter_add_callback(iter
,
613 g_quark_from_static_string("sys_read"),
614 NULL
, 0, handle_sys_read
, NULL
, NULL
, NULL
);
615 bt_ctf_iter_add_callback(iter
,
616 g_quark_from_static_string("sys_open"),
617 NULL
, 0, handle_sys_open
, NULL
, NULL
, NULL
);
618 bt_ctf_iter_add_callback(iter
,
619 g_quark_from_static_string("sys_close"),
620 NULL
, 0, handle_sys_close
, NULL
, NULL
, NULL
);
621 bt_ctf_iter_add_callback(iter
,
622 g_quark_from_static_string(
623 "lttng_statedump_file_descriptor"),
624 NULL
, 0, handle_statedump_file_descriptor
,
628 while ((event
= bt_ctf_iter_read_event(iter
, NULL
)) != NULL
) {
629 if (quit
|| reload_trace
)
631 ret
= bt_iter_next(bt_ctf_get_iter(iter
));
636 /* block until quit, we reached the end of the trace */
637 sem_wait(&end_trace_sem
);
640 bt_ctf_iter_destroy(iter
);
644 * bt_context_add_traces_recursive: Open a trace recursively
645 * (copied from BSD code in converter/babeltrace.c)
647 * Find each trace present in the subdirectory starting from the given
648 * path, and add them to the context. The packet_seek parameter can be
649 * NULL: this specify to use the default format packet_seek.
651 * Return: 0 on success, nonzero on failure.
652 * Unable to open toplevel: failure.
653 * Unable to open some subdirectory or file: warn and continue;
655 int bt_context_add_traces_recursive(struct bt_context
*ctx
, const char *path
,
656 const char *format_str
,
657 void (*packet_seek
)(struct stream_pos
*pos
,
658 size_t offset
, int whence
))
663 char lpath
[PATH_MAX
];
664 char * const paths
[2] = { lpath
, NULL
};
668 * Need to copy path, because fts_open can change it.
669 * It is the pointer array, not the strings, that are constant.
671 strncpy(lpath
, path
, PATH_MAX
);
672 lpath
[PATH_MAX
- 1] = '\0';
674 tree
= fts_open(paths
, FTS_NOCHDIR
| FTS_LOGICAL
, 0);
676 fprintf(stderr
, "[error] [Context] Cannot traverse \"%s\" for reading.\n",
681 trace_ids
= g_array_new(FALSE
, TRUE
, sizeof(int));
683 while ((node
= fts_read(tree
))) {
686 if (!(node
->fts_info
& FTS_D
))
689 dirfd
= open(node
->fts_accpath
, 0);
691 fprintf(stderr
, "[error] [Context] Unable to open trace "
692 "directory file descriptor.\n");
696 metafd
= openat(dirfd
, "metadata", O_RDONLY
);
715 trace_id
= bt_context_add_trace(ctx
,
716 node
->fts_accpath
, format_str
,
717 packet_seek
, NULL
, NULL
);
719 fprintf(stderr
, "[warning] [Context] opening trace \"%s\" from %s "
720 "for reading.\n", node
->fts_accpath
, path
);
721 /* Allow to skip erroneous traces. */
724 g_array_append_val(trace_ids
, trace_id
);
728 g_array_free(trace_ids
, TRUE
);
735 static int check_field_requirements(const struct bt_ctf_field_decl
*const * field_list
,
736 int field_cnt
, int *tid_check
, int *pid_check
,
737 int *procname_check
, int *ppid_check
)
740 struct perfcounter
*global
;
743 for (j
= 0; j
< field_cnt
; j
++) {
744 name
= bt_ctf_get_decl_field_name(field_list
[j
]);
745 if (*tid_check
== 0) {
746 if (strncmp(name
, "tid", 3) == 0)
749 if (*pid_check
== 0) {
750 if (strncmp(name
, "tid", 3) == 0)
753 if (*ppid_check
== 0) {
754 if (strncmp(name
, "ppid", 4) == 0)
757 if (*procname_check
== 0) {
758 if (strncmp(name
, "procname", 8) == 0)
761 if (strncmp(name
, "perf_", 5) == 0) {
762 global
= g_hash_table_lookup(global_perf_liszt
, (gpointer
) name
);
764 global
= g_new0(struct perfcounter
, 1);
765 /* by default, sort on the first perf context */
766 if (g_hash_table_size(global_perf_liszt
) == 0)
769 g_hash_table_insert(global_perf_liszt
, (gpointer
) strdup(name
), global
);
774 if (*tid_check
== 1 && *pid_check
== 1 && *ppid_check
== 1 &&
775 *procname_check
== 1)
782 * check_requirements: check if the required context informations are available
784 * If each mandatory context information is available for at least in one
785 * event, return 0 otherwise return -1.
787 int check_requirements(struct bt_context
*ctx
)
789 unsigned int i
, evt_cnt
, field_cnt
;
790 struct bt_ctf_event_decl
*const * evt_list
;
791 const struct bt_ctf_field_decl
*const * field_list
;
794 int procname_check
= 0;
798 bt_ctf_get_event_decl_list(0, ctx
, &evt_list
, &evt_cnt
);
799 for (i
= 0; i
< evt_cnt
; i
++) {
800 bt_ctf_get_decl_fields(evt_list
[i
], BT_STREAM_EVENT_CONTEXT
,
801 &field_list
, &field_cnt
);
802 ret
= check_field_requirements(field_list
, field_cnt
,
803 &tid_check
, &pid_check
, &procname_check
,
806 bt_ctf_get_decl_fields(evt_list
[i
], BT_EVENT_CONTEXT
,
807 &field_list
, &field_cnt
);
808 ret
= check_field_requirements(field_list
, field_cnt
,
809 &tid_check
, &pid_check
, &procname_check
,
812 bt_ctf_get_decl_fields(evt_list
[i
], BT_STREAM_PACKET_CONTEXT
,
813 &field_list
, &field_cnt
);
814 ret
= check_field_requirements(field_list
, field_cnt
,
815 &tid_check
, &pid_check
, &procname_check
,
819 if (tid_check
== 0) {
821 fprintf(stderr
, "[error] missing tid context information\n");
823 if (pid_check
== 0) {
825 fprintf(stderr
, "[error] missing pid context information\n");
827 if (ppid_check
== 0) {
829 fprintf(stderr
, "[error] missing ppid context information\n");
831 if (procname_check
== 0) {
833 fprintf(stderr
, "[error] missing procname context information\n");
839 ssize_t
read_subbuffer(struct lttng_consumer_stream
*kconsumerd_fd
,
840 struct lttng_consumer_local_data
*ctx
)
845 int infd
= helper_get_lttng_consumer_stream_wait_fd(kconsumerd_fd
);
847 if (helper_get_lttng_consumer_stream_output(kconsumerd_fd
) == LTTNG_EVENT_SPLICE
) {
848 /* Get the next subbuffer */
849 err
= helper_kernctl_get_next_subbuf(infd
);
852 perror("Reserving sub buffer failed (everything is normal, "
853 "it is due to concurrency)");
856 /* read the whole subbuffer */
857 err
= helper_kernctl_get_padded_subbuf_size(infd
, &len
);
860 perror("Getting sub-buffer len failed.");
864 /* splice the subbuffer to the tracefile */
865 ret
= helper_lttng_consumer_on_read_subbuffer_splice(ctx
, kconsumerd_fd
, len
);
868 * display the error but continue processing to try
869 * to release the subbuffer
871 fprintf(stderr
,"Error splicing to tracefile\n");
873 err
= helper_kernctl_put_next_subbuf(infd
);
876 perror("Reserving sub buffer failed (everything is normal, "
877 "it is due to concurrency)");
880 sem_post(&metadata_available
);
887 int on_update_fd(int key
, uint32_t state
)
889 /* let the lib handle the metadata FD */
890 if (key
== sessiond_metadata
)
895 int on_recv_fd(struct lttng_consumer_stream
*kconsumerd_fd
)
898 struct mmap_stream
*new_mmap_stream
;
900 /* Opening the tracefile in write mode */
901 if (helper_get_lttng_consumer_stream_path_name(kconsumerd_fd
) != NULL
) {
902 ret
= open(helper_get_lttng_consumer_stream_path_name(kconsumerd_fd
),
903 O_WRONLY
|O_CREAT
|O_TRUNC
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
908 helper_set_lttng_consumer_stream_out_fd(kconsumerd_fd
, ret
);
911 if (helper_get_lttng_consumer_stream_output(kconsumerd_fd
) == LTTNG_EVENT_MMAP
) {
912 new_mmap_stream
= malloc(sizeof(struct mmap_stream
));
913 new_mmap_stream
->fd
= helper_get_lttng_consumer_stream_wait_fd(
915 bt_list_add(&new_mmap_stream
->list
, &mmap_list
.head
);
917 g_ptr_array_add(lttng_consumer_stream_array
, kconsumerd_fd
);
918 /* keep mmap FDs internally */
921 consumerd_metadata
= helper_get_lttng_consumer_stream_wait_fd(kconsumerd_fd
);
922 sessiond_metadata
= helper_get_lttng_consumer_stream_key(kconsumerd_fd
);
932 void live_consume(struct bt_context
**bt_ctx
)
937 sem_wait(&metadata_available
);
938 if (access("/tmp/livesession/kernel/metadata", F_OK
) != 0) {
939 fprintf(stderr
,"no metadata\n");
942 metadata_fp
= fopen("/tmp/livesession/kernel/metadata", "r");
944 *bt_ctx
= bt_context_create();
945 ret
= bt_context_add_trace(*bt_ctx
, NULL
, "ctf",
946 lttngtop_ctf_packet_seek
, &mmap_list
, metadata_fp
);
948 printf("Error adding trace\n");
956 int setup_consumer(char *command_sock_path
, pthread_t
*threads
,
957 struct lttng_consumer_local_data
*ctx
)
961 ctx
= helper_lttng_consumer_create(HELPER_LTTNG_CONSUMER_KERNEL
,
962 read_subbuffer
, NULL
, on_recv_fd
, on_update_fd
);
966 unlink(command_sock_path
);
967 helper_lttng_consumer_set_command_sock_path(ctx
, command_sock_path
);
968 helper_lttng_consumer_init();
970 /* Create the thread to manage the receive of fd */
971 ret
= pthread_create(&threads
[0], NULL
, helper_lttng_consumer_thread_receive_fds
,
974 perror("pthread_create receive fd");
977 /* Create thread to manage the polling/writing of traces */
978 ret
= pthread_create(&threads
[1], NULL
, helper_lttng_consumer_thread_poll_fds
,
981 perror("pthread_create poll fd");
989 int setup_live_tracing()
991 struct lttng_domain dom
;
992 struct lttng_channel chan
;
993 char *channel_name
= "mmapchan";
994 struct lttng_event ev
;
996 char *command_sock_path
= "/tmp/consumerd_sock";
997 static pthread_t threads
[2]; /* recv_fd, poll */
998 struct lttng_event_context kctxpid
, kctxcomm
, kctxppid
, kctxtid
;
1000 struct lttng_handle
*handle
;
1002 BT_INIT_LIST_HEAD(&mmap_list
.head
);
1004 lttng_consumer_stream_array
= g_ptr_array_new();
1006 if ((ret
= setup_consumer(command_sock_path
, threads
, ctx
)) < 0) {
1007 fprintf(stderr
,"error setting up consumer\n");
1011 available_snapshots
= g_ptr_array_new();
1013 /* setup the session */
1014 dom
.type
= LTTNG_DOMAIN_KERNEL
;
1016 ret
= unlink("/tmp/livesession");
1018 lttng_destroy_session("test");
1019 if ((ret
= lttng_create_session("test", "/tmp/livesession")) < 0) {
1020 fprintf(stderr
,"error creating the session : %s\n",
1021 helper_lttcomm_get_readable_code(ret
));
1025 if ((handle
= lttng_create_handle("test", &dom
)) == NULL
) {
1026 fprintf(stderr
,"error creating handle\n");
1031 * FIXME : need to let the
1032 * helper_lttng_consumer_thread_receive_fds create the
1034 * Cleaner solution ?
1036 while (access(command_sock_path
, F_OK
)) {
1040 if ((ret
= lttng_register_consumer(handle
, command_sock_path
)) < 0) {
1041 fprintf(stderr
,"error registering consumer : %s\n",
1042 helper_lttcomm_get_readable_code(ret
));
1046 strcpy(chan
.name
, channel_name
);
1047 chan
.attr
.overwrite
= 0;
1048 if (opt_tid
&& opt_textdump
) {
1049 chan
.attr
.subbuf_size
= 32768;
1050 chan
.attr
.num_subbuf
= 8;
1052 chan
.attr
.subbuf_size
= 1048576; /* 1MB */
1053 chan
.attr
.num_subbuf
= 4;
1055 chan
.attr
.switch_timer_interval
= 0;
1056 chan
.attr
.read_timer_interval
= 200;
1057 chan
.attr
.output
= LTTNG_EVENT_MMAP
;
1059 if ((ret
= lttng_enable_channel(handle
, &chan
)) < 0) {
1060 fprintf(stderr
,"error creating channel : %s\n",
1061 helper_lttcomm_get_readable_code(ret
));
1065 memset(&ev
, '\0', sizeof(struct lttng_event
));
1066 //sprintf(ev.name, "sched_switch");
1067 ev
.type
= LTTNG_EVENT_TRACEPOINT
;
1068 if ((ret
= lttng_enable_event(handle
, &ev
, channel_name
)) < 0) {
1069 fprintf(stderr
,"error enabling event : %s\n",
1070 helper_lttcomm_get_readable_code(ret
));
1074 ev
.type
= LTTNG_EVENT_SYSCALL
;
1075 if ((ret
= lttng_enable_event(handle
, &ev
, channel_name
)) < 0) {
1076 fprintf(stderr
,"error enabling syscalls : %s\n",
1077 helper_lttcomm_get_readable_code(ret
));
1081 kctxpid
.ctx
= LTTNG_EVENT_CONTEXT_PID
;
1082 lttng_add_context(handle
, &kctxpid
, NULL
, NULL
);
1083 kctxtid
.ctx
= LTTNG_EVENT_CONTEXT_TID
;
1084 lttng_add_context(handle
, &kctxtid
, NULL
, NULL
);
1085 kctxppid
.ctx
= LTTNG_EVENT_CONTEXT_PPID
;
1086 lttng_add_context(handle
, &kctxppid
, NULL
, NULL
);
1087 kctxcomm
.ctx
= LTTNG_EVENT_CONTEXT_PROCNAME
;
1088 lttng_add_context(handle
, &kctxcomm
, NULL
, NULL
);
1089 kctxpid
.ctx
= LTTNG_EVENT_CONTEXT_VPID
;
1090 lttng_add_context(handle
, &kctxpid
, NULL
, NULL
);
1091 kctxtid
.ctx
= LTTNG_EVENT_CONTEXT_VTID
;
1092 lttng_add_context(handle
, &kctxtid
, NULL
, NULL
);
1093 kctxtid
.ctx
= LTTNG_EVENT_CONTEXT_HOSTNAME
;
1094 lttng_add_context(handle
, &kctxtid
, NULL
, NULL
);
1096 if ((ret
= lttng_start_tracing("test")) < 0) {
1097 fprintf(stderr
,"error starting tracing : %s\n",
1098 helper_lttcomm_get_readable_code(ret
));
1102 helper_kernctl_buffer_flush(consumerd_metadata
);
1104 /* block until metadata is ready */
1105 sem_init(&metadata_available
, 0, 0);
1110 lttng_destroy_session("test");
1115 int main(int argc
, char **argv
)
1118 struct bt_context
*bt_ctx
= NULL
;
1119 struct mmap_stream
*mmap_info
;
1120 unsigned long mmap_len
;
1122 ret
= parse_options(argc
, argv
);
1124 fprintf(stdout
, "Error parsing options.\n\n");
1127 } else if (ret
> 0) {
1131 if (!opt_input_path
) {
1133 signal(SIGTERM
, handle_textdump_sigterm
);
1134 signal(SIGINT
, handle_textdump_sigterm
);
1136 ret
= setup_live_tracing();
1141 if (!opt_textdump
) {
1142 pthread_create(&display_thread
, NULL
, ncurses_display
, (void *) NULL
);
1143 pthread_create(&timer_thread
, NULL
, refresh_thread
, (void *) NULL
);
1147 live_consume(&bt_ctx
);
1149 ret
= bt_context_remove_trace(bt_ctx
, 0);
1151 fprintf(stderr
, "error removing trace\n");
1153 bt_context_put(bt_ctx
);
1157 * since we receive all FDs every time there is an
1158 * update and the FD number is different every time,
1159 * we don't know which one are valid.
1160 * so we check if all FDs are usable with a simple
1163 bt_list_for_each_entry(mmap_info
, &mmap_list
.head
, list
) {
1164 ret
= helper_kernctl_get_mmap_len(mmap_info
->fd
, &mmap_len
);
1166 bt_list_del(&mmap_info
->list
);
1169 sem_post(&metadata_available
);
1172 pthread_join(timer_thread
, NULL
);
1174 pthread_join(display_thread
, NULL
);
1176 lttng_stop_tracing("test");
1177 lttng_destroy_session("test");
1183 bt_ctx
= bt_context_create();
1184 ret
= bt_context_add_traces_recursive(bt_ctx
, opt_input_path
, "ctf", NULL
);
1186 fprintf(stderr
, "[error] Opening the trace\n");
1190 ret
= check_requirements(bt_ctx
);
1192 fprintf(stderr
, "[error] some mandatory contexts were missing, exiting.\n");
1195 pthread_create(&display_thread
, NULL
, ncurses_display
, (void *) NULL
);
1196 pthread_create(&timer_thread
, NULL
, refresh_thread
, (void *) NULL
);
1200 pthread_join(display_thread
, NULL
);
1202 pthread_join(timer_thread
, NULL
);
1207 bt_context_put(bt_ctx
);