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
= g_new0(struct perfcounter
, 1);
161 /* by default, make it visible in the UI */
163 g_hash_table_insert(table
, (gpointer
) name
, ret
);
165 global
= g_hash_table_lookup(lttngtop
.perf_list
, (gpointer
) name
);
167 global
= g_new0(struct perfcounter
, 1);
168 memcpy(global
, ret
, sizeof(struct perfcounter
));
169 /* by default, sort on the first perf context */
170 if (g_hash_table_size(lttngtop
.perf_list
) == 0)
172 g_hash_table_insert(lttngtop
.perf_list
, (gpointer
) name
, global
);
182 void update_perf_value(struct processtop
*proc
, struct cputime
*cpu
,
183 const char *name
, int value
)
185 struct perfcounter
*cpu_perf
, *process_perf
;
187 cpu_perf
= get_perf_counter(name
, NULL
, cpu
);
188 if (cpu_perf
->count
< value
) {
189 process_perf
= get_perf_counter(name
, proc
, NULL
);
190 process_perf
->count
+= value
- cpu_perf
->count
;
191 cpu_perf
->count
= value
;
195 void extract_perf_counter_scope(struct bt_ctf_event
*event
,
196 struct definition
*scope
,
197 struct processtop
*proc
,
200 struct definition
const * const *list
= NULL
;
207 ret
= bt_ctf_get_field_list(event
, scope
, &list
, &count
);
211 for (i
= 0; i
< count
; i
++) {
212 const char *name
= bt_ctf_field_name(list
[i
]);
213 if (strncmp(name
, "_perf_", 6) == 0) {
214 int value
= bt_ctf_get_uint64(list
[i
]);
215 if (bt_ctf_field_get_error())
217 update_perf_value(proc
, cpu
, name
, value
);
225 void update_perf_counter(struct processtop
*proc
, struct bt_ctf_event
*event
)
227 struct definition
*scope
;
231 scope
= bt_ctf_get_top_level_scope(event
, BT_STREAM_PACKET_CONTEXT
);
232 cpu_id
= bt_ctf_get_uint64(bt_ctf_get_field(event
, scope
, "cpu_id"));
233 if (bt_ctf_field_get_error()) {
234 fprintf(stderr
, "[error] get cpu_id\n");
237 cpu
= get_cpu(cpu_id
);
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
);
252 enum bt_cb_ret
fix_process_table(struct bt_ctf_event
*call_data
,
257 struct processtop
*parent
, *child
;
258 struct definition
*scope
;
259 unsigned long timestamp
;
261 /* FIXME : check context pid, tid, ppid and comm */
263 timestamp
= bt_ctf_get_timestamp(call_data
);
264 if (timestamp
== -1ULL)
267 scope
= bt_ctf_get_top_level_scope(call_data
, BT_STREAM_EVENT_CONTEXT
);
269 pid
= bt_ctf_get_int64(bt_ctf_get_field(call_data
, scope
, "_pid"));
270 if (bt_ctf_field_get_error()) {
271 // fprintf(stderr, "Missing pid context info\n");
274 tid
= bt_ctf_get_int64(bt_ctf_get_field(call_data
, scope
, "_tid"));
275 if (bt_ctf_field_get_error()) {
276 // fprintf(stderr, "Missing tid context info\n");
279 ppid
= bt_ctf_get_int64(bt_ctf_get_field(call_data
, scope
, "_ppid"));
280 if (bt_ctf_field_get_error()) {
281 // fprintf(stderr, "Missing ppid context info\n");
284 comm
= bt_ctf_get_char_array(bt_ctf_get_field(call_data
, scope
, "_procname"));
285 if (bt_ctf_field_get_error()) {
286 // fprintf(stderr, "Missing procname context info\n");
290 /* find or create the current process */
291 child
= find_process_tid(<tngtop
, tid
, comm
);
293 child
= add_proc(<tngtop
, tid
, comm
, timestamp
);
294 update_proc(child
, pid
, tid
, ppid
, comm
);
297 /* find or create the parent */
298 parent
= find_process_tid(<tngtop
, pid
, comm
);
300 parent
= add_proc(<tngtop
, pid
, comm
, timestamp
);
304 /* attach the parent to the current process */
305 child
->threadparent
= parent
;
306 add_thread(parent
, child
);
309 update_perf_counter(child
, call_data
);
314 return BT_CB_ERROR_STOP
;
319 copies
= g_ptr_array_new();
320 lttngtop
.perf_list
= g_hash_table_new(g_direct_hash
, g_direct_equal
);
322 sem_init(&goodtodisplay
, 0, 0);
323 sem_init(&goodtoupdate
, 0, 1);
324 sem_init(&timer
, 0, 1);
325 sem_init(&bootstrap
, 0, 0);
326 sem_init(&pause_sem
, 0, 1);
327 sem_init(&end_trace_sem
, 0, 0);
329 lttngtop
.process_table
= g_ptr_array_new();
330 lttngtop
.files_table
= g_ptr_array_new();
331 lttngtop
.cpu_table
= g_ptr_array_new();
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 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
);
421 bt_ctf_iter_add_callback(iter
,
422 g_quark_from_static_string("exit_syscall"),
423 NULL
, 0, handle_exit_syscall
, NULL
, NULL
, NULL
);
424 bt_ctf_iter_add_callback(iter
,
425 g_quark_from_static_string("sys_write"),
426 NULL
, 0, handle_sys_write
, NULL
, NULL
, NULL
);
427 bt_ctf_iter_add_callback(iter
,
428 g_quark_from_static_string("sys_read"),
429 NULL
, 0, handle_sys_read
, NULL
, NULL
, NULL
);
430 while ((event
= bt_ctf_iter_read_event(iter
)) != NULL
) {
431 ret
= bt_iter_next(bt_ctf_get_iter(iter
));
436 /* block until quit, we reached the end of the trace */
437 sem_wait(&end_trace_sem
);
440 bt_iter_destroy(bt_ctf_get_iter(iter
));
444 * bt_context_add_traces_recursive: Open a trace recursively
445 * (copied from BSD code in converter/babeltrace.c)
447 * Find each trace present in the subdirectory starting from the given
448 * path, and add them to the context. The packet_seek parameter can be
449 * NULL: this specify to use the default format packet_seek.
451 * Return: 0 on success, nonzero on failure.
452 * Unable to open toplevel: failure.
453 * Unable to open some subdirectory or file: warn and continue;
455 int bt_context_add_traces_recursive(struct bt_context
*ctx
, const char *path
,
456 const char *format_str
,
457 void (*packet_seek
)(struct stream_pos
*pos
,
458 size_t offset
, int whence
))
463 char lpath
[PATH_MAX
];
464 char * const paths
[2] = { lpath
, NULL
};
468 * Need to copy path, because fts_open can change it.
469 * It is the pointer array, not the strings, that are constant.
471 strncpy(lpath
, path
, PATH_MAX
);
472 lpath
[PATH_MAX
- 1] = '\0';
474 tree
= fts_open(paths
, FTS_NOCHDIR
| FTS_LOGICAL
, 0);
476 fprintf(stderr
, "[error] [Context] Cannot traverse \"%s\" for reading.\n",
481 trace_ids
= g_array_new(FALSE
, TRUE
, sizeof(int));
483 while ((node
= fts_read(tree
))) {
486 if (!(node
->fts_info
& FTS_D
))
489 dirfd
= open(node
->fts_accpath
, 0);
491 fprintf(stderr
, "[error] [Context] Unable to open trace "
492 "directory file descriptor.\n");
496 metafd
= openat(dirfd
, "metadata", O_RDONLY
);
517 trace_id
= bt_context_add_trace(ctx
,
518 node
->fts_accpath
, format_str
,
519 packet_seek
, NULL
, NULL
);
521 fprintf(stderr
, "[error] [Context] opening trace \"%s\" from %s "
522 "for reading.\n", node
->fts_accpath
, path
);
526 g_array_append_val(trace_ids
, trace_id
);
530 g_array_free(trace_ids
, TRUE
);
537 int main(int argc
, char **argv
)
540 struct bt_context
*bt_ctx
= NULL
;
542 ret
= parse_options(argc
, argv
);
544 fprintf(stdout
, "Error parsing options.\n\n");
547 } else if (ret
> 0) {
553 bt_ctx
= bt_context_create();
554 ret
= bt_context_add_traces_recursive(bt_ctx
, opt_input_path
, "ctf", NULL
);
556 printf("[error] Opening the trace\n");
560 pthread_create(&display_thread
, NULL
, ncurses_display
, (void *) NULL
);
561 pthread_create(&timer_thread
, NULL
, refresh_thread
, (void *) NULL
);
566 pthread_join(display_thread
, NULL
);