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 while testing the speed and
21 functionality of the state and stats computations. */
24 #include <lttv/lttv.h>
25 #include <lttv/attribute.h>
26 #include <lttv/hook.h>
27 #include <lttv/option.h>
28 #include <lttv/module.h>
29 #include <lttv/processTrace.h>
30 #include <lttv/state.h>
31 #include <lttv/stats.h>
32 #include <ltt/trace.h>
33 #include <ltt/event.h>
35 static LttvTraceset
*traceset
;
50 static char *a_dump_tracefiles
;
52 static char *a_save_sample
;
69 typedef struct _save_state
{
80 static void lttv_trace_option(void *hook_data
)
84 trace
= ltt_trace_open(a_trace
);
85 if(trace
== NULL
) g_critical("cannot open trace %s", a_trace
);
86 lttv_traceset_add(traceset
, lttv_trace_new(trace
));
89 static double get_time()
93 g_get_current_time(>
);
94 return gt
.tv_sec
+ (double)gt
.tv_usec
/ (double)1000000.0;
97 static double run_one_test(LttvTracesetState
*ts
, LttTime start
, LttTime end
)
101 lttv_traceset_context_add_hooks(&ts
->parent
,
102 before_traceset
, after_traceset
, NULL
, before_trace
, after_trace
,
103 NULL
, before_tracefile
, after_tracefile
, NULL
, before_event
, after_event
);
106 lttv_state_traceset_seek_time_closest(ts
, start
);
107 lttv_process_traceset(&ts
->parent
, end
, G_MAXULONG
);
110 lttv_traceset_context_remove_hooks(&ts
->parent
,
111 before_traceset
, after_traceset
, NULL
, before_trace
, after_trace
,
112 NULL
, before_tracefile
, after_tracefile
, NULL
, before_event
, after_event
);
118 gboolean
count_event(void *hook_data
, void *call_data
)
120 guint
*pcount
= (guint
*)hook_data
;
127 gboolean
save_state_event(void *hook_data
, void *call_data
)
129 SaveState
*save_state
= (SaveState
*)hook_data
;
131 LttvTracefileState
*tfs
= (LttvTracefileState
*)call_data
;
133 LttvTraceState
*ts
= (LttvTraceState
*)tfs
->parent
.t_context
;
139 (save_state
->count
)++;
140 if(save_state
->count
% save_state
->interval
== 0 &&
141 save_state
->position
< save_state
->size
) {
142 if(a_save_sample
!= NULL
) {
143 filename
= g_string_new("");
144 g_string_printf(filename
, "%s.%u.xml.%u", a_save_sample
,
145 save_state
->position
, save_state
->version
);
146 fp
= fopen(filename
->str
, "w");
147 if(fp
== NULL
) g_error("Cannot open %s", filename
->str
);
148 g_string_free(filename
, TRUE
);
149 lttv_state_write(ts
, tfs
->parent
.timestamp
, fp
);
151 } else lttv_state_write(ts
, tfs
->parent
.timestamp
, save_state
->fp
);
153 save_state
->write_time
[save_state
->position
] = tfs
->parent
.timestamp
;
154 save_state
->position
++;
160 static gboolean
process_traceset(void *hook_data
, void *call_data
)
162 LttvTracesetStats
*tscs
;
164 LttvTracesetState
*ts
;
166 LttvTracesetContext
*tc
;
174 guint i
, j
, count
, nb_control
, nb_tracefile
, nb_block
, nb_event
, nb_equal
;
178 LttTracefile
*tracefile
, *tf
;
182 LttFacility
*facility
;
186 LttEventType
*event_type
;
188 LttTime time
, previous_time
;
190 LttEventPosition
*event_position
;
192 LttTime zero_time
= ltt_time_zero
;
194 LttTime max_time
= { G_MAXULONG
, G_MAXULONG
};
196 if(a_dump_tracefiles
!= NULL
) {
197 event_position
= ltt_event_position_new();
198 for(i
= 0 ; i
< lttv_traceset_number(traceset
) ; i
++) {
199 trace
= lttv_trace(lttv_traceset_get(traceset
, i
));
200 nb_control
= ltt_trace_control_tracefile_number(trace
);
201 nb_tracefile
= nb_control
+ ltt_trace_per_cpu_tracefile_number(trace
);
202 for(j
= 0 ; j
< nb_tracefile
; j
++) {
204 tracefile
= ltt_trace_control_tracefile_get(trace
,j
);
207 tracefile
= ltt_trace_per_cpu_tracefile_get(trace
,j
- nb_control
);
210 filename
= g_string_new("");
211 g_string_printf(filename
, "%s.%u.%u.trace", a_dump_tracefiles
, i
, j
);
212 fp
= fopen(filename
->str
, "w");
213 if(fp
== NULL
) g_error("Cannot open %s", filename
->str
);
214 g_string_free(filename
, TRUE
);
215 ltt_tracefile_seek_time(tracefile
, zero_time
);
216 previous_time
= zero_time
;
218 while((event
= ltt_tracefile_read(tracefile
)) != NULL
) {
219 facility
= ltt_event_facility(event
);
220 event_type
= ltt_event_eventtype(event
);
221 time
= ltt_event_time(event
);
222 ltt_event_position(event
, event_position
);
223 ltt_event_position_get(event_position
, &nb_block
, &nb_event
, &tf
);
224 fprintf(fp
,"%s.%s: %lu.%09lu position %u/%u\n",
225 ltt_facility_name(facility
), ltt_eventtype_name(event_type
),
226 (long)time
.tv_sec
, time
.tv_nsec
, nb_block
, nb_event
);
228 if(ltt_time_compare(time
, previous_time
) == 0) nb_equal
++;
229 else if(nb_equal
> 0) {
230 g_warning("Consecutive %d events with time %lu.%lu",
231 nb_equal
+ 1, previous_time
.tv_sec
, previous_time
.tv_nsec
);
235 if(ltt_time_compare(time
, previous_time
) < 0) {
236 g_warning("Time decreasing trace %d tracefile %d position %u/%u",
237 i
, j
, nb_block
, nb_event
);
239 previous_time
= time
;
244 g_free(event_position
);
247 tscs
= g_object_new(LTTV_TRACESET_STATS_TYPE
, NULL
);
249 tc
= &tscs
->parent
.parent
;
251 lttv_context_init(tc
, traceset
);
252 for(i
= 0 ; i
< lttv_traceset_number(traceset
) ; i
++) {
253 ((LttvTraceState
*)(tc
->traces
[i
]))->save_interval
= a_save_interval
;
256 /* For each case compute and print the elapsed time.
257 The first case is simply to run through all events with a
260 if(a_test1
|| a_test_all
) {
262 lttv_hooks_add(after_event
, count_event
, &count
);
263 t
= run_one_test(ts
, zero_time
, max_time
);
264 lttv_hooks_remove_data(after_event
, count_event
, &count
);
266 "Processing trace while counting events (%u events in %g seconds)",
270 /* Run through all events computing the state. */
272 if(a_test2
|| a_test_all
) {
273 lttv_state_add_event_hooks(ts
);
274 t
= run_one_test(ts
, zero_time
, max_time
);
275 lttv_state_remove_event_hooks(ts
);
276 g_warning("Processing trace while updating state (%g seconds)", t
);
279 /* Run through all events computing the state and writing it out
282 SaveState save_state
;
284 save_state
.interval
= a_sample_interval
;
285 save_state
.size
= a_sample_number
;
286 save_state
.fp
= stderr
;
287 save_state
.write_time
= g_new(LttTime
, a_sample_number
);
290 if(a_test3
|| a_test_all
) {
291 for(i
= 0 ; i
< 2 ; i
++) {
292 save_state
.count
= 0;
293 save_state
.position
= 0;
294 save_state
.version
= i
;
295 lttv_state_add_event_hooks(ts
);
296 lttv_hooks_add(after_event
, save_state_event
, &save_state
);
297 t
= run_one_test(ts
, zero_time
, max_time
);
298 lttv_state_remove_event_hooks(ts
);
299 lttv_hooks_remove_data(after_event
, save_state_event
, &save_state
);
300 g_warning("Processing while updating/writing state (%g seconds)", t
);
304 /* Run through all events computing the stats. */
306 if(a_test4
|| a_test_all
) {
307 if(lttv_profile_memory
) {
308 g_message("Memory summary before computing stats");
312 lttv_stats_add_event_hooks(tscs
);
313 t
= run_one_test(ts
, zero_time
, max_time
);
314 lttv_stats_remove_event_hooks(tscs
);
315 g_warning("Processing trace while counting stats (%g seconds)", t
);
317 if(lttv_profile_memory
) {
318 g_message("Memory summary after computing stats");
323 /* Run through all events computing the state and stats. */
325 if(a_test5
|| a_test_all
) {
326 if(lttv_profile_memory
) {
327 g_message("Memory summary before computing state and stats");
331 lttv_state_add_event_hooks(ts
);
332 lttv_stats_add_event_hooks(tscs
);
333 t
= run_one_test(ts
, zero_time
, max_time
);
334 lttv_state_remove_event_hooks(ts
);
335 lttv_stats_remove_event_hooks(tscs
);
337 "Processing trace while counting state and stats (%g seconds)", t
);
339 if(lttv_profile_memory
) {
340 g_message("Memory summary after computing and state and stats");
345 /* Run through all events computing and saving the state. */
347 if(a_test6
|| a_test_all
) {
348 if(lttv_profile_memory
) {
349 g_message("Memory summary before computing and saving state");
353 lttv_state_add_event_hooks(ts
);
354 lttv_state_save_add_event_hooks(ts
);
355 t
= run_one_test(ts
, zero_time
, max_time
);
356 lttv_state_remove_event_hooks(ts
);
357 lttv_state_save_remove_event_hooks(ts
);
358 g_warning("Processing trace while updating/saving state (%g seconds)", t
);
360 if(lttv_profile_memory
) {
361 g_message("Memory summary after computing/saving state");
366 /* Seek a few times to each saved position */
368 if((a_test7
&& a_test3
) || a_test_all
) {
371 for(i
= 0 ; i
< 2 ; i
++) {
372 for(j
= save_state
.position
- 1 ; j
>= 0 ; j
--) {
373 lttv_state_add_event_hooks(ts
);
374 t
= run_one_test(ts
, save_state
.write_time
[j
],
375 save_state
.write_time
[j
]);
376 lttv_state_remove_event_hooks(ts
);
377 g_warning("Seeking to %lu.%lu (%g seconds)",
378 save_state
.write_time
[j
].tv_sec
, save_state
.write_time
[j
].tv_nsec
,
381 if(a_save_sample
!= NULL
) {
382 filename
= g_string_new("");
383 g_string_printf(filename
, "%s.%d.xml.bak%d", a_save_sample
, j
, i
);
384 fp
= fopen(filename
->str
, "w");
385 if(fp
== NULL
) g_error("Cannot open %s", filename
->str
);
386 g_string_free(filename
, TRUE
);
387 lttv_state_write((LttvTraceState
*)tc
->traces
[0],
388 save_state
.write_time
[j
], fp
);
391 else lttv_state_write((LttvTraceState
*)tc
->traces
[0],
392 save_state
.write_time
[j
], save_state
.fp
);
397 g_free(save_state
.write_time
);
398 lttv_context_fini(tc
);
399 g_object_unref(tscs
);
401 g_info("BatchTest end process traceset");
407 LttvAttributeValue value
;
409 LttvIAttribute
*attributes
= LTTV_IATTRIBUTE(lttv_global_attributes());
411 g_info("Init batchtest.c");
413 lttv_option_add("trace", 't',
414 "add a trace to the trace set to analyse",
415 "pathname of the directory containing the trace",
416 LTTV_OPT_STRING
, &a_trace
, lttv_trace_option
, NULL
);
418 a_dump_tracefiles
= NULL
;
419 lttv_option_add("dump-tracefiles", 'D',
420 "Write event by event the content of tracefiles",
421 "basename for the files where to dump events",
422 LTTV_OPT_STRING
, &a_dump_tracefiles
, NULL
, NULL
);
424 a_save_sample
= NULL
;
425 lttv_option_add("save-sample", 's',
426 "Save state samples to multiple files",
427 "basename for the files containing the state samples",
428 LTTV_OPT_STRING
, &a_save_sample
, NULL
, NULL
);
430 a_save_interval
= 100000;
431 lttv_option_add("save-interval", 'i',
432 "Interval between saving state",
433 "number of events before a block start triggers saving state",
434 LTTV_OPT_INT
, &a_save_interval
, NULL
, NULL
);
436 a_sample_interval
= 100000;
437 lttv_option_add("sample-interval", 'S',
438 "Interval between sampling state",
439 "number of events before sampling and writing state",
440 LTTV_OPT_INT
, &a_sample_interval
, NULL
, NULL
);
442 a_sample_number
= 20;
443 lttv_option_add("sample-number", 'N',
444 "Number of state samples",
446 LTTV_OPT_INT
, &a_sample_number
, NULL
, NULL
);
449 lttv_option_add("test1", '1', "Test just counting events", "",
450 LTTV_OPT_NONE
, &a_test1
, NULL
, NULL
);
453 lttv_option_add("test2", '2', "Test computing the state", "",
454 LTTV_OPT_NONE
, &a_test2
, NULL
, NULL
);
457 lttv_option_add("test3", '3', "Test computing the state, writing out a few",
458 "", LTTV_OPT_NONE
, &a_test3
, NULL
, NULL
);
461 lttv_option_add("test4", '4', "Test computing the stats", "",
462 LTTV_OPT_NONE
, &a_test4
, NULL
, NULL
);
465 lttv_option_add("test5", '5', "Test computing the state and stats", "",
466 LTTV_OPT_NONE
, &a_test5
, NULL
, NULL
);
469 lttv_option_add("test6", '6', "Test computing and saving the state", "",
470 LTTV_OPT_NONE
, &a_test6
, NULL
, NULL
);
473 lttv_option_add("test7", '7', "Test seeking to positions written out in 3",
474 "", LTTV_OPT_NONE
, &a_test7
, NULL
, NULL
);
477 lttv_option_add("testall", 'a', "Run all tests ", "",
478 LTTV_OPT_NONE
, &a_test_all
, NULL
, NULL
);
480 traceset
= lttv_traceset_new();
482 before_traceset
= lttv_hooks_new();
483 after_traceset
= lttv_hooks_new();
484 before_trace
= lttv_hooks_new();
485 after_trace
= lttv_hooks_new();
486 before_tracefile
= lttv_hooks_new();
487 after_tracefile
= lttv_hooks_new();
488 before_event
= lttv_hooks_new();
489 after_event
= lttv_hooks_new();
491 g_assert(lttv_iattribute_find_by_path(attributes
, "hooks/traceset/before",
492 LTTV_POINTER
, &value
));
493 *(value
.v_pointer
) = before_traceset
;
494 g_assert(lttv_iattribute_find_by_path(attributes
, "hooks/traceset/after",
495 LTTV_POINTER
, &value
));
496 *(value
.v_pointer
) = after_traceset
;
497 g_assert(lttv_iattribute_find_by_path(attributes
, "hooks/trace/before",
498 LTTV_POINTER
, &value
));
499 *(value
.v_pointer
) = before_trace
;
500 g_assert(lttv_iattribute_find_by_path(attributes
, "hooks/trace/after",
501 LTTV_POINTER
, &value
));
502 *(value
.v_pointer
) = after_trace
;
503 g_assert(lttv_iattribute_find_by_path(attributes
, "hooks/tracefile/before",
504 LTTV_POINTER
, &value
));
505 *(value
.v_pointer
) = before_tracefile
;
506 g_assert(lttv_iattribute_find_by_path(attributes
, "hooks/tracefile/after",
507 LTTV_POINTER
, &value
));
508 *(value
.v_pointer
) = after_tracefile
;
509 g_assert(lttv_iattribute_find_by_path(attributes
, "hooks/event/before",
510 LTTV_POINTER
, &value
));
511 *(value
.v_pointer
) = before_event
;
512 g_assert(lttv_iattribute_find_by_path(attributes
, "hooks/event/after",
513 LTTV_POINTER
, &value
));
514 *(value
.v_pointer
) = after_event
;
516 g_assert(lttv_iattribute_find_by_path(attributes
, "hooks/main/before",
517 LTTV_POINTER
, &value
));
518 g_assert((main_hooks
= *(value
.v_pointer
)) != NULL
);
519 lttv_hooks_add(main_hooks
, process_traceset
, NULL
);
523 static void destroy()
529 g_info("Destroy batchAnalysis.c");
531 lttv_option_remove("trace");
532 lttv_option_remove("dump-tracefiles");
533 lttv_option_remove("save-sample");
534 lttv_option_remove("sample-interval");
535 lttv_option_remove("sample-number");
536 lttv_option_remove("save-interval");
537 lttv_option_remove("test1");
538 lttv_option_remove("test2");
539 lttv_option_remove("test3");
540 lttv_option_remove("test4");
541 lttv_option_remove("test5");
542 lttv_option_remove("test6");
543 lttv_option_remove("test7");
544 lttv_option_remove("testall");
546 lttv_hooks_destroy(before_traceset
);
547 lttv_hooks_destroy(after_traceset
);
548 lttv_hooks_destroy(before_trace
);
549 lttv_hooks_destroy(after_trace
);
550 lttv_hooks_destroy(before_tracefile
);
551 lttv_hooks_destroy(after_tracefile
);
552 lttv_hooks_destroy(before_event
);
553 lttv_hooks_destroy(after_event
);
554 lttv_hooks_remove_data(main_hooks
, process_traceset
, NULL
);
556 nb
= lttv_traceset_number(traceset
);
557 for(i
= 0 ; i
< nb
; i
++) {
558 trace
= lttv_traceset_get(traceset
, i
);
559 ltt_trace_close(lttv_trace(trace
));
560 lttv_trace_destroy(trace
);
563 lttv_traceset_destroy(traceset
);
567 LTTV_MODULE("batchtest", "Batch processing of a trace for tests", \
568 "Run through a trace calling all the registered hooks for tests", \
569 init
, destroy
, "state", "stats", "option" )
This page took 0.0410430000000001 seconds and 4 git commands to generate.