fd23e74da416c746cf437586ecb275d3c0628a2d
[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 /*
136 * hook on each event to check the timestamp and refresh the display if
137 * necessary
138 */
139 enum bt_cb_ret check_timestamp(struct bt_ctf_event *call_data, void *private_data)
140 {
141 unsigned long timestamp;
142
143 timestamp = bt_ctf_get_timestamp(call_data);
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
160 error:
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 */
169 struct perfcounter *get_perf_counter(const char *name, struct processtop *proc,
170 struct cputime *cpu)
171 {
172 struct perfcounter *ret;
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
186 ret = g_new0(struct perfcounter, 1);
187 /* by default, make it visible in the UI */
188 ret->visible = 1;
189 g_hash_table_insert(table, (gpointer) strdup(name), ret);
190
191 end:
192 return ret;
193
194 error:
195 return NULL;
196 }
197
198 void 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
211 void extract_perf_counter_scope(const struct bt_ctf_event *event,
212 const struct bt_definition *scope,
213 struct processtop *proc,
214 struct cputime *cpu)
215 {
216 struct bt_definition const * const *list = NULL;
217 const struct bt_definition *field;
218 unsigned int count;
219 struct perfcounter *perfcounter;
220 GHashTableIter iter;
221 gpointer key;
222 int ret;
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
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);
239 if (bt_ctf_field_get_error())
240 continue;
241 update_perf_value(proc, cpu, (char *) key, value);
242 }
243 }
244
245 end:
246 return;
247 }
248
249 void update_perf_counter(struct processtop *proc, const struct bt_ctf_event *event)
250 {
251 struct cputime *cpu;
252 const struct bt_definition *scope;
253
254 cpu = get_cpu(get_cpu_id(event));
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);
264 }
265
266 enum bt_cb_ret fix_process_table(struct bt_ctf_event *call_data,
267 void *private_data)
268 {
269 int pid, tid, ppid;
270 char *comm;
271 struct processtop *parent, *child;
272 unsigned long timestamp;
273
274 timestamp = bt_ctf_get_timestamp(call_data);
275 if (timestamp == -1ULL)
276 goto error;
277
278 pid = get_context_pid(call_data);
279 if (pid == -1ULL) {
280 goto error;
281 }
282 tid = get_context_tid(call_data);
283 if (tid == -1ULL) {
284 goto error;
285 }
286 ppid = get_context_ppid(call_data);
287 if (ppid == -1ULL) {
288 goto error;
289 }
290 comm = get_context_comm(call_data);
291 if (!comm) {
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
318 error:
319 return BT_CB_ERROR_STOP;
320 }
321
322 void init_lttngtop()
323 {
324 copies = g_ptr_array_new();
325 global_perf_liszt = g_hash_table_new(g_str_hash, g_str_equal);
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
334 reset_global_counters();
335 lttngtop.nbproc = 0;
336 lttngtop.nbthreads = 0;
337 lttngtop.nbfiles = 0;
338
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
344 void usage(FILE *fp)
345 {
346 fprintf(fp, "LTTngTop %s\n\n", VERSION);
347 fprintf(fp, "Usage : lttngtop /path/to/trace\n");
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 */
354 static int parse_options(int argc, char **argv)
355 {
356 poptContext pc;
357 int opt, ret = 0;
358
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;
368 default:
369 ret = -EINVAL;
370 goto end;
371 }
372 }
373
374 opt_input_path = poptGetArg(pc);
375
376 end:
377 if (pc) {
378 poptFreeContext(pc);
379 }
380 return ret;
381 }
382
383 void iter_trace(struct bt_context *bt_ctx)
384 {
385 struct bt_ctf_iter *iter;
386 struct bt_iter_pos begin_pos;
387 const struct bt_ctf_event *event;
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);
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);
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);
426 bt_ctf_iter_add_callback(iter,
427 g_quark_from_static_string("sys_open"),
428 NULL, 0, handle_sys_open, NULL, NULL, NULL);
429 bt_ctf_iter_add_callback(iter,
430 g_quark_from_static_string("sys_close"),
431 NULL, 0, handle_sys_close, NULL, NULL, NULL);
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);
437
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
447 end_iter:
448 bt_ctf_iter_destroy(iter);
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 */
463 int bt_context_add_traces_recursive(struct bt_context *ctx, const char *path,
464 const char *format_str,
465 void (*packet_seek)(struct bt_stream_pos *pos,
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 };
473 int ret = -1;
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) {
506 close(dirfd);
507 ret = -1;
508 continue;
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) {
527 fprintf(stderr, "[warning] [Context] opening trace \"%s\" from %s "
528 "for reading.\n", node->fts_accpath, path);
529 /* Allow to skip erroneous traces. */
530 continue;
531 }
532 g_array_append_val(trace_ids, trace_id);
533 }
534 }
535
536 g_array_free(trace_ids, TRUE);
537 return ret;
538
539 error:
540 return ret;
541 }
542
543 static int check_field_requirements(const struct bt_ctf_field_decl *const * field_list,
544 int field_cnt, int *tid_check, int *pid_check,
545 int *procname_check, int *ppid_check)
546 {
547 int j;
548 struct perfcounter *global;
549 const char *name;
550
551 for (j = 0; j < field_cnt; j++) {
552 name = bt_ctf_get_decl_field_name(field_list[j]);
553 if (*tid_check == 0) {
554 if (strncmp(name, "tid", 3) == 0)
555 (*tid_check)++;
556 }
557 if (*pid_check == 0) {
558 if (strncmp(name, "pid", 3) == 0)
559 (*pid_check)++;
560 }
561 if (*ppid_check == 0) {
562 if (strncmp(name, "ppid", 4) == 0)
563 (*ppid_check)++;
564 }
565 if (*procname_check == 0) {
566 if (strncmp(name, "procname", 8) == 0)
567 (*procname_check)++;
568 }
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 }
580 }
581
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.
594 */
595 int 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);
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);
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);
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
644 return ret;
645 }
646
647 void dump_snapshot()
648 {
649 #if 0
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));
677 printf("Taking snapshot of fd : %d\n", helper_get_lttng_consumer_stream_wait_fd(iter));
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);
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 }
693 while (helper_get_lttng_consumer_stream_wait_last_pos(iter) < spos) {
694 new_snapshot = g_new0(struct mmap_stream, 1);
695 new_snapshot->fd = helper_get_lttng_consumer_stream_wait_fd(iter);
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);
700 g_ptr_array_add(available_snapshots, new_snapshot);
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 }
705 }
706
707 if (!metadata_ready) {
708 fprintf(stderr, "BLOCKING BEFORE METADATA\n");
709 sem_wait(&metadata_available);
710 fprintf(stderr,"OPENING TRACE\n");
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
719
720 end:
721 return;
722 #endif
723 }
724
725 ssize_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 }
770 sem_post(&metadata_available);
771 }
772
773 end:
774 return 0;
775 }
776
777 int 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
785 int 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
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);
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);
827 ret = 0;
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
834 end:
835 return ret;
836 }
837
838 void *live_consume()
839 {
840 struct bt_context *bt_ctx = NULL;
841 int ret;
842
843 while (1) {
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");
852 return;
853 }
854 metadata_ready = 1;
855 metadata_fp = fopen("/tmp/livesession/kernel/metadata", "r");
856 }
857
858 if (!trace_opened) {
859 bt_ctx = bt_context_create();
860 ret = bt_context_add_trace(bt_ctx, NULL, "ctf",
861 lttngtop_ctf_packet_seek, &mmap_list, metadata_fp);
862 trace_opened = 1;
863 }
864 iter_trace(bt_ctx);
865 sleep(1);
866 }
867 }
868
869 int setup_consumer(char *command_sock_path, pthread_t *threads,
870 struct lttng_consumer_local_data *ctx)
871 {
872 int ret = 0;
873
874 ctx = helper_lttng_consumer_create(HELPER_LTTNG_CONSUMER_KERNEL,
875 read_subbuffer, NULL, on_recv_fd, on_update_fd);
876 if (!ctx)
877 goto end;
878
879 unlink(command_sock_path);
880 helper_lttng_consumer_set_command_sock_path(ctx, command_sock_path);
881 helper_lttng_consumer_init();
882
883 /* Create the thread to manage the receive of fd */
884 ret = pthread_create(&threads[0], NULL, helper_lttng_consumer_thread_receive_fds,
885 (void *) ctx);
886 if (ret != 0) {
887 perror("pthread_create receive fd");
888 goto end;
889 }
890 /* Create thread to manage the polling/writing of traces */
891 ret = pthread_create(&threads[1], NULL, helper_lttng_consumer_thread_poll_fds,
892 (void *) ctx);
893 if (ret != 0) {
894 perror("pthread_create poll fd");
895 goto end;
896 }
897
898 end:
899 return ret;
900 }
901
902 void *setup_live_tracing()
903 {
904 struct lttng_domain dom;
905 struct lttng_channel chan;
906 char *channel_name = "mmapchan";
907 struct lttng_event ev;
908 int ret = 0;
909 char *command_sock_path = "/tmp/consumerd_sock";
910 static pthread_t threads[2]; /* recv_fd, poll */
911 struct lttng_event_context kctxpid, kctxcomm, kctxppid, kctxtid;
912
913 struct lttng_handle *handle;
914
915 BT_INIT_LIST_HEAD(&mmap_list.head);
916
917 lttng_consumer_stream_array = g_ptr_array_new();
918
919 if ((ret = setup_consumer(command_sock_path, threads, ctx)) < 0) {
920 fprintf(stderr,"error setting up consumer\n");
921 goto end;
922 }
923
924 available_snapshots = g_ptr_array_new();
925
926 /* setup the session */
927 dom.type = LTTNG_DOMAIN_KERNEL;
928
929 ret = system("rm -rf /tmp/livesession");
930
931 if ((ret = lttng_create_session("test", "/tmp/livesession")) < 0) {
932 fprintf(stderr,"error creating the session : %s\n",
933 helper_lttcomm_get_readable_code(ret));
934 goto end;
935 }
936
937 if ((handle = lttng_create_handle("test", &dom)) == NULL) {
938 fprintf(stderr,"error creating handle\n");
939 goto end;
940 }
941
942 if ((ret = lttng_register_consumer(handle, command_sock_path)) < 0) {
943 fprintf(stderr,"error registering consumer : %s\n",
944 helper_lttcomm_get_readable_code(ret));
945 goto end;
946 }
947
948 strcpy(chan.name, channel_name);
949 chan.attr.overwrite = 0;
950 chan.attr.subbuf_size = 32768;
951 // chan.attr.subbuf_size = 1048576; /* 1MB */
952 chan.attr.num_subbuf = 4;
953 chan.attr.switch_timer_interval = 0;
954 chan.attr.read_timer_interval = 200;
955 chan.attr.output = LTTNG_EVENT_MMAP;
956
957 if ((ret = lttng_enable_channel(handle, &chan)) < 0) {
958 fprintf(stderr,"error creating channel : %s\n", helper_lttcomm_get_readable_code(ret));
959 goto end;
960 }
961
962 sprintf(ev.name, "sched_switch");
963 ev.type = LTTNG_EVENT_TRACEPOINT;
964
965 //if ((ret = lttng_enable_event(handle, NULL, channel_name)) < 0) {
966 if ((ret = lttng_enable_event(handle, &ev, channel_name)) < 0) {
967 fprintf(stderr,"error enabling event : %s\n", helper_lttcomm_get_readable_code(ret));
968 goto end;
969 }
970
971 kctxpid.ctx = LTTNG_EVENT_CONTEXT_PID;
972 lttng_add_context(handle, &kctxpid, NULL, NULL);
973 kctxppid.ctx = LTTNG_EVENT_CONTEXT_PPID;
974 lttng_add_context(handle, &kctxppid, NULL, NULL);
975 kctxcomm.ctx = LTTNG_EVENT_CONTEXT_PROCNAME;
976 lttng_add_context(handle, &kctxcomm, NULL, NULL);
977 kctxtid.ctx = LTTNG_EVENT_CONTEXT_TID;
978 lttng_add_context(handle, &kctxtid, NULL, NULL);
979
980 if ((ret = lttng_start_tracing("test")) < 0) {
981 fprintf(stderr,"error starting tracing : %s\n", helper_lttcomm_get_readable_code(ret));
982 goto end;
983 }
984
985 helper_kernctl_buffer_flush(consumerd_metadata);
986
987 /* Create thread to manage the polling/writing of traces */
988 ret = pthread_create(&thread_live_consume, NULL, live_consume, NULL);
989 if (ret != 0) {
990 perror("pthread_create");
991 goto end;
992 }
993
994 // pthread_cancel(live_trace_thread);
995
996 /* block until metadata is ready */
997 sem_init(&metadata_available, 0, 0);
998
999 //init_lttngtop();
1000
1001 end:
1002 return ret;
1003 }
1004
1005 int main(int argc, char **argv)
1006 {
1007 int ret;
1008 struct bt_context *bt_ctx = NULL;
1009
1010 ret = parse_options(argc, argv);
1011 if (ret < 0) {
1012 fprintf(stdout, "Error parsing options.\n\n");
1013 usage(stdout);
1014 exit(EXIT_FAILURE);
1015 } else if (ret > 0) {
1016 exit(EXIT_SUCCESS);
1017 }
1018
1019 if (!opt_input_path) {
1020 printf("live tracing enabled\n");
1021 pthread_create(&live_trace_thread, NULL, setup_live_tracing, (void *) NULL);
1022 sleep(20);
1023 printf("STOPPING\n");
1024 lttng_stop_tracing("test");
1025 printf("DESTROYING\n");
1026 lttng_destroy_session("test");
1027
1028 printf("CANCELLING\n");
1029 pthread_cancel(live_trace_thread);
1030 goto end;
1031 } else {
1032 init_lttngtop();
1033
1034 bt_ctx = bt_context_create();
1035 ret = bt_context_add_traces_recursive(bt_ctx, opt_input_path, "ctf", NULL);
1036 if (ret < 0) {
1037 fprintf(stderr, "[error] Opening the trace\n");
1038 goto end;
1039 }
1040
1041 ret = check_requirements(bt_ctx);
1042 if (ret < 0) {
1043 fprintf(stderr, "[error] some mandatory contexts were missing, exiting.\n");
1044 goto end;
1045 }
1046 pthread_create(&display_thread, NULL, ncurses_display, (void *) NULL);
1047 pthread_create(&timer_thread, NULL, refresh_thread, (void *) NULL);
1048
1049 iter_trace(bt_ctx);
1050
1051 quit = 1;
1052 pthread_join(display_thread, NULL);
1053 pthread_join(timer_thread, NULL);
1054 }
1055
1056 end:
1057 if (bt_ctx)
1058 bt_context_put(bt_ctx);
1059
1060 return 0;
1061 }
This page took 0.057239 seconds and 4 git commands to generate.