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