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