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