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