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;
95 sem_wait(&goodtodisplay
);
98 copy
= g_ptr_array_index(copies
, current_display_index
);
100 display(current_display_index
++);
102 sem_post(&goodtoupdate
);
103 sem_post(&pause_sem
);
113 * hook on each event to check the timestamp and refresh the display if
116 enum bt_cb_ret
check_timestamp(struct bt_ctf_event
*call_data
, void *private_data
)
118 unsigned long timestamp
;
120 timestamp
= bt_ctf_get_timestamp(call_data
);
121 if (timestamp
== -1ULL)
124 if (last_display_update
== 0)
125 last_display_update
= timestamp
;
127 if (timestamp
- last_display_update
>= refresh_display
) {
128 sem_wait(&goodtoupdate
);
129 g_ptr_array_add(copies
, get_copy_lttngtop(last_display_update
,
131 sem_post(&goodtodisplay
);
132 sem_post(&bootstrap
);
133 last_display_update
= timestamp
;
138 fprintf(stderr
, "check_timestamp callback error\n");
139 return BT_CB_ERROR_STOP
;
143 * get_perf_counter : get or create and return a perf_counter struct for
144 * either a process or a cpu (only one of the 2 parameters mandatory)
146 struct perfcounter
*get_perf_counter(const char *name
, struct processtop
*proc
,
149 struct perfcounter
*ret
, *global
;
159 ret
= g_hash_table_lookup(table
, (gpointer
) name
);
163 ret
= g_new0(struct perfcounter
, 1);
164 /* by default, make it visible in the UI */
166 g_hash_table_insert(table
, (gpointer
) strdup(name
), ret
);
168 global
= g_hash_table_lookup(lttngtop
.perf_list
, (gpointer
) name
);
170 global
= g_new0(struct perfcounter
, 1);
171 memcpy(global
, ret
, sizeof(struct perfcounter
));
172 /* by default, sort on the first perf context */
173 if (g_hash_table_size(lttngtop
.perf_list
) == 0)
175 g_hash_table_insert(lttngtop
.perf_list
, (gpointer
) strdup(name
), global
);
185 void update_perf_value(struct processtop
*proc
, struct cputime
*cpu
,
186 const char *name
, int value
)
188 struct perfcounter
*cpu_perf
, *process_perf
;
190 cpu_perf
= get_perf_counter(name
, NULL
, cpu
);
191 if (cpu_perf
->count
< value
) {
192 process_perf
= get_perf_counter(name
, proc
, NULL
);
193 process_perf
->count
+= value
- cpu_perf
->count
;
194 cpu_perf
->count
= value
;
198 void extract_perf_counter_scope(const struct bt_ctf_event
*event
,
199 const struct definition
*scope
,
200 struct processtop
*proc
,
203 struct definition
const * const *list
= NULL
;
210 ret
= bt_ctf_get_field_list(event
, scope
, &list
, &count
);
214 for (i
= 0; i
< count
; i
++) {
215 const char *name
= bt_ctf_field_name(list
[i
]);
216 if (strncmp(name
, "perf_", 5) == 0) {
217 int value
= bt_ctf_get_uint64(list
[i
]);
218 if (bt_ctf_field_get_error())
220 update_perf_value(proc
, cpu
, name
, value
);
228 void update_perf_counter(struct processtop
*proc
, const struct bt_ctf_event
*event
)
231 const struct definition
*scope
;
233 cpu
= get_cpu(get_cpu_id(event
));
235 scope
= bt_ctf_get_top_level_scope(event
, BT_STREAM_EVENT_CONTEXT
);
236 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
238 scope
= bt_ctf_get_top_level_scope(event
, BT_STREAM_PACKET_CONTEXT
);
239 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
241 scope
= bt_ctf_get_top_level_scope(event
, BT_EVENT_CONTEXT
);
242 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
245 enum bt_cb_ret
fix_process_table(struct bt_ctf_event
*call_data
,
250 struct processtop
*parent
, *child
;
251 unsigned long timestamp
;
253 /* FIXME : display nice error when missing context pid, tid, ppid and comm */
255 timestamp
= bt_ctf_get_timestamp(call_data
);
256 if (timestamp
== -1ULL)
259 pid
= get_context_pid(call_data
);
261 // fprintf(stderr, "Missing pid context info\n");
264 tid
= get_context_tid(call_data
);
266 // fprintf(stderr, "Missing tid context info\n");
269 ppid
= get_context_ppid(call_data
);
271 // fprintf(stderr, "Missing ppid context info\n");
274 comm
= get_context_comm(call_data
);
276 // fprintf(stderr, "Missing procname context info\n");
280 /* find or create the current process */
281 child
= find_process_tid(<tngtop
, tid
, comm
);
283 child
= add_proc(<tngtop
, tid
, comm
, timestamp
);
284 update_proc(child
, pid
, tid
, ppid
, comm
);
287 /* find or create the parent */
288 parent
= find_process_tid(<tngtop
, pid
, comm
);
290 parent
= add_proc(<tngtop
, pid
, comm
, timestamp
);
294 /* attach the parent to the current process */
295 child
->threadparent
= parent
;
296 add_thread(parent
, child
);
299 update_perf_counter(child
, call_data
);
304 return BT_CB_ERROR_STOP
;
309 copies
= g_ptr_array_new();
310 lttngtop
.perf_list
= g_hash_table_new(g_str_hash
, g_str_equal
);
312 sem_init(&goodtodisplay
, 0, 0);
313 sem_init(&goodtoupdate
, 0, 1);
314 sem_init(&timer
, 0, 1);
315 sem_init(&bootstrap
, 0, 0);
316 sem_init(&pause_sem
, 0, 1);
317 sem_init(&end_trace_sem
, 0, 0);
319 lttngtop
.process_table
= g_ptr_array_new();
320 lttngtop
.files_table
= g_ptr_array_new();
321 lttngtop
.cpu_table
= g_ptr_array_new();
326 fprintf(fp
, "LTTngTop %s\n\n", VERSION
);
327 fprintf(fp
, "Usage : lttngtop /path/to/trace\n");
331 * Return 0 if caller should continue, < 0 if caller should return
332 * error, > 0 if caller should exit without reporting error.
334 static int parse_options(int argc
, char **argv
)
341 return 1; /* exit cleanly */
344 pc
= poptGetContext(NULL
, argc
, (const char **) argv
, long_options
, 0);
345 poptReadDefaultConfig(pc
, 0);
347 while ((opt
= poptGetNextOpt(pc
)) != -1) {
351 ret
= 1; /* exit cleanly */
354 // list_formats(stdout);
358 // babeltrace_verbose = 1;
361 // babeltrace_debug = 1;
364 // opt_field_names = 1;
372 opt_input_path
= poptGetArg(pc
);
373 if (!opt_input_path
) {
384 void iter_trace(struct bt_context
*bt_ctx
)
386 struct bt_ctf_iter
*iter
;
387 struct bt_iter_pos begin_pos
;
388 const struct bt_ctf_event
*event
;
391 begin_pos
.type
= BT_SEEK_BEGIN
;
392 iter
= bt_ctf_iter_create(bt_ctx
, &begin_pos
, NULL
);
394 /* at each event check if we need to refresh */
395 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
398 /* at each event, verify the status of the process table */
399 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
402 /* to handle the scheduling events */
403 bt_ctf_iter_add_callback(iter
,
404 g_quark_from_static_string("sched_switch"),
405 NULL
, 0, handle_sched_switch
, NULL
, NULL
, NULL
);
406 /* to clean up the process table */
407 bt_ctf_iter_add_callback(iter
,
408 g_quark_from_static_string("sched_process_free"),
409 NULL
, 0, handle_sched_process_free
, NULL
, NULL
, NULL
);
412 bt_ctf_iter_add_callback(iter
,
413 g_quark_from_static_string("exit_syscall"),
414 NULL
, 0, handle_exit_syscall
, NULL
, NULL
, NULL
);
415 bt_ctf_iter_add_callback(iter
,
416 g_quark_from_static_string("sys_write"),
417 NULL
, 0, handle_sys_write
, NULL
, NULL
, NULL
);
418 bt_ctf_iter_add_callback(iter
,
419 g_quark_from_static_string("sys_read"),
420 NULL
, 0, handle_sys_read
, NULL
, NULL
, NULL
);
421 bt_ctf_iter_add_callback(iter
,
422 g_quark_from_static_string("sys_open"),
423 NULL
, 0, handle_sys_open
, NULL
, NULL
, NULL
);
425 bt_ctf_iter_add_callback(iter
,
426 g_quark_from_static_string("sys_close"),
427 NULL
, 0, handle_sys_close
, NULL
, NULL
, NULL
);
428 while ((event
= bt_ctf_iter_read_event(iter
)) != NULL
) {
429 ret
= bt_iter_next(bt_ctf_get_iter(iter
));
434 /* block until quit, we reached the end of the trace */
435 sem_wait(&end_trace_sem
);
438 bt_ctf_iter_destroy(iter
);
442 * bt_context_add_traces_recursive: Open a trace recursively
443 * (copied from BSD code in converter/babeltrace.c)
445 * Find each trace present in the subdirectory starting from the given
446 * path, and add them to the context. The packet_seek parameter can be
447 * NULL: this specify to use the default format packet_seek.
449 * Return: 0 on success, nonzero on failure.
450 * Unable to open toplevel: failure.
451 * Unable to open some subdirectory or file: warn and continue;
453 int bt_context_add_traces_recursive(struct bt_context
*ctx
, const char *path
,
454 const char *format_str
,
455 void (*packet_seek
)(struct stream_pos
*pos
,
456 size_t offset
, int whence
))
461 char lpath
[PATH_MAX
];
462 char * const paths
[2] = { lpath
, NULL
};
466 * Need to copy path, because fts_open can change it.
467 * It is the pointer array, not the strings, that are constant.
469 strncpy(lpath
, path
, PATH_MAX
);
470 lpath
[PATH_MAX
- 1] = '\0';
472 tree
= fts_open(paths
, FTS_NOCHDIR
| FTS_LOGICAL
, 0);
474 fprintf(stderr
, "[error] [Context] Cannot traverse \"%s\" for reading.\n",
479 trace_ids
= g_array_new(FALSE
, TRUE
, sizeof(int));
481 while ((node
= fts_read(tree
))) {
484 if (!(node
->fts_info
& FTS_D
))
487 dirfd
= open(node
->fts_accpath
, 0);
489 fprintf(stderr
, "[error] [Context] Unable to open trace "
490 "directory file descriptor.\n");
494 metafd
= openat(dirfd
, "metadata", O_RDONLY
);
515 trace_id
= bt_context_add_trace(ctx
,
516 node
->fts_accpath
, format_str
,
517 packet_seek
, NULL
, NULL
);
519 fprintf(stderr
, "[error] [Context] opening trace \"%s\" from %s "
520 "for reading.\n", node
->fts_accpath
, path
);
524 g_array_append_val(trace_ids
, trace_id
);
528 g_array_free(trace_ids
, TRUE
);
535 int main(int argc
, char **argv
)
538 struct bt_context
*bt_ctx
= NULL
;
540 ret
= parse_options(argc
, argv
);
542 fprintf(stdout
, "Error parsing options.\n\n");
545 } else if (ret
> 0) {
551 bt_ctx
= bt_context_create();
552 ret
= bt_context_add_traces_recursive(bt_ctx
, opt_input_path
, "ctf", NULL
);
554 printf("[error] Opening the trace\n");
558 pthread_create(&display_thread
, NULL
, ncurses_display
, (void *) NULL
);
559 pthread_create(&timer_thread
, NULL
, refresh_thread
, (void *) NULL
);
564 pthread_join(display_thread
, NULL
);
565 pthread_join(timer_thread
, NULL
);
568 bt_context_put(bt_ctx
);