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