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