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>
36 static LttvTraceset
*traceset
;
51 static char *a_dump_tracefiles
;
53 static char *a_save_sample
;
70 typedef struct _save_state
{
81 static void lttv_trace_option(void *hook_data
)
85 trace
= ltt_trace_open(a_trace
);
86 if(trace
== NULL
) g_critical("cannot open trace %s", a_trace
);
87 lttv_traceset_add(traceset
, lttv_trace_new(trace
));
90 static double get_time()
94 g_get_current_time(>
);
95 return gt
.tv_sec
+ (double)gt
.tv_usec
/ (double)1000000.0;
98 static double run_one_test(LttvTracesetState
*ts
, LttTime start
, LttTime end
)
102 lttv_traceset_context_add_hooks(&ts
->parent
,
103 before_traceset
, after_traceset
, NULL
, before_trace
, after_trace
,
104 NULL
, before_tracefile
, after_tracefile
, NULL
, before_event
, after_event
);
107 lttv_state_traceset_seek_time_closest(ts
, start
);
108 lttv_process_traceset(&ts
->parent
, end
, G_MAXULONG
);
111 lttv_traceset_context_remove_hooks(&ts
->parent
,
112 before_traceset
, after_traceset
, NULL
, before_trace
, after_trace
,
113 NULL
, before_tracefile
, after_tracefile
, NULL
, before_event
, after_event
);
119 gboolean
count_event(void *hook_data
, void *call_data
)
121 guint
*pcount
= (guint
*)hook_data
;
128 gboolean
save_state_event(void *hook_data
, void *call_data
)
130 SaveState
*save_state
= (SaveState
*)hook_data
;
132 LttvTracefileState
*tfs
= (LttvTracefileState
*)call_data
;
134 LttvTraceState
*ts
= (LttvTraceState
*)tfs
->parent
.t_context
;
140 (save_state
->count
)++;
141 if(save_state
->count
% save_state
->interval
== 0 &&
142 save_state
->position
< save_state
->size
) {
143 if(a_save_sample
!= NULL
) {
144 filename
= g_string_new("");
145 g_string_printf(filename
, "%s.%u.xml.%u", a_save_sample
,
146 save_state
->position
, save_state
->version
);
147 fp
= fopen(filename
->str
, "w");
148 if(fp
== NULL
) g_error("Cannot open %s", filename
->str
);
149 g_string_free(filename
, TRUE
);
150 lttv_state_write(ts
, tfs
->parent
.timestamp
, fp
);
152 } else lttv_state_write(ts
, tfs
->parent
.timestamp
, save_state
->fp
);
154 save_state
->write_time
[save_state
->position
] = tfs
->parent
.timestamp
;
155 save_state
->position
++;
161 static gboolean
process_traceset(void *hook_data
, void *call_data
)
163 LttvTracesetStats
*tscs
;
165 LttvTracesetState
*ts
;
167 LttvTracesetContext
*tc
;
175 guint i
, j
, count
, nb_control
, nb_tracefile
, nb_block
, nb_event
, nb_equal
;
179 LttTracefile
*tracefile
, *tf
;
183 LttFacility
*facility
;
187 LttEventType
*event_type
;
189 LttTime time
, previous_time
;
191 long long unsigned cycle_count
, start_count
, delta_cycle
;
193 long long unsigned start_nsec
, end_nsec
, delta_nsec
, added_nsec
, added_nsec2
;
195 double cycle_per_nsec
, nsec_per_cycle
;
197 long long interpolated_nsec
, interpolated_nsec2
, end_nsec_sec
, end_nsec_nsec
;
201 LttEventPosition
*event_position
;
203 LttTime zero_time
= ltt_time_zero
;
205 LttTime max_time
= { G_MAXULONG
, G_MAXULONG
};
207 if(a_dump_tracefiles
!= NULL
) {
208 event_position
= ltt_event_position_new();
209 for(i
= 0 ; i
< lttv_traceset_number(traceset
) ; i
++) {
210 trace
= lttv_trace(lttv_traceset_get(traceset
, i
));
211 nb_control
= ltt_trace_control_tracefile_number(trace
);
212 nb_tracefile
= nb_control
+ ltt_trace_per_cpu_tracefile_number(trace
);
213 for(j
= 0 ; j
< nb_tracefile
; j
++) {
215 tracefile
= ltt_trace_control_tracefile_get(trace
,j
);
218 tracefile
= ltt_trace_per_cpu_tracefile_get(trace
,j
- nb_control
);
221 filename
= g_string_new("");
222 g_string_printf(filename
, "%s.%u.%u.trace", a_dump_tracefiles
, i
, j
);
223 fp
= fopen(filename
->str
, "w");
224 if(fp
== NULL
) g_error("Cannot open %s", filename
->str
);
225 g_string_free(filename
, TRUE
);
226 ltt_tracefile_seek_time(tracefile
, zero_time
);
227 previous_time
= zero_time
;
229 while((event
= ltt_tracefile_read(tracefile
)) != NULL
) {
230 facility
= ltt_event_facility(event
);
231 event_type
= ltt_event_eventtype(event
);
232 time
= ltt_event_time(event
);
233 cycle_count
= ltt_event_cycle_count(event
);
234 ltt_event_position(event
, event_position
);
235 ltt_event_position_get(event_position
, &nb_block
, &nb_event
, &tf
);
236 fprintf(fp
,"%s.%s: %llu %lu.%09lu position %u/%u\n",
237 ltt_facility_name(facility
), ltt_eventtype_name(event_type
),
238 cycle_count
, (unsigned long)time
.tv_sec
,
239 (unsigned long)time
.tv_nsec
,
242 if(ltt_time_compare(time
, previous_time
) < 0) {
243 g_warning("Time decreasing trace %d tracefile %d position %u/%u",
244 i
, j
, nb_block
, nb_event
);
247 if(strcmp(ltt_eventtype_name(event_type
),"block_start") == 0) {
248 start_count
= cycle_count
;
251 else if(strcmp(ltt_eventtype_name(event_type
),"block_end") == 0) {
252 delta_cycle
= cycle_count
- start_count
;
253 end_nsec_sec
= (long long unsigned)time
.tv_sec
* (long long unsigned)1000000000;
254 end_nsec_nsec
= time
.tv_nsec
;
255 end_nsec
= end_nsec_sec
+ end_nsec_nsec
;
256 start_nsec
= (long long unsigned)start_time
.tv_sec
* (long long unsigned)1000000000 + (long long unsigned)start_time
.tv_nsec
;
257 delta_nsec
= end_nsec
- start_nsec
;
258 cycle_per_nsec
= (double)delta_cycle
/ (double)delta_nsec
;
259 nsec_per_cycle
= (double)delta_nsec
/ (double)delta_cycle
;
260 added_nsec
= (double)delta_cycle
* nsec_per_cycle
;
261 interpolated_nsec
= start_nsec
+ added_nsec
;
262 added_nsec2
= (double)delta_cycle
/ cycle_per_nsec
;
263 interpolated_nsec2
= start_nsec
+ added_nsec2
;
265 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
);
268 if(ltt_time_compare(time
, previous_time
) == 0) nb_equal
++;
269 else if(nb_equal
> 0) {
270 g_warning("Consecutive %d events with time %lu.%09lu",
271 nb_equal
+ 1, previous_time
.tv_sec
, previous_time
.tv_nsec
);
274 previous_time
= time
;
280 g_free(event_position
);
283 tscs
= g_object_new(LTTV_TRACESET_STATS_TYPE
, NULL
);
285 tc
= &tscs
->parent
.parent
;
287 lttv_context_init(tc
, traceset
);
288 for(i
= 0 ; i
< lttv_traceset_number(traceset
) ; i
++) {
289 ((LttvTraceState
*)(tc
->traces
[i
]))->save_interval
= a_save_interval
;
292 /* For each case compute and print the elapsed time.
293 The first case is simply to run through all events with a
296 if(a_test1
|| a_test_all
) {
298 lttv_hooks_add(after_event
, count_event
, &count
);
299 t
= run_one_test(ts
, zero_time
, max_time
);
300 lttv_hooks_remove_data(after_event
, count_event
, &count
);
302 "Processing trace while counting events (%u events in %g seconds)",
306 /* Run through all events computing the state. */
308 if(a_test2
|| a_test_all
) {
309 lttv_state_add_event_hooks(ts
);
310 t
= run_one_test(ts
, zero_time
, max_time
);
311 lttv_state_remove_event_hooks(ts
);
312 g_warning("Processing trace while updating state (%g seconds)", t
);
315 /* Run through all events computing the state and writing it out
318 SaveState save_state
;
320 save_state
.interval
= a_sample_interval
;
321 save_state
.size
= a_sample_number
;
322 save_state
.fp
= stderr
;
323 save_state
.write_time
= g_new(LttTime
, a_sample_number
);
326 if(a_test3
|| a_test_all
) {
327 for(i
= 0 ; i
< 2 ; i
++) {
328 save_state
.count
= 0;
329 save_state
.position
= 0;
330 save_state
.version
= i
;
331 lttv_state_add_event_hooks(ts
);
332 lttv_hooks_add(after_event
, save_state_event
, &save_state
);
333 t
= run_one_test(ts
, zero_time
, max_time
);
334 lttv_state_remove_event_hooks(ts
);
335 lttv_hooks_remove_data(after_event
, save_state_event
, &save_state
);
336 g_warning("Processing while updating/writing state (%g seconds)", t
);
340 /* Run through all events computing the stats. */
342 if(a_test4
|| a_test_all
) {
343 if(lttv_profile_memory
) {
344 g_message("Memory summary before computing stats");
348 lttv_stats_add_event_hooks(tscs
);
349 t
= run_one_test(ts
, zero_time
, max_time
);
350 lttv_stats_remove_event_hooks(tscs
);
351 g_warning("Processing trace while counting stats (%g seconds)", t
);
353 if(lttv_profile_memory
) {
354 g_message("Memory summary after computing stats");
359 /* Run through all events computing the state and stats. */
361 if(a_test5
|| a_test_all
) {
362 if(lttv_profile_memory
) {
363 g_message("Memory summary before computing state and stats");
367 lttv_state_add_event_hooks(ts
);
368 lttv_stats_add_event_hooks(tscs
);
369 t
= run_one_test(ts
, zero_time
, max_time
);
370 lttv_state_remove_event_hooks(ts
);
371 lttv_stats_remove_event_hooks(tscs
);
373 "Processing trace while counting state and stats (%g seconds)", t
);
375 if(lttv_profile_memory
) {
376 g_message("Memory summary after computing and state and stats");
381 /* Run through all events computing and saving the state. */
383 if(a_test6
|| a_test_all
) {
384 if(lttv_profile_memory
) {
385 g_message("Memory summary before computing and saving state");
389 lttv_state_add_event_hooks(ts
);
390 lttv_state_save_add_event_hooks(ts
);
391 t
= run_one_test(ts
, zero_time
, max_time
);
392 lttv_state_remove_event_hooks(ts
);
393 lttv_state_save_remove_event_hooks(ts
);
394 g_warning("Processing trace while updating/saving state (%g seconds)", t
);
396 if(lttv_profile_memory
) {
397 g_message("Memory summary after computing/saving state");
402 /* Seek a few times to each saved position */
404 if((a_test7
&& a_test3
) || a_test_all
) {
407 for(i
= 0 ; i
< 2 ; i
++) {
408 for(j
= save_state
.position
- 1 ; j
>= 0 ; j
--) {
409 lttv_state_add_event_hooks(ts
);
410 t
= run_one_test(ts
, save_state
.write_time
[j
],
411 save_state
.write_time
[j
]);
412 lttv_state_remove_event_hooks(ts
);
413 g_warning("Seeking to %lu.%lu (%g seconds)",
414 save_state
.write_time
[j
].tv_sec
, save_state
.write_time
[j
].tv_nsec
,
417 if(a_save_sample
!= NULL
) {
418 filename
= g_string_new("");
419 g_string_printf(filename
, "%s.%d.xml.bak%d", a_save_sample
, j
, i
);
420 fp
= fopen(filename
->str
, "w");
421 if(fp
== NULL
) g_error("Cannot open %s", filename
->str
);
422 g_string_free(filename
, TRUE
);
423 lttv_state_write((LttvTraceState
*)tc
->traces
[0],
424 save_state
.write_time
[j
], fp
);
427 else lttv_state_write((LttvTraceState
*)tc
->traces
[0],
428 save_state
.write_time
[j
], save_state
.fp
);
433 g_free(save_state
.write_time
);
434 lttv_context_fini(tc
);
435 g_object_unref(tscs
);
437 g_info("BatchTest end process traceset");
443 LttvAttributeValue value
;
445 LttvIAttribute
*attributes
= LTTV_IATTRIBUTE(lttv_global_attributes());
447 g_info("Init batchtest.c");
449 lttv_option_add("trace", 't',
450 "add a trace to the trace set to analyse",
451 "pathname of the directory containing the trace",
452 LTTV_OPT_STRING
, &a_trace
, lttv_trace_option
, NULL
);
454 a_dump_tracefiles
= NULL
;
455 lttv_option_add("dump-tracefiles", 'D',
456 "Write event by event the content of tracefiles",
457 "basename for the files where to dump events",
458 LTTV_OPT_STRING
, &a_dump_tracefiles
, NULL
, NULL
);
460 a_save_sample
= NULL
;
461 lttv_option_add("save-sample", 's',
462 "Save state samples to multiple files",
463 "basename for the files containing the state samples",
464 LTTV_OPT_STRING
, &a_save_sample
, NULL
, NULL
);
466 a_save_interval
= 100000;
467 lttv_option_add("save-interval", 'i',
468 "Interval between saving state",
469 "number of events before a block start triggers saving state",
470 LTTV_OPT_INT
, &a_save_interval
, NULL
, NULL
);
472 a_sample_interval
= 100000;
473 lttv_option_add("sample-interval", 'S',
474 "Interval between sampling state",
475 "number of events before sampling and writing state",
476 LTTV_OPT_INT
, &a_sample_interval
, NULL
, NULL
);
478 a_sample_number
= 20;
479 lttv_option_add("sample-number", 'N',
480 "Number of state samples",
482 LTTV_OPT_INT
, &a_sample_number
, NULL
, NULL
);
485 lttv_option_add("test1", '1', "Test just counting events", "",
486 LTTV_OPT_NONE
, &a_test1
, NULL
, NULL
);
489 lttv_option_add("test2", '2', "Test computing the state", "",
490 LTTV_OPT_NONE
, &a_test2
, NULL
, NULL
);
493 lttv_option_add("test3", '3', "Test computing the state, writing out a few",
494 "", LTTV_OPT_NONE
, &a_test3
, NULL
, NULL
);
497 lttv_option_add("test4", '4', "Test computing the stats", "",
498 LTTV_OPT_NONE
, &a_test4
, NULL
, NULL
);
501 lttv_option_add("test5", '5', "Test computing the state and stats", "",
502 LTTV_OPT_NONE
, &a_test5
, NULL
, NULL
);
505 lttv_option_add("test6", '6', "Test computing and saving the state", "",
506 LTTV_OPT_NONE
, &a_test6
, NULL
, NULL
);
509 lttv_option_add("test7", '7', "Test seeking to positions written out in 3",
510 "", LTTV_OPT_NONE
, &a_test7
, NULL
, NULL
);
513 lttv_option_add("testall", 'a', "Run all tests ", "",
514 LTTV_OPT_NONE
, &a_test_all
, NULL
, NULL
);
516 traceset
= lttv_traceset_new();
518 before_traceset
= lttv_hooks_new();
519 after_traceset
= lttv_hooks_new();
520 before_trace
= lttv_hooks_new();
521 after_trace
= lttv_hooks_new();
522 before_tracefile
= lttv_hooks_new();
523 after_tracefile
= lttv_hooks_new();
524 before_event
= lttv_hooks_new();
525 after_event
= lttv_hooks_new();
527 g_assert(lttv_iattribute_find_by_path(attributes
, "hooks/traceset/before",
528 LTTV_POINTER
, &value
));
529 *(value
.v_pointer
) = before_traceset
;
530 g_assert(lttv_iattribute_find_by_path(attributes
, "hooks/traceset/after",
531 LTTV_POINTER
, &value
));
532 *(value
.v_pointer
) = after_traceset
;
533 g_assert(lttv_iattribute_find_by_path(attributes
, "hooks/trace/before",
534 LTTV_POINTER
, &value
));
535 *(value
.v_pointer
) = before_trace
;
536 g_assert(lttv_iattribute_find_by_path(attributes
, "hooks/trace/after",
537 LTTV_POINTER
, &value
));
538 *(value
.v_pointer
) = after_trace
;
539 g_assert(lttv_iattribute_find_by_path(attributes
, "hooks/tracefile/before",
540 LTTV_POINTER
, &value
));
541 *(value
.v_pointer
) = before_tracefile
;
542 g_assert(lttv_iattribute_find_by_path(attributes
, "hooks/tracefile/after",
543 LTTV_POINTER
, &value
));
544 *(value
.v_pointer
) = after_tracefile
;
545 g_assert(lttv_iattribute_find_by_path(attributes
, "hooks/event/before",
546 LTTV_POINTER
, &value
));
547 *(value
.v_pointer
) = before_event
;
548 g_assert(lttv_iattribute_find_by_path(attributes
, "hooks/event/after",
549 LTTV_POINTER
, &value
));
550 *(value
.v_pointer
) = after_event
;
552 g_assert(lttv_iattribute_find_by_path(attributes
, "hooks/main/before",
553 LTTV_POINTER
, &value
));
554 g_assert((main_hooks
= *(value
.v_pointer
)) != NULL
);
555 lttv_hooks_add(main_hooks
, process_traceset
, NULL
);
559 static void destroy()
565 g_info("Destroy batchAnalysis.c");
567 lttv_option_remove("trace");
568 lttv_option_remove("dump-tracefiles");
569 lttv_option_remove("save-sample");
570 lttv_option_remove("sample-interval");
571 lttv_option_remove("sample-number");
572 lttv_option_remove("save-interval");
573 lttv_option_remove("test1");
574 lttv_option_remove("test2");
575 lttv_option_remove("test3");
576 lttv_option_remove("test4");
577 lttv_option_remove("test5");
578 lttv_option_remove("test6");
579 lttv_option_remove("test7");
580 lttv_option_remove("testall");
582 lttv_hooks_destroy(before_traceset
);
583 lttv_hooks_destroy(after_traceset
);
584 lttv_hooks_destroy(before_trace
);
585 lttv_hooks_destroy(after_trace
);
586 lttv_hooks_destroy(before_tracefile
);
587 lttv_hooks_destroy(after_tracefile
);
588 lttv_hooks_destroy(before_event
);
589 lttv_hooks_destroy(after_event
);
590 lttv_hooks_remove_data(main_hooks
, process_traceset
, NULL
);
592 nb
= lttv_traceset_number(traceset
);
593 for(i
= 0 ; i
< nb
; i
++) {
594 trace
= lttv_traceset_get(traceset
, i
);
595 ltt_trace_close(lttv_trace(trace
));
596 lttv_trace_destroy(trace
);
599 lttv_traceset_destroy(traceset
);
603 LTTV_MODULE("batchtest", "Batch processing of a trace for tests", \
604 "Run through a trace calling all the registered hooks for tests", \
605 init
, destroy
, "state", "stats", "option" )
This page took 0.041805 seconds and 4 git commands to generate.