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