| 1 | /* This file is part of the Linux Trace Toolkit viewer |
| 2 | * Copyright (C) 2003-2004 Michel Dagenais |
| 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 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this program; if not, write to the Free Software |
| 15 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
| 16 | * MA 02111-1307, USA. |
| 17 | */ |
| 18 | |
| 19 | /* This module inserts a hook in the program main loop. This hook processes |
| 20 | all the events in the main tracefile. */ |
| 21 | |
| 22 | #ifdef HAVE_CONFIG_H |
| 23 | #include <config.h> |
| 24 | #endif |
| 25 | |
| 26 | #include <glib.h> |
| 27 | #include <unistd.h> |
| 28 | #include <lttv/lttv.h> |
| 29 | #include <lttv/attribute.h> |
| 30 | #include <lttv/hook.h> |
| 31 | #include <lttv/option.h> |
| 32 | #include <lttv/module.h> |
| 33 | #include <lttv/tracecontext.h> |
| 34 | #include <lttv/state.h> |
| 35 | #include <lttv/stats.h> |
| 36 | #include <lttv/filter.h> |
| 37 | #include <ltt/trace.h> |
| 38 | #include <lttv/sync/sync_chain_lttv.h> |
| 39 | |
| 40 | static LttvTraceset *traceset; |
| 41 | |
| 42 | static LttvHooks |
| 43 | *before_traceset, |
| 44 | *after_traceset, |
| 45 | *before_trace, |
| 46 | *after_trace, |
| 47 | *before_tracefile, |
| 48 | *after_tracefile, |
| 49 | *event_hook, |
| 50 | *main_hooks; |
| 51 | |
| 52 | static char *a_trace; |
| 53 | |
| 54 | static gboolean a_stats; |
| 55 | static gboolean a_live; |
| 56 | static int a_live_update_period; |
| 57 | |
| 58 | #define DEFAULT_LIVE_UPDATE_PERIOD 1 |
| 59 | |
| 60 | void lttv_trace_option(void *hook_data) |
| 61 | { |
| 62 | LttTrace *trace; |
| 63 | |
| 64 | if(a_live) { |
| 65 | trace = ltt_trace_open_live(a_trace); |
| 66 | } else { |
| 67 | trace = ltt_trace_open(a_trace); |
| 68 | } |
| 69 | if(trace == NULL) g_critical("cannot open trace %s", a_trace); |
| 70 | lttv_traceset_add(traceset, lttv_trace_new(trace)); |
| 71 | } |
| 72 | |
| 73 | |
| 74 | static gboolean process_traceset(void *hook_data, void *call_data) |
| 75 | { |
| 76 | LttvAttributeValue value_expression, value_filter; |
| 77 | |
| 78 | LttvIAttribute *attributes = LTTV_IATTRIBUTE(lttv_global_attributes()); |
| 79 | |
| 80 | LttvTracesetStats *tscs = NULL; |
| 81 | |
| 82 | LttvTracesetState *tss; |
| 83 | |
| 84 | LttvTracesetContext *tc; |
| 85 | |
| 86 | LttTime start, end; |
| 87 | gboolean retval; |
| 88 | |
| 89 | g_info("BatchAnalysis begin process traceset"); |
| 90 | |
| 91 | if (a_stats) { |
| 92 | tscs = g_object_new(LTTV_TRACESET_STATS_TYPE, NULL); |
| 93 | tss = &tscs->parent; |
| 94 | } else { |
| 95 | tss = g_object_new(LTTV_TRACESET_STATE_TYPE, NULL); |
| 96 | } |
| 97 | tc = &tss->parent; |
| 98 | |
| 99 | g_info("BatchAnalysis initialize context"); |
| 100 | |
| 101 | lttv_context_init(tc, traceset); |
| 102 | |
| 103 | syncTraceset(tc); |
| 104 | |
| 105 | lttv_state_add_event_hooks(tss); |
| 106 | if(a_stats) lttv_stats_add_event_hooks(tscs); |
| 107 | |
| 108 | retval= lttv_iattribute_find_by_path(attributes, "filter/expression", |
| 109 | LTTV_POINTER, &value_expression); |
| 110 | g_assert(retval); |
| 111 | |
| 112 | retval= lttv_iattribute_find_by_path(attributes, "filter/lttv_filter", |
| 113 | LTTV_POINTER, &value_filter); |
| 114 | g_assert(retval); |
| 115 | |
| 116 | /* Repeat the search for the first element, the second search might have |
| 117 | * moved the first element (by creating the second element) |
| 118 | */ |
| 119 | retval= lttv_iattribute_find_by_path(attributes, "filter/expression", |
| 120 | LTTV_POINTER, &value_expression); |
| 121 | g_assert(retval); |
| 122 | |
| 123 | *(value_filter.v_pointer) = lttv_filter_new(); |
| 124 | //g_debug("Filter string: %s",((GString*)*(value_expression.v_pointer))->str); |
| 125 | |
| 126 | lttv_filter_append_expression(*(value_filter.v_pointer),((GString*)*(value_expression.v_pointer))->str); |
| 127 | |
| 128 | //lttv_traceset_context_add_hooks(tc, |
| 129 | //before_traceset, after_traceset, NULL, before_trace, after_trace, |
| 130 | //NULL, before_tracefile, after_tracefile, NULL, before_event, after_event); |
| 131 | lttv_process_traceset_begin(tc, |
| 132 | before_traceset, |
| 133 | before_trace, |
| 134 | before_tracefile, |
| 135 | event_hook, |
| 136 | NULL); |
| 137 | |
| 138 | start.tv_sec = 0; |
| 139 | start.tv_nsec = 0; |
| 140 | end.tv_sec = G_MAXULONG; |
| 141 | end.tv_nsec = G_MAXULONG; |
| 142 | |
| 143 | g_info("BatchAnalysis process traceset"); |
| 144 | |
| 145 | lttv_process_traceset_seek_time(tc, start); |
| 146 | /* Read as long a we do not reach the end (0) */ |
| 147 | unsigned int count; |
| 148 | unsigned int updated_count; |
| 149 | do { |
| 150 | count = lttv_process_traceset_middle(tc, |
| 151 | end, |
| 152 | G_MAXULONG, |
| 153 | NULL); |
| 154 | |
| 155 | updated_count = lttv_process_traceset_update(tc); |
| 156 | |
| 157 | sleep(a_live_update_period); |
| 158 | } while(count != 0 || updated_count > 0); |
| 159 | |
| 160 | |
| 161 | //lttv_traceset_context_remove_hooks(tc, |
| 162 | //before_traceset, after_traceset, NULL, before_trace, after_trace, |
| 163 | //NULL, before_tracefile, after_tracefile, NULL, before_event, after_event); |
| 164 | lttv_process_traceset_end(tc, |
| 165 | after_traceset, |
| 166 | after_trace, |
| 167 | after_tracefile, |
| 168 | event_hook, |
| 169 | NULL); |
| 170 | |
| 171 | g_info("BatchAnalysis destroy context"); |
| 172 | |
| 173 | lttv_filter_destroy(*(value_filter.v_pointer)); |
| 174 | lttv_state_remove_event_hooks(tss); |
| 175 | if(a_stats) lttv_stats_remove_event_hooks(tscs); |
| 176 | lttv_context_fini(tc); |
| 177 | if (a_stats) |
| 178 | g_object_unref(tscs); |
| 179 | else |
| 180 | g_object_unref(tss); |
| 181 | |
| 182 | g_info("BatchAnalysis end process traceset"); |
| 183 | return FALSE; |
| 184 | } |
| 185 | |
| 186 | |
| 187 | static void init() |
| 188 | { |
| 189 | LttvAttributeValue value; |
| 190 | |
| 191 | LttvIAttribute *attributes = LTTV_IATTRIBUTE(lttv_global_attributes()); |
| 192 | gboolean retval; |
| 193 | |
| 194 | g_info("Init batchAnalysis.c"); |
| 195 | |
| 196 | lttv_option_add("trace", 't', |
| 197 | "add a trace to the trace set to analyse", |
| 198 | "pathname of the directory containing the trace", |
| 199 | LTTV_OPT_STRING, &a_trace, lttv_trace_option, NULL); |
| 200 | |
| 201 | a_stats = FALSE; |
| 202 | lttv_option_add("stats", 's', |
| 203 | "write the traceset and trace statistics", |
| 204 | "", |
| 205 | LTTV_OPT_NONE, &a_stats, NULL, NULL); |
| 206 | |
| 207 | a_live = FALSE; |
| 208 | lttv_option_add("live", 0, |
| 209 | "define if the traceset is receiving live informations", |
| 210 | "", |
| 211 | LTTV_OPT_NONE, &a_live, NULL, NULL); |
| 212 | |
| 213 | a_live_update_period = DEFAULT_LIVE_UPDATE_PERIOD; |
| 214 | lttv_option_add("live-period", 0, |
| 215 | "period to update a live trace", |
| 216 | "in seconds", |
| 217 | LTTV_OPT_INT, |
| 218 | &a_live_update_period, |
| 219 | NULL, NULL); |
| 220 | |
| 221 | |
| 222 | traceset = lttv_traceset_new(); |
| 223 | |
| 224 | before_traceset = lttv_hooks_new(); |
| 225 | after_traceset = lttv_hooks_new(); |
| 226 | before_trace = lttv_hooks_new(); |
| 227 | after_trace = lttv_hooks_new(); |
| 228 | before_tracefile = lttv_hooks_new(); |
| 229 | after_tracefile = lttv_hooks_new(); |
| 230 | //before_event = lttv_hooks_new(); |
| 231 | //after_event = lttv_hooks_new(); |
| 232 | event_hook = lttv_hooks_new(); |
| 233 | |
| 234 | retval= lttv_iattribute_find_by_path(attributes, "hooks/traceset/before", |
| 235 | LTTV_POINTER, &value); |
| 236 | g_assert(retval); |
| 237 | *(value.v_pointer) = before_traceset; |
| 238 | retval= lttv_iattribute_find_by_path(attributes, "hooks/traceset/after", |
| 239 | LTTV_POINTER, &value); |
| 240 | g_assert(retval); |
| 241 | *(value.v_pointer) = after_traceset; |
| 242 | retval= lttv_iattribute_find_by_path(attributes, "hooks/trace/before", |
| 243 | LTTV_POINTER, &value); |
| 244 | g_assert(retval); |
| 245 | *(value.v_pointer) = before_trace; |
| 246 | retval= lttv_iattribute_find_by_path(attributes, "hooks/trace/after", |
| 247 | LTTV_POINTER, &value); |
| 248 | g_assert(retval); |
| 249 | *(value.v_pointer) = after_trace; |
| 250 | retval= lttv_iattribute_find_by_path(attributes, "hooks/tracefile/before", |
| 251 | LTTV_POINTER, &value); |
| 252 | g_assert(retval); |
| 253 | *(value.v_pointer) = before_tracefile; |
| 254 | retval= lttv_iattribute_find_by_path(attributes, "hooks/tracefile/after", |
| 255 | LTTV_POINTER, &value); |
| 256 | g_assert(retval); |
| 257 | *(value.v_pointer) = after_tracefile; |
| 258 | //g_assert(lttv_iattribute_find_by_path(attributes, "hooks/event/before", |
| 259 | // LTTV_POINTER, &value)); |
| 260 | //*(value.v_pointer) = before_event; |
| 261 | //g_assert(lttv_iattribute_find_by_path(attributes, "hooks/event/after", |
| 262 | // LTTV_POINTER, &value)); |
| 263 | //*(value.v_pointer) = after_event; |
| 264 | retval= lttv_iattribute_find_by_path(attributes, "hooks/event", |
| 265 | LTTV_POINTER, &value); |
| 266 | g_assert(retval); |
| 267 | *(value.v_pointer) = event_hook; |
| 268 | |
| 269 | retval= lttv_iattribute_find_by_path(attributes, "hooks/main/before", |
| 270 | LTTV_POINTER, &value); |
| 271 | g_assert(retval); |
| 272 | g_assert((main_hooks = *(value.v_pointer)) != NULL); |
| 273 | lttv_hooks_add(main_hooks, process_traceset, NULL, LTTV_PRIO_DEFAULT); |
| 274 | } |
| 275 | |
| 276 | static void destroy() |
| 277 | { |
| 278 | guint i, nb; |
| 279 | |
| 280 | LttvTrace *trace; |
| 281 | |
| 282 | g_info("Destroy batchAnalysis.c"); |
| 283 | |
| 284 | lttv_option_remove("trace"); |
| 285 | lttv_option_remove("stats"); |
| 286 | lttv_option_remove("live"); |
| 287 | lttv_option_remove("live-period"); |
| 288 | |
| 289 | lttv_hooks_destroy(before_traceset); |
| 290 | lttv_hooks_destroy(after_traceset); |
| 291 | lttv_hooks_destroy(before_trace); |
| 292 | lttv_hooks_destroy(after_trace); |
| 293 | lttv_hooks_destroy(before_tracefile); |
| 294 | lttv_hooks_destroy(after_tracefile); |
| 295 | //lttv_hooks_destroy(before_event); |
| 296 | //lttv_hooks_destroy(after_event); |
| 297 | lttv_hooks_destroy(event_hook); |
| 298 | lttv_hooks_remove_data(main_hooks, process_traceset, NULL); |
| 299 | |
| 300 | nb = lttv_traceset_number(traceset); |
| 301 | for(i = 0 ; i < nb ; i++) { |
| 302 | trace = lttv_traceset_get(traceset, i); |
| 303 | ltt_trace_close(lttv_trace(trace)); |
| 304 | /* This will be done by lttv_traceset_destroy */ |
| 305 | //lttv_trace_destroy(trace); |
| 306 | } |
| 307 | |
| 308 | lttv_traceset_destroy(traceset); |
| 309 | } |
| 310 | |
| 311 | LTTV_MODULE("batchAnalysis", "Batch processing of a trace", \ |
| 312 | "Run through a trace calling all the registered hooks", \ |
| 313 | init, destroy, "state", "stats", "option","textFilter", "sync") |