1 /* This file is part of the Linux Trace Toolkit viewer
2 * Copyright (C) 2003-2004 Michel Dagenais
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;
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.
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,
19 /* This module inserts a hook in the program main loop. This hook processes
20 all the events in the main tracefile. */
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>
40 #include <babeltrace/context.h>
42 static LttvTraceset
*traceset
;
56 static gboolean a_stats
;
57 static gboolean a_live
;
58 static int a_live_update_period
;
60 #define DEFAULT_LIVE_UPDATE_PERIOD 1
62 void lttv_trace_option(void *hook_data
)
65 //trace_collection *trace;
66 //format *fmt = bt_lookup_format(g_quark_from_static_string("ctf"));
69 //trace = ltt_trace_open_live(a_trace);
72 //trace = bt_add_trace(a_trace,"ctf");
74 if(trace == NULL) g_critical("cannot open trace %s", a_trace);
75 lttv_traceset_add(traceset, lttv_trace_new(trace));*/
76 if(bt_context_add_trace(lttv_traceset_get_context(traceset
),
82 printf("Cannot add trace %s", a_trace
);
87 static gboolean
process_traceset(void *hook_data
, void *call_data
)
89 LttvAttributeValue value_expression
, value_filter
;
91 LttvIAttribute
*attributes
= LTTV_IATTRIBUTE(lttv_global_attributes());
93 LttvTracesetStats
*tscs
= NULL
;
95 LttvTracesetState
*tss
;
97 LttvTracesetContext
*tc
;
102 g_info("BatchAnalysis begin process traceset");
105 tscs
= g_object_new(LTTV_TRACESET_STATS_TYPE
, NULL
);
108 tss
= g_object_new(LTTV_TRACESET_STATE_TYPE
, NULL
);
112 g_info("BatchAnalysis initialize context");
114 lttv_context_init(tc
, traceset
);
118 lttv_state_add_event_hooks(tss
);
119 if(a_stats
) lttv_stats_add_event_hooks(tscs
);
121 retval
= lttv_iattribute_find_by_path(attributes
, "filter/expression",
122 LTTV_POINTER
, &value_expression
);
125 retval
= lttv_iattribute_find_by_path(attributes
, "filter/lttv_filter",
126 LTTV_POINTER
, &value_filter
);
129 /* Repeat the search for the first element, the second search might have
130 * moved the first element (by creating the second element)
132 retval
= lttv_iattribute_find_by_path(attributes
, "filter/expression",
133 LTTV_POINTER
, &value_expression
);
136 *(value_filter
.v_pointer
) = lttv_filter_new();
137 //g_debug("Filter string: %s",((GString*)*(value_expression.v_pointer))->str);
139 lttv_filter_append_expression(*(value_filter
.v_pointer
),((GString
*)*(value_expression
.v_pointer
))->str
);
141 //lttv_traceset_context_add_hooks(tc,
142 //before_traceset, after_traceset, NULL, before_trace, after_trace,
143 //NULL, before_tracefile, after_tracefile, NULL, before_event, after_event);
144 lttv_process_traceset_begin(tc
,
153 end
.tv_sec
= G_MAXULONG
;
154 end
.tv_nsec
= G_MAXULONG
;
156 g_info("BatchAnalysis process traceset");
158 lttv_process_traceset_seek_time(tc
, start
);
159 /* Read as long a we do not reach the end (0) */
161 unsigned int updated_count
;
163 count
= lttv_process_traceset_middle(tc
,
168 updated_count
= lttv_process_traceset_update(tc
);
170 sleep(a_live_update_period
);
171 } while(count
!= 0 || updated_count
> 0);
174 //lttv_traceset_context_remove_hooks(tc,
175 //before_traceset, after_traceset, NULL, before_trace, after_trace,
176 //NULL, before_tracefile, after_tracefile, NULL, before_event, after_event);
177 lttv_process_traceset_end(tc
,
184 g_info("BatchAnalysis destroy context");
186 lttv_filter_destroy(*(value_filter
.v_pointer
));
187 lttv_state_remove_event_hooks(tss
);
188 if(a_stats
) lttv_stats_remove_event_hooks(tscs
);
189 lttv_context_fini(tc
);
191 g_object_unref(tscs
);
195 g_info("BatchAnalysis end process traceset");
202 LttvAttributeValue value
;
204 LttvIAttribute
*attributes
= LTTV_IATTRIBUTE(lttv_global_attributes());
207 g_info("Init batchAnalysis.c");
209 lttv_option_add("trace", 't',
210 "add a trace to the trace set to analyse",
211 "pathname of the directory containing the trace",
212 LTTV_OPT_STRING
, &a_trace
, lttv_trace_option
, NULL
);
215 lttv_option_add("stats", 's',
216 "write the traceset and trace statistics",
218 LTTV_OPT_NONE
, &a_stats
, NULL
, NULL
);
221 lttv_option_add("live", 0,
222 "define if the traceset is receiving live informations",
224 LTTV_OPT_NONE
, &a_live
, NULL
, NULL
);
226 a_live_update_period
= DEFAULT_LIVE_UPDATE_PERIOD
;
227 lttv_option_add("live-period", 0,
228 "period to update a live trace",
231 &a_live_update_period
,
235 traceset
= lttv_traceset_new();
237 before_traceset
= lttv_hooks_new();
238 after_traceset
= lttv_hooks_new();
239 before_trace
= lttv_hooks_new();
240 after_trace
= lttv_hooks_new();
241 before_tracefile
= lttv_hooks_new();
242 after_tracefile
= lttv_hooks_new();
243 //before_event = lttv_hooks_new();
244 //after_event = lttv_hooks_new();
245 event_hook
= lttv_hooks_new();
247 retval
= lttv_iattribute_find_by_path(attributes
, "hooks/traceset/before",
248 LTTV_POINTER
, &value
);
250 *(value
.v_pointer
) = before_traceset
;
251 retval
= lttv_iattribute_find_by_path(attributes
, "hooks/traceset/after",
252 LTTV_POINTER
, &value
);
254 *(value
.v_pointer
) = after_traceset
;
255 retval
= lttv_iattribute_find_by_path(attributes
, "hooks/trace/before",
256 LTTV_POINTER
, &value
);
258 *(value
.v_pointer
) = before_trace
;
259 retval
= lttv_iattribute_find_by_path(attributes
, "hooks/trace/after",
260 LTTV_POINTER
, &value
);
262 *(value
.v_pointer
) = after_trace
;
263 retval
= lttv_iattribute_find_by_path(attributes
, "hooks/tracefile/before",
264 LTTV_POINTER
, &value
);
266 *(value
.v_pointer
) = before_tracefile
;
267 retval
= lttv_iattribute_find_by_path(attributes
, "hooks/tracefile/after",
268 LTTV_POINTER
, &value
);
270 *(value
.v_pointer
) = after_tracefile
;
271 //g_assert(lttv_iattribute_find_by_path(attributes, "hooks/event/before",
272 // LTTV_POINTER, &value));
273 //*(value.v_pointer) = before_event;
274 //g_assert(lttv_iattribute_find_by_path(attributes, "hooks/event/after",
275 // LTTV_POINTER, &value));
276 //*(value.v_pointer) = after_event;
277 retval
= lttv_iattribute_find_by_path(attributes
, "hooks/event",
278 LTTV_POINTER
, &value
);
280 *(value
.v_pointer
) = event_hook
;
282 retval
= lttv_iattribute_find_by_path(attributes
, "hooks/main/before",
283 LTTV_POINTER
, &value
);
285 g_assert((main_hooks
= *(value
.v_pointer
)) != NULL
);
286 lttv_hooks_add(main_hooks
, process_traceset
, NULL
, LTTV_PRIO_DEFAULT
);
289 static void destroy()
295 g_info("Destroy batchAnalysis.c");
297 lttv_option_remove("trace");
298 lttv_option_remove("stats");
299 lttv_option_remove("live");
300 lttv_option_remove("live-period");
302 lttv_hooks_destroy(before_traceset
);
303 lttv_hooks_destroy(after_traceset
);
304 lttv_hooks_destroy(before_trace
);
305 lttv_hooks_destroy(after_trace
);
306 lttv_hooks_destroy(before_tracefile
);
307 lttv_hooks_destroy(after_tracefile
);
308 //lttv_hooks_destroy(before_event);
309 //lttv_hooks_destroy(after_event);
310 lttv_hooks_destroy(event_hook
);
311 lttv_hooks_remove_data(main_hooks
, process_traceset
, NULL
);
313 nb
= lttv_traceset_number(traceset
);
314 for(i
= 0 ; i
< nb
; i
++) {
315 trace
= lttv_traceset_get(traceset
, i
);
316 ltt_trace_close(lttv_trace(trace
));
317 /* This will be done by lttv_traceset_destroy */
318 //lttv_trace_destroy(trace);
321 lttv_traceset_destroy(traceset
);
324 LTTV_MODULE("batchAnalysis", "Batch processing of a trace", \
325 "Run through a trace calling all the registered hooks", \
326 init
, destroy
, "state", "stats", "option","textFilter", "sync")