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