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