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