wrapper to access context fields
[lttngtop.git] / src / lttngtop.c
CommitLineData
1fc22eb4
JD
1/*
2 * Copyright (C) 2011 Julien Desfossez
3 *
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;
7 *
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.
12 *
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,
16 * MA 02111-1307, USA.
17 */
18
19#define _GNU_SOURCE
20#include <config.h>
21#include <stdio.h>
22#include <stdint.h>
23#include <babeltrace/babeltrace.h>
24#include <babeltrace/ctf/events.h>
25#include <babeltrace/ctf/callbacks.h>
26#include <fcntl.h>
27#include <pthread.h>
28#include <popt.h>
29#include <stdlib.h>
30#include <ftw.h>
31#include <dirent.h>
32#include <ctype.h>
33#include <sys/stat.h>
34#include <unistd.h>
35#include <string.h>
36#include <errno.h>
37#include <sys/types.h>
38#include <fts.h>
85db4618 39#include <assert.h>
1fc22eb4
JD
40
41#include "lttngtoptypes.h"
42#include "cputop.h"
43#include "iostreamtop.h"
44#include "cursesdisplay.h"
45#include "common.h"
46
47#define DEFAULT_FILE_ARRAY_SIZE 1
48
49const char *opt_input_path;
50
51struct lttngtop *copy;
52pthread_t display_thread;
53pthread_t timer_thread;
54
55unsigned long refresh_display = 1 * NSEC_PER_SEC;
56unsigned long last_display_update = 0;
57int quit = 0;
58
59enum {
60 OPT_NONE = 0,
61 OPT_HELP,
62 OPT_LIST,
63 OPT_VERBOSE,
64 OPT_DEBUG,
65 OPT_NAMES,
66};
67
68static 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 },
72};
73
74void *refresh_thread(void *p)
75{
76 while (1) {
85db4618
JD
77 if (quit)
78 return NULL;
1fc22eb4
JD
79 sem_wait(&pause_sem);
80 sem_post(&pause_sem);
81 sem_post(&timer);
82 sleep(refresh_display/NSEC_PER_SEC);
83 }
84}
85
86void *ncurses_display(void *p)
87{
88 unsigned int current_display_index = 0;
89
90 sem_wait(&bootstrap);
91 init_ncurses();
92
93 while (1) {
94 sem_wait(&timer);
95 sem_wait(&goodtodisplay);
96 sem_wait(&pause_sem);
97
98 copy = g_ptr_array_index(copies, current_display_index);
85db4618
JD
99 assert(copy);
100 display(current_display_index++);
1fc22eb4
JD
101
102 sem_post(&goodtoupdate);
103 sem_post(&pause_sem);
104
105 if (quit) {
106 reset_ncurses();
107 pthread_exit(0);
108 }
109 }
110}
111
112/*
113 * hook on each event to check the timestamp and refresh the display if
114 * necessary
115 */
116enum bt_cb_ret check_timestamp(struct bt_ctf_event *call_data, void *private_data)
117{
118 unsigned long timestamp;
119
120 timestamp = bt_ctf_get_timestamp(call_data);
121 if (timestamp == -1ULL)
122 goto error;
123
124 if (last_display_update == 0)
125 last_display_update = timestamp;
126
127 if (timestamp - last_display_update >= refresh_display) {
128 sem_wait(&goodtoupdate);
129 g_ptr_array_add(copies, get_copy_lttngtop(last_display_update,
130 timestamp));
131 sem_post(&goodtodisplay);
132 sem_post(&bootstrap);
133 last_display_update = timestamp;
134 }
135 return BT_CB_OK;
136
137error:
138 fprintf(stderr, "check_timestamp callback error\n");
139 return BT_CB_ERROR_STOP;
140}
141
142/*
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)
145 */
146struct perfcounter *get_perf_counter(const char *name, struct processtop *proc,
147 struct cputime *cpu)
148{
149 struct perfcounter *ret, *global;
150 GHashTable *table;
151
152 if (proc)
153 table = proc->perf;
154 else if (cpu)
155 table = cpu->perf;
156 else
157 goto error;
158
159 ret = g_hash_table_lookup(table, (gpointer) name);
160 if (ret)
161 goto end;
162
559c9f86 163 ret = g_new0(struct perfcounter, 1);
1fc22eb4
JD
164 /* by default, make it visible in the UI */
165 ret->visible = 1;
85db4618 166 g_hash_table_insert(table, (gpointer) strdup(name), ret);
1fc22eb4
JD
167
168 global = g_hash_table_lookup(lttngtop.perf_list, (gpointer) name);
169 if (!global) {
559c9f86 170 global = g_new0(struct perfcounter, 1);
1fc22eb4
JD
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)
174 global->sort = 1;
85db4618 175 g_hash_table_insert(lttngtop.perf_list, (gpointer) strdup(name), global);
1fc22eb4
JD
176 }
177
178end:
179 return ret;
180
181error:
182 return NULL;
183}
184
185void update_perf_value(struct processtop *proc, struct cputime *cpu,
186 const char *name, int value)
187{
188 struct perfcounter *cpu_perf, *process_perf;
189
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;
195 }
196}
197
198void extract_perf_counter_scope(struct bt_ctf_event *event,
199 struct definition *scope,
200 struct processtop *proc,
201 struct cputime *cpu)
202{
203 struct definition const * const *list = NULL;
204 unsigned int count;
205 int i, ret;
206
207 if (!scope)
208 goto end;
209
210 ret = bt_ctf_get_field_list(event, scope, &list, &count);
211 if (ret < 0)
212 goto end;
213
214 for (i = 0; i < count; i++) {
215 const char *name = bt_ctf_field_name(list[i]);
216 if (strncmp(name, "_perf_", 6) == 0) {
217 int value = bt_ctf_get_uint64(list[i]);
218 if (bt_ctf_field_get_error())
219 continue;
220 update_perf_value(proc, cpu, name, value);
221 }
222 }
223
224end:
225 return;
226}
227
228void update_perf_counter(struct processtop *proc, struct bt_ctf_event *event)
229{
1fc22eb4 230 struct cputime *cpu;
d67167cd 231 struct definition *scope;
1fc22eb4 232
d67167cd 233 cpu = get_cpu(get_cpu_id(event));
1fc22eb4
JD
234
235 scope = bt_ctf_get_top_level_scope(event, BT_STREAM_EVENT_CONTEXT);
236 extract_perf_counter_scope(event, scope, proc, cpu);
237
238 scope = bt_ctf_get_top_level_scope(event, BT_STREAM_PACKET_CONTEXT);
239 extract_perf_counter_scope(event, scope, proc, cpu);
240
241 scope = bt_ctf_get_top_level_scope(event, BT_EVENT_CONTEXT);
242 extract_perf_counter_scope(event, scope, proc, cpu);
1fc22eb4
JD
243}
244
245enum bt_cb_ret fix_process_table(struct bt_ctf_event *call_data,
246 void *private_data)
247{
248 int pid, tid, ppid;
249 char *comm;
250 struct processtop *parent, *child;
251 struct definition *scope;
252 unsigned long timestamp;
253
85db4618 254 /* FIXME : display nice error when missing context pid, tid, ppid and comm */
1fc22eb4
JD
255
256 timestamp = bt_ctf_get_timestamp(call_data);
257 if (timestamp == -1ULL)
258 goto error;
259
1dec520a
JD
260 pid = get_context_pid(call_data);
261 if (pid == -1ULL) {
1fc22eb4
JD
262// fprintf(stderr, "Missing pid context info\n");
263 goto error;
264 }
1dec520a
JD
265 tid = get_context_tid(call_data);
266 if (tid == -1ULL) {
1fc22eb4
JD
267// fprintf(stderr, "Missing tid context info\n");
268 goto error;
269 }
1dec520a
JD
270 ppid = get_context_ppid(call_data);
271 if (ppid == -1ULL) {
1fc22eb4
JD
272// fprintf(stderr, "Missing ppid context info\n");
273 goto error;
274 }
1dec520a
JD
275 comm = get_context_comm(call_data);
276 if (!comm) {
1fc22eb4
JD
277// fprintf(stderr, "Missing procname context info\n");
278 goto error;
279 }
280
281 /* find or create the current process */
282 child = find_process_tid(&lttngtop, tid, comm);
283 if (!child)
284 child = add_proc(&lttngtop, tid, comm, timestamp);
285 update_proc(child, pid, tid, ppid, comm);
286
287 if (pid != tid) {
288 /* find or create the parent */
289 parent = find_process_tid(&lttngtop, pid, comm);
290 if (!parent) {
291 parent = add_proc(&lttngtop, pid, comm, timestamp);
292 parent->pid = pid;
293 }
294
295 /* attach the parent to the current process */
296 child->threadparent = parent;
297 add_thread(parent, child);
298 }
299
300 update_perf_counter(child, call_data);
301
302 return BT_CB_OK;
303
304error:
305 return BT_CB_ERROR_STOP;
306}
307
308void init_lttngtop()
309{
310 copies = g_ptr_array_new();
85db4618 311 lttngtop.perf_list = g_hash_table_new(g_str_hash, g_str_equal);
1fc22eb4
JD
312
313 sem_init(&goodtodisplay, 0, 0);
314 sem_init(&goodtoupdate, 0, 1);
315 sem_init(&timer, 0, 1);
316 sem_init(&bootstrap, 0, 0);
317 sem_init(&pause_sem, 0, 1);
318 sem_init(&end_trace_sem, 0, 0);
319
320 lttngtop.process_table = g_ptr_array_new();
321 lttngtop.files_table = g_ptr_array_new();
322 lttngtop.cpu_table = g_ptr_array_new();
323}
324
c263c4eb 325void usage(FILE *fp)
1fc22eb4 326{
c263c4eb
JD
327 fprintf(fp, "LTTngTop %s\n\n", VERSION);
328 fprintf(fp, "Usage : lttngtop /path/to/trace\n");
1fc22eb4
JD
329}
330
331/*
332 * Return 0 if caller should continue, < 0 if caller should return
333 * error, > 0 if caller should exit without reporting error.
334 */
335static int parse_options(int argc, char **argv)
336{
337 poptContext pc;
338 int opt, ret = 0;
339
340 if (argc == 1) {
341 usage(stdout);
342 return 1; /* exit cleanly */
343 }
344
345 pc = poptGetContext(NULL, argc, (const char **) argv, long_options, 0);
346 poptReadDefaultConfig(pc, 0);
347
348 while ((opt = poptGetNextOpt(pc)) != -1) {
349 switch (opt) {
350 case OPT_HELP:
351 usage(stdout);
352 ret = 1; /* exit cleanly */
353 goto end;
354 case OPT_LIST:
355 // list_formats(stdout);
356 ret = 1;
357 goto end;
358 case OPT_VERBOSE:
359// babeltrace_verbose = 1;
360 break;
361 case OPT_DEBUG:
362// babeltrace_debug = 1;
363 break;
364 case OPT_NAMES:
365// opt_field_names = 1;
366 break;
367 default:
368 ret = -EINVAL;
369 goto end;
370 }
371 }
372
373 opt_input_path = poptGetArg(pc);
374 if (!opt_input_path) {
375 ret = -EINVAL;
376 goto end;
377 }
378end:
379 if (pc) {
380 poptFreeContext(pc);
381 }
382 return ret;
383}
384
385void iter_trace(struct bt_context *bt_ctx)
386{
387 struct bt_ctf_iter *iter;
388 struct bt_iter_pos begin_pos;
389 struct bt_ctf_event *event;
390 int ret = 0;
391
392 begin_pos.type = BT_SEEK_BEGIN;
393 iter = bt_ctf_iter_create(bt_ctx, &begin_pos, NULL);
394
395 /* at each event check if we need to refresh */
396 bt_ctf_iter_add_callback(iter, 0, NULL, 0,
397 check_timestamp,
398 NULL, NULL, NULL);
399 /* at each event, verify the status of the process table */
400 bt_ctf_iter_add_callback(iter, 0, NULL, 0,
401 fix_process_table,
402 NULL, NULL, NULL);
403 /* to handle the scheduling events */
404 bt_ctf_iter_add_callback(iter,
405 g_quark_from_static_string("sched_switch"),
406 NULL, 0, handle_sched_switch, NULL, NULL, NULL);
407 /* to clean up the process table */
408 bt_ctf_iter_add_callback(iter,
409 g_quark_from_static_string("sched_process_free"),
410 NULL, 0, handle_sched_process_free, NULL, NULL, NULL);
411
412 /* for IO top */
413 bt_ctf_iter_add_callback(iter,
414 g_quark_from_static_string("exit_syscall"),
415 NULL, 0, handle_exit_syscall, NULL, NULL, NULL);
416 bt_ctf_iter_add_callback(iter,
417 g_quark_from_static_string("sys_write"),
418 NULL, 0, handle_sys_write, NULL, NULL, NULL);
419 bt_ctf_iter_add_callback(iter,
420 g_quark_from_static_string("sys_read"),
421 NULL, 0, handle_sys_read, NULL, NULL, NULL);
b093de8a
MB
422 bt_ctf_iter_add_callback(iter,
423 g_quark_from_static_string("sys_open"),
424 NULL, 0, handle_sys_open, NULL, NULL, NULL);
425
426 bt_ctf_iter_add_callback(iter,
427 g_quark_from_static_string("sys_close"),
428 NULL, 0, handle_sys_close, NULL, NULL, NULL);
1fc22eb4
JD
429 while ((event = bt_ctf_iter_read_event(iter)) != NULL) {
430 ret = bt_iter_next(bt_ctf_get_iter(iter));
431 if (ret < 0)
432 goto end_iter;
433 }
434
435 /* block until quit, we reached the end of the trace */
436 sem_wait(&end_trace_sem);
437
438end_iter:
439 bt_iter_destroy(bt_ctf_get_iter(iter));
440}
441
442/*
443 * bt_context_add_traces_recursive: Open a trace recursively
444 * (copied from BSD code in converter/babeltrace.c)
445 *
446 * Find each trace present in the subdirectory starting from the given
447 * path, and add them to the context. The packet_seek parameter can be
448 * NULL: this specify to use the default format packet_seek.
449 *
450 * Return: 0 on success, nonzero on failure.
451 * Unable to open toplevel: failure.
452 * Unable to open some subdirectory or file: warn and continue;
453 */
454int bt_context_add_traces_recursive(struct bt_context *ctx, const char *path,
455 const char *format_str,
456 void (*packet_seek)(struct stream_pos *pos,
457 size_t offset, int whence))
458{
459 FTS *tree;
460 FTSENT *node;
461 GArray *trace_ids;
462 char lpath[PATH_MAX];
463 char * const paths[2] = { lpath, NULL };
464 int ret;
465
466 /*
467 * Need to copy path, because fts_open can change it.
468 * It is the pointer array, not the strings, that are constant.
469 */
470 strncpy(lpath, path, PATH_MAX);
471 lpath[PATH_MAX - 1] = '\0';
472
473 tree = fts_open(paths, FTS_NOCHDIR | FTS_LOGICAL, 0);
474 if (tree == NULL) {
475 fprintf(stderr, "[error] [Context] Cannot traverse \"%s\" for reading.\n",
476 path);
477 return -EINVAL;
478 }
479
480 trace_ids = g_array_new(FALSE, TRUE, sizeof(int));
481
482 while ((node = fts_read(tree))) {
483 int dirfd, metafd;
484
485 if (!(node->fts_info & FTS_D))
486 continue;
487
488 dirfd = open(node->fts_accpath, 0);
489 if (dirfd < 0) {
490 fprintf(stderr, "[error] [Context] Unable to open trace "
491 "directory file descriptor.\n");
492 ret = dirfd;
493 goto error;
494 }
495 metafd = openat(dirfd, "metadata", O_RDONLY);
496 if (metafd < 0) {
497 ret = close(dirfd);
498 if (ret < 0) {
499 perror("close");
500 goto error;
501 }
502 } else {
503 int trace_id;
504
505 ret = close(metafd);
506 if (ret < 0) {
507 perror("close");
508 goto error;
509 }
510 ret = close(dirfd);
511 if (ret < 0) {
512 perror("close");
513 goto error;
514 }
515
516 trace_id = bt_context_add_trace(ctx,
517 node->fts_accpath, format_str,
518 packet_seek, NULL, NULL);
519 if (trace_id < 0) {
520 fprintf(stderr, "[error] [Context] opening trace \"%s\" from %s "
521 "for reading.\n", node->fts_accpath, path);
522 ret = trace_id;
523 goto error;
524 }
525 g_array_append_val(trace_ids, trace_id);
526 }
527 }
528
529 g_array_free(trace_ids, TRUE);
530 return 0;
531
532error:
533 return ret;
534}
535
536int main(int argc, char **argv)
537{
538 int ret;
539 struct bt_context *bt_ctx = NULL;
540
541 ret = parse_options(argc, argv);
542 if (ret < 0) {
543 fprintf(stdout, "Error parsing options.\n\n");
544 usage(stdout);
545 exit(EXIT_FAILURE);
546 } else if (ret > 0) {
547 exit(EXIT_SUCCESS);
548 }
549
550 init_lttngtop();
551
552 bt_ctx = bt_context_create();
553 ret = bt_context_add_traces_recursive(bt_ctx, opt_input_path, "ctf", NULL);
554 if (ret < 0) {
555 printf("[error] Opening the trace\n");
556 goto end;
557 }
558
559 pthread_create(&display_thread, NULL, ncurses_display, (void *) NULL);
560 pthread_create(&timer_thread, NULL, refresh_thread, (void *) NULL);
561
562 iter_trace(bt_ctx);
563
564 quit = 1;
565 pthread_join(display_thread, NULL);
85db4618 566 pthread_join(timer_thread, NULL);
1fc22eb4
JD
567
568end:
85db4618 569 bt_context_put(bt_ctx);
1fc22eb4
JD
570 return 0;
571}
This page took 0.042331 seconds and 4 git commands to generate.