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