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