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
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
23 #include <babeltrace/babeltrace.h>
24 #include <babeltrace/ctf/events.h>
25 #include <babeltrace/ctf/callbacks.h>
37 #include <sys/types.h>
40 #include "lttngtoptypes.h"
42 #include "iostreamtop.h"
43 #include "cursesdisplay.h"
46 #define DEFAULT_FILE_ARRAY_SIZE 1
48 const char *opt_input_path
;
50 struct lttngtop
*copy
;
51 pthread_t display_thread
;
52 pthread_t timer_thread
;
54 unsigned long refresh_display
= 1 * NSEC_PER_SEC
;
55 unsigned long last_display_update
= 0;
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
)
79 sleep(refresh_display
/NSEC_PER_SEC
);
83 void *ncurses_display(void *p
)
85 unsigned int current_display_index
= 0;
92 sem_wait(&goodtodisplay
);
95 copy
= g_ptr_array_index(copies
, current_display_index
);
97 display(current_display_index
++);
99 sem_post(&goodtoupdate
);
100 sem_post(&pause_sem
);
110 * hook on each event to check the timestamp and refresh the display if
113 enum bt_cb_ret
check_timestamp(struct bt_ctf_event
*call_data
, void *private_data
)
115 unsigned long timestamp
;
117 timestamp
= bt_ctf_get_timestamp(call_data
);
118 if (timestamp
== -1ULL)
121 if (last_display_update
== 0)
122 last_display_update
= timestamp
;
124 if (timestamp
- last_display_update
>= refresh_display
) {
125 sem_wait(&goodtoupdate
);
126 g_ptr_array_add(copies
, get_copy_lttngtop(last_display_update
,
128 sem_post(&goodtodisplay
);
129 sem_post(&bootstrap
);
130 last_display_update
= timestamp
;
135 fprintf(stderr
, "check_timestamp callback error\n");
136 return BT_CB_ERROR_STOP
;
140 * get_perf_counter : get or create and return a perf_counter struct for
141 * either a process or a cpu (only one of the 2 parameters mandatory)
143 struct perfcounter
*get_perf_counter(const char *name
, struct processtop
*proc
,
146 struct perfcounter
*ret
, *global
;
156 ret
= g_hash_table_lookup(table
, (gpointer
) name
);
160 ret
= malloc(sizeof(struct perfcounter
));
161 memset(ret
, 0, sizeof(struct perfcounter
));
162 /* by default, make it visible in the UI */
164 g_hash_table_insert(table
, (gpointer
) name
, ret
);
166 global
= g_hash_table_lookup(lttngtop
.perf_list
, (gpointer
) name
);
168 global
= malloc(sizeof(struct perfcounter
));
169 memset(global
, 0, sizeof(struct perfcounter
));
170 memcpy(global
, ret
, sizeof(struct perfcounter
));
171 /* by default, sort on the first perf context */
172 if (g_hash_table_size(lttngtop
.perf_list
) == 0)
174 g_hash_table_insert(lttngtop
.perf_list
, (gpointer
) name
, global
);
184 void update_perf_value(struct processtop
*proc
, struct cputime
*cpu
,
185 const char *name
, int value
)
187 struct perfcounter
*cpu_perf
, *process_perf
;
189 cpu_perf
= get_perf_counter(name
, NULL
, cpu
);
190 if (cpu_perf
->count
< value
) {
191 process_perf
= get_perf_counter(name
, proc
, NULL
);
192 process_perf
->count
+= value
- cpu_perf
->count
;
193 cpu_perf
->count
= value
;
197 void extract_perf_counter_scope(struct bt_ctf_event
*event
,
198 struct definition
*scope
,
199 struct processtop
*proc
,
202 struct definition
const * const *list
= NULL
;
209 ret
= bt_ctf_get_field_list(event
, scope
, &list
, &count
);
213 for (i
= 0; i
< count
; i
++) {
214 const char *name
= bt_ctf_field_name(list
[i
]);
215 if (strncmp(name
, "_perf_", 6) == 0) {
216 int value
= bt_ctf_get_uint64(list
[i
]);
217 if (bt_ctf_field_get_error())
219 update_perf_value(proc
, cpu
, name
, value
);
227 void update_perf_counter(struct processtop
*proc
, struct bt_ctf_event
*event
)
229 struct definition
*scope
;
233 scope
= bt_ctf_get_top_level_scope(event
, BT_STREAM_PACKET_CONTEXT
);
234 cpu_id
= bt_ctf_get_uint64(bt_ctf_get_field(event
, scope
, "cpu_id"));
235 if (bt_ctf_field_get_error()) {
236 fprintf(stderr
, "[error] get cpu_id\n");
239 cpu
= get_cpu(cpu_id
);
241 scope
= bt_ctf_get_top_level_scope(event
, BT_STREAM_EVENT_CONTEXT
);
242 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
244 scope
= bt_ctf_get_top_level_scope(event
, BT_STREAM_PACKET_CONTEXT
);
245 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
247 scope
= bt_ctf_get_top_level_scope(event
, BT_EVENT_CONTEXT
);
248 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
254 enum bt_cb_ret
fix_process_table(struct bt_ctf_event
*call_data
,
259 struct processtop
*parent
, *child
;
260 struct definition
*scope
;
261 unsigned long timestamp
;
263 /* FIXME : check context pid, tid, ppid and comm */
265 timestamp
= bt_ctf_get_timestamp(call_data
);
266 if (timestamp
== -1ULL)
269 scope
= bt_ctf_get_top_level_scope(call_data
, BT_STREAM_EVENT_CONTEXT
);
271 pid
= bt_ctf_get_int64(bt_ctf_get_field(call_data
, scope
, "_pid"));
272 if (bt_ctf_field_get_error()) {
273 // fprintf(stderr, "Missing pid context info\n");
276 tid
= bt_ctf_get_int64(bt_ctf_get_field(call_data
, scope
, "_tid"));
277 if (bt_ctf_field_get_error()) {
278 // fprintf(stderr, "Missing tid context info\n");
281 ppid
= bt_ctf_get_int64(bt_ctf_get_field(call_data
, scope
, "_ppid"));
282 if (bt_ctf_field_get_error()) {
283 // fprintf(stderr, "Missing ppid context info\n");
286 comm
= bt_ctf_get_char_array(bt_ctf_get_field(call_data
, scope
, "_procname"));
287 if (bt_ctf_field_get_error()) {
288 // fprintf(stderr, "Missing procname context info\n");
292 /* find or create the current process */
293 child
= find_process_tid(<tngtop
, tid
, comm
);
295 child
= add_proc(<tngtop
, tid
, comm
, timestamp
);
296 update_proc(child
, pid
, tid
, ppid
, comm
);
299 /* find or create the parent */
300 parent
= find_process_tid(<tngtop
, pid
, comm
);
302 parent
= add_proc(<tngtop
, pid
, comm
, timestamp
);
306 /* attach the parent to the current process */
307 child
->threadparent
= parent
;
308 add_thread(parent
, child
);
311 update_perf_counter(child
, call_data
);
316 return BT_CB_ERROR_STOP
;
321 copies
= g_ptr_array_new();
322 lttngtop
.perf_list
= g_hash_table_new(g_direct_hash
, g_direct_equal
);
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 lttngtop
.process_table
= g_ptr_array_new();
332 lttngtop
.files_table
= g_ptr_array_new();
333 lttngtop
.cpu_table
= g_ptr_array_new();
342 * Return 0 if caller should continue, < 0 if caller should return
343 * error, > 0 if caller should exit without reporting error.
345 static int parse_options(int argc
, char **argv
)
352 return 1; /* exit cleanly */
355 pc
= poptGetContext(NULL
, argc
, (const char **) argv
, long_options
, 0);
356 poptReadDefaultConfig(pc
, 0);
358 while ((opt
= poptGetNextOpt(pc
)) != -1) {
362 ret
= 1; /* exit cleanly */
365 // list_formats(stdout);
369 // babeltrace_verbose = 1;
372 // babeltrace_debug = 1;
375 // opt_field_names = 1;
383 opt_input_path
= poptGetArg(pc
);
384 if (!opt_input_path
) {
395 void iter_trace(struct bt_context
*bt_ctx
)
397 struct bt_ctf_iter
*iter
;
398 struct bt_iter_pos begin_pos
;
399 struct bt_ctf_event
*event
;
402 begin_pos
.type
= BT_SEEK_BEGIN
;
403 iter
= bt_ctf_iter_create(bt_ctx
, &begin_pos
, NULL
);
405 /* at each event check if we need to refresh */
406 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
409 /* at each event, verify the status of the process table */
410 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
413 /* to handle the scheduling events */
414 bt_ctf_iter_add_callback(iter
,
415 g_quark_from_static_string("sched_switch"),
416 NULL
, 0, handle_sched_switch
, NULL
, NULL
, NULL
);
417 /* to clean up the process table */
418 bt_ctf_iter_add_callback(iter
,
419 g_quark_from_static_string("sched_process_free"),
420 NULL
, 0, handle_sched_process_free
, NULL
, NULL
, NULL
);
423 bt_ctf_iter_add_callback(iter
,
424 g_quark_from_static_string("exit_syscall"),
425 NULL
, 0, handle_exit_syscall
, NULL
, NULL
, NULL
);
426 bt_ctf_iter_add_callback(iter
,
427 g_quark_from_static_string("sys_write"),
428 NULL
, 0, handle_sys_write
, NULL
, NULL
, NULL
);
429 bt_ctf_iter_add_callback(iter
,
430 g_quark_from_static_string("sys_read"),
431 NULL
, 0, handle_sys_read
, NULL
, NULL
, NULL
);
432 while ((event
= bt_ctf_iter_read_event(iter
)) != NULL
) {
433 ret
= bt_iter_next(bt_ctf_get_iter(iter
));
438 /* block until quit, we reached the end of the trace */
439 sem_wait(&end_trace_sem
);
442 bt_iter_destroy(bt_ctf_get_iter(iter
));
446 * bt_context_add_traces_recursive: Open a trace recursively
447 * (copied from BSD code in converter/babeltrace.c)
449 * Find each trace present in the subdirectory starting from the given
450 * path, and add them to the context. The packet_seek parameter can be
451 * NULL: this specify to use the default format packet_seek.
453 * Return: 0 on success, nonzero on failure.
454 * Unable to open toplevel: failure.
455 * Unable to open some subdirectory or file: warn and continue;
457 int bt_context_add_traces_recursive(struct bt_context
*ctx
, const char *path
,
458 const char *format_str
,
459 void (*packet_seek
)(struct stream_pos
*pos
,
460 size_t offset
, int whence
))
465 char lpath
[PATH_MAX
];
466 char * const paths
[2] = { lpath
, NULL
};
470 * Need to copy path, because fts_open can change it.
471 * It is the pointer array, not the strings, that are constant.
473 strncpy(lpath
, path
, PATH_MAX
);
474 lpath
[PATH_MAX
- 1] = '\0';
476 tree
= fts_open(paths
, FTS_NOCHDIR
| FTS_LOGICAL
, 0);
478 fprintf(stderr
, "[error] [Context] Cannot traverse \"%s\" for reading.\n",
483 trace_ids
= g_array_new(FALSE
, TRUE
, sizeof(int));
485 while ((node
= fts_read(tree
))) {
488 if (!(node
->fts_info
& FTS_D
))
491 dirfd
= open(node
->fts_accpath
, 0);
493 fprintf(stderr
, "[error] [Context] Unable to open trace "
494 "directory file descriptor.\n");
498 metafd
= openat(dirfd
, "metadata", O_RDONLY
);
519 trace_id
= bt_context_add_trace(ctx
,
520 node
->fts_accpath
, format_str
,
521 packet_seek
, NULL
, NULL
);
523 fprintf(stderr
, "[error] [Context] opening trace \"%s\" from %s "
524 "for reading.\n", node
->fts_accpath
, path
);
528 g_array_append_val(trace_ids
, trace_id
);
532 g_array_free(trace_ids
, TRUE
);
539 int main(int argc
, char **argv
)
542 struct bt_context
*bt_ctx
= NULL
;
544 ret
= parse_options(argc
, argv
);
546 fprintf(stdout
, "Error parsing options.\n\n");
549 } else if (ret
> 0) {
555 bt_ctx
= bt_context_create();
556 ret
= bt_context_add_traces_recursive(bt_ctx
, opt_input_path
, "ctf", NULL
);
558 printf("[error] Opening the trace\n");
562 pthread_create(&display_thread
, NULL
, ncurses_display
, (void *) NULL
);
563 pthread_create(&timer_thread
, NULL
, refresh_thread
, (void *) NULL
);
568 pthread_join(display_thread
, NULL
);