rename lttngtop/ to src/
[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>
39
40#include "lttngtoptypes.h"
41#include "cputop.h"
42#include "iostreamtop.h"
43#include "cursesdisplay.h"
44#include "common.h"
45
46#define DEFAULT_FILE_ARRAY_SIZE 1
47
48const char *opt_input_path;
49
50struct lttngtop *copy;
51pthread_t display_thread;
52pthread_t timer_thread;
53
54unsigned long refresh_display = 1 * NSEC_PER_SEC;
55unsigned long last_display_update = 0;
56int quit = 0;
57
58enum {
59 OPT_NONE = 0,
60 OPT_HELP,
61 OPT_LIST,
62 OPT_VERBOSE,
63 OPT_DEBUG,
64 OPT_NAMES,
65};
66
67static 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 },
71};
72
73void *refresh_thread(void *p)
74{
75 while (1) {
76 sem_wait(&pause_sem);
77 sem_post(&pause_sem);
78 sem_post(&timer);
79 sleep(refresh_display/NSEC_PER_SEC);
80 }
81}
82
83void *ncurses_display(void *p)
84{
85 unsigned int current_display_index = 0;
86
87 sem_wait(&bootstrap);
88 init_ncurses();
89
90 while (1) {
91 sem_wait(&timer);
92 sem_wait(&goodtodisplay);
93 sem_wait(&pause_sem);
94
95 copy = g_ptr_array_index(copies, current_display_index);
96 if (copy)
97 display(current_display_index++);
98
99 sem_post(&goodtoupdate);
100 sem_post(&pause_sem);
101
102 if (quit) {
103 reset_ncurses();
104 pthread_exit(0);
105 }
106 }
107}
108
109/*
110 * hook on each event to check the timestamp and refresh the display if
111 * necessary
112 */
113enum bt_cb_ret check_timestamp(struct bt_ctf_event *call_data, void *private_data)
114{
115 unsigned long timestamp;
116
117 timestamp = bt_ctf_get_timestamp(call_data);
118 if (timestamp == -1ULL)
119 goto error;
120
121 if (last_display_update == 0)
122 last_display_update = timestamp;
123
124 if (timestamp - last_display_update >= refresh_display) {
125 sem_wait(&goodtoupdate);
126 g_ptr_array_add(copies, get_copy_lttngtop(last_display_update,
127 timestamp));
128 sem_post(&goodtodisplay);
129 sem_post(&bootstrap);
130 last_display_update = timestamp;
131 }
132 return BT_CB_OK;
133
134error:
135 fprintf(stderr, "check_timestamp callback error\n");
136 return BT_CB_ERROR_STOP;
137}
138
139/*
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)
142 */
143struct perfcounter *get_perf_counter(const char *name, struct processtop *proc,
144 struct cputime *cpu)
145{
146 struct perfcounter *ret, *global;
147 GHashTable *table;
148
149 if (proc)
150 table = proc->perf;
151 else if (cpu)
152 table = cpu->perf;
153 else
154 goto error;
155
156 ret = g_hash_table_lookup(table, (gpointer) name);
157 if (ret)
158 goto end;
159
160 ret = malloc(sizeof(struct perfcounter));
161 memset(ret, 0, sizeof(struct perfcounter));
162 /* by default, make it visible in the UI */
163 ret->visible = 1;
164 g_hash_table_insert(table, (gpointer) name, ret);
165
166 global = g_hash_table_lookup(lttngtop.perf_list, (gpointer) name);
167 if (!global) {
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)
173 global->sort = 1;
174 g_hash_table_insert(lttngtop.perf_list, (gpointer) name, global);
175 }
176
177end:
178 return ret;
179
180error:
181 return NULL;
182}
183
184void update_perf_value(struct processtop *proc, struct cputime *cpu,
185 const char *name, int value)
186{
187 struct perfcounter *cpu_perf, *process_perf;
188
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;
194 }
195}
196
197void extract_perf_counter_scope(struct bt_ctf_event *event,
198 struct definition *scope,
199 struct processtop *proc,
200 struct cputime *cpu)
201{
202 struct definition const * const *list = NULL;
203 unsigned int count;
204 int i, ret;
205
206 if (!scope)
207 goto end;
208
209 ret = bt_ctf_get_field_list(event, scope, &list, &count);
210 if (ret < 0)
211 goto end;
212
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())
218 continue;
219 update_perf_value(proc, cpu, name, value);
220 }
221 }
222
223end:
224 return;
225}
226
227void update_perf_counter(struct processtop *proc, struct bt_ctf_event *event)
228{
229 struct definition *scope;
230 uint64_t cpu_id;
231 struct cputime *cpu;
232
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");
237 goto end;
238 }
239 cpu = get_cpu(cpu_id);
240
241 scope = bt_ctf_get_top_level_scope(event, BT_STREAM_EVENT_CONTEXT);
242 extract_perf_counter_scope(event, scope, proc, cpu);
243
244 scope = bt_ctf_get_top_level_scope(event, BT_STREAM_PACKET_CONTEXT);
245 extract_perf_counter_scope(event, scope, proc, cpu);
246
247 scope = bt_ctf_get_top_level_scope(event, BT_EVENT_CONTEXT);
248 extract_perf_counter_scope(event, scope, proc, cpu);
249
250end:
251 return;
252}
253
254enum bt_cb_ret fix_process_table(struct bt_ctf_event *call_data,
255 void *private_data)
256{
257 int pid, tid, ppid;
258 char *comm;
259 struct processtop *parent, *child;
260 struct definition *scope;
261 unsigned long timestamp;
262
263 /* FIXME : check context pid, tid, ppid and comm */
264
265 timestamp = bt_ctf_get_timestamp(call_data);
266 if (timestamp == -1ULL)
267 goto error;
268
269 scope = bt_ctf_get_top_level_scope(call_data, BT_STREAM_EVENT_CONTEXT);
270
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");
274 goto error;
275 }
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");
279 goto error;
280 }
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");
284 goto error;
285 }
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");
289 goto error;
290 }
291
292 /* find or create the current process */
293 child = find_process_tid(&lttngtop, tid, comm);
294 if (!child)
295 child = add_proc(&lttngtop, tid, comm, timestamp);
296 update_proc(child, pid, tid, ppid, comm);
297
298 if (pid != tid) {
299 /* find or create the parent */
300 parent = find_process_tid(&lttngtop, pid, comm);
301 if (!parent) {
302 parent = add_proc(&lttngtop, pid, comm, timestamp);
303 parent->pid = pid;
304 }
305
306 /* attach the parent to the current process */
307 child->threadparent = parent;
308 add_thread(parent, child);
309 }
310
311 update_perf_counter(child, call_data);
312
313 return BT_CB_OK;
314
315error:
316 return BT_CB_ERROR_STOP;
317}
318
319void init_lttngtop()
320{
321 copies = g_ptr_array_new();
322 lttngtop.perf_list = g_hash_table_new(g_direct_hash, g_direct_equal);
323
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);
330
331 lttngtop.process_table = g_ptr_array_new();
332 lttngtop.files_table = g_ptr_array_new();
333 lttngtop.cpu_table = g_ptr_array_new();
334}
335
336void usage(FILE *fd)
337{
338
339}
340
341/*
342 * Return 0 if caller should continue, < 0 if caller should return
343 * error, > 0 if caller should exit without reporting error.
344 */
345static int parse_options(int argc, char **argv)
346{
347 poptContext pc;
348 int opt, ret = 0;
349
350 if (argc == 1) {
351 usage(stdout);
352 return 1; /* exit cleanly */
353 }
354
355 pc = poptGetContext(NULL, argc, (const char **) argv, long_options, 0);
356 poptReadDefaultConfig(pc, 0);
357
358 while ((opt = poptGetNextOpt(pc)) != -1) {
359 switch (opt) {
360 case OPT_HELP:
361 usage(stdout);
362 ret = 1; /* exit cleanly */
363 goto end;
364 case OPT_LIST:
365 // list_formats(stdout);
366 ret = 1;
367 goto end;
368 case OPT_VERBOSE:
369// babeltrace_verbose = 1;
370 break;
371 case OPT_DEBUG:
372// babeltrace_debug = 1;
373 break;
374 case OPT_NAMES:
375// opt_field_names = 1;
376 break;
377 default:
378 ret = -EINVAL;
379 goto end;
380 }
381 }
382
383 opt_input_path = poptGetArg(pc);
384 if (!opt_input_path) {
385 ret = -EINVAL;
386 goto end;
387 }
388end:
389 if (pc) {
390 poptFreeContext(pc);
391 }
392 return ret;
393}
394
395void iter_trace(struct bt_context *bt_ctx)
396{
397 struct bt_ctf_iter *iter;
398 struct bt_iter_pos begin_pos;
399 struct bt_ctf_event *event;
400 int ret = 0;
401
402 begin_pos.type = BT_SEEK_BEGIN;
403 iter = bt_ctf_iter_create(bt_ctx, &begin_pos, NULL);
404
405 /* at each event check if we need to refresh */
406 bt_ctf_iter_add_callback(iter, 0, NULL, 0,
407 check_timestamp,
408 NULL, NULL, NULL);
409 /* at each event, verify the status of the process table */
410 bt_ctf_iter_add_callback(iter, 0, NULL, 0,
411 fix_process_table,
412 NULL, NULL, NULL);
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);
421
422 /* for IO top */
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));
434 if (ret < 0)
435 goto end_iter;
436 }
437
438 /* block until quit, we reached the end of the trace */
439 sem_wait(&end_trace_sem);
440
441end_iter:
442 bt_iter_destroy(bt_ctf_get_iter(iter));
443}
444
445/*
446 * bt_context_add_traces_recursive: Open a trace recursively
447 * (copied from BSD code in converter/babeltrace.c)
448 *
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.
452 *
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;
456 */
457int 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))
461{
462 FTS *tree;
463 FTSENT *node;
464 GArray *trace_ids;
465 char lpath[PATH_MAX];
466 char * const paths[2] = { lpath, NULL };
467 int ret;
468
469 /*
470 * Need to copy path, because fts_open can change it.
471 * It is the pointer array, not the strings, that are constant.
472 */
473 strncpy(lpath, path, PATH_MAX);
474 lpath[PATH_MAX - 1] = '\0';
475
476 tree = fts_open(paths, FTS_NOCHDIR | FTS_LOGICAL, 0);
477 if (tree == NULL) {
478 fprintf(stderr, "[error] [Context] Cannot traverse \"%s\" for reading.\n",
479 path);
480 return -EINVAL;
481 }
482
483 trace_ids = g_array_new(FALSE, TRUE, sizeof(int));
484
485 while ((node = fts_read(tree))) {
486 int dirfd, metafd;
487
488 if (!(node->fts_info & FTS_D))
489 continue;
490
491 dirfd = open(node->fts_accpath, 0);
492 if (dirfd < 0) {
493 fprintf(stderr, "[error] [Context] Unable to open trace "
494 "directory file descriptor.\n");
495 ret = dirfd;
496 goto error;
497 }
498 metafd = openat(dirfd, "metadata", O_RDONLY);
499 if (metafd < 0) {
500 ret = close(dirfd);
501 if (ret < 0) {
502 perror("close");
503 goto error;
504 }
505 } else {
506 int trace_id;
507
508 ret = close(metafd);
509 if (ret < 0) {
510 perror("close");
511 goto error;
512 }
513 ret = close(dirfd);
514 if (ret < 0) {
515 perror("close");
516 goto error;
517 }
518
519 trace_id = bt_context_add_trace(ctx,
520 node->fts_accpath, format_str,
521 packet_seek, NULL, NULL);
522 if (trace_id < 0) {
523 fprintf(stderr, "[error] [Context] opening trace \"%s\" from %s "
524 "for reading.\n", node->fts_accpath, path);
525 ret = trace_id;
526 goto error;
527 }
528 g_array_append_val(trace_ids, trace_id);
529 }
530 }
531
532 g_array_free(trace_ids, TRUE);
533 return 0;
534
535error:
536 return ret;
537}
538
539int main(int argc, char **argv)
540{
541 int ret;
542 struct bt_context *bt_ctx = NULL;
543
544 ret = parse_options(argc, argv);
545 if (ret < 0) {
546 fprintf(stdout, "Error parsing options.\n\n");
547 usage(stdout);
548 exit(EXIT_FAILURE);
549 } else if (ret > 0) {
550 exit(EXIT_SUCCESS);
551 }
552
553 init_lttngtop();
554
555 bt_ctx = bt_context_create();
556 ret = bt_context_add_traces_recursive(bt_ctx, opt_input_path, "ctf", NULL);
557 if (ret < 0) {
558 printf("[error] Opening the trace\n");
559 goto end;
560 }
561
562 pthread_create(&display_thread, NULL, ncurses_display, (void *) NULL);
563 pthread_create(&timer_thread, NULL, refresh_thread, (void *) NULL);
564
565 iter_trace(bt_ctx);
566
567 quit = 1;
568 pthread_join(display_thread, NULL);
569
570end:
571 return 0;
572}
This page took 0.041211 seconds and 4 git commands to generate.