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