08b1c66e |
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 while testing the speed and |
21 | functionality of the state and stats computations. */ |
08b1c66e |
22 | |
4e4d11b3 |
23 | #ifdef HAVE_CONFIG_H |
24 | #include <config.h> |
25 | #endif |
26 | |
27 | #include <string.h> |
08b1c66e |
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> |
d8f124de |
33 | #include <lttv/tracecontext.h> |
08b1c66e |
34 | #include <lttv/state.h> |
35 | #include <lttv/stats.h> |
36 | #include <ltt/trace.h> |
37 | #include <ltt/event.h> |
338d4282 |
38 | #include <ltt/type.h> |
00e74b69 |
39 | #include <ltt/facility.h> |
40 | |
41 | #define __UNUSED__ __attribute__((__unused__)) |
08b1c66e |
42 | |
43 | static LttvTraceset *traceset; |
44 | |
45 | static LttvHooks |
46 | *before_traceset, |
47 | *after_traceset, |
48 | *before_trace, |
49 | *after_trace, |
50 | *before_tracefile, |
51 | *after_tracefile, |
b8eccacd |
52 | //*before_event, |
53 | //*after_event, |
54 | *event_hook, |
08b1c66e |
55 | *main_hooks; |
56 | |
57 | static char *a_trace; |
58 | |
59 | static char *a_dump_tracefiles; |
60 | |
61 | static char *a_save_sample; |
62 | |
63 | static int |
64 | a_sample_interval, |
65 | a_sample_number, |
911b7a3c |
66 | a_seek_number, |
08b1c66e |
67 | a_save_interval; |
68 | |
69 | static gboolean |
f95bc830 |
70 | a_trace_event, |
71 | a_save_state_copy, |
08b1c66e |
72 | a_test1, |
73 | a_test2, |
74 | a_test3, |
75 | a_test4, |
76 | a_test5, |
77 | a_test6, |
78 | a_test7, |
728d0c3e |
79 | a_test8, |
08b1c66e |
80 | a_test_all; |
81 | |
eed2ef37 |
82 | static GQuark QUARK_BLOCK_START, |
83 | QUARK_BLOCK_END; |
84 | |
f95bc830 |
85 | LttEventPosition *a_event_position; |
86 | |
08b1c66e |
87 | typedef struct _save_state { |
88 | guint count; |
89 | FILE *fp; |
90 | guint interval; |
91 | guint position; |
92 | guint size; |
93 | LttTime *write_time; |
94 | guint version; |
95 | } SaveState; |
96 | |
97 | |
00e74b69 |
98 | static void lttv_trace_option(void __UNUSED__ *hook_data) |
08b1c66e |
99 | { |
100 | LttTrace *trace; |
101 | |
102 | trace = ltt_trace_open(a_trace); |
e45551ac |
103 | if(trace == NULL) { |
104 | g_critical("cannot open trace %s", a_trace); |
105 | } else { |
106 | lttv_traceset_add(traceset, lttv_trace_new(trace)); |
107 | } |
08b1c66e |
108 | } |
109 | |
110 | static double get_time() |
111 | { |
112 | GTimeVal gt; |
113 | |
114 | g_get_current_time(>); |
115 | return gt.tv_sec + (double)gt.tv_usec / (double)1000000.0; |
116 | } |
117 | |
118 | static double run_one_test(LttvTracesetState *ts, LttTime start, LttTime end) |
119 | { |
120 | double t0, t1; |
121 | |
00e74b69 |
122 | unsigned int i; |
f95bc830 |
123 | |
b8eccacd |
124 | //lttv_traceset_context_add_hooks(&ts->parent, |
125 | //before_traceset, after_traceset, NULL, before_trace, after_trace, |
126 | //NULL, before_tracefile, after_tracefile, NULL, before_event, after_event); |
127 | lttv_process_traceset_begin(&ts->parent, |
128 | before_traceset, |
129 | before_trace, |
130 | before_tracefile, |
131 | event_hook, |
132 | NULL); |
08b1c66e |
133 | |
f95bc830 |
134 | for(i = 0 ; i < lttv_traceset_number(traceset) ; i++) { |
135 | ((LttvTraceState *)(ts->parent.traces[i]))->save_interval =a_save_interval; |
136 | } |
137 | |
08b1c66e |
138 | t0 = get_time(); |
139 | lttv_state_traceset_seek_time_closest(ts, start); |
b8eccacd |
140 | //lttv_process_traceset(&ts->parent, end, G_MAXULONG); |
141 | lttv_process_traceset_middle(&ts->parent, |
142 | end, |
143 | G_MAXULONG, |
144 | NULL); |
08b1c66e |
145 | t1 = get_time(); |
146 | |
b8eccacd |
147 | //lttv_traceset_context_remove_hooks(&ts->parent, |
148 | //before_traceset, after_traceset, NULL, before_trace, after_trace, |
149 | //NULL, before_tracefile, after_tracefile, NULL, before_event, after_event); |
150 | lttv_process_traceset_end(&ts->parent, |
151 | after_traceset, |
152 | after_trace, |
153 | after_tracefile, |
154 | event_hook, |
155 | NULL); |
08b1c66e |
156 | |
157 | return t1 - t0; |
158 | } |
159 | |
160 | |
00e74b69 |
161 | gboolean trace_event(void __UNUSED__ *hook_data, void *call_data) |
f95bc830 |
162 | { |
163 | LttvTracefileState *tfs = (LttvTracefileState *)call_data; |
164 | |
eed2ef37 |
165 | guint nb_block, offset; |
f95bc830 |
166 | |
eed2ef37 |
167 | guint64 tsc; |
f95bc830 |
168 | |
eed2ef37 |
169 | LttTracefile *tf; |
170 | LttEvent *e = ltt_tracefile_get_event(tfs->parent.tf); |
171 | ltt_event_position(e, a_event_position); |
172 | ltt_event_position_get(a_event_position, &tf, &nb_block, &offset, &tsc); |
36d36c9f |
173 | fprintf(stderr,"Event %s %lu.%09lu [%u 0x%x tsc %llu]\n", |
eed2ef37 |
174 | g_quark_to_string(ltt_eventtype_name(ltt_event_eventtype(e))), |
f95bc830 |
175 | tfs->parent.timestamp.tv_sec, tfs->parent.timestamp.tv_nsec, |
eed2ef37 |
176 | nb_block, offset, tsc); |
f95bc830 |
177 | return FALSE; |
178 | } |
179 | |
8fe3c6b6 |
180 | static LttTime count_previous_time = { 0, 0 }; |
f95bc830 |
181 | |
00e74b69 |
182 | gboolean count_event(void *hook_data, void __UNUSED__ *call_data) |
08b1c66e |
183 | { |
8fe3c6b6 |
184 | LttvTracefileState *tfs = (LttvTracefileState *)call_data; |
185 | LttTracefile *tracefile = tfs->parent.tf; |
186 | guint nb_block, offset; |
187 | LttTracefile *tf_pos; |
188 | guint64 tsc; |
189 | LttEvent * event = ltt_tracefile_get_event(tracefile); |
190 | LttTime time; |
08b1c66e |
191 | guint *pcount = (guint *)hook_data; |
192 | |
193 | (*pcount)++; |
8fe3c6b6 |
194 | |
195 | time = ltt_event_time(event); |
196 | ltt_event_position(event, a_event_position); |
197 | ltt_event_position_get(a_event_position, &tf_pos, &nb_block, &offset, &tsc); |
198 | |
199 | if(ltt_time_compare(time, count_previous_time) < 0) { |
36d36c9f |
200 | g_warning("Time decreasing trace %s tracefile %s cpu %u position %u/0x%x", |
8fe3c6b6 |
201 | g_quark_to_string(ltt_trace_name(ltt_tracefile_get_trace(tracefile))), |
202 | g_quark_to_string(ltt_tracefile_name(tracefile)), |
203 | ltt_tracefile_num(tracefile), nb_block, offset); |
204 | g_warning("last time %lu.%lu vs current %lu.%lu", |
205 | count_previous_time.tv_sec, count_previous_time.tv_nsec, |
206 | time.tv_sec, time.tv_nsec); |
207 | } |
208 | count_previous_time = time; |
209 | |
210 | |
211 | |
08b1c66e |
212 | return FALSE; |
213 | } |
214 | |
215 | |
f95bc830 |
216 | gboolean save_state_copy_event(void *hook_data, void *call_data) |
217 | { |
00e74b69 |
218 | SaveState __UNUSED__ *save_state = (SaveState *)hook_data; |
f95bc830 |
219 | |
220 | LttvTracefileState *tfs = (LttvTracefileState *)call_data; |
221 | |
222 | LttvTraceState *ts = (LttvTraceState *)tfs->parent.t_context; |
223 | |
eed2ef37 |
224 | LttEvent *e = ltt_tracefile_get_event(tfs->parent.tf); |
225 | |
f95bc830 |
226 | GString *filename; |
227 | |
228 | FILE *fp; |
229 | |
eed2ef37 |
230 | if(ts->nb_event == 0 && |
231 | ltt_eventtype_name(ltt_event_eventtype(e)) |
232 | == QUARK_BLOCK_START) { |
f95bc830 |
233 | if(a_save_sample != NULL) { |
234 | filename = g_string_new(""); |
235 | g_string_printf(filename, "%s.copy.%lu.%09lu.xml", a_save_sample, |
236 | tfs->parent.timestamp.tv_sec, tfs->parent.timestamp.tv_nsec); |
237 | fp = fopen(filename->str, "w"); |
238 | if(fp == NULL) g_error("Cannot open %s", filename->str); |
239 | g_string_free(filename, TRUE); |
240 | lttv_state_write(ts, tfs->parent.timestamp, fp); |
241 | fclose(fp); |
242 | } //else lttv_state_write(ts, tfs->parent.timestamp, save_state->fp); |
243 | } |
244 | return FALSE; |
245 | } |
246 | |
247 | |
08b1c66e |
248 | gboolean save_state_event(void *hook_data, void *call_data) |
249 | { |
250 | SaveState *save_state = (SaveState *)hook_data; |
251 | |
252 | LttvTracefileState *tfs = (LttvTracefileState *)call_data; |
253 | |
254 | LttvTraceState *ts = (LttvTraceState *)tfs->parent.t_context; |
255 | |
256 | GString *filename; |
257 | |
258 | FILE *fp; |
259 | |
260 | (save_state->count)++; |
261 | if(save_state->count % save_state->interval == 0 && |
262 | save_state->position < save_state->size) { |
263 | if(a_save_sample != NULL) { |
264 | filename = g_string_new(""); |
265 | g_string_printf(filename, "%s.%u.xml.%u", a_save_sample, |
266 | save_state->position, save_state->version); |
267 | fp = fopen(filename->str, "w"); |
268 | if(fp == NULL) g_error("Cannot open %s", filename->str); |
269 | g_string_free(filename, TRUE); |
270 | lttv_state_write(ts, tfs->parent.timestamp, fp); |
271 | fclose(fp); |
f95bc830 |
272 | } //else lttv_state_write(ts, tfs->parent.timestamp, save_state->fp); |
08b1c66e |
273 | |
274 | save_state->write_time[save_state->position] = tfs->parent.timestamp; |
275 | save_state->position++; |
276 | } |
277 | return FALSE; |
278 | } |
279 | |
280 | |
d3d34f49 |
281 | static void sanitize_name(gchar *name) |
282 | { |
283 | while(*name != '\0') { |
284 | if(*name == '/') *name = '_'; |
285 | name++; |
286 | } |
287 | |
288 | } |
289 | |
eed2ef37 |
290 | |
27304273 |
291 | static void compute_tracefile(LttTracefile *tracefile, void *hook_data) |
eed2ef37 |
292 | { |
293 | GString *filename; |
d3d34f49 |
294 | guint nb_equal, nb_block, offset; |
eed2ef37 |
295 | guint64 tsc; |
296 | FILE *fp; |
297 | LttTime time, previous_time; |
27304273 |
298 | LttEvent *event = ltt_tracefile_get_event(tracefile); |
eed2ef37 |
299 | LttFacility *facility; |
300 | LttEventType *event_type; |
301 | int err; |
d3d34f49 |
302 | gchar mod_name[PATH_MAX]; |
eed2ef37 |
303 | |
304 | /* start_count is always initialized in this function _if_ there is always |
305 | * a block_start before a block_end. |
306 | */ |
307 | long long unsigned cycle_count, start_count=0, delta_cycle; |
308 | |
309 | |
310 | filename = g_string_new(""); |
d3d34f49 |
311 | strcpy(mod_name, g_quark_to_string(ltt_tracefile_name(tracefile))); |
312 | |
313 | sanitize_name(mod_name); |
314 | |
d052ffc3 |
315 | g_warning("test %s test", g_quark_to_string(ltt_tracefile_name(tracefile))); |
d3d34f49 |
316 | g_string_printf(filename, "%s.%s.%u.trace", a_dump_tracefiles, |
317 | mod_name, ltt_tracefile_num(tracefile)); |
eed2ef37 |
318 | fp = fopen(filename->str, "w"); |
319 | if(fp == NULL) g_error("Cannot open %s", filename->str); |
320 | g_string_free(filename, TRUE); |
321 | err = ltt_tracefile_seek_time(tracefile, ltt_time_zero); |
322 | if(err) goto close; |
323 | |
324 | previous_time = ltt_time_zero; |
325 | nb_equal = 0; |
326 | |
327 | do { |
328 | LttTracefile *tf_pos; |
329 | facility = ltt_event_facility(event); |
330 | event_type = ltt_event_eventtype(event); |
331 | time = ltt_event_time(event); |
332 | ltt_event_position(event, a_event_position); |
333 | ltt_event_position_get(a_event_position, &tf_pos, &nb_block, &offset, &tsc); |
27304273 |
334 | //fprintf(fp,"%s.%s: %llu %lu.%09lu position %u/%u\n", |
335 | fprintf(fp, "%s.%s: %llu %lu.%09lu position %u/%u, tracefile %s\n", |
336 | g_quark_to_string(ltt_facility_name(facility)), |
337 | g_quark_to_string(ltt_eventtype_name(event_type)), |
eed2ef37 |
338 | tsc, (unsigned long)time.tv_sec, |
339 | (unsigned long)time.tv_nsec, |
27304273 |
340 | nb_block, offset, |
341 | g_quark_to_string(ltt_tracefile_name(tracefile))); |
eed2ef37 |
342 | |
343 | if(ltt_time_compare(time, previous_time) < 0) { |
36d36c9f |
344 | g_warning("Time decreasing trace %s tracefile %s cpu %u position %u/0x%x", |
d3d34f49 |
345 | g_quark_to_string(ltt_trace_name(ltt_tracefile_get_trace(tracefile))), |
d052ffc3 |
346 | g_quark_to_string(ltt_tracefile_name(tracefile)), |
347 | ltt_tracefile_num(tracefile), nb_block, offset); |
348 | g_warning("last time %lu.%lu vs current %lu.%lu", |
349 | previous_time.tv_sec, previous_time.tv_nsec, |
350 | time.tv_sec, time.tv_nsec); |
eed2ef37 |
351 | } |
352 | |
353 | #if 0 //FIXME |
354 | if(ltt_eventtype_name(event_type) == QUARK_BLOCK_START) { |
355 | start_count = cycle_count; |
356 | start_time = time; |
357 | } |
358 | else if(ltt_eventtype_name(event_type) == QUARK_BLOCK_END) { |
359 | delta_cycle = cycle_count - start_count; |
360 | end_nsec_sec = (long long unsigned)time.tv_sec * (long long unsigned)1000000000; |
361 | end_nsec_nsec = time.tv_nsec; |
362 | end_nsec = end_nsec_sec + end_nsec_nsec; |
363 | start_nsec = (long long unsigned)start_time.tv_sec * (long long unsigned)1000000000 + (long long unsigned)start_time.tv_nsec; |
364 | delta_nsec = end_nsec - start_nsec; |
365 | cycle_per_nsec = (double)delta_cycle / (double)delta_nsec; |
366 | nsec_per_cycle = (double)delta_nsec / (double)delta_cycle; |
367 | added_nsec = (double)delta_cycle * nsec_per_cycle; |
368 | interpolated_nsec = start_nsec + added_nsec; |
369 | added_nsec2 = (double)delta_cycle / cycle_per_nsec; |
370 | interpolated_nsec2 = start_nsec + added_nsec2; |
371 | |
372 | fprintf(fp,"Time: start_count %llu, end_count %llu, delta_cycle %llu, start_nsec %llu, end_nsec_sec %llu, end_nsec_nsec %llu, end_nsec %llu, delta_nsec %llu, cycle_per_nsec %.25f, nsec_per_cycle %.25f, added_nsec %llu, added_nsec2 %llu, interpolated_nsec %llu, interpolated_nsec2 %llu\n", start_count, cycle_count, delta_cycle, start_nsec, end_nsec_sec, end_nsec_nsec, end_nsec, delta_nsec, cycle_per_nsec, nsec_per_cycle, added_nsec, added_nsec2, interpolated_nsec, interpolated_nsec2); |
373 | } |
374 | else { |
375 | #endif //0 |
376 | if(ltt_time_compare(time, previous_time) == 0) nb_equal++; |
377 | else if(nb_equal > 0) { |
378 | g_warning("Consecutive %d events with time %lu.%09lu", |
379 | nb_equal + 1, previous_time.tv_sec, previous_time.tv_nsec); |
380 | nb_equal = 0; |
381 | } |
382 | previous_time = time; |
383 | //} |
384 | } while((!ltt_tracefile_read(tracefile))); |
385 | |
386 | close: |
387 | fclose(fp); |
eed2ef37 |
388 | } |
389 | |
00e74b69 |
390 | static gboolean process_traceset(void __UNUSED__ *hook_data, |
391 | void __UNUSED__ *call_data) |
08b1c66e |
392 | { |
eed2ef37 |
393 | GString *filename; |
08b1c66e |
394 | LttvTracesetStats *tscs; |
395 | |
396 | LttvTracesetState *ts; |
397 | |
398 | LttvTracesetContext *tc; |
399 | |
08b1c66e |
400 | FILE *fp; |
401 | |
402 | double t; |
403 | |
eed2ef37 |
404 | //guint count, nb_control, nb_tracefile, nb_block, nb_event; |
405 | //guint i, j, count, nb_control, nb_tracefile, nb_block, nb_event, nb_equal; |
406 | guint i, j, count; |
08b1c66e |
407 | |
408 | LttTrace *trace; |
409 | |
338d4282 |
410 | long long unsigned start_nsec, end_nsec, delta_nsec, added_nsec, added_nsec2; |
411 | |
412 | double cycle_per_nsec, nsec_per_cycle; |
413 | |
414 | long long interpolated_nsec, interpolated_nsec2, end_nsec_sec, end_nsec_nsec; |
415 | |
416 | LttTime start_time; |
417 | |
08b1c66e |
418 | LttTime max_time = { G_MAXULONG, G_MAXULONG }; |
419 | |
f95bc830 |
420 | a_event_position = ltt_event_position_new(); |
421 | |
3865ea09 |
422 | GData **tracefiles_groups; |
eed2ef37 |
423 | |
27304273 |
424 | struct compute_tracefile_group_args args; |
425 | |
426 | args.func = compute_tracefile; |
427 | args.func_args = NULL; |
428 | |
08b1c66e |
429 | if(a_dump_tracefiles != NULL) { |
08b1c66e |
430 | for(i = 0 ; i < lttv_traceset_number(traceset) ; i++) { |
431 | trace = lttv_trace(lttv_traceset_get(traceset, i)); |
eed2ef37 |
432 | tracefiles_groups = ltt_trace_get_tracefiles_groups(trace); |
08b1c66e |
433 | |
3865ea09 |
434 | g_datalist_foreach(tracefiles_groups, |
eed2ef37 |
435 | (GDataForeachFunc)compute_tracefile_group, |
27304273 |
436 | &args); |
eed2ef37 |
437 | |
08b1c66e |
438 | } |
08b1c66e |
439 | } |
440 | |
441 | tscs = g_object_new(LTTV_TRACESET_STATS_TYPE, NULL); |
442 | ts = &tscs->parent; |
443 | tc = &tscs->parent.parent; |
444 | |
445 | lttv_context_init(tc, traceset); |
08b1c66e |
446 | |
447 | /* For each case compute and print the elapsed time. |
448 | The first case is simply to run through all events with a |
449 | simple counter. */ |
450 | |
451 | if(a_test1 || a_test_all) { |
452 | count = 0; |
b8eccacd |
453 | lttv_hooks_add(event_hook, count_event, &count, LTTV_PRIO_DEFAULT); |
eed2ef37 |
454 | t = run_one_test(ts, ltt_time_zero, max_time); |
b8eccacd |
455 | lttv_hooks_remove_data(event_hook, count_event, &count); |
021eeb41 |
456 | g_message( |
08b1c66e |
457 | "Processing trace while counting events (%u events in %g seconds)", |
458 | count, t); |
459 | } |
460 | |
461 | /* Run through all events computing the state. */ |
462 | |
463 | if(a_test2 || a_test_all) { |
464 | lttv_state_add_event_hooks(ts); |
eed2ef37 |
465 | t = run_one_test(ts, ltt_time_zero, max_time); |
08b1c66e |
466 | lttv_state_remove_event_hooks(ts); |
021eeb41 |
467 | g_message("Processing trace while updating state (%g seconds)", t); |
08b1c66e |
468 | } |
469 | |
470 | /* Run through all events computing the state and writing it out |
471 | periodically. */ |
472 | |
473 | SaveState save_state; |
474 | |
475 | save_state.interval = a_sample_interval; |
476 | save_state.size = a_sample_number; |
477 | save_state.fp = stderr; |
478 | save_state.write_time = g_new(LttTime, a_sample_number); |
479 | |
480 | |
481 | if(a_test3 || a_test_all) { |
482 | for(i = 0 ; i < 2 ; i++) { |
483 | save_state.count = 0; |
484 | save_state.position = 0; |
485 | save_state.version = i; |
486 | lttv_state_add_event_hooks(ts); |
b8eccacd |
487 | lttv_hooks_add(event_hook, save_state_event, &save_state, |
488 | LTTV_PRIO_DEFAULT); |
eed2ef37 |
489 | t = run_one_test(ts, ltt_time_zero, max_time); |
08b1c66e |
490 | lttv_state_remove_event_hooks(ts); |
b8eccacd |
491 | lttv_hooks_remove_data(event_hook, save_state_event, &save_state); |
08b1c66e |
492 | g_warning("Processing while updating/writing state (%g seconds)", t); |
493 | } |
494 | } |
495 | |
496 | /* Run through all events computing the stats. */ |
497 | |
498 | if(a_test4 || a_test_all) { |
499 | if(lttv_profile_memory) { |
500 | g_message("Memory summary before computing stats"); |
501 | g_mem_profile(); |
502 | } |
503 | |
504 | lttv_stats_add_event_hooks(tscs); |
eed2ef37 |
505 | t = run_one_test(ts, ltt_time_zero, max_time); |
08b1c66e |
506 | lttv_stats_remove_event_hooks(tscs); |
021eeb41 |
507 | g_message("Processing trace while counting stats (%g seconds)", t); |
08b1c66e |
508 | |
509 | if(lttv_profile_memory) { |
510 | g_message("Memory summary after computing stats"); |
511 | g_mem_profile(); |
512 | } |
f95bc830 |
513 | |
514 | lttv_stats_sum_traceset(tscs); |
515 | |
516 | if(lttv_profile_memory) { |
517 | g_message("Memory summary after summing stats"); |
518 | g_mem_profile(); |
519 | } |
520 | |
521 | lttv_context_fini(tc); |
522 | lttv_context_init(tc, traceset); |
523 | |
524 | if(lttv_profile_memory) { |
525 | g_message("Memory summary after cleaning up the stats"); |
526 | g_mem_profile(); |
527 | } |
08b1c66e |
528 | } |
529 | |
530 | /* Run through all events computing the state and stats. */ |
531 | |
532 | if(a_test5 || a_test_all) { |
533 | if(lttv_profile_memory) { |
534 | g_message("Memory summary before computing state and stats"); |
535 | g_mem_profile(); |
536 | } |
537 | |
538 | lttv_state_add_event_hooks(ts); |
539 | lttv_stats_add_event_hooks(tscs); |
eed2ef37 |
540 | t = run_one_test(ts, ltt_time_zero, max_time); |
08b1c66e |
541 | lttv_state_remove_event_hooks(ts); |
542 | lttv_stats_remove_event_hooks(tscs); |
021eeb41 |
543 | g_message( |
08b1c66e |
544 | "Processing trace while counting state and stats (%g seconds)", t); |
545 | |
546 | if(lttv_profile_memory) { |
547 | g_message("Memory summary after computing and state and stats"); |
548 | g_mem_profile(); |
549 | } |
f95bc830 |
550 | |
551 | lttv_context_fini(tc); |
552 | lttv_context_init(tc, traceset); |
553 | |
554 | if(lttv_profile_memory) { |
555 | g_message("Memory summary after cleaning up the stats"); |
556 | g_mem_profile(); |
557 | } |
08b1c66e |
558 | } |
559 | |
560 | /* Run through all events computing and saving the state. */ |
561 | |
b8eccacd |
562 | if(a_trace_event) lttv_hooks_add(event_hook, trace_event, NULL, |
563 | LTTV_PRIO_DEFAULT); |
f95bc830 |
564 | |
08b1c66e |
565 | if(a_test6 || a_test_all) { |
566 | if(lttv_profile_memory) { |
567 | g_message("Memory summary before computing and saving state"); |
568 | g_mem_profile(); |
569 | } |
570 | |
571 | lttv_state_add_event_hooks(ts); |
572 | lttv_state_save_add_event_hooks(ts); |
f95bc830 |
573 | if(a_save_state_copy) |
b8eccacd |
574 | lttv_hooks_add(event_hook, save_state_copy_event, &save_state, |
575 | LTTV_PRIO_DEFAULT); |
eed2ef37 |
576 | t = run_one_test(ts, ltt_time_zero, max_time); |
08b1c66e |
577 | lttv_state_remove_event_hooks(ts); |
578 | lttv_state_save_remove_event_hooks(ts); |
f95bc830 |
579 | if(a_save_state_copy) |
b8eccacd |
580 | lttv_hooks_remove_data(event_hook,save_state_copy_event, &save_state); |
f95bc830 |
581 | |
021eeb41 |
582 | g_message("Processing trace while updating/saving state (%g seconds)", t); |
08b1c66e |
583 | |
584 | if(lttv_profile_memory) { |
585 | g_message("Memory summary after computing/saving state"); |
586 | g_mem_profile(); |
587 | } |
588 | } |
589 | |
590 | /* Seek a few times to each saved position */ |
591 | |
592 | if((a_test7 && a_test3) || a_test_all) { |
00e74b69 |
593 | g_assert(a_seek_number >= 0); |
594 | for(i = 0 ; i < (guint)a_seek_number ; i++) { |
595 | gint reverse_j; /* just to make sure j is unsigned */ |
596 | for(reverse_j = save_state.position - 1 ; reverse_j >= 0 ; reverse_j--) { |
597 | j = (guint)reverse_j; |
08b1c66e |
598 | lttv_state_add_event_hooks(ts); |
599 | t = run_one_test(ts, save_state.write_time[j], |
600 | save_state.write_time[j]); |
601 | lttv_state_remove_event_hooks(ts); |
021eeb41 |
602 | g_message("Seeking to %lu.%lu (%g seconds)", |
08b1c66e |
603 | save_state.write_time[j].tv_sec, save_state.write_time[j].tv_nsec, |
604 | t); |
605 | |
606 | if(a_save_sample != NULL) { |
607 | filename = g_string_new(""); |
608 | g_string_printf(filename, "%s.%d.xml.bak%d", a_save_sample, j, i); |
609 | fp = fopen(filename->str, "w"); |
610 | if(fp == NULL) g_error("Cannot open %s", filename->str); |
611 | g_string_free(filename, TRUE); |
612 | lttv_state_write((LttvTraceState *)tc->traces[0], |
613 | save_state.write_time[j], fp); |
614 | fclose(fp); |
615 | } |
f95bc830 |
616 | //else lttv_state_write((LttvTraceState *)tc->traces[0], |
617 | // save_state.write_time[j], save_state.fp); |
08b1c66e |
618 | } |
619 | } |
620 | } |
621 | |
728d0c3e |
622 | /* Seek at specified interval, using states computed in 6, making |
623 | * sure that there is no more than the number of events between |
624 | * state save interval to read before getting there. |
625 | */ |
626 | |
627 | if((a_test8 && a_test6) || a_test_all) { |
628 | g_message("Running test 8 : check save interval"); |
629 | LttTime time = tc->time_span.start_time; |
630 | LttTime interval; |
631 | interval.tv_sec = 0; |
632 | interval.tv_nsec = 175674987; |
633 | guint count; |
634 | |
635 | while(ltt_time_compare(time, tc->time_span.end_time) < 0) { |
636 | //g_message("Seeking at time %u.%u", time.tv_sec, time.tv_nsec); |
637 | lttv_process_traceset_seek_time(&ts->parent, ltt_time_zero); |
638 | lttv_state_traceset_seek_time_closest(ts, time); |
639 | /* We add no hook to the traceset, not necessary */ |
640 | count = lttv_process_traceset_middle(&ts->parent, |
641 | time, G_MAXUINT, NULL); |
642 | g_info("Number of events to jump over : %u", count); |
643 | |
644 | if(count > LTTV_STATE_SAVE_INTERVAL) |
645 | g_warning("Oops! Save interval is %u and it took %u events to seek to a time %lu.%lu supposed to be closer from the last saved state.", |
646 | LTTV_STATE_SAVE_INTERVAL, count, time.tv_sec, time.tv_nsec); |
647 | time = ltt_time_add(time, interval); |
648 | } |
649 | |
650 | } |
651 | |
b8eccacd |
652 | if(a_trace_event) lttv_hooks_remove_data(event_hook, trace_event, NULL); |
f95bc830 |
653 | |
08b1c66e |
654 | g_free(save_state.write_time); |
f95bc830 |
655 | g_free(a_event_position); |
08b1c66e |
656 | lttv_context_fini(tc); |
657 | g_object_unref(tscs); |
658 | |
f95bc830 |
659 | if(lttv_profile_memory) { |
660 | g_message("Memory summary at the end of batchtest"); |
661 | g_mem_profile(); |
662 | } |
663 | |
08b1c66e |
664 | g_info("BatchTest end process traceset"); |
00e74b69 |
665 | return 0; |
08b1c66e |
666 | } |
667 | |
668 | |
669 | static void init() |
670 | { |
671 | LttvAttributeValue value; |
672 | |
673 | LttvIAttribute *attributes = LTTV_IATTRIBUTE(lttv_global_attributes()); |
674 | |
675 | g_info("Init batchtest.c"); |
676 | |
eed2ef37 |
677 | /* Init GQuarks */ |
678 | QUARK_BLOCK_START = g_quark_from_string("block_start"); |
679 | QUARK_BLOCK_END = g_quark_from_string("block_end"); |
680 | |
681 | |
08b1c66e |
682 | lttv_option_add("trace", 't', |
683 | "add a trace to the trace set to analyse", |
684 | "pathname of the directory containing the trace", |
685 | LTTV_OPT_STRING, &a_trace, lttv_trace_option, NULL); |
686 | |
f95bc830 |
687 | a_trace_event = FALSE; |
688 | |
08b1c66e |
689 | a_dump_tracefiles = NULL; |
690 | lttv_option_add("dump-tracefiles", 'D', |
691 | "Write event by event the content of tracefiles", |
692 | "basename for the files where to dump events", |
693 | LTTV_OPT_STRING, &a_dump_tracefiles, NULL, NULL); |
694 | |
695 | a_save_sample = NULL; |
696 | lttv_option_add("save-sample", 's', |
697 | "Save state samples to multiple files", |
698 | "basename for the files containing the state samples", |
699 | LTTV_OPT_STRING, &a_save_sample, NULL, NULL); |
700 | |
f95bc830 |
701 | a_save_state_copy = FALSE; |
702 | lttv_option_add("save-state-copy", 'S', "Write the state saved for seeking", |
703 | "", LTTV_OPT_NONE, &a_save_state_copy, NULL, NULL); |
704 | |
08b1c66e |
705 | a_save_interval = 100000; |
706 | lttv_option_add("save-interval", 'i', |
707 | "Interval between saving state", |
708 | "number of events before a block start triggers saving state", |
709 | LTTV_OPT_INT, &a_save_interval, NULL, NULL); |
710 | |
711 | a_sample_interval = 100000; |
712 | lttv_option_add("sample-interval", 'S', |
713 | "Interval between sampling state", |
714 | "number of events before sampling and writing state", |
715 | LTTV_OPT_INT, &a_sample_interval, NULL, NULL); |
716 | |
717 | a_sample_number = 20; |
718 | lttv_option_add("sample-number", 'N', |
719 | "Number of state samples", |
720 | "maximum number", |
721 | LTTV_OPT_INT, &a_sample_number, NULL, NULL); |
722 | |
911b7a3c |
723 | a_seek_number = 200; |
724 | lttv_option_add("seek-number", 'K', |
725 | "Number of seek", |
726 | "number", |
727 | LTTV_OPT_INT, &a_seek_number, NULL, NULL); |
728 | |
08b1c66e |
729 | a_test1 = FALSE; |
730 | lttv_option_add("test1", '1', "Test just counting events", "", |
731 | LTTV_OPT_NONE, &a_test1, NULL, NULL); |
732 | |
733 | a_test2 = FALSE; |
734 | lttv_option_add("test2", '2', "Test computing the state", "", |
735 | LTTV_OPT_NONE, &a_test2, NULL, NULL); |
736 | |
737 | a_test3 = FALSE; |
738 | lttv_option_add("test3", '3', "Test computing the state, writing out a few", |
739 | "", LTTV_OPT_NONE, &a_test3, NULL, NULL); |
740 | |
741 | a_test4 = FALSE; |
742 | lttv_option_add("test4", '4', "Test computing the stats", "", |
743 | LTTV_OPT_NONE, &a_test4, NULL, NULL); |
744 | |
745 | a_test5 = FALSE; |
746 | lttv_option_add("test5", '5', "Test computing the state and stats", "", |
747 | LTTV_OPT_NONE, &a_test5, NULL, NULL); |
748 | |
749 | a_test6 = FALSE; |
750 | lttv_option_add("test6", '6', "Test computing and saving the state", "", |
751 | LTTV_OPT_NONE, &a_test6, NULL, NULL); |
752 | |
753 | a_test7 = FALSE; |
754 | lttv_option_add("test7", '7', "Test seeking to positions written out in 3", |
755 | "", LTTV_OPT_NONE, &a_test7, NULL, NULL); |
756 | |
728d0c3e |
757 | a_test8 = FALSE; |
758 | lttv_option_add("test8", '8', "Test seeking to positions using saved states computed at 6 : check if number of events fits", |
759 | "", LTTV_OPT_NONE, &a_test8, NULL, NULL); |
760 | |
08b1c66e |
761 | a_test_all = FALSE; |
762 | lttv_option_add("testall", 'a', "Run all tests ", "", |
763 | LTTV_OPT_NONE, &a_test_all, NULL, NULL); |
764 | |
765 | traceset = lttv_traceset_new(); |
766 | |
767 | before_traceset = lttv_hooks_new(); |
768 | after_traceset = lttv_hooks_new(); |
769 | before_trace = lttv_hooks_new(); |
770 | after_trace = lttv_hooks_new(); |
771 | before_tracefile = lttv_hooks_new(); |
772 | after_tracefile = lttv_hooks_new(); |
b8eccacd |
773 | //before_event = lttv_hooks_new(); |
774 | //after_event = lttv_hooks_new(); |
775 | event_hook = lttv_hooks_new(); |
776 | |
08b1c66e |
777 | |
778 | g_assert(lttv_iattribute_find_by_path(attributes, "hooks/traceset/before", |
779 | LTTV_POINTER, &value)); |
780 | *(value.v_pointer) = before_traceset; |
781 | g_assert(lttv_iattribute_find_by_path(attributes, "hooks/traceset/after", |
782 | LTTV_POINTER, &value)); |
783 | *(value.v_pointer) = after_traceset; |
784 | g_assert(lttv_iattribute_find_by_path(attributes, "hooks/trace/before", |
785 | LTTV_POINTER, &value)); |
786 | *(value.v_pointer) = before_trace; |
787 | g_assert(lttv_iattribute_find_by_path(attributes, "hooks/trace/after", |
788 | LTTV_POINTER, &value)); |
789 | *(value.v_pointer) = after_trace; |
790 | g_assert(lttv_iattribute_find_by_path(attributes, "hooks/tracefile/before", |
791 | LTTV_POINTER, &value)); |
792 | *(value.v_pointer) = before_tracefile; |
793 | g_assert(lttv_iattribute_find_by_path(attributes, "hooks/tracefile/after", |
794 | LTTV_POINTER, &value)); |
795 | *(value.v_pointer) = after_tracefile; |
b8eccacd |
796 | //g_assert(lttv_iattribute_find_by_path(attributes, "hooks/event/before", |
797 | // LTTV_POINTER, &value)); |
798 | //*(value.v_pointer) = before_event; |
799 | //g_assert(lttv_iattribute_find_by_path(attributes, "hooks/event/after", |
800 | // LTTV_POINTER, &value)); |
801 | //*(value.v_pointer) = after_event; |
63a95338 |
802 | g_assert(lttv_iattribute_find_by_path(attributes, "hooks/event", |
08b1c66e |
803 | LTTV_POINTER, &value)); |
b8eccacd |
804 | *(value.v_pointer) = event_hook; |
08b1c66e |
805 | g_assert(lttv_iattribute_find_by_path(attributes, "hooks/main/before", |
806 | LTTV_POINTER, &value)); |
807 | g_assert((main_hooks = *(value.v_pointer)) != NULL); |
b8eccacd |
808 | lttv_hooks_add(main_hooks, process_traceset, NULL, LTTV_PRIO_DEFAULT); |
08b1c66e |
809 | } |
810 | |
811 | |
812 | static void destroy() |
813 | { |
814 | guint i, nb; |
815 | |
816 | LttvTrace *trace; |
817 | |
818 | g_info("Destroy batchAnalysis.c"); |
819 | |
820 | lttv_option_remove("trace"); |
821 | lttv_option_remove("dump-tracefiles"); |
822 | lttv_option_remove("save-sample"); |
f95bc830 |
823 | lttv_option_remove("save-state-copy"); |
08b1c66e |
824 | lttv_option_remove("sample-interval"); |
825 | lttv_option_remove("sample-number"); |
911b7a3c |
826 | lttv_option_remove("seek-number"); |
08b1c66e |
827 | lttv_option_remove("save-interval"); |
828 | lttv_option_remove("test1"); |
829 | lttv_option_remove("test2"); |
830 | lttv_option_remove("test3"); |
831 | lttv_option_remove("test4"); |
832 | lttv_option_remove("test5"); |
833 | lttv_option_remove("test6"); |
834 | lttv_option_remove("test7"); |
728d0c3e |
835 | lttv_option_remove("test8"); |
08b1c66e |
836 | lttv_option_remove("testall"); |
837 | |
838 | lttv_hooks_destroy(before_traceset); |
839 | lttv_hooks_destroy(after_traceset); |
840 | lttv_hooks_destroy(before_trace); |
841 | lttv_hooks_destroy(after_trace); |
842 | lttv_hooks_destroy(before_tracefile); |
843 | lttv_hooks_destroy(after_tracefile); |
b8eccacd |
844 | //lttv_hooks_destroy(before_event); |
845 | //lttv_hooks_destroy(after_event); |
846 | lttv_hooks_destroy(event_hook); |
08b1c66e |
847 | lttv_hooks_remove_data(main_hooks, process_traceset, NULL); |
848 | |
849 | nb = lttv_traceset_number(traceset); |
850 | for(i = 0 ; i < nb ; i++) { |
851 | trace = lttv_traceset_get(traceset, i); |
443a558d |
852 | lttv_traceset_remove(traceset,i); |
08b1c66e |
853 | ltt_trace_close(lttv_trace(trace)); |
854 | lttv_trace_destroy(trace); |
855 | } |
856 | |
857 | lttv_traceset_destroy(traceset); |
858 | } |
859 | |
860 | |
861 | LTTV_MODULE("batchtest", "Batch processing of a trace for tests", \ |
862 | "Run through a trace calling all the registered hooks for tests", \ |
863 | init, destroy, "state", "stats", "option" ) |