exit cleanly on ctrl+c in textdump live
[lttngtop.git] / src / lttngtop.c
CommitLineData
1fc22eb4 1/*
aa15ac1c 2 * Copyright (C) 2011-2012 Julien Desfossez
1fc22eb4
JD
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 *
71bd7ce1
AM
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.
1fc22eb4
JD
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>
3ba84bed 25#include <babeltrace/ctf/iterator.h>
1fc22eb4
JD
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>
16b22a0f
JD
40#include <sys/mman.h>
41#include <lttng/lttng.h>
42#include <lttng/lttngtop-helper.h>
c78f2cdc 43#include <babeltrace/lttngtopmmappacketseek.h>
1fc22eb4
JD
44
45#include "lttngtoptypes.h"
46#include "cputop.h"
47#include "iostreamtop.h"
48#include "cursesdisplay.h"
49#include "common.h"
50
51#define DEFAULT_FILE_ARRAY_SIZE 1
52
53const char *opt_input_path;
32b70e07 54static int opt_textdump;
32b70e07 55static int opt_child;
1fc22eb4 56
ae9e85c7
JD
57int quit = 0;
58
1fc22eb4
JD
59struct lttngtop *copy;
60pthread_t display_thread;
61pthread_t timer_thread;
62
63unsigned long refresh_display = 1 * NSEC_PER_SEC;
64unsigned long last_display_update = 0;
1fc22eb4 65
16b22a0f
JD
66/* list of FDs available for being read with snapshots */
67struct mmap_stream_list mmap_list;
68GPtrArray *lttng_consumer_stream_array;
69int sessiond_metadata, consumerd_metadata;
4e6aeb3d
JD
70struct lttng_consumer_local_data *ctx = NULL;
71/* list of snapshots currently not consumed */
72GPtrArray *available_snapshots;
73sem_t metadata_available;
050f9cf9 74int reload_trace = 0;
16b22a0f 75
1fc22eb4
JD
76enum {
77 OPT_NONE = 0,
78 OPT_HELP,
6777529d 79 OPT_TEXTDUMP,
32b70e07
JD
80 OPT_PID,
81 OPT_CHILD,
1fc22eb4
JD
82};
83
84static struct poptOption long_options[] = {
85 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
86 { "help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL },
6777529d 87 { "textdump", 't', POPT_ARG_NONE, NULL, OPT_TEXTDUMP, NULL, NULL },
32b70e07
JD
88 { "child", 'f', POPT_ARG_NONE, NULL, OPT_CHILD, NULL, NULL },
89 { "pid", 'p', POPT_ARG_INT, &opt_pid, OPT_PID, NULL, NULL },
1fc22eb4
JD
90 { NULL, 0, 0, NULL, 0, NULL, NULL },
91};
92
24797198
JD
93static void handle_textdump_sigterm(int signal)
94{
95 quit = 1;
96 lttng_destroy_session("test");
97}
98
1fc22eb4
JD
99void *refresh_thread(void *p)
100{
cade3418
JD
101 struct mmap_stream *mmap_info;
102
1fc22eb4 103 while (1) {
ae9e85c7
JD
104 if (quit) {
105 sem_post(&pause_sem);
106 sem_post(&timer);
1402044a 107 sem_post(&end_trace_sem);
ae9e85c7 108 sem_post(&goodtodisplay);
1402044a 109 sem_post(&goodtoupdate);
ae9e85c7
JD
110 pthread_exit(0);
111 }
1402044a 112 if (!opt_input_path) {
d2d680e0
JD
113 bt_list_for_each_entry(mmap_info, &mmap_list.head, list)
114 helper_kernctl_buffer_flush(mmap_info->fd);
1402044a 115 }
1fc22eb4
JD
116 sem_wait(&pause_sem);
117 sem_post(&pause_sem);
118 sem_post(&timer);
119 sleep(refresh_display/NSEC_PER_SEC);
120 }
121}
122
123void *ncurses_display(void *p)
124{
125 unsigned int current_display_index = 0;
126
127 sem_wait(&bootstrap);
b332d28f
JD
128 /*
129 * Prevent the 1 second delay when we hit ESC
130 */
131 ESCDELAY = 0;
1fc22eb4
JD
132 init_ncurses();
133
134 while (1) {
135 sem_wait(&timer);
136 sem_wait(&goodtodisplay);
137 sem_wait(&pause_sem);
138
ae9e85c7 139 if (quit) {
33572a17
JD
140 sem_post(&pause_sem);
141 sem_post(&timer);
ae9e85c7
JD
142 reset_ncurses();
143 pthread_exit(0);
144 }
145
1fc22eb4 146 copy = g_ptr_array_index(copies, current_display_index);
85db4618
JD
147 assert(copy);
148 display(current_display_index++);
1fc22eb4
JD
149
150 sem_post(&goodtoupdate);
151 sem_post(&pause_sem);
1fc22eb4
JD
152 }
153}
154
6112d0d4
JD
155/*
156 * hook on each event to check the timestamp and refresh the display if
157 * necessary
158 */
159enum bt_cb_ret print_timestamp(struct bt_ctf_event *call_data, void *private_data)
160{
161 unsigned long timestamp;
162 struct tm start;
b520ab45 163 uint64_t ts_nsec_start;
32b70e07 164 int pid;
d2d680e0
JD
165 int64_t syscall_ret;
166 const struct definition *scope;
6112d0d4 167
6112d0d4
JD
168 timestamp = bt_ctf_get_timestamp(call_data);
169
b520ab45 170 start = format_timestamp(timestamp);
6112d0d4
JD
171 ts_nsec_start = timestamp % NSEC_PER_SEC;
172
32b70e07
JD
173 pid = get_context_pid(call_data);
174 if (pid == -1ULL && opt_pid) {
175 goto error;
176 }
177
178 if (opt_pid && opt_pid != pid)
179 goto end;
180
d2d680e0
JD
181 if (strcmp(bt_ctf_event_name(call_data), "exit_syscall") == 0) {
182 scope = bt_ctf_get_top_level_scope(call_data,
183 BT_EVENT_FIELDS);
184 syscall_ret = bt_ctf_get_int64(bt_ctf_get_field(call_data,
185 scope, "_ret"));
186 printf("= %ld\n", syscall_ret);
187 } else {
188 printf("%02d:%02d:%02d.%09" PRIu64 " %d : %s ", start.tm_hour,
189 start.tm_min, start.tm_sec, ts_nsec_start,
190 pid, bt_ctf_event_name(call_data));
191 }
6112d0d4 192
32b70e07 193end:
6112d0d4 194 return BT_CB_OK;
32b70e07
JD
195error:
196 return BT_CB_ERROR_STOP;
6112d0d4
JD
197}
198
1fc22eb4
JD
199/*
200 * hook on each event to check the timestamp and refresh the display if
201 * necessary
202 */
4adc8274 203enum bt_cb_ret check_timestamp(struct bt_ctf_event *call_data, void *private_data)
1fc22eb4
JD
204{
205 unsigned long timestamp;
206
c78f2cdc 207 timestamp = bt_ctf_get_timestamp(call_data);
1fc22eb4
JD
208 if (timestamp == -1ULL)
209 goto error;
210
211 if (last_display_update == 0)
212 last_display_update = timestamp;
213
214 if (timestamp - last_display_update >= refresh_display) {
215 sem_wait(&goodtoupdate);
216 g_ptr_array_add(copies, get_copy_lttngtop(last_display_update,
217 timestamp));
218 sem_post(&goodtodisplay);
219 sem_post(&bootstrap);
220 last_display_update = timestamp;
221 }
222 return BT_CB_OK;
223
224error:
225 fprintf(stderr, "check_timestamp callback error\n");
226 return BT_CB_ERROR_STOP;
227}
228
229/*
230 * get_perf_counter : get or create and return a perf_counter struct for
231 * either a process or a cpu (only one of the 2 parameters mandatory)
232 */
233struct perfcounter *get_perf_counter(const char *name, struct processtop *proc,
234 struct cputime *cpu)
235{
bb6d72a2 236 struct perfcounter *ret;
1fc22eb4
JD
237 GHashTable *table;
238
239 if (proc)
240 table = proc->perf;
241 else if (cpu)
242 table = cpu->perf;
243 else
244 goto error;
245
246 ret = g_hash_table_lookup(table, (gpointer) name);
247 if (ret)
248 goto end;
249
559c9f86 250 ret = g_new0(struct perfcounter, 1);
1fc22eb4
JD
251 /* by default, make it visible in the UI */
252 ret->visible = 1;
85db4618 253 g_hash_table_insert(table, (gpointer) strdup(name), ret);
1fc22eb4 254
1fc22eb4
JD
255end:
256 return ret;
257
258error:
259 return NULL;
260}
261
262void update_perf_value(struct processtop *proc, struct cputime *cpu,
263 const char *name, int value)
264{
265 struct perfcounter *cpu_perf, *process_perf;
266
267 cpu_perf = get_perf_counter(name, NULL, cpu);
268 if (cpu_perf->count < value) {
269 process_perf = get_perf_counter(name, proc, NULL);
270 process_perf->count += value - cpu_perf->count;
271 cpu_perf->count = value;
272 }
273}
274
4adc8274 275void extract_perf_counter_scope(const struct bt_ctf_event *event,
2e0a1190 276 const struct bt_definition *scope,
1fc22eb4
JD
277 struct processtop *proc,
278 struct cputime *cpu)
279{
2e0a1190
JD
280 struct bt_definition const * const *list = NULL;
281 const struct bt_definition *field;
1fc22eb4 282 unsigned int count;
bb6d72a2
JD
283 struct perfcounter *perfcounter;
284 GHashTableIter iter;
285 gpointer key;
286 int ret;
1fc22eb4
JD
287
288 if (!scope)
289 goto end;
290
291 ret = bt_ctf_get_field_list(event, scope, &list, &count);
292 if (ret < 0)
293 goto end;
294
bb6d72a2
JD
295 if (count == 0)
296 goto end;
297
298 g_hash_table_iter_init(&iter, global_perf_liszt);
299 while (g_hash_table_iter_next (&iter, &key, (gpointer) &perfcounter)) {
300 field = bt_ctf_get_field(event, scope, (char *) key);
301 if (field) {
302 int value = bt_ctf_get_uint64(field);
1fc22eb4
JD
303 if (bt_ctf_field_get_error())
304 continue;
bb6d72a2 305 update_perf_value(proc, cpu, (char *) key, value);
1fc22eb4
JD
306 }
307 }
308
309end:
310 return;
311}
312
4adc8274 313void update_perf_counter(struct processtop *proc, const struct bt_ctf_event *event)
1fc22eb4 314{
1fc22eb4 315 struct cputime *cpu;
2e0a1190 316 const struct bt_definition *scope;
1fc22eb4 317
d67167cd 318 cpu = get_cpu(get_cpu_id(event));
1fc22eb4
JD
319
320 scope = bt_ctf_get_top_level_scope(event, BT_STREAM_EVENT_CONTEXT);
321 extract_perf_counter_scope(event, scope, proc, cpu);
322
323 scope = bt_ctf_get_top_level_scope(event, BT_STREAM_PACKET_CONTEXT);
324 extract_perf_counter_scope(event, scope, proc, cpu);
325
326 scope = bt_ctf_get_top_level_scope(event, BT_EVENT_CONTEXT);
327 extract_perf_counter_scope(event, scope, proc, cpu);
1fc22eb4
JD
328}
329
4adc8274 330enum bt_cb_ret fix_process_table(struct bt_ctf_event *call_data,
1fc22eb4
JD
331 void *private_data)
332{
1402044a 333 int pid, tid, ppid, vpid, vtid, vppid;
1fc22eb4
JD
334 char *comm;
335 struct processtop *parent, *child;
1fc22eb4
JD
336 unsigned long timestamp;
337
c78f2cdc 338 timestamp = bt_ctf_get_timestamp(call_data);
1fc22eb4
JD
339 if (timestamp == -1ULL)
340 goto error;
341
1dec520a
JD
342 pid = get_context_pid(call_data);
343 if (pid == -1ULL) {
1fc22eb4
JD
344 goto error;
345 }
96aa77de 346
1dec520a
JD
347 tid = get_context_tid(call_data);
348 if (tid == -1ULL) {
1fc22eb4
JD
349 goto error;
350 }
1dec520a
JD
351 ppid = get_context_ppid(call_data);
352 if (ppid == -1ULL) {
1fc22eb4
JD
353 goto error;
354 }
1402044a
JD
355 vpid = get_context_vpid(call_data);
356 if (pid == -1ULL) {
357 vpid = -1;
358 }
359 vtid = get_context_vtid(call_data);
360 if (tid == -1ULL) {
361 vtid = -1;
362 }
363 vppid = get_context_vppid(call_data);
364 if (ppid == -1ULL) {
365 vppid = -1;
366 }
1dec520a
JD
367 comm = get_context_comm(call_data);
368 if (!comm) {
1fc22eb4
JD
369 goto error;
370 }
371
372 /* find or create the current process */
373 child = find_process_tid(&lttngtop, tid, comm);
374 if (!child)
375 child = add_proc(&lttngtop, tid, comm, timestamp);
96aa77de
JD
376 if (!child)
377 goto end;
1402044a 378 update_proc(child, pid, tid, ppid, vpid, vtid, vppid, comm);
1fc22eb4
JD
379
380 if (pid != tid) {
381 /* find or create the parent */
382 parent = find_process_tid(&lttngtop, pid, comm);
383 if (!parent) {
384 parent = add_proc(&lttngtop, pid, comm, timestamp);
96aa77de
JD
385 if (parent)
386 parent->pid = pid;
1fc22eb4
JD
387 }
388
389 /* attach the parent to the current process */
390 child->threadparent = parent;
391 add_thread(parent, child);
392 }
393
394 update_perf_counter(child, call_data);
395
96aa77de 396end:
1fc22eb4
JD
397 return BT_CB_OK;
398
399error:
400 return BT_CB_ERROR_STOP;
401}
402
403void init_lttngtop()
404{
405 copies = g_ptr_array_new();
0d91c12a 406 global_perf_liszt = g_hash_table_new(g_str_hash, g_str_equal);
1fc22eb4
JD
407
408 sem_init(&goodtodisplay, 0, 0);
409 sem_init(&goodtoupdate, 0, 1);
410 sem_init(&timer, 0, 1);
411 sem_init(&bootstrap, 0, 0);
412 sem_init(&pause_sem, 0, 1);
413 sem_init(&end_trace_sem, 0, 0);
414
e05a35a6
JD
415 reset_global_counters();
416 lttngtop.nbproc = 0;
417 lttngtop.nbthreads = 0;
418 lttngtop.nbfiles = 0;
419
30b646c4
JD
420 lttngtop.process_hash_table = g_hash_table_new(g_direct_hash,
421 g_direct_equal);
1fc22eb4
JD
422 lttngtop.process_table = g_ptr_array_new();
423 lttngtop.files_table = g_ptr_array_new();
424 lttngtop.cpu_table = g_ptr_array_new();
425}
426
c263c4eb 427void usage(FILE *fp)
1fc22eb4 428{
c263c4eb
JD
429 fprintf(fp, "LTTngTop %s\n\n", VERSION);
430 fprintf(fp, "Usage : lttngtop /path/to/trace\n");
1fc22eb4
JD
431}
432
433/*
434 * Return 0 if caller should continue, < 0 if caller should return
435 * error, > 0 if caller should exit without reporting error.
436 */
437static int parse_options(int argc, char **argv)
438{
439 poptContext pc;
440 int opt, ret = 0;
441
1fc22eb4
JD
442 pc = poptGetContext(NULL, argc, (const char **) argv, long_options, 0);
443 poptReadDefaultConfig(pc, 0);
444
445 while ((opt = poptGetNextOpt(pc)) != -1) {
446 switch (opt) {
447 case OPT_HELP:
448 usage(stdout);
449 ret = 1; /* exit cleanly */
450 goto end;
6777529d
JD
451 case OPT_TEXTDUMP:
452 opt_textdump = 1;
32b70e07
JD
453 break;
454 case OPT_CHILD:
455 opt_textdump = 1;
456 opt_child = 1;
457 break;
458 case OPT_PID:
96aa77de 459 //opt_textdump = 1;
32b70e07 460 break;
1fc22eb4
JD
461 default:
462 ret = -EINVAL;
463 goto end;
464 }
465 }
466
467 opt_input_path = poptGetArg(pc);
16b22a0f 468
1fc22eb4
JD
469end:
470 if (pc) {
471 poptFreeContext(pc);
472 }
473 return ret;
474}
475
476void iter_trace(struct bt_context *bt_ctx)
477{
478 struct bt_ctf_iter *iter;
479 struct bt_iter_pos begin_pos;
4adc8274 480 const struct bt_ctf_event *event;
1fc22eb4
JD
481 int ret = 0;
482
483 begin_pos.type = BT_SEEK_BEGIN;
484 iter = bt_ctf_iter_create(bt_ctx, &begin_pos, NULL);
485
6777529d
JD
486 if (opt_textdump) {
487 bt_ctf_iter_add_callback(iter, 0, NULL, 0,
488 print_timestamp,
489 NULL, NULL, NULL);
490 } else {
491 /* at each event check if we need to refresh */
492 bt_ctf_iter_add_callback(iter, 0, NULL, 0,
493 check_timestamp,
494 NULL, NULL, NULL);
495 /* at each event, verify the status of the process table */
496 bt_ctf_iter_add_callback(iter, 0, NULL, 0,
497 fix_process_table,
498 NULL, NULL, NULL);
499 /* to handle the scheduling events */
500 bt_ctf_iter_add_callback(iter,
501 g_quark_from_static_string("sched_switch"),
502 NULL, 0, handle_sched_switch, NULL, NULL, NULL);
503 /* to clean up the process table */
504 bt_ctf_iter_add_callback(iter,
505 g_quark_from_static_string("sched_process_free"),
506 NULL, 0, handle_sched_process_free, NULL, NULL, NULL);
507 /* to get all the process from the statedumps */
508 bt_ctf_iter_add_callback(iter,
509 g_quark_from_static_string(
510 "lttng_statedump_process_state"),
511 NULL, 0, handle_statedump_process_state,
512 NULL, NULL, NULL);
513
514 /* for IO top */
515 bt_ctf_iter_add_callback(iter,
516 g_quark_from_static_string("exit_syscall"),
517 NULL, 0, handle_exit_syscall, NULL, NULL, NULL);
518 bt_ctf_iter_add_callback(iter,
519 g_quark_from_static_string("sys_write"),
520 NULL, 0, handle_sys_write, NULL, NULL, NULL);
521 bt_ctf_iter_add_callback(iter,
522 g_quark_from_static_string("sys_read"),
523 NULL, 0, handle_sys_read, NULL, NULL, NULL);
524 bt_ctf_iter_add_callback(iter,
525 g_quark_from_static_string("sys_open"),
526 NULL, 0, handle_sys_open, NULL, NULL, NULL);
527 bt_ctf_iter_add_callback(iter,
528 g_quark_from_static_string("sys_close"),
529 NULL, 0, handle_sys_close, NULL, NULL, NULL);
530 bt_ctf_iter_add_callback(iter,
531 g_quark_from_static_string(
ceb3a221 532 "lttng_statedump_file_descriptor"),
6777529d
JD
533 NULL, 0, handle_statedump_file_descriptor,
534 NULL, NULL, NULL);
535 }
536
6db97c51 537 while ((event = bt_ctf_iter_read_event(iter, NULL)) != NULL) {
050f9cf9 538 if (quit || reload_trace)
ae9e85c7 539 goto end_iter;
1fc22eb4
JD
540 ret = bt_iter_next(bt_ctf_get_iter(iter));
541 if (ret < 0)
542 goto end_iter;
543 }
544
545 /* block until quit, we reached the end of the trace */
546 sem_wait(&end_trace_sem);
547
548end_iter:
06570214 549 bt_ctf_iter_destroy(iter);
1fc22eb4
JD
550}
551
552/*
553 * bt_context_add_traces_recursive: Open a trace recursively
554 * (copied from BSD code in converter/babeltrace.c)
555 *
556 * Find each trace present in the subdirectory starting from the given
557 * path, and add them to the context. The packet_seek parameter can be
558 * NULL: this specify to use the default format packet_seek.
559 *
560 * Return: 0 on success, nonzero on failure.
561 * Unable to open toplevel: failure.
562 * Unable to open some subdirectory or file: warn and continue;
563 */
564int bt_context_add_traces_recursive(struct bt_context *ctx, const char *path,
565 const char *format_str,
2e0a1190 566 void (*packet_seek)(struct bt_stream_pos *pos,
1fc22eb4
JD
567 size_t offset, int whence))
568{
569 FTS *tree;
570 FTSENT *node;
571 GArray *trace_ids;
572 char lpath[PATH_MAX];
573 char * const paths[2] = { lpath, NULL };
0d567cf1 574 int ret = -1;
1fc22eb4
JD
575
576 /*
577 * Need to copy path, because fts_open can change it.
578 * It is the pointer array, not the strings, that are constant.
579 */
580 strncpy(lpath, path, PATH_MAX);
581 lpath[PATH_MAX - 1] = '\0';
582
583 tree = fts_open(paths, FTS_NOCHDIR | FTS_LOGICAL, 0);
584 if (tree == NULL) {
585 fprintf(stderr, "[error] [Context] Cannot traverse \"%s\" for reading.\n",
586 path);
587 return -EINVAL;
588 }
589
590 trace_ids = g_array_new(FALSE, TRUE, sizeof(int));
591
592 while ((node = fts_read(tree))) {
593 int dirfd, metafd;
594
595 if (!(node->fts_info & FTS_D))
596 continue;
597
598 dirfd = open(node->fts_accpath, 0);
599 if (dirfd < 0) {
600 fprintf(stderr, "[error] [Context] Unable to open trace "
601 "directory file descriptor.\n");
602 ret = dirfd;
603 goto error;
604 }
605 metafd = openat(dirfd, "metadata", O_RDONLY);
606 if (metafd < 0) {
7314c855
JD
607 close(dirfd);
608 ret = -1;
609 continue;
1fc22eb4
JD
610 } else {
611 int trace_id;
612
613 ret = close(metafd);
614 if (ret < 0) {
615 perror("close");
616 goto error;
617 }
618 ret = close(dirfd);
619 if (ret < 0) {
620 perror("close");
621 goto error;
622 }
623
624 trace_id = bt_context_add_trace(ctx,
625 node->fts_accpath, format_str,
626 packet_seek, NULL, NULL);
627 if (trace_id < 0) {
0d567cf1 628 fprintf(stderr, "[warning] [Context] opening trace \"%s\" from %s "
1fc22eb4 629 "for reading.\n", node->fts_accpath, path);
0d567cf1
JD
630 /* Allow to skip erroneous traces. */
631 continue;
1fc22eb4
JD
632 }
633 g_array_append_val(trace_ids, trace_id);
634 }
635 }
636
637 g_array_free(trace_ids, TRUE);
0d567cf1 638 return ret;
1fc22eb4
JD
639
640error:
641 return ret;
642}
643
4553b4d1 644static int check_field_requirements(const struct bt_ctf_field_decl *const * field_list,
a54d0d2c
JD
645 int field_cnt, int *tid_check, int *pid_check,
646 int *procname_check, int *ppid_check)
647{
648 int j;
bb6d72a2
JD
649 struct perfcounter *global;
650 const char *name;
a54d0d2c
JD
651
652 for (j = 0; j < field_cnt; j++) {
bb6d72a2 653 name = bt_ctf_get_decl_field_name(field_list[j]);
a54d0d2c 654 if (*tid_check == 0) {
bb6d72a2 655 if (strncmp(name, "tid", 3) == 0)
a54d0d2c 656 (*tid_check)++;
a54d0d2c
JD
657 }
658 if (*pid_check == 0) {
da401e9c 659 if (strncmp(name, "pid", 3) == 0)
a54d0d2c
JD
660 (*pid_check)++;
661 }
662 if (*ppid_check == 0) {
bb6d72a2 663 if (strncmp(name, "ppid", 4) == 0)
a54d0d2c
JD
664 (*ppid_check)++;
665 }
666 if (*procname_check == 0) {
bb6d72a2 667 if (strncmp(name, "procname", 8) == 0)
a54d0d2c
JD
668 (*procname_check)++;
669 }
bb6d72a2
JD
670 if (strncmp(name, "perf_", 5) == 0) {
671 global = g_hash_table_lookup(global_perf_liszt, (gpointer) name);
672 if (!global) {
673 global = g_new0(struct perfcounter, 1);
674 /* by default, sort on the first perf context */
675 if (g_hash_table_size(global_perf_liszt) == 0)
676 global->sort = 1;
677 global->visible = 1;
678 g_hash_table_insert(global_perf_liszt, (gpointer) strdup(name), global);
679 }
680 }
a54d0d2c 681 }
bb6d72a2 682
a54d0d2c
JD
683 if (*tid_check == 1 && *pid_check == 1 && *ppid_check == 1 &&
684 *procname_check == 1)
685 return 0;
686
687 return -1;
688}
689
690/*
691 * check_requirements: check if the required context informations are available
692 *
693 * If each mandatory context information is available for at least in one
694 * event, return 0 otherwise return -1.
a54d0d2c
JD
695 */
696int check_requirements(struct bt_context *ctx)
697{
698 unsigned int i, evt_cnt, field_cnt;
699 struct bt_ctf_event_decl *const * evt_list;
700 const struct bt_ctf_field_decl *const * field_list;
701 int tid_check = 0;
702 int pid_check = 0;
703 int procname_check = 0;
704 int ppid_check = 0;
705 int ret = 0;
706
707 bt_ctf_get_event_decl_list(0, ctx, &evt_list, &evt_cnt);
708 for (i = 0; i < evt_cnt; i++) {
709 bt_ctf_get_decl_fields(evt_list[i], BT_STREAM_EVENT_CONTEXT,
710 &field_list, &field_cnt);
711 ret = check_field_requirements(field_list, field_cnt,
712 &tid_check, &pid_check, &procname_check,
713 &ppid_check);
a54d0d2c
JD
714
715 bt_ctf_get_decl_fields(evt_list[i], BT_EVENT_CONTEXT,
716 &field_list, &field_cnt);
717 ret = check_field_requirements(field_list, field_cnt,
718 &tid_check, &pid_check, &procname_check,
719 &ppid_check);
a54d0d2c
JD
720
721 bt_ctf_get_decl_fields(evt_list[i], BT_STREAM_PACKET_CONTEXT,
722 &field_list, &field_cnt);
723 ret = check_field_requirements(field_list, field_cnt,
724 &tid_check, &pid_check, &procname_check,
725 &ppid_check);
a54d0d2c
JD
726 }
727
728 if (tid_check == 0) {
729 ret = -1;
730 fprintf(stderr, "[error] missing tid context information\n");
731 }
732 if (pid_check == 0) {
733 ret = -1;
734 fprintf(stderr, "[error] missing pid context information\n");
735 }
736 if (ppid_check == 0) {
737 ret = -1;
738 fprintf(stderr, "[error] missing ppid context information\n");
739 }
740 if (procname_check == 0) {
741 ret = -1;
742 fprintf(stderr, "[error] missing procname context information\n");
743 }
744
a54d0d2c
JD
745 return ret;
746}
747
16b22a0f
JD
748ssize_t read_subbuffer(struct lttng_consumer_stream *kconsumerd_fd,
749 struct lttng_consumer_local_data *ctx)
750{
751 unsigned long len;
752 int err;
753 long ret = 0;
754 int infd = helper_get_lttng_consumer_stream_wait_fd(kconsumerd_fd);
755
756 if (helper_get_lttng_consumer_stream_output(kconsumerd_fd) == LTTNG_EVENT_SPLICE) {
757 /* Get the next subbuffer */
16b22a0f
JD
758 err = helper_kernctl_get_next_subbuf(infd);
759 if (err != 0) {
760 ret = errno;
761 perror("Reserving sub buffer failed (everything is normal, "
762 "it is due to concurrency)");
763 goto end;
764 }
765 /* read the whole subbuffer */
766 err = helper_kernctl_get_padded_subbuf_size(infd, &len);
767 if (err != 0) {
768 ret = errno;
769 perror("Getting sub-buffer len failed.");
770 goto end;
771 }
16b22a0f
JD
772
773 /* splice the subbuffer to the tracefile */
774 ret = helper_lttng_consumer_on_read_subbuffer_splice(ctx, kconsumerd_fd, len);
775 if (ret < 0) {
776 /*
777 * display the error but continue processing to try
778 * to release the subbuffer
779 */
780 fprintf(stderr,"Error splicing to tracefile\n");
781 }
16b22a0f
JD
782 err = helper_kernctl_put_next_subbuf(infd);
783 if (err != 0) {
784 ret = errno;
785 perror("Reserving sub buffer failed (everything is normal, "
786 "it is due to concurrency)");
787 goto end;
788 }
4e6aeb3d 789 sem_post(&metadata_available);
16b22a0f
JD
790 }
791
792end:
793 return 0;
794}
795
796int on_update_fd(int key, uint32_t state)
797{
798 /* let the lib handle the metadata FD */
799 if (key == sessiond_metadata)
800 return 0;
801 return 1;
802}
803
804int on_recv_fd(struct lttng_consumer_stream *kconsumerd_fd)
805{
806 int ret;
b520ab45 807 struct mmap_stream *new_mmap_stream;
16b22a0f 808
16b22a0f
JD
809 /* Opening the tracefile in write mode */
810 if (helper_get_lttng_consumer_stream_path_name(kconsumerd_fd) != NULL) {
811 ret = open(helper_get_lttng_consumer_stream_path_name(kconsumerd_fd),
812 O_WRONLY|O_CREAT|O_TRUNC, S_IRWXU|S_IRWXG|S_IRWXO);
813 if (ret < 0) {
814 perror("open");
815 goto end;
816 }
817 helper_set_lttng_consumer_stream_out_fd(kconsumerd_fd, ret);
818 }
819
820 if (helper_get_lttng_consumer_stream_output(kconsumerd_fd) == LTTNG_EVENT_MMAP) {
b520ab45
JD
821 new_mmap_stream = malloc(sizeof(struct mmap_stream));
822 new_mmap_stream->fd = helper_get_lttng_consumer_stream_wait_fd(
823 kconsumerd_fd);
824 bt_list_add(&new_mmap_stream->list, &mmap_list.head);
16b22a0f
JD
825
826 g_ptr_array_add(lttng_consumer_stream_array, kconsumerd_fd);
6112d0d4
JD
827 /* keep mmap FDs internally */
828 ret = 1;
16b22a0f
JD
829 } else {
830 consumerd_metadata = helper_get_lttng_consumer_stream_wait_fd(kconsumerd_fd);
831 sessiond_metadata = helper_get_lttng_consumer_stream_key(kconsumerd_fd);
832 ret = 0;
833 }
834
050f9cf9
JD
835 reload_trace = 1;
836
16b22a0f
JD
837end:
838 return ret;
839}
840
eb677852 841void live_consume(struct bt_context **bt_ctx)
e91044a8 842{
e91044a8 843 int ret;
050f9cf9 844 FILE *metadata_fp;
e91044a8 845
050f9cf9
JD
846 sem_wait(&metadata_available);
847 if (access("/tmp/livesession/kernel/metadata", F_OK) != 0) {
848 fprintf(stderr,"no metadata\n");
849 goto end;
71c74ca6 850 }
050f9cf9 851 metadata_fp = fopen("/tmp/livesession/kernel/metadata", "r");
c78f2cdc 852
050f9cf9
JD
853 *bt_ctx = bt_context_create();
854 ret = bt_context_add_trace(*bt_ctx, NULL, "ctf",
855 lttngtop_ctf_packet_seek, &mmap_list, metadata_fp);
856 if (ret < 0) {
857 printf("Error adding trace\n");
858 goto end;
e91044a8 859 }
b520ab45 860
eb677852
JD
861end:
862 return;
e91044a8 863}
16b22a0f
JD
864
865int setup_consumer(char *command_sock_path, pthread_t *threads,
866 struct lttng_consumer_local_data *ctx)
867{
868 int ret = 0;
869
870 ctx = helper_lttng_consumer_create(HELPER_LTTNG_CONSUMER_KERNEL,
871 read_subbuffer, NULL, on_recv_fd, on_update_fd);
872 if (!ctx)
873 goto end;
874
875 unlink(command_sock_path);
876 helper_lttng_consumer_set_command_sock_path(ctx, command_sock_path);
877 helper_lttng_consumer_init();
878
879 /* Create the thread to manage the receive of fd */
880 ret = pthread_create(&threads[0], NULL, helper_lttng_consumer_thread_receive_fds,
881 (void *) ctx);
882 if (ret != 0) {
c78f2cdc 883 perror("pthread_create receive fd");
16b22a0f
JD
884 goto end;
885 }
e91044a8
JD
886 /* Create thread to manage the polling/writing of traces */
887 ret = pthread_create(&threads[1], NULL, helper_lttng_consumer_thread_poll_fds,
888 (void *) ctx);
889 if (ret != 0) {
c78f2cdc 890 perror("pthread_create poll fd");
e91044a8
JD
891 goto end;
892 }
893
16b22a0f
JD
894end:
895 return ret;
896}
897
1a70b8ac 898int setup_live_tracing()
16b22a0f
JD
899{
900 struct lttng_domain dom;
901 struct lttng_channel chan;
902 char *channel_name = "mmapchan";
16b22a0f
JD
903 struct lttng_event ev;
904 int ret = 0;
905 char *command_sock_path = "/tmp/consumerd_sock";
906 static pthread_t threads[2]; /* recv_fd, poll */
907 struct lttng_event_context kctxpid, kctxcomm, kctxppid, kctxtid;
16b22a0f
JD
908
909 struct lttng_handle *handle;
910
911 BT_INIT_LIST_HEAD(&mmap_list.head);
912
913 lttng_consumer_stream_array = g_ptr_array_new();
914
915 if ((ret = setup_consumer(command_sock_path, threads, ctx)) < 0) {
916 fprintf(stderr,"error setting up consumer\n");
1a70b8ac 917 goto error;
16b22a0f
JD
918 }
919
920 available_snapshots = g_ptr_array_new();
921
922 /* setup the session */
923 dom.type = LTTNG_DOMAIN_KERNEL;
924
eb677852 925 ret = unlink("/tmp/livesession");
16b22a0f 926
71c74ca6 927 lttng_destroy_session("test");
16b22a0f
JD
928 if ((ret = lttng_create_session("test", "/tmp/livesession")) < 0) {
929 fprintf(stderr,"error creating the session : %s\n",
930 helper_lttcomm_get_readable_code(ret));
1a70b8ac 931 goto error;
16b22a0f
JD
932 }
933
934 if ((handle = lttng_create_handle("test", &dom)) == NULL) {
935 fprintf(stderr,"error creating handle\n");
1a70b8ac 936 goto error_session;
16b22a0f
JD
937 }
938
050f9cf9
JD
939 /*
940 * FIXME : need to let the
941 * helper_lttng_consumer_thread_receive_fds create the
942 * socket.
943 * Cleaner solution ?
944 */
945 while (access(command_sock_path, F_OK)) {
946 sleep(0.1);
947 }
948
16b22a0f
JD
949 if ((ret = lttng_register_consumer(handle, command_sock_path)) < 0) {
950 fprintf(stderr,"error registering consumer : %s\n",
951 helper_lttcomm_get_readable_code(ret));
1a70b8ac 952 goto error_session;
16b22a0f
JD
953 }
954
955 strcpy(chan.name, channel_name);
c78f2cdc 956 chan.attr.overwrite = 0;
96aa77de 957 if (opt_pid && opt_textdump) {
d2d680e0
JD
958 chan.attr.subbuf_size = 32768;
959 chan.attr.num_subbuf = 8;
960 } else {
961 chan.attr.subbuf_size = 1048576; /* 1MB */
962 chan.attr.num_subbuf = 4;
963 }
16b22a0f
JD
964 chan.attr.switch_timer_interval = 0;
965 chan.attr.read_timer_interval = 200;
966 chan.attr.output = LTTNG_EVENT_MMAP;
967
968 if ((ret = lttng_enable_channel(handle, &chan)) < 0) {
b872c906
JD
969 fprintf(stderr,"error creating channel : %s\n",
970 helper_lttcomm_get_readable_code(ret));
1a70b8ac 971 goto error_session;
16b22a0f
JD
972 }
973
b872c906
JD
974 memset(&ev, '\0', sizeof(struct lttng_event));
975 //sprintf(ev.name, "sched_switch");
96aa77de 976// if (!opt_pid) {
d2d680e0
JD
977 ev.type = LTTNG_EVENT_TRACEPOINT;
978 if ((ret = lttng_enable_event(handle, &ev, channel_name)) < 0) {
979 fprintf(stderr,"error enabling event : %s\n",
980 helper_lttcomm_get_readable_code(ret));
981 goto error_session;
982 }
96aa77de 983// }
16b22a0f 984
6777529d
JD
985 ev.type = LTTNG_EVENT_SYSCALL;
986 if ((ret = lttng_enable_event(handle, &ev, channel_name)) < 0) {
987 fprintf(stderr,"error enabling syscalls : %s\n",
988 helper_lttcomm_get_readable_code(ret));
1a70b8ac 989 goto error_session;
6777529d
JD
990 }
991
16b22a0f
JD
992 kctxpid.ctx = LTTNG_EVENT_CONTEXT_PID;
993 lttng_add_context(handle, &kctxpid, NULL, NULL);
16b22a0f
JD
994 kctxtid.ctx = LTTNG_EVENT_CONTEXT_TID;
995 lttng_add_context(handle, &kctxtid, NULL, NULL);
96aa77de 996// if (!opt_pid) {
d2d680e0
JD
997 kctxppid.ctx = LTTNG_EVENT_CONTEXT_PPID;
998 lttng_add_context(handle, &kctxppid, NULL, NULL);
999 kctxcomm.ctx = LTTNG_EVENT_CONTEXT_PROCNAME;
1000 lttng_add_context(handle, &kctxcomm, NULL, NULL);
1001 kctxpid.ctx = LTTNG_EVENT_CONTEXT_VPID;
1002 lttng_add_context(handle, &kctxpid, NULL, NULL);
1003 kctxtid.ctx = LTTNG_EVENT_CONTEXT_VTID;
1004 lttng_add_context(handle, &kctxtid, NULL, NULL);
96aa77de 1005// }
16b22a0f
JD
1006
1007 if ((ret = lttng_start_tracing("test")) < 0) {
b872c906
JD
1008 fprintf(stderr,"error starting tracing : %s\n",
1009 helper_lttcomm_get_readable_code(ret));
1a70b8ac 1010 goto error_session;
16b22a0f
JD
1011 }
1012
1013 helper_kernctl_buffer_flush(consumerd_metadata);
e91044a8 1014
16b22a0f 1015 /* block until metadata is ready */
4e6aeb3d 1016 sem_init(&metadata_available, 0, 0);
16b22a0f 1017
1a70b8ac
JD
1018 return 0;
1019
1020error_session:
1021 lttng_destroy_session("test");
1022error:
1023 return -1;
16b22a0f
JD
1024}
1025
1fc22eb4
JD
1026int main(int argc, char **argv)
1027{
1028 int ret;
1029 struct bt_context *bt_ctx = NULL;
050f9cf9
JD
1030 struct mmap_stream *mmap_info;
1031 unsigned long mmap_len;
1fc22eb4
JD
1032
1033 ret = parse_options(argc, argv);
1034 if (ret < 0) {
1035 fprintf(stdout, "Error parsing options.\n\n");
1036 usage(stdout);
1037 exit(EXIT_FAILURE);
1038 } else if (ret > 0) {
1039 exit(EXIT_SUCCESS);
1040 }
1041
16b22a0f 1042 if (!opt_input_path) {
24797198
JD
1043 if (opt_textdump) {
1044 signal(SIGTERM, handle_textdump_sigterm);
1045 signal(SIGINT, handle_textdump_sigterm);
1046 }
1a70b8ac
JD
1047 ret = setup_live_tracing();
1048 if (ret < 0) {
1049 goto end;
1050 }
6777529d
JD
1051 init_lttngtop();
1052 if (!opt_textdump) {
1053 pthread_create(&display_thread, NULL, ncurses_display, (void *) NULL);
1054 pthread_create(&timer_thread, NULL, refresh_thread, (void *) NULL);
1055 }
050f9cf9
JD
1056 while (!quit) {
1057 reload_trace = 0;
1058 live_consume(&bt_ctx);
1059 iter_trace(bt_ctx);
1060 ret = bt_context_remove_trace(bt_ctx, 0);
1061 if (ret != 0)
1062 fprintf(stderr, "error removing trace\n");
1063 if (bt_ctx) {
1064 bt_context_put(bt_ctx);
1065 }
1066
1067 /*
1068 * since we receive all FDs every time there is an
1069 * update and the FD number is different every time,
1070 * we don't know which one are valid.
1071 * so we check if all FDs are usable with a simple
1072 * ioctl call.
1073 */
1074 bt_list_for_each_entry(mmap_info, &mmap_list.head, list) {
1075 ret = helper_kernctl_get_mmap_len(mmap_info->fd, &mmap_len);
1076 if (ret != 0) {
050f9cf9
JD
1077 bt_list_del(&mmap_info->list);
1078 }
1079 }
1080 sem_post(&metadata_available);
1081 }
eb677852 1082
ae9e85c7 1083 pthread_join(timer_thread, NULL);
eb677852
JD
1084 quit = 1;
1085 pthread_join(display_thread, NULL);
6777529d 1086
c78f2cdc 1087 lttng_stop_tracing("test");
c78f2cdc
JD
1088 lttng_destroy_session("test");
1089
1fc22eb4 1090 goto end;
16b22a0f
JD
1091 } else {
1092 init_lttngtop();
1093
1094 bt_ctx = bt_context_create();
1095 ret = bt_context_add_traces_recursive(bt_ctx, opt_input_path, "ctf", NULL);
1096 if (ret < 0) {
1097 fprintf(stderr, "[error] Opening the trace\n");
1098 goto end;
1099 }
1fc22eb4 1100
16b22a0f
JD
1101 ret = check_requirements(bt_ctx);
1102 if (ret < 0) {
1103 fprintf(stderr, "[error] some mandatory contexts were missing, exiting.\n");
1104 goto end;
1105 }
1106 pthread_create(&display_thread, NULL, ncurses_display, (void *) NULL);
1107 pthread_create(&timer_thread, NULL, refresh_thread, (void *) NULL);
1fc22eb4 1108
16b22a0f 1109 iter_trace(bt_ctx);
1fc22eb4 1110
16b22a0f 1111 pthread_join(display_thread, NULL);
ae9e85c7 1112 quit = 1;
16b22a0f
JD
1113 pthread_join(timer_thread, NULL);
1114 }
1fc22eb4
JD
1115
1116end:
16b22a0f
JD
1117 if (bt_ctx)
1118 bt_context_put(bt_ctx);
1119
1fc22eb4
JD
1120 return 0;
1121}
This page took 0.078044 seconds and 4 git commands to generate.