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