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 static LttvTraceset
*traceset
;
54 static gboolean a_stats
;
55 static gboolean a_live
;
56 static int a_live_update_period
;
58 #define DEFAULT_LIVE_UPDATE_PERIOD 1
60 void lttv_trace_option(void *hook_data
)
65 trace
= ltt_trace_open_live(a_trace
);
67 trace
= ltt_trace_open(a_trace
);
69 if(trace
== NULL
) g_critical("cannot open trace %s", a_trace
);
70 lttv_traceset_add(traceset
, lttv_trace_new(trace
));
74 static gboolean
process_traceset(void *hook_data
, void *call_data
)
76 LttvAttributeValue value_expression
, value_filter
;
78 LttvIAttribute
*attributes
= LTTV_IATTRIBUTE(lttv_global_attributes());
80 LttvTracesetStats
*tscs
= NULL
;
82 LttvTracesetState
*tss
;
84 LttvTracesetContext
*tc
;
89 g_info("BatchAnalysis begin process traceset");
92 tscs
= g_object_new(LTTV_TRACESET_STATS_TYPE
, NULL
);
95 tss
= g_object_new(LTTV_TRACESET_STATE_TYPE
, NULL
);
99 g_info("BatchAnalysis initialize context");
101 lttv_context_init(tc
, traceset
);
105 lttv_state_add_event_hooks(tss
);
106 if(a_stats
) lttv_stats_add_event_hooks(tscs
);
108 retval
= lttv_iattribute_find_by_path(attributes
, "filter/expression",
109 LTTV_POINTER
, &value_expression
);
112 retval
= lttv_iattribute_find_by_path(attributes
, "filter/lttv_filter",
113 LTTV_POINTER
, &value_filter
);
116 /* Repeat the search for the first element, the second search might have
117 * moved the first element (by creating the second element)
119 retval
= lttv_iattribute_find_by_path(attributes
, "filter/expression",
120 LTTV_POINTER
, &value_expression
);
123 *(value_filter
.v_pointer
) = lttv_filter_new();
124 //g_debug("Filter string: %s",((GString*)*(value_expression.v_pointer))->str);
126 lttv_filter_append_expression(*(value_filter
.v_pointer
),((GString
*)*(value_expression
.v_pointer
))->str
);
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
,
140 end
.tv_sec
= G_MAXULONG
;
141 end
.tv_nsec
= G_MAXULONG
;
143 g_info("BatchAnalysis process traceset");
145 lttv_process_traceset_seek_time(tc
, start
);
146 /* Read as long a we do not reach the end (0) */
148 unsigned int updated_count
;
150 count
= lttv_process_traceset_middle(tc
,
155 updated_count
= lttv_process_traceset_update(tc
);
157 sleep(a_live_update_period
);
158 } while(count
!= 0 || updated_count
> 0);
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
,
171 g_info("BatchAnalysis destroy context");
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
);
178 g_object_unref(tscs
);
182 g_info("BatchAnalysis end process traceset");
189 LttvAttributeValue value
;
191 LttvIAttribute
*attributes
= LTTV_IATTRIBUTE(lttv_global_attributes());
194 g_info("Init batchAnalysis.c");
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
);
202 lttv_option_add("stats", 's',
203 "write the traceset and trace statistics",
205 LTTV_OPT_NONE
, &a_stats
, NULL
, NULL
);
208 lttv_option_add("live", 0,
209 "define if the traceset is receiving live informations",
211 LTTV_OPT_NONE
, &a_live
, NULL
, NULL
);
213 a_live_update_period
= DEFAULT_LIVE_UPDATE_PERIOD
;
214 lttv_option_add("live-period", 0,
215 "period to update a live trace",
218 &a_live_update_period
,
222 traceset
= lttv_traceset_new();
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();
234 retval
= lttv_iattribute_find_by_path(attributes
, "hooks/traceset/before",
235 LTTV_POINTER
, &value
);
237 *(value
.v_pointer
) = before_traceset
;
238 retval
= lttv_iattribute_find_by_path(attributes
, "hooks/traceset/after",
239 LTTV_POINTER
, &value
);
241 *(value
.v_pointer
) = after_traceset
;
242 retval
= lttv_iattribute_find_by_path(attributes
, "hooks/trace/before",
243 LTTV_POINTER
, &value
);
245 *(value
.v_pointer
) = before_trace
;
246 retval
= lttv_iattribute_find_by_path(attributes
, "hooks/trace/after",
247 LTTV_POINTER
, &value
);
249 *(value
.v_pointer
) = after_trace
;
250 retval
= lttv_iattribute_find_by_path(attributes
, "hooks/tracefile/before",
251 LTTV_POINTER
, &value
);
253 *(value
.v_pointer
) = before_tracefile
;
254 retval
= lttv_iattribute_find_by_path(attributes
, "hooks/tracefile/after",
255 LTTV_POINTER
, &value
);
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
);
267 *(value
.v_pointer
) = event_hook
;
269 retval
= lttv_iattribute_find_by_path(attributes
, "hooks/main/before",
270 LTTV_POINTER
, &value
);
272 g_assert((main_hooks
= *(value
.v_pointer
)) != NULL
);
273 lttv_hooks_add(main_hooks
, process_traceset
, NULL
, LTTV_PRIO_DEFAULT
);
276 static void destroy()
282 g_info("Destroy batchAnalysis.c");
284 lttv_option_remove("trace");
285 lttv_option_remove("stats");
286 lttv_option_remove("live");
287 lttv_option_remove("live-period");
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
);
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);
308 lttv_traceset_destroy(traceset
);
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")