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