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