2 * Copyright (C) 2011 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 "lttngtoptypes.h"
43 #include "iostreamtop.h"
44 #include "cursesdisplay.h"
47 #define DEFAULT_FILE_ARRAY_SIZE 1
49 const char *opt_input_path
;
51 struct lttngtop
*copy
;
52 pthread_t display_thread
;
53 pthread_t timer_thread
;
55 unsigned long refresh_display
= 1 * NSEC_PER_SEC
;
56 unsigned long last_display_update
= 0;
68 static struct poptOption long_options
[] = {
69 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
70 { "help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
71 { NULL
, 0, 0, NULL
, 0, NULL
, NULL
},
74 void *refresh_thread(void *p
)
82 sleep(refresh_display
/NSEC_PER_SEC
);
86 void *ncurses_display(void *p
)
88 unsigned int current_display_index
= 0;
92 * Prevent the 1 second delay when we hit ESC
99 sem_wait(&goodtodisplay
);
100 sem_wait(&pause_sem
);
102 copy
= g_ptr_array_index(copies
, current_display_index
);
104 display(current_display_index
++);
106 sem_post(&goodtoupdate
);
107 sem_post(&pause_sem
);
117 * hook on each event to check the timestamp and refresh the display if
120 enum bt_cb_ret
check_timestamp(struct bt_ctf_event
*call_data
, void *private_data
)
122 unsigned long timestamp
;
124 timestamp
= bt_ctf_get_timestamp(call_data
);
125 if (timestamp
== -1ULL)
128 if (last_display_update
== 0)
129 last_display_update
= timestamp
;
131 if (timestamp
- last_display_update
>= refresh_display
) {
132 sem_wait(&goodtoupdate
);
133 g_ptr_array_add(copies
, get_copy_lttngtop(last_display_update
,
135 sem_post(&goodtodisplay
);
136 sem_post(&bootstrap
);
137 last_display_update
= timestamp
;
142 fprintf(stderr
, "check_timestamp callback error\n");
143 return BT_CB_ERROR_STOP
;
147 * get_perf_counter : get or create and return a perf_counter struct for
148 * either a process or a cpu (only one of the 2 parameters mandatory)
150 struct perfcounter
*get_perf_counter(const char *name
, struct processtop
*proc
,
153 struct perfcounter
*ret
, *global
;
163 ret
= g_hash_table_lookup(table
, (gpointer
) name
);
167 ret
= g_new0(struct perfcounter
, 1);
168 /* by default, make it visible in the UI */
170 g_hash_table_insert(table
, (gpointer
) strdup(name
), ret
);
172 global
= g_hash_table_lookup(global_perf_liszt
, (gpointer
) name
);
174 global
= g_new0(struct perfcounter
, 1);
175 memcpy(global
, ret
, sizeof(struct perfcounter
));
176 /* by default, sort on the first perf context */
177 if (g_hash_table_size(global_perf_liszt
) == 0)
179 g_hash_table_insert(global_perf_liszt
, (gpointer
) strdup(name
), global
);
189 void update_perf_value(struct processtop
*proc
, struct cputime
*cpu
,
190 const char *name
, int value
)
192 struct perfcounter
*cpu_perf
, *process_perf
;
194 cpu_perf
= get_perf_counter(name
, NULL
, cpu
);
195 if (cpu_perf
->count
< value
) {
196 process_perf
= get_perf_counter(name
, proc
, NULL
);
197 process_perf
->count
+= value
- cpu_perf
->count
;
198 cpu_perf
->count
= value
;
202 void extract_perf_counter_scope(const struct bt_ctf_event
*event
,
203 const struct definition
*scope
,
204 struct processtop
*proc
,
207 struct definition
const * const *list
= NULL
;
214 ret
= bt_ctf_get_field_list(event
, scope
, &list
, &count
);
218 for (i
= 0; i
< count
; i
++) {
219 const char *name
= bt_ctf_field_name(list
[i
]);
220 if (strncmp(name
, "perf_", 5) == 0) {
221 int value
= bt_ctf_get_uint64(list
[i
]);
222 if (bt_ctf_field_get_error())
224 update_perf_value(proc
, cpu
, name
, value
);
232 void update_perf_counter(struct processtop
*proc
, const struct bt_ctf_event
*event
)
235 const struct definition
*scope
;
237 cpu
= get_cpu(get_cpu_id(event
));
239 scope
= bt_ctf_get_top_level_scope(event
, BT_STREAM_EVENT_CONTEXT
);
240 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
242 scope
= bt_ctf_get_top_level_scope(event
, BT_STREAM_PACKET_CONTEXT
);
243 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
245 scope
= bt_ctf_get_top_level_scope(event
, BT_EVENT_CONTEXT
);
246 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
249 enum bt_cb_ret
fix_process_table(struct bt_ctf_event
*call_data
,
254 struct processtop
*parent
, *child
;
255 unsigned long timestamp
;
257 /* FIXME : display nice error when missing context pid, tid, ppid and comm */
259 timestamp
= bt_ctf_get_timestamp(call_data
);
260 if (timestamp
== -1ULL)
263 pid
= get_context_pid(call_data
);
265 // fprintf(stderr, "Missing pid context info\n");
268 tid
= get_context_tid(call_data
);
270 // fprintf(stderr, "Missing tid context info\n");
273 ppid
= get_context_ppid(call_data
);
275 // fprintf(stderr, "Missing ppid context info\n");
278 comm
= get_context_comm(call_data
);
280 // fprintf(stderr, "Missing procname context info\n");
284 /* find or create the current process */
285 child
= find_process_tid(<tngtop
, tid
, comm
);
287 child
= add_proc(<tngtop
, tid
, comm
, timestamp
);
288 update_proc(child
, pid
, tid
, ppid
, comm
);
291 /* find or create the parent */
292 parent
= find_process_tid(<tngtop
, pid
, comm
);
294 parent
= add_proc(<tngtop
, pid
, comm
, timestamp
);
298 /* attach the parent to the current process */
299 child
->threadparent
= parent
;
300 add_thread(parent
, child
);
303 update_perf_counter(child
, call_data
);
308 return BT_CB_ERROR_STOP
;
313 copies
= g_ptr_array_new();
314 global_perf_liszt
= g_hash_table_new(g_str_hash
, g_str_equal
);
316 sem_init(&goodtodisplay
, 0, 0);
317 sem_init(&goodtoupdate
, 0, 1);
318 sem_init(&timer
, 0, 1);
319 sem_init(&bootstrap
, 0, 0);
320 sem_init(&pause_sem
, 0, 1);
321 sem_init(&end_trace_sem
, 0, 0);
323 reset_global_counters();
325 lttngtop
.nbthreads
= 0;
326 lttngtop
.nbfiles
= 0;
328 lttngtop
.process_table
= g_ptr_array_new();
329 lttngtop
.files_table
= g_ptr_array_new();
330 lttngtop
.cpu_table
= g_ptr_array_new();
335 fprintf(fp
, "LTTngTop %s\n\n", VERSION
);
336 fprintf(fp
, "Usage : lttngtop /path/to/trace\n");
340 * Return 0 if caller should continue, < 0 if caller should return
341 * error, > 0 if caller should exit without reporting error.
343 static int parse_options(int argc
, char **argv
)
350 return 1; /* exit cleanly */
353 pc
= poptGetContext(NULL
, argc
, (const char **) argv
, long_options
, 0);
354 poptReadDefaultConfig(pc
, 0);
356 while ((opt
= poptGetNextOpt(pc
)) != -1) {
360 ret
= 1; /* exit cleanly */
363 // list_formats(stdout);
367 // babeltrace_verbose = 1;
370 // babeltrace_debug = 1;
373 // opt_field_names = 1;
381 opt_input_path
= poptGetArg(pc
);
382 if (!opt_input_path
) {
393 void iter_trace(struct bt_context
*bt_ctx
)
395 struct bt_ctf_iter
*iter
;
396 struct bt_iter_pos begin_pos
;
397 const struct bt_ctf_event
*event
;
400 begin_pos
.type
= BT_SEEK_BEGIN
;
401 iter
= bt_ctf_iter_create(bt_ctx
, &begin_pos
, NULL
);
403 /* at each event check if we need to refresh */
404 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
407 /* at each event, verify the status of the process table */
408 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
411 /* to handle the scheduling events */
412 bt_ctf_iter_add_callback(iter
,
413 g_quark_from_static_string("sched_switch"),
414 NULL
, 0, handle_sched_switch
, NULL
, NULL
, NULL
);
415 /* to clean up the process table */
416 bt_ctf_iter_add_callback(iter
,
417 g_quark_from_static_string("sched_process_free"),
418 NULL
, 0, handle_sched_process_free
, NULL
, NULL
, NULL
);
419 /* to get all the process from the statedumps */
420 bt_ctf_iter_add_callback(iter
,
421 g_quark_from_static_string(
422 "lttng_statedump_process_state"),
423 NULL
, 0, handle_statedump_process_state
,
427 bt_ctf_iter_add_callback(iter
,
428 g_quark_from_static_string("exit_syscall"),
429 NULL
, 0, handle_exit_syscall
, NULL
, NULL
, NULL
);
430 bt_ctf_iter_add_callback(iter
,
431 g_quark_from_static_string("sys_write"),
432 NULL
, 0, handle_sys_write
, NULL
, NULL
, NULL
);
433 bt_ctf_iter_add_callback(iter
,
434 g_quark_from_static_string("sys_read"),
435 NULL
, 0, handle_sys_read
, NULL
, NULL
, NULL
);
436 bt_ctf_iter_add_callback(iter
,
437 g_quark_from_static_string("sys_open"),
438 NULL
, 0, handle_sys_open
, NULL
, NULL
, NULL
);
439 bt_ctf_iter_add_callback(iter
,
440 g_quark_from_static_string("sys_close"),
441 NULL
, 0, handle_sys_close
, NULL
, NULL
, NULL
);
442 bt_ctf_iter_add_callback(iter
,
443 g_quark_from_static_string(
444 "lttng_statedump_file_descriptor"),
445 NULL
, 0, handle_statedump_file_descriptor
,
448 while ((event
= bt_ctf_iter_read_event(iter
)) != NULL
) {
449 ret
= bt_iter_next(bt_ctf_get_iter(iter
));
454 /* block until quit, we reached the end of the trace */
455 sem_wait(&end_trace_sem
);
458 bt_ctf_iter_destroy(iter
);
462 * bt_context_add_traces_recursive: Open a trace recursively
463 * (copied from BSD code in converter/babeltrace.c)
465 * Find each trace present in the subdirectory starting from the given
466 * path, and add them to the context. The packet_seek parameter can be
467 * NULL: this specify to use the default format packet_seek.
469 * Return: 0 on success, nonzero on failure.
470 * Unable to open toplevel: failure.
471 * Unable to open some subdirectory or file: warn and continue;
473 int bt_context_add_traces_recursive(struct bt_context
*ctx
, const char *path
,
474 const char *format_str
,
475 void (*packet_seek
)(struct stream_pos
*pos
,
476 size_t offset
, int whence
))
481 char lpath
[PATH_MAX
];
482 char * const paths
[2] = { lpath
, NULL
};
486 * Need to copy path, because fts_open can change it.
487 * It is the pointer array, not the strings, that are constant.
489 strncpy(lpath
, path
, PATH_MAX
);
490 lpath
[PATH_MAX
- 1] = '\0';
492 tree
= fts_open(paths
, FTS_NOCHDIR
| FTS_LOGICAL
, 0);
494 fprintf(stderr
, "[error] [Context] Cannot traverse \"%s\" for reading.\n",
499 trace_ids
= g_array_new(FALSE
, TRUE
, sizeof(int));
501 while ((node
= fts_read(tree
))) {
504 if (!(node
->fts_info
& FTS_D
))
507 dirfd
= open(node
->fts_accpath
, 0);
509 fprintf(stderr
, "[error] [Context] Unable to open trace "
510 "directory file descriptor.\n");
514 metafd
= openat(dirfd
, "metadata", O_RDONLY
);
535 trace_id
= bt_context_add_trace(ctx
,
536 node
->fts_accpath
, format_str
,
537 packet_seek
, NULL
, NULL
);
539 fprintf(stderr
, "[warning] [Context] opening trace \"%s\" from %s "
540 "for reading.\n", node
->fts_accpath
, path
);
541 /* Allow to skip erroneous traces. */
544 g_array_append_val(trace_ids
, trace_id
);
548 g_array_free(trace_ids
, TRUE
);
555 static int check_field_requirements(const struct bt_ctf_field_decl
*const * field_list
,
556 int field_cnt
, int *tid_check
, int *pid_check
,
557 int *procname_check
, int *ppid_check
)
561 for (j
= 0; j
< field_cnt
; j
++) {
562 if (*tid_check
== 0) {
563 if (strncmp(bt_ctf_get_decl_field_name(field_list
[j
]), "tid", 3) == 0) {
567 if (*pid_check
== 0) {
568 if (strncmp(bt_ctf_get_decl_field_name(field_list
[j
]), "pid", 3) == 0)
571 if (*ppid_check
== 0) {
572 if (strncmp(bt_ctf_get_decl_field_name(field_list
[j
]), "ppid", 4) == 0)
575 if (*procname_check
== 0) {
576 if (strncmp(bt_ctf_get_decl_field_name(field_list
[j
]), "procname", 8) == 0)
580 /* if all checks are OK, no need to continue the checks */
581 if (*tid_check
== 1 && *pid_check
== 1 && *ppid_check
== 1 &&
582 *procname_check
== 1)
589 * check_requirements: check if the required context informations are available
591 * If each mandatory context information is available for at least in one
592 * event, return 0 otherwise return -1.
594 int check_requirements(struct bt_context
*ctx
)
596 unsigned int i
, evt_cnt
, field_cnt
;
597 struct bt_ctf_event_decl
*const * evt_list
;
598 const struct bt_ctf_field_decl
*const * field_list
;
601 int procname_check
= 0;
605 bt_ctf_get_event_decl_list(0, ctx
, &evt_list
, &evt_cnt
);
606 for (i
= 0; i
< evt_cnt
; i
++) {
607 bt_ctf_get_decl_fields(evt_list
[i
], BT_STREAM_EVENT_CONTEXT
,
608 &field_list
, &field_cnt
);
609 ret
= check_field_requirements(field_list
, field_cnt
,
610 &tid_check
, &pid_check
, &procname_check
,
615 bt_ctf_get_decl_fields(evt_list
[i
], BT_EVENT_CONTEXT
,
616 &field_list
, &field_cnt
);
617 ret
= check_field_requirements(field_list
, field_cnt
,
618 &tid_check
, &pid_check
, &procname_check
,
623 bt_ctf_get_decl_fields(evt_list
[i
], BT_STREAM_PACKET_CONTEXT
,
624 &field_list
, &field_cnt
);
625 ret
= check_field_requirements(field_list
, field_cnt
,
626 &tid_check
, &pid_check
, &procname_check
,
632 if (tid_check
== 0) {
634 fprintf(stderr
, "[error] missing tid context information\n");
636 if (pid_check
== 0) {
638 fprintf(stderr
, "[error] missing pid context information\n");
640 if (ppid_check
== 0) {
642 fprintf(stderr
, "[error] missing ppid context information\n");
644 if (procname_check
== 0) {
646 fprintf(stderr
, "[error] missing procname context information\n");
653 int main(int argc
, char **argv
)
656 struct bt_context
*bt_ctx
= NULL
;
658 ret
= parse_options(argc
, argv
);
660 fprintf(stdout
, "Error parsing options.\n\n");
663 } else if (ret
> 0) {
669 bt_ctx
= bt_context_create();
670 ret
= bt_context_add_traces_recursive(bt_ctx
, opt_input_path
, "ctf", NULL
);
672 fprintf(stderr
, "[error] Opening the trace\n");
676 ret
= check_requirements(bt_ctx
);
678 fprintf(stderr
, "[error] some mandatory contexts were missing, exiting.\n");
682 pthread_create(&display_thread
, NULL
, ncurses_display
, (void *) NULL
);
683 pthread_create(&timer_thread
, NULL
, refresh_thread
, (void *) NULL
);
688 pthread_join(display_thread
, NULL
);
689 pthread_join(timer_thread
, NULL
);
692 bt_context_put(bt_ctx
);