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