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