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 | ||
1fc22eb4 JD |
18 | #include <config.h> |
19 | #include <stdio.h> | |
20 | #include <stdint.h> | |
715cf83c | 21 | extern "C" { |
1fc22eb4 JD |
22 | #include <babeltrace/babeltrace.h> |
23 | #include <babeltrace/ctf/events.h> | |
24 | #include <babeltrace/ctf/callbacks.h> | |
3ba84bed | 25 | #include <babeltrace/ctf/iterator.h> |
715cf83c | 26 | } |
1fc22eb4 JD |
27 | #include <fcntl.h> |
28 | #include <pthread.h> | |
29 | #include <popt.h> | |
30 | #include <stdlib.h> | |
31 | #include <ftw.h> | |
32 | #include <dirent.h> | |
33 | #include <ctype.h> | |
34 | #include <sys/stat.h> | |
35 | #include <unistd.h> | |
36 | #include <string.h> | |
37 | #include <errno.h> | |
38 | #include <sys/types.h> | |
39 | #include <fts.h> | |
85db4618 | 40 | #include <assert.h> |
1fc22eb4 JD |
41 | |
42 | #include "lttngtoptypes.h" | |
43 | #include "cputop.h" | |
44 | #include "iostreamtop.h" | |
1fc22eb4 | 45 | #include "common.h" |
715cf83c | 46 | #include "cursesdisplay.h" |
1fc22eb4 JD |
47 | |
48 | #define DEFAULT_FILE_ARRAY_SIZE 1 | |
49 | ||
50 | const char *opt_input_path; | |
51 | ||
1fc22eb4 JD |
52 | pthread_t display_thread; |
53 | pthread_t timer_thread; | |
54 | ||
1fc22eb4 | 55 | int quit = 0; |
715cf83c | 56 | std::string history_file; |
1fc22eb4 JD |
57 | |
58 | enum { | |
59 | OPT_NONE = 0, | |
60 | OPT_HELP, | |
61 | OPT_LIST, | |
62 | OPT_VERBOSE, | |
63 | OPT_DEBUG, | |
64 | OPT_NAMES, | |
65 | }; | |
66 | ||
67 | static struct poptOption long_options[] = { | |
68 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ | |
69 | { "help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL }, | |
70 | { NULL, 0, 0, NULL, 0, NULL, NULL }, | |
71 | }; | |
72 | ||
73 | void *refresh_thread(void *p) | |
74 | { | |
75 | while (1) { | |
85db4618 JD |
76 | if (quit) |
77 | return NULL; | |
1fc22eb4 JD |
78 | sem_wait(&pause_sem); |
79 | sem_post(&pause_sem); | |
80 | sem_post(&timer); | |
81 | sleep(refresh_display/NSEC_PER_SEC); | |
82 | } | |
83 | } | |
84 | ||
85 | void *ncurses_display(void *p) | |
86 | { | |
1fc22eb4 | 87 | sem_wait(&bootstrap); |
b332d28f JD |
88 | /* |
89 | * Prevent the 1 second delay when we hit ESC | |
90 | */ | |
91 | ESCDELAY = 0; | |
1fc22eb4 JD |
92 | init_ncurses(); |
93 | ||
94 | while (1) { | |
95 | sem_wait(&timer); | |
96 | sem_wait(&goodtodisplay); | |
97 | sem_wait(&pause_sem); | |
98 | ||
715cf83c | 99 | display(); |
1fc22eb4 JD |
100 | |
101 | sem_post(&goodtoupdate); | |
102 | sem_post(&pause_sem); | |
103 | ||
104 | if (quit) { | |
105 | reset_ncurses(); | |
106 | pthread_exit(0); | |
107 | } | |
108 | } | |
109 | } | |
110 | ||
715cf83c PBT |
111 | /* |
112 | * We soft update each attribute during the execution for perfromance reasons. | |
113 | * Here we write the actual intervals to disk, which are needed to query history. | |
114 | */ | |
115 | void create_intervals(unsigned long timestamp) | |
116 | { | |
117 | StateValue::SharedPtr value; | |
118 | ||
119 | for (std::set<Quark>::iterator i = modified_quarks.begin(); | |
120 | i != modified_quarks.end(); | |
121 | i++) { | |
122 | value = state_system->getCurrentStateValue(*i); | |
123 | /* To force the creation of an interval (which is what we want) | |
124 | we need to pass a different value than the current value. | |
125 | We reapply the correct value afterwards */ | |
126 | if (std::tr1::dynamic_pointer_cast<NullStateValue>(value)) { | |
127 | state_system->modifyAttribute(timestamp, *i, 0); | |
128 | } else { | |
129 | state_system->modifyAttribute(timestamp, *i, | |
130 | StateValue::getNullValue()); | |
131 | } | |
132 | state_system->updateCurrentState(*i, value); | |
133 | } | |
134 | modified_quarks.clear(); | |
135 | } | |
136 | ||
1fc22eb4 JD |
137 | /* |
138 | * hook on each event to check the timestamp and refresh the display if | |
139 | * necessary | |
140 | */ | |
715cf83c PBT |
141 | enum bt_cb_ret check_timestamp(struct bt_ctf_event *call_data, |
142 | void *private_data) | |
1fc22eb4 JD |
143 | { |
144 | unsigned long timestamp; | |
145 | ||
480b40ce | 146 | timestamp = bt_ctf_get_timestamp(call_data); |
1fc22eb4 JD |
147 | if (timestamp == -1ULL) |
148 | goto error; | |
149 | ||
150 | if (last_display_update == 0) | |
151 | last_display_update = timestamp; | |
152 | ||
715cf83c PBT |
153 | if (first_display_update == 0) |
154 | first_display_update = timestamp; | |
155 | ||
1fc22eb4 JD |
156 | if (timestamp - last_display_update >= refresh_display) { |
157 | sem_wait(&goodtoupdate); | |
715cf83c PBT |
158 | create_intervals(timestamp); |
159 | update_state_on_refresh(last_display_update, timestamp); | |
1fc22eb4 JD |
160 | sem_post(&goodtodisplay); |
161 | sem_post(&bootstrap); | |
162 | last_display_update = timestamp; | |
163 | } | |
164 | return BT_CB_OK; | |
165 | ||
166 | error: | |
167 | fprintf(stderr, "check_timestamp callback error\n"); | |
168 | return BT_CB_ERROR_STOP; | |
169 | } | |
170 | ||
171 | /* | |
172 | * get_perf_counter : get or create and return a perf_counter struct for | |
715cf83c | 173 | * either a process or a cpu (specified by root) |
1fc22eb4 | 174 | */ |
715cf83c | 175 | Quark get_perf_counter(unsigned long timestamp, Quark root, std::string name) |
1fc22eb4 | 176 | { |
715cf83c PBT |
177 | Quark perf_counter; |
178 | ||
179 | if (state_system->attributeExists(root, "perf/" + name)) { | |
180 | return state_system->getQuark(root, "perf/" + name); | |
181 | } else { | |
182 | perf_counter = state_system->getQuark(root, "perf/" + name); | |
183 | modify_attribute(timestamp, &perf_counter, "count", 0); | |
184 | modify_attribute(timestamp, &perf_counter, "visible", 1); | |
185 | add_in_sequence(timestamp, perf_counter, | |
186 | state_system->getQuark(root, "perf")); | |
187 | return perf_counter; | |
188 | } | |
1fc22eb4 JD |
189 | } |
190 | ||
715cf83c PBT |
191 | void update_perf_value(unsigned long timestamp, Quark proc, Quark cpu, |
192 | std::string name, int value) | |
1fc22eb4 | 193 | { |
715cf83c PBT |
194 | Quark cpu_perf, process_perf; |
195 | int count; | |
196 | ||
197 | cpu_perf = get_perf_counter(timestamp, cpu, name); | |
198 | get_current_attribute_value_int(&cpu_perf, "count", count); | |
199 | if (count < value) { | |
200 | process_perf = get_perf_counter(timestamp, proc, name); | |
201 | increase_attribute( | |
202 | timestamp, &process_perf, "count", value - count); | |
203 | modify_attribute(timestamp, &cpu_perf, "count", value); | |
1fc22eb4 JD |
204 | } |
205 | } | |
206 | ||
715cf83c PBT |
207 | void extract_perf_counter_scope(unsigned long timestamp, |
208 | const struct bt_ctf_event *event, | |
209 | const struct definition *scope, | |
210 | Quark proc, Quark cpu) | |
1fc22eb4 JD |
211 | { |
212 | struct definition const * const *list = NULL; | |
bb6d72a2 | 213 | const struct definition *field; |
1fc22eb4 | 214 | unsigned int count; |
bb6d72a2 | 215 | int ret; |
715cf83c PBT |
216 | std::string key; |
217 | Quark perf_quark; | |
1fc22eb4 JD |
218 | |
219 | if (!scope) | |
220 | goto end; | |
221 | ||
222 | ret = bt_ctf_get_field_list(event, scope, &list, &count); | |
223 | if (ret < 0) | |
224 | goto end; | |
225 | ||
bb6d72a2 JD |
226 | if (count == 0) |
227 | goto end; | |
228 | ||
715cf83c PBT |
229 | if (get_current_attribute_value_quark( |
230 | NULL, "perf", perf_quark)) { | |
231 | do { | |
232 | get_current_attribute_value_string( | |
233 | &perf_quark, "key", key); | |
234 | field = bt_ctf_get_field(event, scope, key.c_str()); | |
235 | if (field) { | |
236 | int value = bt_ctf_get_uint64(field); | |
237 | if (bt_ctf_field_get_error()) | |
238 | continue; | |
239 | update_perf_value( | |
240 | timestamp, proc, cpu, key, value); | |
241 | } | |
242 | } while (get_current_attribute_value_quark( | |
243 | &perf_quark, "next", perf_quark)); | |
1fc22eb4 JD |
244 | } |
245 | ||
715cf83c | 246 | |
1fc22eb4 JD |
247 | end: |
248 | return; | |
249 | } | |
250 | ||
715cf83c PBT |
251 | void update_perf_counter(unsigned long timestamp, Quark proc, |
252 | const struct bt_ctf_event *event) | |
1fc22eb4 | 253 | { |
715cf83c | 254 | Quark cpu; |
3ba84bed | 255 | const struct definition *scope; |
1fc22eb4 | 256 | |
715cf83c | 257 | cpu = get_cpu(get_cpu_id(event), timestamp); |
1fc22eb4 JD |
258 | |
259 | scope = bt_ctf_get_top_level_scope(event, BT_STREAM_EVENT_CONTEXT); | |
715cf83c | 260 | extract_perf_counter_scope(timestamp, event, scope, proc, cpu); |
1fc22eb4 JD |
261 | |
262 | scope = bt_ctf_get_top_level_scope(event, BT_STREAM_PACKET_CONTEXT); | |
715cf83c | 263 | extract_perf_counter_scope(timestamp, event, scope, proc, cpu); |
1fc22eb4 JD |
264 | |
265 | scope = bt_ctf_get_top_level_scope(event, BT_EVENT_CONTEXT); | |
715cf83c | 266 | extract_perf_counter_scope(timestamp, event, scope, proc, cpu); |
1fc22eb4 JD |
267 | } |
268 | ||
4adc8274 | 269 | enum bt_cb_ret fix_process_table(struct bt_ctf_event *call_data, |
715cf83c PBT |
270 | void *private_data) |
271 | { int pid, tid, ppid; | |
1fc22eb4 | 272 | char *comm; |
715cf83c | 273 | Quark parent, child; |
1fc22eb4 JD |
274 | unsigned long timestamp; |
275 | ||
480b40ce | 276 | timestamp = bt_ctf_get_timestamp(call_data); |
1fc22eb4 JD |
277 | if (timestamp == -1ULL) |
278 | goto error; | |
279 | ||
1dec520a | 280 | pid = get_context_pid(call_data); |
715cf83c | 281 | if ((unsigned long)pid == -1ULL) { |
1fc22eb4 JD |
282 | goto error; |
283 | } | |
1dec520a | 284 | tid = get_context_tid(call_data); |
715cf83c | 285 | if ((unsigned long)tid == -1ULL) { |
1fc22eb4 JD |
286 | goto error; |
287 | } | |
1dec520a | 288 | ppid = get_context_ppid(call_data); |
715cf83c | 289 | if ((unsigned long)ppid == -1ULL) { |
1fc22eb4 JD |
290 | goto error; |
291 | } | |
1dec520a JD |
292 | comm = get_context_comm(call_data); |
293 | if (!comm) { | |
1fc22eb4 JD |
294 | goto error; |
295 | } | |
296 | ||
297 | /* find or create the current process */ | |
715cf83c PBT |
298 | if (!find_process_tid(tid, child)) |
299 | child = add_proc(tid, comm, timestamp); | |
300 | update_proc(timestamp, child, pid, tid, ppid, comm); | |
1fc22eb4 JD |
301 | |
302 | if (pid != tid) { | |
303 | /* find or create the parent */ | |
715cf83c PBT |
304 | if (!find_process_tid(pid, parent)) { |
305 | parent = add_proc(pid, comm, timestamp); | |
306 | modify_attribute(timestamp, &parent, "pid", pid); | |
1fc22eb4 JD |
307 | } |
308 | ||
309 | /* attach the parent to the current process */ | |
715cf83c PBT |
310 | modify_attribute(timestamp, &parent, "threadparent", parent); |
311 | add_thread(timestamp, parent, child); | |
1fc22eb4 JD |
312 | } |
313 | ||
715cf83c | 314 | update_perf_counter(timestamp, child, call_data); |
1fc22eb4 JD |
315 | |
316 | return BT_CB_OK; | |
317 | ||
318 | error: | |
319 | return BT_CB_ERROR_STOP; | |
320 | } | |
321 | ||
322 | void init_lttngtop() | |
323 | { | |
1fc22eb4 JD |
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 | ||
715cf83c PBT |
331 | // TODO: real file name |
332 | std::stringstream ss; | |
333 | ss << "history" << time(NULL) << ".hst"; | |
334 | history_file = ss.str(); | |
335 | IntervalHistoryProvider *ihp = g_new(IntervalHistoryProvider, 1); | |
336 | new (ihp) IntervalHistoryProvider(history_file); | |
337 | state_system = g_new(StateSystem, 1); | |
338 | new (state_system) StateSystem(ihp); | |
339 | ||
340 | /* Create global attributes */ | |
341 | state_system->updateCurrentState( | |
342 | state_system->getQuark("nbproc"), 0); | |
343 | state_system->updateCurrentState( | |
344 | state_system->getQuark("nbnewproc"), 0); | |
345 | state_system->updateCurrentState( | |
346 | state_system->getQuark("nbdeadproc"), 0); | |
347 | state_system->updateCurrentState( | |
348 | state_system->getQuark("nbthreads"), 0); | |
349 | state_system->updateCurrentState( | |
350 | state_system->getQuark("nbnewthreads"), 0); | |
351 | state_system->updateCurrentState( | |
352 | state_system->getQuark("nbdeadthreads"), 0); | |
353 | state_system->updateCurrentState( | |
354 | state_system->getQuark("nbfiles"), 0); | |
355 | state_system->updateCurrentState( | |
356 | state_system->getQuark("nbnewfiles"), 0); | |
357 | state_system->updateCurrentState( | |
358 | state_system->getQuark("nbdeadfiles"), 0); | |
1fc22eb4 JD |
359 | } |
360 | ||
c263c4eb | 361 | void usage(FILE *fp) |
1fc22eb4 | 362 | { |
c263c4eb JD |
363 | fprintf(fp, "LTTngTop %s\n\n", VERSION); |
364 | fprintf(fp, "Usage : lttngtop /path/to/trace\n"); | |
1fc22eb4 JD |
365 | } |
366 | ||
367 | /* | |
368 | * Return 0 if caller should continue, < 0 if caller should return | |
369 | * error, > 0 if caller should exit without reporting error. | |
370 | */ | |
371 | static int parse_options(int argc, char **argv) | |
372 | { | |
373 | poptContext pc; | |
374 | int opt, ret = 0; | |
375 | ||
376 | if (argc == 1) { | |
377 | usage(stdout); | |
378 | return 1; /* exit cleanly */ | |
379 | } | |
380 | ||
381 | pc = poptGetContext(NULL, argc, (const char **) argv, long_options, 0); | |
382 | poptReadDefaultConfig(pc, 0); | |
383 | ||
384 | while ((opt = poptGetNextOpt(pc)) != -1) { | |
385 | switch (opt) { | |
386 | case OPT_HELP: | |
387 | usage(stdout); | |
388 | ret = 1; /* exit cleanly */ | |
389 | goto end; | |
1fc22eb4 JD |
390 | default: |
391 | ret = -EINVAL; | |
392 | goto end; | |
393 | } | |
394 | } | |
395 | ||
396 | opt_input_path = poptGetArg(pc); | |
397 | if (!opt_input_path) { | |
398 | ret = -EINVAL; | |
399 | goto end; | |
400 | } | |
401 | end: | |
402 | if (pc) { | |
403 | poptFreeContext(pc); | |
404 | } | |
405 | return ret; | |
406 | } | |
407 | ||
408 | void iter_trace(struct bt_context *bt_ctx) | |
409 | { | |
410 | struct bt_ctf_iter *iter; | |
411 | struct bt_iter_pos begin_pos; | |
4adc8274 | 412 | const struct bt_ctf_event *event; |
1fc22eb4 JD |
413 | int ret = 0; |
414 | ||
715cf83c | 415 | begin_pos.type = bt_iter_pos::BT_SEEK_BEGIN; |
1fc22eb4 JD |
416 | iter = bt_ctf_iter_create(bt_ctx, &begin_pos, NULL); |
417 | ||
418 | /* at each event check if we need to refresh */ | |
419 | bt_ctf_iter_add_callback(iter, 0, NULL, 0, | |
420 | check_timestamp, | |
421 | NULL, NULL, NULL); | |
422 | /* at each event, verify the status of the process table */ | |
423 | bt_ctf_iter_add_callback(iter, 0, NULL, 0, | |
424 | fix_process_table, | |
425 | NULL, NULL, NULL); | |
426 | /* to handle the scheduling events */ | |
427 | bt_ctf_iter_add_callback(iter, | |
428 | g_quark_from_static_string("sched_switch"), | |
429 | NULL, 0, handle_sched_switch, NULL, NULL, NULL); | |
430 | /* to clean up the process table */ | |
431 | bt_ctf_iter_add_callback(iter, | |
432 | g_quark_from_static_string("sched_process_free"), | |
433 | NULL, 0, handle_sched_process_free, NULL, NULL, NULL); | |
928f18a6 MB |
434 | /* to get all the process from the statedumps */ |
435 | bt_ctf_iter_add_callback(iter, | |
436 | g_quark_from_static_string( | |
437 | "lttng_statedump_process_state"), | |
438 | NULL, 0, handle_statedump_process_state, | |
439 | NULL, NULL, NULL); | |
1fc22eb4 JD |
440 | |
441 | /* for IO top */ | |
442 | bt_ctf_iter_add_callback(iter, | |
443 | g_quark_from_static_string("exit_syscall"), | |
444 | NULL, 0, handle_exit_syscall, NULL, NULL, NULL); | |
445 | bt_ctf_iter_add_callback(iter, | |
446 | g_quark_from_static_string("sys_write"), | |
447 | NULL, 0, handle_sys_write, NULL, NULL, NULL); | |
448 | bt_ctf_iter_add_callback(iter, | |
449 | g_quark_from_static_string("sys_read"), | |
450 | NULL, 0, handle_sys_read, NULL, NULL, NULL); | |
b093de8a MB |
451 | bt_ctf_iter_add_callback(iter, |
452 | g_quark_from_static_string("sys_open"), | |
453 | NULL, 0, handle_sys_open, NULL, NULL, NULL); | |
b093de8a MB |
454 | bt_ctf_iter_add_callback(iter, |
455 | g_quark_from_static_string("sys_close"), | |
456 | NULL, 0, handle_sys_close, NULL, NULL, NULL); | |
ceb3a221 MB |
457 | bt_ctf_iter_add_callback(iter, |
458 | g_quark_from_static_string( | |
459 | "lttng_statedump_file_descriptor"), | |
460 | NULL, 0, handle_statedump_file_descriptor, | |
461 | NULL, NULL, NULL); | |
59288610 | 462 | |
1fc22eb4 JD |
463 | while ((event = bt_ctf_iter_read_event(iter)) != NULL) { |
464 | ret = bt_iter_next(bt_ctf_get_iter(iter)); | |
465 | if (ret < 0) | |
466 | goto end_iter; | |
467 | } | |
468 | ||
469 | /* block until quit, we reached the end of the trace */ | |
470 | sem_wait(&end_trace_sem); | |
471 | ||
472 | end_iter: | |
06570214 | 473 | bt_ctf_iter_destroy(iter); |
1fc22eb4 JD |
474 | } |
475 | ||
476 | /* | |
477 | * bt_context_add_traces_recursive: Open a trace recursively | |
478 | * (copied from BSD code in converter/babeltrace.c) | |
479 | * | |
480 | * Find each trace present in the subdirectory starting from the given | |
481 | * path, and add them to the context. The packet_seek parameter can be | |
482 | * NULL: this specify to use the default format packet_seek. | |
483 | * | |
484 | * Return: 0 on success, nonzero on failure. | |
485 | * Unable to open toplevel: failure. | |
486 | * Unable to open some subdirectory or file: warn and continue; | |
487 | */ | |
488 | int bt_context_add_traces_recursive(struct bt_context *ctx, const char *path, | |
489 | const char *format_str, | |
490 | void (*packet_seek)(struct stream_pos *pos, | |
491 | size_t offset, int whence)) | |
492 | { | |
493 | FTS *tree; | |
494 | FTSENT *node; | |
495 | GArray *trace_ids; | |
496 | char lpath[PATH_MAX]; | |
497 | char * const paths[2] = { lpath, NULL }; | |
0d567cf1 | 498 | int ret = -1; |
1fc22eb4 JD |
499 | |
500 | /* | |
501 | * Need to copy path, because fts_open can change it. | |
502 | * It is the pointer array, not the strings, that are constant. | |
503 | */ | |
504 | strncpy(lpath, path, PATH_MAX); | |
505 | lpath[PATH_MAX - 1] = '\0'; | |
506 | ||
507 | tree = fts_open(paths, FTS_NOCHDIR | FTS_LOGICAL, 0); | |
508 | if (tree == NULL) { | |
509 | fprintf(stderr, "[error] [Context] Cannot traverse \"%s\" for reading.\n", | |
510 | path); | |
511 | return -EINVAL; | |
512 | } | |
513 | ||
514 | trace_ids = g_array_new(FALSE, TRUE, sizeof(int)); | |
515 | ||
516 | while ((node = fts_read(tree))) { | |
517 | int dirfd, metafd; | |
518 | ||
519 | if (!(node->fts_info & FTS_D)) | |
520 | continue; | |
521 | ||
522 | dirfd = open(node->fts_accpath, 0); | |
523 | if (dirfd < 0) { | |
524 | fprintf(stderr, "[error] [Context] Unable to open trace " | |
525 | "directory file descriptor.\n"); | |
526 | ret = dirfd; | |
527 | goto error; | |
528 | } | |
529 | metafd = openat(dirfd, "metadata", O_RDONLY); | |
530 | if (metafd < 0) { | |
7314c855 JD |
531 | close(dirfd); |
532 | ret = -1; | |
533 | continue; | |
1fc22eb4 JD |
534 | } else { |
535 | int trace_id; | |
536 | ||
537 | ret = close(metafd); | |
538 | if (ret < 0) { | |
539 | perror("close"); | |
540 | goto error; | |
541 | } | |
542 | ret = close(dirfd); | |
543 | if (ret < 0) { | |
544 | perror("close"); | |
545 | goto error; | |
546 | } | |
547 | ||
548 | trace_id = bt_context_add_trace(ctx, | |
549 | node->fts_accpath, format_str, | |
550 | packet_seek, NULL, NULL); | |
551 | if (trace_id < 0) { | |
0d567cf1 | 552 | fprintf(stderr, "[warning] [Context] opening trace \"%s\" from %s " |
1fc22eb4 | 553 | "for reading.\n", node->fts_accpath, path); |
0d567cf1 JD |
554 | /* Allow to skip erroneous traces. */ |
555 | continue; | |
1fc22eb4 JD |
556 | } |
557 | g_array_append_val(trace_ids, trace_id); | |
558 | } | |
559 | } | |
560 | ||
561 | g_array_free(trace_ids, TRUE); | |
0d567cf1 | 562 | return ret; |
1fc22eb4 JD |
563 | |
564 | error: | |
565 | return ret; | |
566 | } | |
567 | ||
4553b4d1 | 568 | static int check_field_requirements(const struct bt_ctf_field_decl *const * field_list, |
a54d0d2c JD |
569 | int field_cnt, int *tid_check, int *pid_check, |
570 | int *procname_check, int *ppid_check) | |
571 | { | |
572 | int j; | |
bb6d72a2 | 573 | const char *name; |
715cf83c PBT |
574 | Quark perf; |
575 | Quark perf_root = state_system->getQuark("perf"); | |
576 | static bool first_perf_counter = true; | |
a54d0d2c JD |
577 | |
578 | for (j = 0; j < field_cnt; j++) { | |
bb6d72a2 | 579 | name = bt_ctf_get_decl_field_name(field_list[j]); |
a54d0d2c | 580 | if (*tid_check == 0) { |
bb6d72a2 | 581 | if (strncmp(name, "tid", 3) == 0) |
a54d0d2c | 582 | (*tid_check)++; |
a54d0d2c JD |
583 | } |
584 | if (*pid_check == 0) { | |
bb6d72a2 | 585 | if (strncmp(name, "tid", 3) == 0) |
a54d0d2c JD |
586 | (*pid_check)++; |
587 | } | |
588 | if (*ppid_check == 0) { | |
bb6d72a2 | 589 | if (strncmp(name, "ppid", 4) == 0) |
a54d0d2c JD |
590 | (*ppid_check)++; |
591 | } | |
592 | if (*procname_check == 0) { | |
bb6d72a2 | 593 | if (strncmp(name, "procname", 8) == 0) |
a54d0d2c JD |
594 | (*procname_check)++; |
595 | } | |
bb6d72a2 | 596 | if (strncmp(name, "perf_", 5) == 0) { |
715cf83c PBT |
597 | if (!state_system->attributeExists( |
598 | perf_root, name+5)) { | |
599 | perf = state_system->getQuark(perf_root, name+5); | |
bb6d72a2 | 600 | /* by default, sort on the first perf context */ |
715cf83c PBT |
601 | if (first_perf_counter) { |
602 | modify_attribute(0, &perf, "sort", 1); | |
603 | first_perf_counter = false; | |
604 | } | |
605 | modify_attribute(0, &perf, "visible", 1); | |
606 | add_in_sequence(0, perf, perf_root); | |
bb6d72a2 JD |
607 | } |
608 | } | |
a54d0d2c | 609 | } |
bb6d72a2 | 610 | |
a54d0d2c JD |
611 | if (*tid_check == 1 && *pid_check == 1 && *ppid_check == 1 && |
612 | *procname_check == 1) | |
613 | return 0; | |
614 | ||
615 | return -1; | |
616 | } | |
617 | ||
618 | /* | |
619 | * check_requirements: check if the required context informations are available | |
620 | * | |
621 | * If each mandatory context information is available for at least in one | |
622 | * event, return 0 otherwise return -1. | |
a54d0d2c JD |
623 | */ |
624 | int check_requirements(struct bt_context *ctx) | |
625 | { | |
626 | unsigned int i, evt_cnt, field_cnt; | |
627 | struct bt_ctf_event_decl *const * evt_list; | |
628 | const struct bt_ctf_field_decl *const * field_list; | |
629 | int tid_check = 0; | |
630 | int pid_check = 0; | |
631 | int procname_check = 0; | |
632 | int ppid_check = 0; | |
633 | int ret = 0; | |
634 | ||
635 | bt_ctf_get_event_decl_list(0, ctx, &evt_list, &evt_cnt); | |
636 | for (i = 0; i < evt_cnt; i++) { | |
637 | bt_ctf_get_decl_fields(evt_list[i], BT_STREAM_EVENT_CONTEXT, | |
638 | &field_list, &field_cnt); | |
639 | ret = check_field_requirements(field_list, field_cnt, | |
640 | &tid_check, &pid_check, &procname_check, | |
641 | &ppid_check); | |
a54d0d2c JD |
642 | |
643 | bt_ctf_get_decl_fields(evt_list[i], BT_EVENT_CONTEXT, | |
644 | &field_list, &field_cnt); | |
645 | ret = check_field_requirements(field_list, field_cnt, | |
646 | &tid_check, &pid_check, &procname_check, | |
647 | &ppid_check); | |
a54d0d2c JD |
648 | |
649 | bt_ctf_get_decl_fields(evt_list[i], BT_STREAM_PACKET_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 | ||
656 | if (tid_check == 0) { | |
657 | ret = -1; | |
658 | fprintf(stderr, "[error] missing tid context information\n"); | |
659 | } | |
660 | if (pid_check == 0) { | |
661 | ret = -1; | |
662 | fprintf(stderr, "[error] missing pid context information\n"); | |
663 | } | |
664 | if (ppid_check == 0) { | |
665 | ret = -1; | |
666 | fprintf(stderr, "[error] missing ppid context information\n"); | |
667 | } | |
668 | if (procname_check == 0) { | |
669 | ret = -1; | |
670 | fprintf(stderr, "[error] missing procname context information\n"); | |
671 | } | |
672 | ||
a54d0d2c JD |
673 | return ret; |
674 | } | |
675 | ||
1fc22eb4 JD |
676 | int main(int argc, char **argv) |
677 | { | |
678 | int ret; | |
679 | struct bt_context *bt_ctx = NULL; | |
680 | ||
681 | ret = parse_options(argc, argv); | |
682 | if (ret < 0) { | |
683 | fprintf(stdout, "Error parsing options.\n\n"); | |
684 | usage(stdout); | |
685 | exit(EXIT_FAILURE); | |
686 | } else if (ret > 0) { | |
687 | exit(EXIT_SUCCESS); | |
688 | } | |
689 | ||
690 | init_lttngtop(); | |
691 | ||
692 | bt_ctx = bt_context_create(); | |
693 | ret = bt_context_add_traces_recursive(bt_ctx, opt_input_path, "ctf", NULL); | |
694 | if (ret < 0) { | |
a54d0d2c JD |
695 | fprintf(stderr, "[error] Opening the trace\n"); |
696 | goto end; | |
697 | } | |
698 | ||
699 | ret = check_requirements(bt_ctx); | |
700 | if (ret < 0) { | |
6ff9c173 | 701 | fprintf(stderr, "[error] some mandatory contexts were missing, exiting.\n"); |
1fc22eb4 JD |
702 | goto end; |
703 | } | |
704 | ||
705 | pthread_create(&display_thread, NULL, ncurses_display, (void *) NULL); | |
706 | pthread_create(&timer_thread, NULL, refresh_thread, (void *) NULL); | |
707 | ||
708 | iter_trace(bt_ctx); | |
709 | ||
710 | quit = 1; | |
711 | pthread_join(display_thread, NULL); | |
85db4618 | 712 | pthread_join(timer_thread, NULL); |
1fc22eb4 JD |
713 | |
714 | end: | |
85db4618 | 715 | bt_context_put(bt_ctx); |
1fc22eb4 JD |
716 | return 0; |
717 | } |