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