f78f37749f28e27f7d4f22ae5bfe6eea600f5c71
[lttv.git] / lttv / lttv / modules / text / batchAnalysis.c
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 <lttv/lttv.h>
28 #include <lttv/attribute.h>
29 #include <lttv/hook.h>
30 #include <lttv/option.h>
31 #include <lttv/module.h>
32 #include <lttv/tracecontext.h>
33 #include <lttv/state.h>
34 #include <lttv/stats.h>
35 #include <lttv/filter.h>
36 #include <ltt/trace.h>
37
38 static LttvTraceset *traceset;
39
40 static LttvHooks
41 *before_traceset,
42 *after_traceset,
43 *before_trace,
44 *after_trace,
45 *before_tracefile,
46 *after_tracefile,
47 *event_hook,
48 *main_hooks;
49
50 static char *a_trace;
51
52 static gboolean a_stats;
53
54 void lttv_trace_option(void *hook_data)
55 {
56 LttTrace *trace;
57
58 trace = ltt_trace_open(a_trace);
59 if(trace == NULL) g_critical("cannot open trace %s", a_trace);
60 lttv_traceset_add(traceset, lttv_trace_new(trace));
61 }
62
63
64 static gboolean process_traceset(void *hook_data, void *call_data)
65 {
66 LttvAttributeValue value_expression, value_filter;
67
68 LttvIAttribute *attributes = LTTV_IATTRIBUTE(lttv_global_attributes());
69
70 LttvTracesetStats *tscs;
71
72 LttvTracesetState *tss;
73
74 LttvTracesetContext *tc;
75
76 LttTime start, end;
77
78 g_info("BatchAnalysis begin process traceset");
79
80 if (a_stats) {
81 tscs = g_object_new(LTTV_TRACESET_STATS_TYPE, NULL);
82 tss = &tscs->parent;
83 } else {
84 tss = g_object_new(LTTV_TRACESET_STATE_TYPE, NULL);
85 }
86 tc = &tss->parent;
87
88 g_info("BatchAnalysis initialize context");
89
90 lttv_context_init(tc, traceset);
91 lttv_state_add_event_hooks(tc);
92 if(a_stats) lttv_stats_add_event_hooks(tscs);
93
94 g_assert(lttv_iattribute_find_by_path(attributes, "filter/expression",
95 LTTV_POINTER, &value_expression));
96
97 g_assert(lttv_iattribute_find_by_path(attributes, "filter/lttv_filter",
98 LTTV_POINTER, &value_filter));
99
100 *(value_filter.v_pointer) = lttv_filter_new();
101 //g_debug("Filter string: %s",((GString*)*(value_expression.v_pointer))->str);
102
103 lttv_filter_append_expression(*(value_filter.v_pointer),((GString*)*(value_expression.v_pointer))->str);
104
105 //lttv_traceset_context_add_hooks(tc,
106 //before_traceset, after_traceset, NULL, before_trace, after_trace,
107 //NULL, before_tracefile, after_tracefile, NULL, before_event, after_event);
108 lttv_process_traceset_begin(tc,
109 before_traceset,
110 before_trace,
111 before_tracefile,
112 event_hook,
113 NULL);
114
115 start.tv_sec = 0;
116 start.tv_nsec = 0;
117 end.tv_sec = G_MAXULONG;
118 end.tv_nsec = G_MAXULONG;
119
120 g_info("BatchAnalysis process traceset");
121
122 lttv_process_traceset_seek_time(tc, start);
123 lttv_process_traceset_middle(tc,
124 end,
125 G_MAXULONG,
126 NULL);
127
128
129 //lttv_traceset_context_remove_hooks(tc,
130 //before_traceset, after_traceset, NULL, before_trace, after_trace,
131 //NULL, before_tracefile, after_tracefile, NULL, before_event, after_event);
132 lttv_process_traceset_end(tc,
133 after_traceset,
134 after_trace,
135 after_tracefile,
136 event_hook,
137 NULL);
138
139 g_info("BatchAnalysis destroy context");
140
141 lttv_filter_destroy(*(value_filter.v_pointer));
142 lttv_state_remove_event_hooks(tss);
143 if(a_stats) lttv_stats_remove_event_hooks(tscs);
144 lttv_context_fini(tc);
145 if (a_stats)
146 g_object_unref(tscs);
147 else
148 g_object_unref(tss);
149
150 g_info("BatchAnalysis end process traceset");
151 return FALSE;
152 }
153
154
155 static void init()
156 {
157 LttvAttributeValue value;
158
159 LttvIAttribute *attributes = LTTV_IATTRIBUTE(lttv_global_attributes());
160
161 g_info("Init batchAnalysis.c");
162
163 lttv_option_add("trace", 't',
164 "add a trace to the trace set to analyse",
165 "pathname of the directory containing the trace",
166 LTTV_OPT_STRING, &a_trace, lttv_trace_option, NULL);
167
168 a_stats = FALSE;
169 lttv_option_add("stats", 's',
170 "write the traceset and trace statistics",
171 "",
172 LTTV_OPT_NONE, &a_stats, NULL, NULL);
173
174
175 traceset = lttv_traceset_new();
176
177 before_traceset = lttv_hooks_new();
178 after_traceset = lttv_hooks_new();
179 before_trace = lttv_hooks_new();
180 after_trace = lttv_hooks_new();
181 before_tracefile = lttv_hooks_new();
182 after_tracefile = lttv_hooks_new();
183 //before_event = lttv_hooks_new();
184 //after_event = lttv_hooks_new();
185 event_hook = lttv_hooks_new();
186
187 g_assert(lttv_iattribute_find_by_path(attributes, "hooks/traceset/before",
188 LTTV_POINTER, &value));
189 *(value.v_pointer) = before_traceset;
190 g_assert(lttv_iattribute_find_by_path(attributes, "hooks/traceset/after",
191 LTTV_POINTER, &value));
192 *(value.v_pointer) = after_traceset;
193 g_assert(lttv_iattribute_find_by_path(attributes, "hooks/trace/before",
194 LTTV_POINTER, &value));
195 *(value.v_pointer) = before_trace;
196 g_assert(lttv_iattribute_find_by_path(attributes, "hooks/trace/after",
197 LTTV_POINTER, &value));
198 *(value.v_pointer) = after_trace;
199 g_assert(lttv_iattribute_find_by_path(attributes, "hooks/tracefile/before",
200 LTTV_POINTER, &value));
201 *(value.v_pointer) = before_tracefile;
202 g_assert(lttv_iattribute_find_by_path(attributes, "hooks/tracefile/after",
203 LTTV_POINTER, &value));
204 *(value.v_pointer) = after_tracefile;
205 //g_assert(lttv_iattribute_find_by_path(attributes, "hooks/event/before",
206 // LTTV_POINTER, &value));
207 //*(value.v_pointer) = before_event;
208 //g_assert(lttv_iattribute_find_by_path(attributes, "hooks/event/after",
209 // LTTV_POINTER, &value));
210 //*(value.v_pointer) = after_event;
211 g_assert(lttv_iattribute_find_by_path(attributes, "hooks/event",
212 LTTV_POINTER, &value));
213 *(value.v_pointer) = event_hook;
214
215 g_assert(lttv_iattribute_find_by_path(attributes, "hooks/main/before",
216 LTTV_POINTER, &value));
217 g_assert((main_hooks = *(value.v_pointer)) != NULL);
218 lttv_hooks_add(main_hooks, process_traceset, NULL, LTTV_PRIO_DEFAULT);
219 }
220
221 static void destroy()
222 {
223 guint i, nb;
224
225 LttvTrace *trace;
226
227 g_info("Destroy batchAnalysis.c");
228
229 lttv_option_remove("trace");
230 lttv_option_remove("stats");
231
232 lttv_hooks_destroy(before_traceset);
233 lttv_hooks_destroy(after_traceset);
234 lttv_hooks_destroy(before_trace);
235 lttv_hooks_destroy(after_trace);
236 lttv_hooks_destroy(before_tracefile);
237 lttv_hooks_destroy(after_tracefile);
238 //lttv_hooks_destroy(before_event);
239 //lttv_hooks_destroy(after_event);
240 lttv_hooks_destroy(event_hook);
241 lttv_hooks_remove_data(main_hooks, process_traceset, NULL);
242
243 nb = lttv_traceset_number(traceset);
244 for(i = 0 ; i < nb ; i++) {
245 trace = lttv_traceset_get(traceset, i);
246 ltt_trace_close(lttv_trace(trace));
247 /* This will be done by lttv_traceset_destroy */
248 //lttv_trace_destroy(trace);
249 }
250
251 lttv_traceset_destroy(traceset);
252 }
253
254 LTTV_MODULE("batchAnalysis", "Batch processing of a trace", \
255 "Run through a trace calling all the registered hooks", \
256 init, destroy, "state", "stats", "option","textFilter")
This page took 0.035007 seconds and 3 git commands to generate.