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