Make modules more flexible (builtin or loaded are identical). Add a test module
[lttv.git] / ltt / branches / poly / lttv / modules / text / batchtest.c
1 /* This file is part of the Linux Trace Toolkit viewer
2 * Copyright (C) 2003-2004 Michel Dagenais
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License Version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
16 * MA 02111-1307, USA.
17 */
18
19 /* This module inserts a hook in the program main loop. This hook processes
20 all the events in the main tracefile while testing the speed and
21 functionality of the state and stats computations. */
22
23
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>
34
35 static LttvTraceset *traceset;
36
37 static LttvHooks
38 *before_traceset,
39 *after_traceset,
40 *before_trace,
41 *after_trace,
42 *before_tracefile,
43 *after_tracefile,
44 *before_event,
45 *after_event,
46 *main_hooks;
47
48 static char *a_trace;
49
50 static char *a_dump_tracefiles;
51
52 static char *a_save_sample;
53
54 static int
55 a_sample_interval,
56 a_sample_number,
57 a_save_interval;
58
59 static gboolean
60 a_test1,
61 a_test2,
62 a_test3,
63 a_test4,
64 a_test5,
65 a_test6,
66 a_test7,
67 a_test_all;
68
69 typedef struct _save_state {
70 guint count;
71 FILE *fp;
72 guint interval;
73 guint position;
74 guint size;
75 LttTime *write_time;
76 guint version;
77 } SaveState;
78
79
80 static void lttv_trace_option(void *hook_data)
81 {
82 LttTrace *trace;
83
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));
87 }
88
89 static double get_time()
90 {
91 GTimeVal gt;
92
93 g_get_current_time(&gt);
94 return gt.tv_sec + (double)gt.tv_usec / (double)1000000.0;
95 }
96
97 static double run_one_test(LttvTracesetState *ts, LttTime start, LttTime end)
98 {
99 double t0, t1;
100
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);
104
105 t0 = get_time();
106 lttv_state_traceset_seek_time_closest(ts, start);
107 lttv_process_traceset(&ts->parent, end, G_MAXULONG);
108 t1 = get_time();
109
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);
113
114 return t1 - t0;
115 }
116
117
118 gboolean count_event(void *hook_data, void *call_data)
119 {
120 guint *pcount = (guint *)hook_data;
121
122 (*pcount)++;
123 return FALSE;
124 }
125
126
127 gboolean save_state_event(void *hook_data, void *call_data)
128 {
129 SaveState *save_state = (SaveState *)hook_data;
130
131 LttvTracefileState *tfs = (LttvTracefileState *)call_data;
132
133 LttvTraceState *ts = (LttvTraceState *)tfs->parent.t_context;
134
135 GString *filename;
136
137 FILE *fp;
138
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);
150 fclose(fp);
151 } else lttv_state_write(ts, tfs->parent.timestamp, save_state->fp);
152
153 save_state->write_time[save_state->position] = tfs->parent.timestamp;
154 save_state->position++;
155 }
156 return FALSE;
157 }
158
159
160 static gboolean process_traceset(void *hook_data, void *call_data)
161 {
162 LttvTracesetStats *tscs;
163
164 LttvTracesetState *ts;
165
166 LttvTracesetContext *tc;
167
168 GString *filename;
169
170 FILE *fp;
171
172 double t;
173
174 guint i, j, count, nb_control, nb_tracefile, nb_block, nb_event, nb_equal;
175
176 LttTrace *trace;
177
178 LttTracefile *tracefile, *tf;
179
180 LttEvent *event;
181
182 LttFacility *facility;
183
184 LttType *type;
185
186 LttEventType *event_type;
187
188 LttTime time, previous_time;
189
190 LttEventPosition *event_position;
191
192 LttTime zero_time = ltt_time_zero;
193
194 LttTime max_time = { G_MAXULONG, G_MAXULONG };
195
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++) {
203 if(j < nb_control) {
204 tracefile = ltt_trace_control_tracefile_get(trace,j);
205 }
206 else {
207 tracefile = ltt_trace_per_cpu_tracefile_get(trace,j - nb_control);
208 }
209
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;
217 nb_equal = 0;
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);
227
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);
232 nb_equal = 0;
233 }
234
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);
238 }
239 previous_time = time;
240 }
241 fclose(fp);
242 }
243 }
244 g_free(event_position);
245 }
246
247 tscs = g_object_new(LTTV_TRACESET_STATS_TYPE, NULL);
248 ts = &tscs->parent;
249 tc = &tscs->parent.parent;
250
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;
254 }
255
256 /* For each case compute and print the elapsed time.
257 The first case is simply to run through all events with a
258 simple counter. */
259
260 if(a_test1 || a_test_all) {
261 count = 0;
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);
265 g_warning(
266 "Processing trace while counting events (%u events in %g seconds)",
267 count, t);
268 }
269
270 /* Run through all events computing the state. */
271
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);
277 }
278
279 /* Run through all events computing the state and writing it out
280 periodically. */
281
282 SaveState save_state;
283
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);
288
289
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);
301 }
302 }
303
304 /* Run through all events computing the stats. */
305
306 if(a_test4 || a_test_all) {
307 if(lttv_profile_memory) {
308 g_message("Memory summary before computing stats");
309 g_mem_profile();
310 }
311
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);
316
317 if(lttv_profile_memory) {
318 g_message("Memory summary after computing stats");
319 g_mem_profile();
320 }
321 }
322
323 /* Run through all events computing the state and stats. */
324
325 if(a_test5 || a_test_all) {
326 if(lttv_profile_memory) {
327 g_message("Memory summary before computing state and stats");
328 g_mem_profile();
329 }
330
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);
336 g_warning(
337 "Processing trace while counting state and stats (%g seconds)", t);
338
339 if(lttv_profile_memory) {
340 g_message("Memory summary after computing and state and stats");
341 g_mem_profile();
342 }
343 }
344
345 /* Run through all events computing and saving the state. */
346
347 if(a_test6 || a_test_all) {
348 if(lttv_profile_memory) {
349 g_message("Memory summary before computing and saving state");
350 g_mem_profile();
351 }
352
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);
359
360 if(lttv_profile_memory) {
361 g_message("Memory summary after computing/saving state");
362 g_mem_profile();
363 }
364 }
365
366 /* Seek a few times to each saved position */
367
368 if((a_test7 && a_test3) || a_test_all) {
369 int i, j;
370
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,
379 t);
380
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);
389 fclose(fp);
390 }
391 else lttv_state_write((LttvTraceState *)tc->traces[0],
392 save_state.write_time[j], save_state.fp);
393 }
394 }
395 }
396
397 g_free(save_state.write_time);
398 lttv_context_fini(tc);
399 g_object_unref(tscs);
400
401 g_info("BatchTest end process traceset");
402 }
403
404
405 static void init()
406 {
407 LttvAttributeValue value;
408
409 LttvIAttribute *attributes = LTTV_IATTRIBUTE(lttv_global_attributes());
410
411 g_info("Init batchtest.c");
412
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);
417
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);
423
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);
429
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);
435
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);
441
442 a_sample_number = 20;
443 lttv_option_add("sample-number", 'N',
444 "Number of state samples",
445 "maximum number",
446 LTTV_OPT_INT, &a_sample_number, NULL, NULL);
447
448 a_test1 = FALSE;
449 lttv_option_add("test1", '1', "Test just counting events", "",
450 LTTV_OPT_NONE, &a_test1, NULL, NULL);
451
452 a_test2 = FALSE;
453 lttv_option_add("test2", '2', "Test computing the state", "",
454 LTTV_OPT_NONE, &a_test2, NULL, NULL);
455
456 a_test3 = FALSE;
457 lttv_option_add("test3", '3', "Test computing the state, writing out a few",
458 "", LTTV_OPT_NONE, &a_test3, NULL, NULL);
459
460 a_test4 = FALSE;
461 lttv_option_add("test4", '4', "Test computing the stats", "",
462 LTTV_OPT_NONE, &a_test4, NULL, NULL);
463
464 a_test5 = FALSE;
465 lttv_option_add("test5", '5', "Test computing the state and stats", "",
466 LTTV_OPT_NONE, &a_test5, NULL, NULL);
467
468 a_test6 = FALSE;
469 lttv_option_add("test6", '6', "Test computing and saving the state", "",
470 LTTV_OPT_NONE, &a_test6, NULL, NULL);
471
472 a_test7 = FALSE;
473 lttv_option_add("test7", '7', "Test seeking to positions written out in 3",
474 "", LTTV_OPT_NONE, &a_test7, NULL, NULL);
475
476 a_test_all = FALSE;
477 lttv_option_add("testall", 'a', "Run all tests ", "",
478 LTTV_OPT_NONE, &a_test_all, NULL, NULL);
479
480 traceset = lttv_traceset_new();
481
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();
490
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;
515
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);
520 }
521
522
523 static void destroy()
524 {
525 guint i, nb;
526
527 LttvTrace *trace;
528
529 g_info("Destroy batchAnalysis.c");
530
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");
545
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);
555
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);
561 }
562
563 lttv_traceset_destroy(traceset);
564 }
565
566
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.