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>
38 #include <sys/types.h>
42 #include "lttngtoptypes.h"
44 #include "iostreamtop.h"
46 #include "cursesdisplay.h"
48 #define DEFAULT_FILE_ARRAY_SIZE 1
50 const char *opt_input_path
;
52 pthread_t display_thread
;
53 pthread_t timer_thread
;
56 std::string history_file
;
67 static struct poptOption long_options
[] = {
68 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
69 { "help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
70 { NULL
, 0, 0, NULL
, 0, NULL
, NULL
},
73 void *refresh_thread(void *p
)
81 sleep(refresh_display
/NSEC_PER_SEC
);
85 void *ncurses_display(void *p
)
89 * Prevent the 1 second delay when we hit ESC
96 sem_wait(&goodtodisplay
);
101 sem_post(&goodtoupdate
);
102 sem_post(&pause_sem
);
112 * We soft update each attribute during the execution for perfromance reasons.
113 * Here we write the actual intervals to disk, which are needed to query history.
115 void create_intervals(unsigned long timestamp
)
117 StateValue::SharedPtr value
;
119 for (std::set
<Quark
>::iterator i
= modified_quarks
.begin();
120 i
!= modified_quarks
.end();
122 value
= state_system
->getCurrentStateValue(*i
);
123 /* To force the creation of an interval (which is what we want)
124 we need to pass a different value than the current value.
125 We reapply the correct value afterwards */
126 if (std::tr1::dynamic_pointer_cast
<NullStateValue
>(value
)) {
127 state_system
->modifyAttribute(timestamp
, *i
, 0);
129 state_system
->modifyAttribute(timestamp
, *i
,
130 StateValue::getNullValue());
132 state_system
->updateCurrentState(*i
, value
);
134 modified_quarks
.clear();
138 * hook on each event to check the timestamp and refresh the display if
141 enum bt_cb_ret
check_timestamp(struct bt_ctf_event
*call_data
,
144 unsigned long timestamp
;
146 timestamp
= bt_ctf_get_timestamp(call_data
);
147 if (timestamp
== -1ULL)
150 if (last_display_update
== 0)
151 last_display_update
= timestamp
;
153 if (first_display_update
== 0)
154 first_display_update
= timestamp
;
156 if (timestamp
- last_display_update
>= refresh_display
) {
157 sem_wait(&goodtoupdate
);
158 create_intervals(timestamp
);
159 update_state_on_refresh(last_display_update
, timestamp
);
160 sem_post(&goodtodisplay
);
161 sem_post(&bootstrap
);
162 last_display_update
= timestamp
;
167 fprintf(stderr
, "check_timestamp callback error\n");
168 return BT_CB_ERROR_STOP
;
172 * get_perf_counter : get or create and return a perf_counter struct for
173 * either a process or a cpu (specified by root)
175 Quark
get_perf_counter(unsigned long timestamp
, Quark root
, std::string name
)
179 if (state_system
->attributeExists(root
, "perf/" + name
)) {
180 return state_system
->getQuark(root
, "perf/" + name
);
182 perf_counter
= state_system
->getQuark(root
, "perf/" + name
);
183 modify_attribute(timestamp
, &perf_counter
, "count", 0);
184 modify_attribute(timestamp
, &perf_counter
, "visible", 1);
185 add_in_sequence(timestamp
, perf_counter
,
186 state_system
->getQuark(root
, "perf"));
191 void update_perf_value(unsigned long timestamp
, Quark proc
, Quark cpu
,
192 std::string name
, int value
)
194 Quark cpu_perf
, process_perf
;
197 cpu_perf
= get_perf_counter(timestamp
, cpu
, name
);
198 get_current_attribute_value_int(&cpu_perf
, "count", count
);
200 process_perf
= get_perf_counter(timestamp
, proc
, name
);
202 timestamp
, &process_perf
, "count", value
- count
);
203 modify_attribute(timestamp
, &cpu_perf
, "count", value
);
207 void extract_perf_counter_scope(unsigned long timestamp
,
208 const struct bt_ctf_event
*event
,
209 const struct definition
*scope
,
210 Quark proc
, Quark cpu
)
212 struct definition
const * const *list
= NULL
;
213 const struct definition
*field
;
222 ret
= bt_ctf_get_field_list(event
, scope
, &list
, &count
);
229 if (get_current_attribute_value_quark(
230 NULL
, "perf", perf_quark
)) {
232 get_current_attribute_value_string(
233 &perf_quark
, "key", key
);
234 field
= bt_ctf_get_field(event
, scope
, key
.c_str());
236 int value
= bt_ctf_get_uint64(field
);
237 if (bt_ctf_field_get_error())
240 timestamp
, proc
, cpu
, key
, value
);
242 } while (get_current_attribute_value_quark(
243 &perf_quark
, "next", perf_quark
));
251 void update_perf_counter(unsigned long timestamp
, Quark proc
,
252 const struct bt_ctf_event
*event
)
255 const struct definition
*scope
;
257 cpu
= get_cpu(get_cpu_id(event
), timestamp
);
259 scope
= bt_ctf_get_top_level_scope(event
, BT_STREAM_EVENT_CONTEXT
);
260 extract_perf_counter_scope(timestamp
, event
, scope
, proc
, cpu
);
262 scope
= bt_ctf_get_top_level_scope(event
, BT_STREAM_PACKET_CONTEXT
);
263 extract_perf_counter_scope(timestamp
, event
, scope
, proc
, cpu
);
265 scope
= bt_ctf_get_top_level_scope(event
, BT_EVENT_CONTEXT
);
266 extract_perf_counter_scope(timestamp
, event
, scope
, proc
, cpu
);
269 enum bt_cb_ret
fix_process_table(struct bt_ctf_event
*call_data
,
271 { int pid
, tid
, ppid
;
274 unsigned long timestamp
;
276 timestamp
= bt_ctf_get_timestamp(call_data
);
277 if (timestamp
== -1ULL)
280 pid
= get_context_pid(call_data
);
281 if ((unsigned long)pid
== -1ULL) {
284 tid
= get_context_tid(call_data
);
285 if ((unsigned long)tid
== -1ULL) {
288 ppid
= get_context_ppid(call_data
);
289 if ((unsigned long)ppid
== -1ULL) {
292 comm
= get_context_comm(call_data
);
297 /* find or create the current process */
298 if (!find_process_tid(tid
, child
))
299 child
= add_proc(tid
, comm
, timestamp
);
300 update_proc(timestamp
, child
, pid
, tid
, ppid
, comm
);
303 /* find or create the parent */
304 if (!find_process_tid(pid
, parent
)) {
305 parent
= add_proc(pid
, comm
, timestamp
);
306 modify_attribute(timestamp
, &parent
, "pid", pid
);
309 /* attach the parent to the current process */
310 modify_attribute(timestamp
, &parent
, "threadparent", parent
);
311 add_thread(timestamp
, parent
, child
);
314 update_perf_counter(timestamp
, child
, call_data
);
319 return BT_CB_ERROR_STOP
;
324 sem_init(&goodtodisplay
, 0, 0);
325 sem_init(&goodtoupdate
, 0, 1);
326 sem_init(&timer
, 0, 1);
327 sem_init(&bootstrap
, 0, 0);
328 sem_init(&pause_sem
, 0, 1);
329 sem_init(&end_trace_sem
, 0, 0);
331 // TODO: real file name
332 std::stringstream ss
;
333 ss
<< "history" << time(NULL
) << ".hst";
334 history_file
= ss
.str();
335 IntervalHistoryProvider
*ihp
= g_new(IntervalHistoryProvider
, 1);
336 new (ihp
) IntervalHistoryProvider(history_file
);
337 state_system
= g_new(StateSystem
, 1);
338 new (state_system
) StateSystem(ihp
);
340 /* Create global attributes */
341 state_system
->updateCurrentState(
342 state_system
->getQuark("nbproc"), 0);
343 state_system
->updateCurrentState(
344 state_system
->getQuark("nbnewproc"), 0);
345 state_system
->updateCurrentState(
346 state_system
->getQuark("nbdeadproc"), 0);
347 state_system
->updateCurrentState(
348 state_system
->getQuark("nbthreads"), 0);
349 state_system
->updateCurrentState(
350 state_system
->getQuark("nbnewthreads"), 0);
351 state_system
->updateCurrentState(
352 state_system
->getQuark("nbdeadthreads"), 0);
353 state_system
->updateCurrentState(
354 state_system
->getQuark("nbfiles"), 0);
355 state_system
->updateCurrentState(
356 state_system
->getQuark("nbnewfiles"), 0);
357 state_system
->updateCurrentState(
358 state_system
->getQuark("nbdeadfiles"), 0);
363 fprintf(fp
, "LTTngTop %s\n\n", VERSION
);
364 fprintf(fp
, "Usage : lttngtop /path/to/trace\n");
368 * Return 0 if caller should continue, < 0 if caller should return
369 * error, > 0 if caller should exit without reporting error.
371 static int parse_options(int argc
, char **argv
)
378 return 1; /* exit cleanly */
381 pc
= poptGetContext(NULL
, argc
, (const char **) argv
, long_options
, 0);
382 poptReadDefaultConfig(pc
, 0);
384 while ((opt
= poptGetNextOpt(pc
)) != -1) {
388 ret
= 1; /* exit cleanly */
396 opt_input_path
= poptGetArg(pc
);
397 if (!opt_input_path
) {
408 void iter_trace(struct bt_context
*bt_ctx
)
410 struct bt_ctf_iter
*iter
;
411 struct bt_iter_pos begin_pos
;
412 const struct bt_ctf_event
*event
;
415 begin_pos
.type
= bt_iter_pos::BT_SEEK_BEGIN
;
416 iter
= bt_ctf_iter_create(bt_ctx
, &begin_pos
, NULL
);
418 /* at each event check if we need to refresh */
419 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
422 /* at each event, verify the status of the process table */
423 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
426 /* to handle the scheduling events */
427 bt_ctf_iter_add_callback(iter
,
428 g_quark_from_static_string("sched_switch"),
429 NULL
, 0, handle_sched_switch
, NULL
, NULL
, NULL
);
430 /* to clean up the process table */
431 bt_ctf_iter_add_callback(iter
,
432 g_quark_from_static_string("sched_process_free"),
433 NULL
, 0, handle_sched_process_free
, NULL
, NULL
, NULL
);
434 /* to get all the process from the statedumps */
435 bt_ctf_iter_add_callback(iter
,
436 g_quark_from_static_string(
437 "lttng_statedump_process_state"),
438 NULL
, 0, handle_statedump_process_state
,
442 bt_ctf_iter_add_callback(iter
,
443 g_quark_from_static_string("exit_syscall"),
444 NULL
, 0, handle_exit_syscall
, NULL
, NULL
, NULL
);
445 bt_ctf_iter_add_callback(iter
,
446 g_quark_from_static_string("sys_write"),
447 NULL
, 0, handle_sys_write
, NULL
, NULL
, NULL
);
448 bt_ctf_iter_add_callback(iter
,
449 g_quark_from_static_string("sys_read"),
450 NULL
, 0, handle_sys_read
, NULL
, NULL
, NULL
);
451 bt_ctf_iter_add_callback(iter
,
452 g_quark_from_static_string("sys_open"),
453 NULL
, 0, handle_sys_open
, NULL
, NULL
, NULL
);
454 bt_ctf_iter_add_callback(iter
,
455 g_quark_from_static_string("sys_close"),
456 NULL
, 0, handle_sys_close
, NULL
, NULL
, NULL
);
457 bt_ctf_iter_add_callback(iter
,
458 g_quark_from_static_string(
459 "lttng_statedump_file_descriptor"),
460 NULL
, 0, handle_statedump_file_descriptor
,
463 while ((event
= bt_ctf_iter_read_event(iter
)) != NULL
) {
464 ret
= bt_iter_next(bt_ctf_get_iter(iter
));
469 /* block until quit, we reached the end of the trace */
470 sem_wait(&end_trace_sem
);
473 bt_ctf_iter_destroy(iter
);
477 * bt_context_add_traces_recursive: Open a trace recursively
478 * (copied from BSD code in converter/babeltrace.c)
480 * Find each trace present in the subdirectory starting from the given
481 * path, and add them to the context. The packet_seek parameter can be
482 * NULL: this specify to use the default format packet_seek.
484 * Return: 0 on success, nonzero on failure.
485 * Unable to open toplevel: failure.
486 * Unable to open some subdirectory or file: warn and continue;
488 int bt_context_add_traces_recursive(struct bt_context
*ctx
, const char *path
,
489 const char *format_str
,
490 void (*packet_seek
)(struct stream_pos
*pos
,
491 size_t offset
, int whence
))
496 char lpath
[PATH_MAX
];
497 char * const paths
[2] = { lpath
, NULL
};
501 * Need to copy path, because fts_open can change it.
502 * It is the pointer array, not the strings, that are constant.
504 strncpy(lpath
, path
, PATH_MAX
);
505 lpath
[PATH_MAX
- 1] = '\0';
507 tree
= fts_open(paths
, FTS_NOCHDIR
| FTS_LOGICAL
, 0);
509 fprintf(stderr
, "[error] [Context] Cannot traverse \"%s\" for reading.\n",
514 trace_ids
= g_array_new(FALSE
, TRUE
, sizeof(int));
516 while ((node
= fts_read(tree
))) {
519 if (!(node
->fts_info
& FTS_D
))
522 dirfd
= open(node
->fts_accpath
, 0);
524 fprintf(stderr
, "[error] [Context] Unable to open trace "
525 "directory file descriptor.\n");
529 metafd
= openat(dirfd
, "metadata", O_RDONLY
);
548 trace_id
= bt_context_add_trace(ctx
,
549 node
->fts_accpath
, format_str
,
550 packet_seek
, NULL
, NULL
);
552 fprintf(stderr
, "[warning] [Context] opening trace \"%s\" from %s "
553 "for reading.\n", node
->fts_accpath
, path
);
554 /* Allow to skip erroneous traces. */
557 g_array_append_val(trace_ids
, trace_id
);
561 g_array_free(trace_ids
, TRUE
);
568 static int check_field_requirements(const struct bt_ctf_field_decl
*const * field_list
,
569 int field_cnt
, int *tid_check
, int *pid_check
,
570 int *procname_check
, int *ppid_check
)
575 Quark perf_root
= state_system
->getQuark("perf");
576 static bool first_perf_counter
= true;
578 for (j
= 0; j
< field_cnt
; j
++) {
579 name
= bt_ctf_get_decl_field_name(field_list
[j
]);
580 if (*tid_check
== 0) {
581 if (strncmp(name
, "tid", 3) == 0)
584 if (*pid_check
== 0) {
585 if (strncmp(name
, "tid", 3) == 0)
588 if (*ppid_check
== 0) {
589 if (strncmp(name
, "ppid", 4) == 0)
592 if (*procname_check
== 0) {
593 if (strncmp(name
, "procname", 8) == 0)
596 if (strncmp(name
, "perf_", 5) == 0) {
597 if (!state_system
->attributeExists(
598 perf_root
, name
+5)) {
599 perf
= state_system
->getQuark(perf_root
, name
+5);
600 /* by default, sort on the first perf context */
601 if (first_perf_counter
) {
602 modify_attribute(0, &perf
, "sort", 1);
603 first_perf_counter
= false;
605 modify_attribute(0, &perf
, "visible", 1);
606 add_in_sequence(0, perf
, perf_root
);
611 if (*tid_check
== 1 && *pid_check
== 1 && *ppid_check
== 1 &&
612 *procname_check
== 1)
619 * check_requirements: check if the required context informations are available
621 * If each mandatory context information is available for at least in one
622 * event, return 0 otherwise return -1.
624 int check_requirements(struct bt_context
*ctx
)
626 unsigned int i
, evt_cnt
, field_cnt
;
627 struct bt_ctf_event_decl
*const * evt_list
;
628 const struct bt_ctf_field_decl
*const * field_list
;
631 int procname_check
= 0;
635 bt_ctf_get_event_decl_list(0, ctx
, &evt_list
, &evt_cnt
);
636 for (i
= 0; i
< evt_cnt
; i
++) {
637 bt_ctf_get_decl_fields(evt_list
[i
], BT_STREAM_EVENT_CONTEXT
,
638 &field_list
, &field_cnt
);
639 ret
= check_field_requirements(field_list
, field_cnt
,
640 &tid_check
, &pid_check
, &procname_check
,
643 bt_ctf_get_decl_fields(evt_list
[i
], BT_EVENT_CONTEXT
,
644 &field_list
, &field_cnt
);
645 ret
= check_field_requirements(field_list
, field_cnt
,
646 &tid_check
, &pid_check
, &procname_check
,
649 bt_ctf_get_decl_fields(evt_list
[i
], BT_STREAM_PACKET_CONTEXT
,
650 &field_list
, &field_cnt
);
651 ret
= check_field_requirements(field_list
, field_cnt
,
652 &tid_check
, &pid_check
, &procname_check
,
656 if (tid_check
== 0) {
658 fprintf(stderr
, "[error] missing tid context information\n");
660 if (pid_check
== 0) {
662 fprintf(stderr
, "[error] missing pid context information\n");
664 if (ppid_check
== 0) {
666 fprintf(stderr
, "[error] missing ppid context information\n");
668 if (procname_check
== 0) {
670 fprintf(stderr
, "[error] missing procname context information\n");
676 int main(int argc
, char **argv
)
679 struct bt_context
*bt_ctx
= NULL
;
681 ret
= parse_options(argc
, argv
);
683 fprintf(stdout
, "Error parsing options.\n\n");
686 } else if (ret
> 0) {
692 bt_ctx
= bt_context_create();
693 ret
= bt_context_add_traces_recursive(bt_ctx
, opt_input_path
, "ctf", NULL
);
695 fprintf(stderr
, "[error] Opening the trace\n");
699 ret
= check_requirements(bt_ctx
);
701 fprintf(stderr
, "[error] some mandatory contexts were missing, exiting.\n");
705 pthread_create(&display_thread
, NULL
, ncurses_display
, (void *) NULL
);
706 pthread_create(&timer_thread
, NULL
, refresh_thread
, (void *) NULL
);
711 pthread_join(display_thread
, NULL
);
712 pthread_join(timer_thread
, NULL
);
715 bt_context_put(bt_ctx
);