libltt compiles
[lttv.git] / ltt / branches / poly / lttv / lttv / tracecontext.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 #ifdef HAVE_CONFIG_H
20 #include <config.h>
21 #endif
22
23 #include <string.h>
24 #include <lttv/lttv.h>
25 #include <lttv/tracecontext.h>
26 #include <ltt/event.h>
27 #include <ltt/facility.h>
28 #include <ltt/trace.h>
29 #include <ltt/type.h>
30 #include <lttv/filter.h>
31 #include <errno.h>
32
33 #define min(a,b) (((a)<(b))?(a):(b))
34
35
36 gint compare_tracefile(gconstpointer a, gconstpointer b)
37 {
38 gint comparison = 0;
39
40 const LttvTracefileContext *trace_a = (const LttvTracefileContext *)a;
41 const LttvTracefileContext *trace_b = (const LttvTracefileContext *)b;
42
43 if(likely(trace_a != trace_b)) {
44 comparison = ltt_time_compare(trace_a->timestamp, trace_b->timestamp);
45 if(unlikely(comparison == 0)) {
46 if(trace_a->index < trace_b->index) comparison = -1;
47 else if(trace_a->index > trace_b->index) comparison = 1;
48 else if(trace_a->t_context->index < trace_b->t_context->index)
49 comparison = -1;
50 else if(trace_a->t_context->index > trace_b->t_context->index)
51 comparison = 1;
52 }
53 }
54 return comparison;
55 }
56
57 typedef struct _LttvTracefileContextPosition {
58 LttEventPosition *event;
59 LttvTracefileContext *tfc;
60 gboolean used; /* Tells if the tfc is at end of traceset position */
61 } LttvTracefileContextPosition;
62
63
64 struct _LttvTracesetContextPosition {
65 GArray *tfcp; /* Array of LttvTracefileContextPosition */
66 LttTime timestamp; /* Current time at the saved position */
67 /* If ltt_time_infinite : no position is
68 * set, else, a position is set (may be end
69 * of trace, with ep->len == 0) */
70 };
71
72 void lttv_context_init(LttvTracesetContext *self, LttvTraceset *ts)
73 {
74 LTTV_TRACESET_CONTEXT_GET_CLASS(self)->init(self, ts);
75 }
76
77
78 void lttv_context_fini(LttvTracesetContext *self)
79 {
80 LTTV_TRACESET_CONTEXT_GET_CLASS(self)->fini(self);
81 }
82
83
84 LttvTracesetContext *
85 lttv_context_new_traceset_context(LttvTracesetContext *self)
86 {
87 return LTTV_TRACESET_CONTEXT_GET_CLASS(self)->new_traceset_context(self);
88 }
89
90
91
92
93 LttvTraceContext *
94 lttv_context_new_trace_context(LttvTracesetContext *self)
95 {
96 return LTTV_TRACESET_CONTEXT_GET_CLASS(self)->new_trace_context(self);
97 }
98
99
100 LttvTracefileContext *
101 lttv_context_new_tracefile_context(LttvTracesetContext *self)
102 {
103 return LTTV_TRACESET_CONTEXT_GET_CLASS(self)->new_tracefile_context(self);
104 }
105
106 /****************************************************************************
107 * lttv_traceset_context_compute_time_span
108 *
109 * Keep the time span is sync with on the fly addition and removal of traces
110 * in a trace set. It must be called each time a trace is added/removed from
111 * the traceset. It could be more efficient to call it only once a bunch
112 * of traces are loaded, but the calculation is not long, so it's not
113 * critical.
114 *
115 * Author : Xang Xiu Yang
116 ***************************************************************************/
117 static void lttv_traceset_context_compute_time_span(
118 LttvTracesetContext *self,
119 TimeInterval *time_span)
120 {
121 LttvTraceset * traceset = self->ts;
122 int numTraces = lttv_traceset_number(traceset);
123 int i;
124 LttTime s, e;
125 LttvTraceContext *tc;
126 LttTrace * trace;
127
128 time_span->start_time.tv_sec = 0;
129 time_span->start_time.tv_nsec = 0;
130 time_span->end_time.tv_sec = 0;
131 time_span->end_time.tv_nsec = 0;
132
133 for(i=0; i<numTraces;i++){
134 tc = self->traces[i];
135 trace = tc->t;
136
137 ltt_trace_time_span_get(trace, &s, &e);
138 tc->time_span.start_time = s;
139 tc->time_span.end_time = e;
140
141 if(i==0){
142 time_span->start_time = s;
143 time_span->end_time = e;
144 }else{
145 if(s.tv_sec < time_span->start_time.tv_sec
146 || (s.tv_sec == time_span->start_time.tv_sec
147 && s.tv_nsec < time_span->start_time.tv_nsec))
148 time_span->start_time = s;
149 if(e.tv_sec > time_span->end_time.tv_sec
150 || (e.tv_sec == time_span->end_time.tv_sec
151 && e.tv_nsec > time_span->end_time.tv_nsec))
152 time_span->end_time = e;
153 }
154 }
155 }
156
157 static void init_tracefile_context(LttTracefile *tracefile,
158 LttvTraceContext *tc)
159 {
160 LttvTracefileContext *tfc;
161 LttvTracesetContext *tsc = tc->ts_context;
162
163 tfc = LTTV_TRACESET_CONTEXT_GET_CLASS(tsc)->new_tracefile_context(tsc);
164
165 tfc->index = tc->tracefiles->len;
166 tc->tracefiles = g_array_append_val(tc->tracefiles, tfc);
167
168 tfc->tf = tracefile;
169
170 tfc->t_context = tc;
171 tfc->event = lttv_hooks_new();
172 tfc->event_by_id = lttv_hooks_by_id_new();
173 tfc->a = g_object_new(LTTV_ATTRIBUTE_TYPE, NULL);
174 tfc->target_pid = -1;
175 }
176
177
178 static void
179 init(LttvTracesetContext *self, LttvTraceset *ts)
180 {
181 guint i, nb_trace;
182
183 LttvTraceContext *tc;
184
185 GData **tracefiles_groups;
186
187 struct compute_tracefile_group_args args;
188
189 nb_trace = lttv_traceset_number(ts);
190 self->ts = ts;
191 self->traces = g_new(LttvTraceContext *, nb_trace);
192 self->a = g_object_new(LTTV_ATTRIBUTE_TYPE, NULL);
193 self->ts_a = lttv_traceset_attribute(ts);
194 for(i = 0 ; i < nb_trace ; i++) {
195 tc = LTTV_TRACESET_CONTEXT_GET_CLASS(self)->new_trace_context(self);
196 self->traces[i] = tc;
197
198 tc->ts_context = self;
199 tc->index = i;
200 tc->vt = lttv_traceset_get(ts, i);
201 tc->t = lttv_trace(tc->vt);
202 tc->a = g_object_new(LTTV_ATTRIBUTE_TYPE, NULL);
203 tc->t_a = lttv_trace_attribute(tc->vt);
204 tc->tracefiles = g_array_sized_new(FALSE, TRUE,
205 sizeof(LttvTracefileContext*), 10);
206
207 tracefiles_groups = ltt_trace_get_tracefiles_groups(tc->t);
208 if(tracefiles_groups != NULL) {
209 args.func = (ForEachTraceFileFunc)init_tracefile_context;
210 args.func_args = tc;
211
212 g_datalist_foreach(tracefiles_groups,
213 (GDataForeachFunc)compute_tracefile_group,
214 &args);
215 }
216
217 #if 0
218 nb_control = ltt_trace_control_tracefile_number(tc->t);
219 nb_per_cpu = ltt_trace_per_cpu_tracefile_number(tc->t);
220 nb_tracefile = nb_control + nb_per_cpu;
221 tc->tracefiles = g_new(LttvTracefileContext *, nb_tracefile);
222
223 for(j = 0 ; j < nb_tracefile ; j++) {
224 tfc = LTTV_TRACESET_CONTEXT_GET_CLASS(self)->new_tracefile_context(self);
225 tc->tracefiles[j] = tfc;
226 tfc->index = j;
227
228 if(j < nb_control) {
229 tfc->control = TRUE;
230 tfc->tf = ltt_trace_control_tracefile_get(tc->t, j);
231 }
232 else {
233 tfc->control = FALSE;
234 tfc->tf = ltt_trace_per_cpu_tracefile_get(tc->t, j - nb_control);
235 }
236
237 tfc->t_context = tc;
238 tfc->e = ltt_event_new();
239 tfc->event = lttv_hooks_new();
240 tfc->event_by_id = lttv_hooks_by_id_new();
241 tfc->a = g_object_new(LTTV_ATTRIBUTE_TYPE, NULL);
242 }
243 #endif //0
244
245 }
246 self->sync_position = lttv_traceset_context_position_new(self);
247 self->pqueue = g_tree_new(compare_tracefile);
248 lttv_process_traceset_seek_time(self, ltt_time_zero);
249 lttv_traceset_context_compute_time_span(self, &self->time_span);
250
251 }
252
253
254 void fini(LttvTracesetContext *self)
255 {
256 guint i, j, nb_trace, nb_tracefile;
257
258 LttvTraceContext *tc;
259
260 LttvTracefileContext **tfc;
261
262 LttvTraceset *ts = self->ts;
263
264 g_tree_destroy(self->pqueue);
265 g_object_unref(self->a);
266 lttv_traceset_context_position_destroy(self->sync_position);
267
268 nb_trace = lttv_traceset_number(ts);
269
270 for(i = 0 ; i < nb_trace ; i++) {
271 tc = self->traces[i];
272
273 g_object_unref(tc->a);
274
275 nb_tracefile = tc->tracefiles->len;
276
277 for(j = 0 ; j < nb_tracefile ; j++) {
278 tfc = &g_array_index(tc->tracefiles, LttvTracefileContext*, j);
279 lttv_hooks_destroy((*tfc)->event);
280 lttv_hooks_by_id_destroy((*tfc)->event_by_id);
281 g_object_unref((*tfc)->a);
282 g_object_unref(*tfc);
283 }
284 g_array_free(tc->tracefiles, TRUE);
285 g_object_unref(tc);
286 }
287 g_free(self->traces);
288 }
289
290
291 void lttv_traceset_context_add_hooks(LttvTracesetContext *self,
292 LttvHooks *before_traceset,
293 LttvHooks *before_trace,
294 LttvHooks *before_tracefile,
295 LttvHooks *event,
296 LttvHooksById *event_by_id)
297 {
298 LttvTraceset *ts = self->ts;
299
300 guint i, nb_trace;
301
302 LttvTraceContext *tc;
303
304 lttv_hooks_call(before_traceset, self);
305
306 nb_trace = lttv_traceset_number(ts);
307
308 for(i = 0 ; i < nb_trace ; i++) {
309 tc = self->traces[i];
310 lttv_trace_context_add_hooks(tc,
311 before_trace,
312 before_tracefile,
313 event,
314 event_by_id);
315 }
316 }
317
318
319 void lttv_traceset_context_remove_hooks(LttvTracesetContext *self,
320 LttvHooks *after_traceset,
321 LttvHooks *after_trace,
322 LttvHooks *after_tracefile,
323 LttvHooks *event,
324 LttvHooksById *event_by_id)
325 {
326
327 LttvTraceset *ts = self->ts;
328
329 guint i, nb_trace;
330
331 LttvTraceContext *tc;
332
333 nb_trace = lttv_traceset_number(ts);
334
335 for(i = 0 ; i < nb_trace ; i++) {
336 tc = self->traces[i];
337 lttv_trace_context_remove_hooks(tc,
338 after_trace,
339 after_tracefile,
340 event,
341 event_by_id);
342 }
343
344 lttv_hooks_call(after_traceset, self);
345
346
347 }
348
349 void lttv_trace_context_add_hooks(LttvTraceContext *self,
350 LttvHooks *before_trace,
351 LttvHooks *before_tracefile,
352 LttvHooks *event,
353 LttvHooksById *event_by_id)
354 {
355 guint i, nb_tracefile;
356
357 LttvTracefileContext **tfc;
358
359 lttv_hooks_call(before_trace, self);
360
361 nb_tracefile = self->tracefiles->len;
362
363 for(i = 0 ; i < nb_tracefile ; i++) {
364 tfc = &g_array_index(self->tracefiles, LttvTracefileContext*, i);
365 lttv_tracefile_context_add_hooks(*tfc,
366 before_tracefile,
367 event,
368 event_by_id);
369 }
370 }
371
372
373
374 void lttv_trace_context_remove_hooks(LttvTraceContext *self,
375 LttvHooks *after_trace,
376 LttvHooks *after_tracefile,
377 LttvHooks *event,
378 LttvHooksById *event_by_id)
379 {
380 guint i, nb_tracefile;
381
382 LttvTracefileContext **tfc;
383
384 nb_tracefile = self->tracefiles->len;
385
386 for(i = 0 ; i < nb_tracefile ; i++) {
387 tfc = &g_array_index(self->tracefiles, LttvTracefileContext*, i);
388 lttv_tracefile_context_remove_hooks(*tfc,
389 after_tracefile,
390 event,
391 event_by_id);
392 }
393
394 lttv_hooks_call(after_trace, self);
395 }
396
397 void lttv_tracefile_context_add_hooks(LttvTracefileContext *self,
398 LttvHooks *before_tracefile,
399 LttvHooks *event,
400 LttvHooksById *event_by_id)
401 {
402 guint i, index;
403
404 LttvHooks *hook;
405
406 lttv_hooks_call(before_tracefile, self);
407 lttv_hooks_add_list(self->event, event);
408 if(event_by_id != NULL) {
409 for(i = 0; i < event_by_id->array->len; i++) {
410 index = g_array_index(event_by_id->array, guint, i);
411 hook = lttv_hooks_by_id_find(self->event_by_id, index);
412 lttv_hooks_add_list(hook, lttv_hooks_by_id_get(event_by_id, index));
413 }
414 }
415 }
416
417 void lttv_tracefile_context_remove_hooks(LttvTracefileContext *self,
418 LttvHooks *after_tracefile,
419 LttvHooks *event,
420 LttvHooksById *event_by_id)
421 {
422 guint i, index;
423
424 LttvHooks *hook;
425
426 lttv_hooks_remove_list(self->event, event);
427 if(event_by_id != NULL) {
428 for(i = 0; i < event_by_id->array->len; i++) {
429 index = g_array_index(event_by_id->array, guint, i);
430 hook = lttv_hooks_by_id_get(self->event_by_id, index);
431 if(hook != NULL)
432 lttv_hooks_remove_list(hook, lttv_hooks_by_id_get(event_by_id, index));
433 }
434 }
435
436 lttv_hooks_call(after_tracefile, self);
437 }
438
439
440
441 void lttv_tracefile_context_add_hooks_by_id(LttvTracefileContext *tfc,
442 unsigned i,
443 LttvHooks *event_by_id)
444 {
445 LttvHooks * h;
446 h = lttv_hooks_by_id_find(tfc->event_by_id, i);
447 lttv_hooks_add_list(h, event_by_id);
448 }
449
450 void lttv_tracefile_context_remove_hooks_by_id(LttvTracefileContext *tfc,
451 unsigned i)
452 {
453 lttv_hooks_by_id_remove(tfc->event_by_id, i);
454 }
455
456 static LttvTracesetContext *
457 new_traceset_context(LttvTracesetContext *self)
458 {
459 return g_object_new(LTTV_TRACESET_CONTEXT_TYPE, NULL);
460 }
461
462
463 static LttvTraceContext *
464 new_trace_context(LttvTracesetContext *self)
465 {
466 return g_object_new(LTTV_TRACE_CONTEXT_TYPE, NULL);
467 }
468
469
470 static LttvTracefileContext *
471 new_tracefile_context(LttvTracesetContext *self)
472 {
473 return g_object_new(LTTV_TRACEFILE_CONTEXT_TYPE, NULL);
474 }
475
476
477 static void
478 traceset_context_instance_init (GTypeInstance *instance, gpointer g_class)
479 {
480 /* Be careful of anything which would not work well with shallow copies */
481 }
482
483
484 static void
485 traceset_context_finalize (LttvTracesetContext *self)
486 {
487 G_OBJECT_CLASS(g_type_class_peek(g_type_parent(LTTV_TRACESET_CONTEXT_TYPE)))
488 ->finalize(G_OBJECT(self));
489 }
490
491
492 static void
493 traceset_context_class_init (LttvTracesetContextClass *klass)
494 {
495 GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
496
497 gobject_class->finalize = (void (*)(GObject *self))traceset_context_finalize;
498 klass->init = init;
499 klass->fini = fini;
500 klass->new_traceset_context = new_traceset_context;
501 klass->new_trace_context = new_trace_context;
502 klass->new_tracefile_context = new_tracefile_context;
503 }
504
505
506 GType
507 lttv_traceset_context_get_type(void)
508 {
509 static GType type = 0;
510 if (type == 0) {
511 static const GTypeInfo info = {
512 sizeof (LttvTracesetContextClass),
513 NULL, /* base_init */
514 NULL, /* base_finalize */
515 (GClassInitFunc) traceset_context_class_init, /* class_init */
516 NULL, /* class_finalize */
517 NULL, /* class_data */
518 sizeof (LttvTracesetContext),
519 0, /* n_preallocs */
520 (GInstanceInitFunc) traceset_context_instance_init, /* instance_init */
521 NULL /* Value handling */
522 };
523
524 type = g_type_register_static (G_TYPE_OBJECT, "LttvTracesetContextType",
525 &info, 0);
526 }
527 return type;
528 }
529
530
531 static void
532 trace_context_instance_init (GTypeInstance *instance, gpointer g_class)
533 {
534 /* Be careful of anything which would not work well with shallow copies */
535 }
536
537
538 static void
539 trace_context_finalize (LttvTraceContext *self)
540 {
541 G_OBJECT_CLASS(g_type_class_peek(g_type_parent(LTTV_TRACE_CONTEXT_TYPE)))->
542 finalize(G_OBJECT(self));
543 }
544
545
546 static void
547 trace_context_class_init (LttvTraceContextClass *klass)
548 {
549 GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
550
551 gobject_class->finalize = (void (*)(GObject *self)) trace_context_finalize;
552 }
553
554
555 GType
556 lttv_trace_context_get_type(void)
557 {
558 static GType type = 0;
559 if (type == 0) {
560 static const GTypeInfo info = {
561 sizeof (LttvTraceContextClass),
562 NULL, /* base_init */
563 NULL, /* base_finalize */
564 (GClassInitFunc) trace_context_class_init, /* class_init */
565 NULL, /* class_finalize */
566 NULL, /* class_data */
567 sizeof (LttvTraceContext),
568 0, /* n_preallocs */
569 (GInstanceInitFunc) trace_context_instance_init, /* instance_init */
570 NULL /* Value handling */
571 };
572
573 type = g_type_register_static (G_TYPE_OBJECT, "LttvTraceContextType",
574 &info, 0);
575 }
576 return type;
577 }
578
579
580 static void
581 tracefile_context_instance_init (GTypeInstance *instance, gpointer g_class)
582 {
583 /* Be careful of anything which would not work well with shallow copies */
584 }
585
586
587 static void
588 tracefile_context_finalize (LttvTracefileContext *self)
589 {
590 G_OBJECT_CLASS(g_type_class_peek(g_type_parent(LTTV_TRACEFILE_CONTEXT_TYPE)))
591 ->finalize(G_OBJECT(self));
592 }
593
594
595 static void
596 tracefile_context_class_init (LttvTracefileContextClass *klass)
597 {
598 GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
599
600 gobject_class->finalize = (void (*)(GObject *self))tracefile_context_finalize;
601 }
602
603
604 GType
605 lttv_tracefile_context_get_type(void)
606 {
607 static GType type = 0;
608 if (type == 0) {
609 static const GTypeInfo info = {
610 sizeof (LttvTracefileContextClass),
611 NULL, /* base_init */
612 NULL, /* base_finalize */
613 (GClassInitFunc) tracefile_context_class_init, /* class_init */
614 NULL, /* class_finalize */
615 NULL, /* class_data */
616 sizeof (LttvTracefileContext),
617 0, /* n_preallocs */
618 (GInstanceInitFunc) tracefile_context_instance_init, /* instance_init */
619 NULL /* Value handling */
620 };
621
622 type = g_type_register_static (G_TYPE_OBJECT, "LttvTracefileContextType",
623 &info, 0);
624 }
625 return type;
626 }
627
628
629
630 static gboolean get_first(gpointer key, gpointer value, gpointer user_data) {
631 g_assert(key == value);
632 *((LttvTracefileContext **)user_data) = (LttvTracefileContext *)value;
633 return TRUE;
634 }
635
636 #ifdef DEBUG
637 // Test to see if pqueue is traversed in the right order.
638 static LttTime test_time;
639
640 static gboolean test_tree(gpointer key, gpointer value, gpointer user_data) {
641
642 LttvTracefileContext *tfc = (LttvTracefileContext *)key;
643
644 g_debug("Tracefile name %s, time %lu.%lu, tfi %u, ti %u",
645 g_quark_to_string(ltt_tracefile_name(tfc->tf)),
646 tfc->timestamp.tv_sec, tfc->timestamp.tv_nsec,
647 tfc->index, tfc->t_context->index);
648
649 if(user_data != NULL) {
650 if(((LttvTracefileContext *)user_data) == (LttvTracefileContext *)value) {
651 g_assert(compare_tracefile(user_data, value) == 0);
652 } else
653 g_assert(compare_tracefile(user_data, value) != 0);
654 }
655 g_assert(ltt_time_compare(test_time, tfc->timestamp) <= 0);
656 test_time.tv_sec = tfc->timestamp.tv_sec;
657 test_time.tv_nsec = tfc->timestamp.tv_nsec;
658
659
660 //g_assert(((LttvTracefileContext *)user_data) != (LttvTracefileContext *)value);
661 return FALSE;
662 }
663 #endif //DEBUG
664
665
666
667 void lttv_process_traceset_begin(LttvTracesetContext *self,
668 LttvHooks *before_traceset,
669 LttvHooks *before_trace,
670 LttvHooks *before_tracefile,
671 LttvHooks *event,
672 LttvHooksById *event_by_id)
673 {
674
675 /* simply add hooks in context. _before hooks are called by add_hooks. */
676 /* It calls all before_traceset, before_trace, and before_tracefile hooks. */
677 lttv_traceset_context_add_hooks(self,
678 before_traceset,
679 before_trace,
680 before_tracefile,
681 event,
682 event_by_id);
683
684 }
685
686 //enum read_state { LAST_NONE, LAST_OK, LAST_EMPTY };
687
688 /* Note : a _middle must be preceded from a _seek or another middle */
689 guint lttv_process_traceset_middle(LttvTracesetContext *self,
690 LttTime end,
691 guint nb_events,
692 const LttvTracesetContextPosition *end_position)
693 {
694 GTree *pqueue = self->pqueue;
695
696 guint fac_id, ev_id, id;
697
698 LttvTracefileContext *tfc;
699
700 LttEvent *e;
701
702 unsigned count = 0;
703
704 guint read_ret;
705
706 //enum read_state last_read_state = LAST_NONE;
707
708 gint last_ret = 0; /* return value of the last hook list called */
709
710 /* Get the next event from the pqueue, call its hooks,
711 reinsert in the pqueue the following event from the same tracefile
712 unless the tracefile is finished or the event is later than the
713 end time. */
714
715 while(TRUE) {
716 tfc = NULL;
717 g_tree_foreach(pqueue, get_first, &tfc);
718 /* End of traceset : tfc is NULL */
719 if(unlikely(tfc == NULL))
720 {
721 return count;
722 }
723
724 /* Have we reached :
725 * - the maximum number of events specified?
726 * - the end position ?
727 * - the end time ?
728 * then the read is finished. We leave the queue in the same state and
729 * break the loop.
730 */
731
732 if(unlikely(last_ret == TRUE ||
733 ((count >= nb_events) && (nb_events != G_MAXULONG)) ||
734 (end_position!=NULL&&lttv_traceset_context_ctx_pos_compare(self,
735 end_position) == 0)||
736 ltt_time_compare(end, tfc->timestamp) <= 0))
737 {
738 return count;
739 }
740
741 /* Get the tracefile with an event for the smallest time found. If two
742 or more tracefiles have events for the same time, hope that lookup
743 and remove are consistent. */
744
745 #ifdef DEBUG
746 test_time.tv_sec = 0;
747 test_time.tv_nsec = 0;
748 g_debug("test tree before remove");
749 g_tree_foreach(pqueue, test_tree, tfc);
750 #endif //DEBUG
751 g_tree_remove(pqueue, tfc);
752
753 #ifdef DEBUG
754 test_time.tv_sec = 0;
755 test_time.tv_nsec = 0;
756 g_debug("test tree after remove");
757 g_tree_foreach(pqueue, test_tree, tfc);
758 #endif //DEBUG
759
760
761 e = ltt_tracefile_get_event(tfc->tf);
762
763 //if(last_read_state != LAST_EMPTY) {
764 /* Only call hooks if the last read has given an event or if we are at the
765 * first pass (not if last read returned end of tracefile) */
766 count++;
767
768 fac_id = ltt_event_facility_id(e);
769 ev_id = ltt_event_eventtype_id(e);
770 id = GET_HOOK_ID(fac_id, ev_id);
771 tfc->target_pid = -1; /* unset target PID */
772 /* Hooks :
773 * return values : 0 : continue read, 1 : go to next position and stop read,
774 * 2 : stay at the current position and stop read */
775 last_ret = lttv_hooks_call_merge(tfc->event, tfc,
776 lttv_hooks_by_id_get(tfc->event_by_id, id), tfc);
777
778 #if 0
779 /* This is buggy : it won't work well with state computation */
780 if(unlikely(last_ret == 2)) {
781 /* This is a case where we want to stay at this position and stop read. */
782 g_tree_insert(pqueue, tfc, tfc);
783 return count - 1;
784 }
785 #endif //0
786 read_ret = ltt_tracefile_read(tfc->tf);
787
788
789 if(likely(!read_ret)) {
790 //g_debug("An event is ready");
791 tfc->timestamp = ltt_event_time(e);
792 g_assert(ltt_time_compare(tfc->timestamp, ltt_time_infinite) != 0);
793 g_tree_insert(pqueue, tfc, tfc);
794 #ifdef DEBUG
795 test_time.tv_sec = 0;
796 test_time.tv_nsec = 0;
797 g_debug("test tree after event ready");
798 g_tree_foreach(pqueue, test_tree, NULL);
799 #endif //DEBUG
800
801 //last_read_state = LAST_OK;
802 } else {
803 tfc->timestamp = ltt_time_infinite;
804
805 if(read_ret == ERANGE) {
806 // last_read_state = LAST_EMPTY;
807 g_debug("End of trace");
808 } else
809 g_error("Error happened in lttv_process_traceset_middle");
810 }
811 }
812 }
813
814
815 void lttv_process_traceset_end(LttvTracesetContext *self,
816 LttvHooks *after_traceset,
817 LttvHooks *after_trace,
818 LttvHooks *after_tracefile,
819 LttvHooks *event,
820 LttvHooksById *event_by_id)
821 {
822 /* Remove hooks from context. _after hooks are called by remove_hooks. */
823 /* It calls all after_traceset, after_trace, and after_tracefile hooks. */
824 lttv_traceset_context_remove_hooks(self,
825 after_traceset,
826 after_trace,
827 after_tracefile,
828 event,
829 event_by_id);
830 }
831
832 /* Subtile modification :
833 * if tracefile has no event at or after the time requested, it is not put in
834 * the queue, as the next read would fail.
835 *
836 * Don't forget to empty the traceset pqueue before calling this.
837 */
838 void lttv_process_trace_seek_time(LttvTraceContext *self, LttTime start)
839 {
840 guint i, nb_tracefile;
841
842 gint ret;
843
844 LttvTracefileContext **tfc;
845
846 nb_tracefile = self->tracefiles->len;
847
848 GTree *pqueue = self->ts_context->pqueue;
849
850 for(i = 0 ; i < nb_tracefile ; i++) {
851 tfc = &g_array_index(self->tracefiles, LttvTracefileContext*, i);
852
853 g_tree_remove(pqueue, *tfc);
854
855 ret = ltt_tracefile_seek_time((*tfc)->tf, start);
856 if(ret == EPERM) g_error("error in lttv_process_trace_seek_time seek");
857
858 if(ret == 0) { /* not ERANGE especially */
859 (*tfc)->timestamp = ltt_event_time(ltt_tracefile_get_event((*tfc)->tf));
860 g_assert(ltt_time_compare((*tfc)->timestamp, ltt_time_infinite) != 0);
861 g_tree_insert(pqueue, (*tfc), (*tfc));
862 } else {
863 (*tfc)->timestamp = ltt_time_infinite;
864 }
865 }
866 #ifdef DEBUG
867 test_time.tv_sec = 0;
868 test_time.tv_nsec = 0;
869 g_debug("test tree after seek_time");
870 g_tree_foreach(pqueue, test_tree, NULL);
871 #endif //DEBUG
872
873
874
875 }
876
877
878 void lttv_process_traceset_seek_time(LttvTracesetContext *self, LttTime start)
879 {
880 guint i, nb_trace;
881
882 LttvTraceContext *tc;
883
884 //g_tree_destroy(self->pqueue);
885 //self->pqueue = g_tree_new(compare_tracefile);
886
887 nb_trace = lttv_traceset_number(self->ts);
888 for(i = 0 ; i < nb_trace ; i++) {
889 tc = self->traces[i];
890 lttv_process_trace_seek_time(tc, start);
891 }
892 }
893
894
895 gboolean lttv_process_traceset_seek_position(LttvTracesetContext *self,
896 const LttvTracesetContextPosition *pos)
897 {
898 guint i;
899 /* If a position is set, seek the traceset to this position */
900 if(ltt_time_compare(pos->timestamp, ltt_time_infinite) != 0) {
901
902 /* Test to see if the traces has been added to the trace set :
903 * It should NEVER happen. Clear all positions if a new trace comes in. */
904 /* FIXME I know this test is not optimal : should keep a number of
905 * tracefiles variable in the traceset.. eventually */
906 guint num_traces = lttv_traceset_number(self->ts);
907 guint tf_count = 0;
908 for(i=0; i<num_traces;i++) {
909 GArray * tracefiles = self->traces[i]->tracefiles;
910 guint j;
911 guint num_tracefiles = tracefiles->len;
912 for(j=0;j<num_tracefiles;j++)
913 tf_count++;
914 }
915 g_assert(tf_count == pos->tfcp->len);
916
917
918 //g_tree_destroy(self->pqueue);
919 //self->pqueue = g_tree_new(compare_tracefile);
920
921 for(i=0;i<pos->tfcp->len; i++) {
922 LttvTracefileContextPosition *tfcp =
923 &g_array_index(pos->tfcp, LttvTracefileContextPosition, i);
924
925 g_tree_remove(self->pqueue, tfcp->tfc);
926
927 if(tfcp->used == TRUE) {
928 if(ltt_tracefile_seek_position(tfcp->tfc->tf, tfcp->event) != 0)
929 return 1;
930 tfcp->tfc->timestamp =
931 ltt_event_time(ltt_tracefile_get_event(tfcp->tfc->tf));
932 g_assert(ltt_time_compare(tfcp->tfc->timestamp,
933 ltt_time_infinite) != 0);
934 g_tree_insert(self->pqueue, tfcp->tfc, tfcp->tfc);
935
936 } else {
937 tfcp->tfc->timestamp = ltt_time_infinite;
938 }
939 }
940 }
941 #ifdef DEBUG
942 test_time.tv_sec = 0;
943 test_time.tv_nsec = 0;
944 g_debug("test tree after seek_position");
945 g_tree_foreach(self->pqueue, test_tree, NULL);
946 #endif //DEBUG
947
948
949
950 return 0;
951 }
952
953
954
955 static LttField *
956 find_field(LttEventType *et, const GQuark field)
957 {
958 LttField *f;
959
960 if(field == 0) return NULL;
961
962 f = ltt_eventtype_field_by_name(et, field);
963 if (!f) {
964 g_warning("Cannot find field %s in event %s.%s", g_quark_to_string(field),
965 g_quark_to_string(ltt_facility_name(ltt_eventtype_facility(et))),
966 g_quark_to_string(ltt_eventtype_name(et)));
967 }
968
969 return f;
970 }
971
972 LttvTraceHookByFacility *lttv_trace_hook_get_fac(LttvTraceHook *th,
973 guint facility_id)
974 {
975 return &g_array_index(th->fac_index, LttvTraceHookByFacility, facility_id);
976 }
977
978 struct marker_info *lttv_trace_hook_get_marker(LttvTraceHook *th)
979 {
980 g_assert(th->fac_list->len > 0);
981 return g_array_index(th->fac_list, LttvTraceHookByFacility*, 0);
982 }
983
984
985 /* Returns 0 on success, -1 if fails. */
986 gint
987 lttv_trace_find_hook(LttTrace *t, GQuark event,
988 GQuark field1, GQuark field2, GQuark field3, LttvHook h, gpointer hook_data,
989 LttvTraceHook *th)
990 {
991 LttEventType *et, *first_et;
992
993 struct marker_info *info;
994
995 guint i, ev_id;
996
997 head = marker_get_info_from_name(t, event);
998
999 if(unlikely(head == NULL))
1000 goto facility_error;
1001
1002 ev_id = marker_get_id_from_info(t, info);
1003
1004 th->h = h;
1005 th->id = ev_id;
1006 th->f1 = find_field(et, field1);
1007 th->f2 = find_field(et, field2);
1008 th->f3 = find_field(et, field3);
1009 th->hook_data = hook_data;
1010
1011 first_thf = thf;
1012 first_et = et;
1013
1014 /* Check for type compatibility too */
1015 for(i=1;i<facilities->len;i++) {
1016 fac_id = g_array_index(facilities, guint, i);
1017 f = ltt_trace_get_facility_by_num(t, fac_id);
1018
1019 et = ltt_facility_eventtype_get_by_name(f, event);
1020 if(unlikely(et == NULL)) goto event_error;
1021
1022 thf = &g_array_index(th->fac_index, LttvTraceHookByFacility, fac_id);
1023 g_array_index(th->fac_list, LttvTraceHookByFacility*, i) = thf;
1024 ev_id = ltt_eventtype_id(et);
1025 thf->h = h;
1026 thf->id = GET_HOOK_ID(fac_id, ev_id);
1027 thf->f1 = find_field(et, field1);
1028 if(check_fields_compatibility(first_et, et,
1029 first_thf->f1, thf->f1))
1030 goto type_error;
1031
1032 thf->f2 = find_field(et, field2);
1033 if(check_fields_compatibility(first_et, et,
1034 first_thf->f2, thf->f2))
1035 goto type_error;
1036
1037 thf->f3 = find_field(et, field3);
1038 if(check_fields_compatibility(first_et, et,
1039 first_thf->f3, thf->f3))
1040 goto type_error;
1041 thf->hook_data = hook_data;
1042 }
1043
1044 return 0;
1045
1046 type_error:
1047 goto free;
1048 event_error:
1049 g_error("Event type does not exist for event %s",
1050 g_quark_to_string(event));
1051 goto free;
1052 facility_error:
1053 //Ignore this type of error : some facilities are not required.
1054 //g_error("No %s facility", g_quark_to_string(facility));
1055 return -1;
1056 free:
1057 g_array_free(th->fac_index, TRUE);
1058 g_array_free(th->fac_list, TRUE);
1059 th->fac_index = NULL;
1060 th->fac_list = NULL;
1061 return -1;
1062 }
1063
1064 void lttv_trace_hook_destroy(LttvTraceHook *th)
1065 {
1066 g_array_free(th->fac_index, TRUE);
1067 g_array_free(th->fac_list, TRUE);
1068 }
1069
1070
1071
1072
1073 LttvTracesetContextPosition *lttv_traceset_context_position_new(
1074 const LttvTracesetContext *self)
1075 {
1076 guint num_traces = lttv_traceset_number(self->ts);
1077 guint tf_count = 0;
1078 guint i;
1079
1080 for(i=0; i<num_traces;i++) {
1081 GArray * tracefiles = self->traces[i]->tracefiles;
1082 guint j;
1083 guint num_tracefiles = tracefiles->len;
1084 for(j=0;j<num_tracefiles;j++)
1085 tf_count++;
1086 }
1087 LttvTracesetContextPosition *pos =
1088 g_new(LttvTracesetContextPosition, 1);
1089 pos->tfcp = g_array_sized_new(FALSE, TRUE,
1090 sizeof(LttvTracefileContextPosition),
1091 tf_count);
1092 g_array_set_size(pos->tfcp, tf_count);
1093 for(i=0;i<pos->tfcp->len;i++) {
1094 LttvTracefileContextPosition *tfcp =
1095 &g_array_index(pos->tfcp, LttvTracefileContextPosition, i);
1096 tfcp->event = ltt_event_position_new();
1097 }
1098
1099 pos->timestamp = ltt_time_infinite;
1100 return pos;
1101 }
1102
1103 /* Save all positions, the ones with infinite time will have NULL
1104 * ep. */
1105 /* note : a position must be destroyed when a trace is added/removed from a
1106 * traceset */
1107 void lttv_traceset_context_position_save(const LttvTracesetContext *self,
1108 LttvTracesetContextPosition *pos)
1109 {
1110 guint i;
1111 guint num_traces = lttv_traceset_number(self->ts);
1112 guint tf_count = 0;
1113
1114 pos->timestamp = ltt_time_infinite;
1115
1116 for(i=0; i<num_traces;i++) {
1117 GArray * tracefiles = self->traces[i]->tracefiles;
1118 guint j;
1119 guint num_tracefiles = tracefiles->len;
1120
1121 for(j=0;j<num_tracefiles;j++) {
1122 g_assert(tf_count < pos->tfcp->len);
1123 LttvTracefileContext **tfc = &g_array_index(tracefiles,
1124 LttvTracefileContext*, j);
1125 LttvTracefileContextPosition *tfcp =
1126 &g_array_index(pos->tfcp, LttvTracefileContextPosition, tf_count);
1127
1128 tfcp->tfc = *tfc;
1129
1130 if(ltt_time_compare((*tfc)->timestamp, ltt_time_infinite) != 0) {
1131 LttEvent *event = ltt_tracefile_get_event((*tfc)->tf);
1132 ltt_event_position(event, tfcp->event);
1133 if(ltt_time_compare((*tfc)->timestamp, pos->timestamp) < 0)
1134 pos->timestamp = (*tfc)->timestamp;
1135 tfcp->used = TRUE;
1136 } else {
1137 tfcp->used = FALSE;
1138 }
1139
1140 //g_array_append_val(pos->tfc, *tfc);
1141 //g_array_append_val(pos->ep, ep);
1142 tf_count++;
1143 }
1144
1145 }
1146 }
1147
1148 void lttv_traceset_context_position_destroy(LttvTracesetContextPosition *pos)
1149 {
1150 int i;
1151
1152 for(i=0;i<pos->tfcp->len;i++) {
1153 LttvTracefileContextPosition *tfcp =
1154 &g_array_index(pos->tfcp, LttvTracefileContextPosition, i);
1155 g_free(tfcp->event);
1156 tfcp->event = NULL;
1157 tfcp->used = FALSE;
1158 }
1159 g_array_free(pos->tfcp, TRUE);
1160 g_free(pos);
1161 }
1162
1163 void lttv_traceset_context_position_copy(LttvTracesetContextPosition *dest,
1164 const LttvTracesetContextPosition *src)
1165 {
1166 int i;
1167 LttvTracefileContextPosition *src_tfcp, *dest_tfcp;
1168
1169 g_assert(src->tfcp->len == src->tfcp->len);
1170
1171 for(i=0;i<src->tfcp->len;i++) {
1172 src_tfcp =
1173 &g_array_index(src->tfcp, LttvTracefileContextPosition, i);
1174 dest_tfcp =
1175 &g_array_index(dest->tfcp, LttvTracefileContextPosition, i);
1176
1177 dest_tfcp->used = src_tfcp->used;
1178 dest_tfcp->tfc = src_tfcp->tfc;
1179
1180 if(src_tfcp->used) {
1181 ltt_event_position_copy(
1182 dest_tfcp->event,
1183 src_tfcp->event);
1184 }
1185 }
1186 dest->timestamp = src->timestamp;
1187 }
1188
1189 gint lttv_traceset_context_ctx_pos_compare(const LttvTracesetContext *self,
1190 const LttvTracesetContextPosition *pos)
1191 {
1192 int i;
1193 int ret = 0;
1194
1195 if(pos->tfcp->len == 0) {
1196 if(lttv_traceset_number(self->ts) == 0) return 0;
1197 else return 1;
1198 }
1199 if(lttv_traceset_number(self->ts) == 0)
1200 return -1;
1201
1202 for(i=0;i<pos->tfcp->len;i++) {
1203 LttvTracefileContextPosition *tfcp =
1204 &g_array_index(pos->tfcp, LttvTracefileContextPosition, i);
1205
1206 if(tfcp->used == FALSE) {
1207 if(ltt_time_compare(tfcp->tfc->timestamp, ltt_time_infinite) < 0) {
1208 ret = -1;
1209 }
1210 } else {
1211 if(ltt_time_compare(tfcp->tfc->timestamp, ltt_time_infinite) == 0) {
1212 ret = 1;
1213 } else {
1214 LttEvent *event = ltt_tracefile_get_event(tfcp->tfc->tf);
1215
1216 ret = ltt_event_position_compare((LttEventPosition*)event,
1217 tfcp->event);
1218 }
1219 }
1220 if(ret != 0) return ret;
1221
1222 }
1223 return 0;
1224 }
1225
1226
1227 gint lttv_traceset_context_pos_pos_compare(
1228 const LttvTracesetContextPosition *pos1,
1229 const LttvTracesetContextPosition *pos2)
1230 {
1231 int i, j;
1232 int ret = 0;
1233
1234 if(ltt_time_compare(pos1->timestamp, ltt_time_infinite) == 0) {
1235 if(ltt_time_compare(pos2->timestamp, ltt_time_infinite) == 0)
1236 return 0;
1237 else
1238 return 1;
1239 }
1240 if(ltt_time_compare(pos2->timestamp, ltt_time_infinite) == 0)
1241 return -1;
1242
1243 for(i=0;i<pos1->tfcp->len;i++) {
1244 LttvTracefileContextPosition *tfcp1 =
1245 &g_array_index(pos1->tfcp, LttvTracefileContextPosition, i);
1246
1247 if(tfcp1->used == TRUE) {
1248 for(j=0;j<pos2->tfcp->len;j++) {
1249 LttvTracefileContextPosition *tfcp2 =
1250 &g_array_index(pos2->tfcp, LttvTracefileContextPosition, j);
1251
1252 if(tfcp1->tfc == tfcp2->tfc) {
1253 if(tfcp2->used == TRUE)
1254 ret = ltt_event_position_compare(tfcp1->event, tfcp2->event);
1255 else
1256 ret = -1;
1257
1258 if(ret != 0) return ret;
1259 }
1260 }
1261
1262 } else {
1263 for(j=0;j<pos2->tfcp->len;j++) {
1264 LttvTracefileContextPosition *tfcp2 =
1265 &g_array_index(pos2->tfcp, LttvTracefileContextPosition, j);
1266
1267 if(tfcp1->tfc == tfcp2->tfc)
1268 if(tfcp2->used == TRUE) ret = 1;
1269 if(ret != 0) return ret;
1270 }
1271 }
1272 }
1273 return 0;
1274 }
1275
1276
1277 LttTime lttv_traceset_context_position_get_time(
1278 const LttvTracesetContextPosition *pos)
1279 {
1280 return pos->timestamp;
1281 }
1282
1283
1284 LttvTracefileContext *lttv_traceset_context_get_current_tfc(LttvTracesetContext *self)
1285 {
1286 GTree *pqueue = self->pqueue;
1287 LttvTracefileContext *tfc = NULL;
1288
1289 g_tree_foreach(pqueue, get_first, &tfc);
1290
1291 return tfc;
1292 }
1293
1294 /* lttv_process_traceset_synchronize_tracefiles
1295 *
1296 * Use the sync_position field of the trace set context to synchronize each
1297 * tracefile with the previously saved position.
1298 *
1299 * If no previous position has been saved, it simply does nothing.
1300 */
1301 void lttv_process_traceset_synchronize_tracefiles(LttvTracesetContext *tsc)
1302 {
1303 g_assert(lttv_process_traceset_seek_position(tsc, tsc->sync_position) == 0);
1304 }
1305
1306
1307
1308
1309 void lttv_process_traceset_get_sync_data(LttvTracesetContext *tsc)
1310 {
1311 lttv_traceset_context_position_save(tsc, tsc->sync_position);
1312 }
1313
1314 struct seek_back_data {
1315 guint first_event; /* Index of the first event in the array : we will always
1316 overwrite at this position : this is a circular array.
1317 */
1318 guint events_found;
1319 guint n; /* number of events requested */
1320 GPtrArray *array; /* array of LttvTracesetContextPositions pointers */
1321 LttvFilter *filter1;
1322 LttvFilter *filter2;
1323 LttvFilter *filter3;
1324 gpointer data;
1325 check_handler *check;
1326 gboolean *stop_flag;
1327 guint raw_event_count;
1328 };
1329
1330 static gint seek_back_event_hook(void *hook_data, void* call_data)
1331 {
1332 struct seek_back_data *sd = (struct seek_back_data*)hook_data;
1333 LttvTracefileContext *tfc = (LttvTracefileContext*)call_data;
1334 LttvTracesetContext *tsc = tfc->t_context->ts_context;
1335 LttvTracesetContextPosition *pos;
1336
1337 if(sd->check && sd->check(sd->raw_event_count, sd->stop_flag, sd->data))
1338 return TRUE;
1339 sd->raw_event_count++;
1340
1341 if(sd->filter1 != NULL && sd->filter1->head != NULL &&
1342 !lttv_filter_tree_parse(sd->filter1->head,
1343 ltt_tracefile_get_event(tfc->tf),
1344 tfc->tf,
1345 tfc->t_context->t,
1346 tfc,NULL,NULL)) {
1347 return FALSE;
1348 }
1349 if(sd->filter2 != NULL && sd->filter2->head != NULL &&
1350 !lttv_filter_tree_parse(sd->filter2->head,
1351 ltt_tracefile_get_event(tfc->tf),
1352 tfc->tf,
1353 tfc->t_context->t,
1354 tfc,NULL,NULL)) {
1355 return FALSE;
1356 }
1357 if(sd->filter3 != NULL && sd->filter3->head != NULL &&
1358 !lttv_filter_tree_parse(sd->filter3->head,
1359 ltt_tracefile_get_event(tfc->tf),
1360 tfc->tf,
1361 tfc->t_context->t,
1362 tfc,NULL,NULL)) {
1363 return FALSE;
1364 }
1365
1366 pos = (LttvTracesetContextPosition*)g_ptr_array_index (sd->array,
1367 sd->first_event);
1368
1369 lttv_traceset_context_position_save(tsc, pos);
1370
1371 if(sd->first_event >= sd->array->len - 1) sd->first_event = 0;
1372 else sd->first_event++;
1373
1374 sd->events_found = min(sd->n, sd->events_found + 1);
1375
1376 return FALSE;
1377 }
1378
1379 /* Seek back n events back from the current position.
1380 *
1381 * Parameters :
1382 * @self The trace set context
1383 * @n number of events to jump over
1384 * @first_offset The initial offset value used.
1385 * never put first_offset at ltt_time_zero.
1386 * @time_seeker Function pointer of the function to use to seek time :
1387 * either lttv_process_traceset_seek_time
1388 * or lttv_state_traceset_seek_time_closest
1389 * @filter The filter to call.
1390 *
1391 * Return value : the number of events found (might be lower than the number
1392 * requested if beginning of traceset is reached).
1393 *
1394 * The first search will go back first_offset and try to find the last n events
1395 * matching the filter. If there are not enough, it will try to go back from the
1396 * new trace point from first_offset*2, and so on, until beginning of trace or n
1397 * events are found.
1398 *
1399 * Note : this function does not take in account the LttvFilter : use the
1400 * similar function found in state.c instead.
1401 *
1402 * Note2 : the caller must make sure that the LttvTracesetContext does not
1403 * contain any hook, as process_traceset_middle is used in this routine.
1404 */
1405 guint lttv_process_traceset_seek_n_backward(LttvTracesetContext *self,
1406 guint n, LttTime first_offset,
1407 seek_time_fct time_seeker,
1408 check_handler *check,
1409 gboolean *stop_flag,
1410 LttvFilter *filter1,
1411 LttvFilter *filter2,
1412 LttvFilter *filter3,
1413 gpointer data)
1414 {
1415 if(lttv_traceset_number(self->ts) == 0) return 0;
1416 g_assert(ltt_time_compare(first_offset, ltt_time_zero) != 0);
1417
1418 guint i;
1419 LttvTracesetContextPosition *next_iter_end_pos =
1420 lttv_traceset_context_position_new(self);
1421 LttvTracesetContextPosition *end_pos =
1422 lttv_traceset_context_position_new(self);
1423 LttvTracesetContextPosition *saved_pos =
1424 lttv_traceset_context_position_new(self);
1425 LttTime time;
1426 LttTime asked_time;
1427 LttTime time_offset;
1428 struct seek_back_data sd;
1429 LttvHooks *hooks = lttv_hooks_new();
1430
1431 sd.first_event = 0;
1432 sd.events_found = 0;
1433 sd.array = g_ptr_array_sized_new(n);
1434 sd.filter1 = filter1;
1435 sd.filter2 = filter2;
1436 sd.filter3 = filter3;
1437 sd.data = data;
1438 sd.n = n;
1439 sd.check = check;
1440 sd.stop_flag = stop_flag;
1441 sd.raw_event_count = 0;
1442 g_ptr_array_set_size(sd.array, n);
1443 for(i=0;i<n;i++) {
1444 g_ptr_array_index (sd.array, i) = lttv_traceset_context_position_new(self);
1445 }
1446
1447 lttv_traceset_context_position_save(self, next_iter_end_pos);
1448 lttv_traceset_context_position_save(self, saved_pos);
1449 /* Get the current time from which we will offset */
1450 time = lttv_traceset_context_position_get_time(next_iter_end_pos);
1451 /* the position saved might be end of traceset... */
1452 if(ltt_time_compare(time, self->time_span.end_time) > 0) {
1453 time = self->time_span.end_time;
1454 }
1455 asked_time = time;
1456 time_offset = first_offset;
1457
1458 lttv_hooks_add(hooks, seek_back_event_hook, &sd, LTTV_PRIO_DEFAULT);
1459
1460 lttv_process_traceset_begin(self, NULL, NULL, NULL, hooks, NULL);
1461
1462 while(1) {
1463 /* stop criteria : - n events found
1464 * - asked_time < beginning of trace */
1465 if(ltt_time_compare(asked_time, self->time_span.start_time) < 0) break;
1466
1467 lttv_traceset_context_position_copy(end_pos, next_iter_end_pos);
1468
1469 /* We must seek the traceset back to time - time_offset */
1470 /* this time becomes the new reference time */
1471 time = ltt_time_sub(time, time_offset);
1472 asked_time = time;
1473
1474 time_seeker(self, time);
1475 lttv_traceset_context_position_save(self, next_iter_end_pos);
1476 /* Resync the time in case of a seek_closest */
1477 time = lttv_traceset_context_position_get_time(next_iter_end_pos);
1478 if(ltt_time_compare(time, self->time_span.end_time) > 0) {
1479 time = self->time_span.end_time;
1480 }
1481
1482 /* Process the traceset, calling a hook which adds events
1483 * to the array, overwriting the tail. It changes first_event and
1484 * events_found too. */
1485 /* We would like to have a clean context here : no other hook than our's */
1486
1487 lttv_process_traceset_middle(self, ltt_time_infinite,
1488 G_MAXUINT, end_pos);
1489
1490 if(sd.events_found < n) {
1491 if(sd.first_event > 0) {
1492 /* Save the first position */
1493 LttvTracesetContextPosition *pos =
1494 (LttvTracesetContextPosition*)g_ptr_array_index (sd.array, 0);
1495 lttv_traceset_context_position_copy(saved_pos, pos);
1496 }
1497 g_assert(n-sd.events_found <= sd.array->len);
1498 /* Change array size to n - events_found */
1499 for(i=n-sd.events_found;i<sd.array->len;i++) {
1500 LttvTracesetContextPosition *pos =
1501 (LttvTracesetContextPosition*)g_ptr_array_index (sd.array, i);
1502 lttv_traceset_context_position_destroy(pos);
1503 }
1504 g_ptr_array_set_size(sd.array, n-sd.events_found);
1505 sd.first_event = 0;
1506
1507 } else break; /* Second end criterion : n events found */
1508
1509 time_offset = ltt_time_mul(time_offset, BACKWARD_SEEK_MUL);
1510 }
1511
1512 lttv_traceset_context_position_destroy(end_pos);
1513 lttv_traceset_context_position_destroy(next_iter_end_pos);
1514
1515 lttv_process_traceset_end(self, NULL, NULL, NULL, hooks, NULL);
1516
1517 if(sd.events_found >= n) {
1518 /* Seek the traceset to the first event in the circular array */
1519 LttvTracesetContextPosition *pos =
1520 (LttvTracesetContextPosition*)g_ptr_array_index (sd.array,
1521 sd.first_event);
1522 g_assert(lttv_process_traceset_seek_position(self, pos) == 0);
1523 } else {
1524 /* Will seek to the last saved position : in the worst case, it will be the
1525 * original position (if events_found is 0) */
1526 g_assert(lttv_process_traceset_seek_position(self, saved_pos) == 0);
1527 }
1528
1529 for(i=0;i<sd.array->len;i++) {
1530 LttvTracesetContextPosition *pos =
1531 (LttvTracesetContextPosition*)g_ptr_array_index (sd.array, i);
1532 lttv_traceset_context_position_destroy(pos);
1533 }
1534 g_ptr_array_free(sd.array, TRUE);
1535
1536 lttv_hooks_destroy(hooks);
1537
1538 lttv_traceset_context_position_destroy(saved_pos);
1539
1540 return sd.events_found;
1541 }
1542
1543
1544 struct seek_forward_data {
1545 guint event_count; /* event counter */
1546 guint n; /* requested number of events to jump over */
1547 LttvFilter *filter1;
1548 LttvFilter *filter2;
1549 LttvFilter *filter3;
1550 gpointer data;
1551 check_handler *check;
1552 gboolean *stop_flag;
1553 guint raw_event_count; /* event counter */
1554 };
1555
1556 static gint seek_forward_event_hook(void *hook_data, void* call_data)
1557 {
1558 struct seek_forward_data *sd = (struct seek_forward_data*)hook_data;
1559 LttvTracefileContext *tfc = (LttvTracefileContext*)call_data;
1560
1561 if(sd->check && sd->check(sd->raw_event_count, sd->stop_flag, sd->data))
1562 return TRUE;
1563 sd->raw_event_count++;
1564
1565 if(sd->filter1 != NULL && sd->filter1->head != NULL &&
1566 !lttv_filter_tree_parse(sd->filter1->head,
1567 ltt_tracefile_get_event(tfc->tf),
1568 tfc->tf,
1569 tfc->t_context->t,
1570 tfc,NULL,NULL)) {
1571 return FALSE;
1572 }
1573 if(sd->filter2 != NULL && sd->filter2->head != NULL &&
1574 !lttv_filter_tree_parse(sd->filter2->head,
1575 ltt_tracefile_get_event(tfc->tf),
1576 tfc->tf,
1577 tfc->t_context->t,
1578 tfc,NULL,NULL)) {
1579 return FALSE;
1580 }
1581 if(sd->filter3 != NULL && sd->filter3->head != NULL &&
1582 !lttv_filter_tree_parse(sd->filter3->head,
1583 ltt_tracefile_get_event(tfc->tf),
1584 tfc->tf,
1585 tfc->t_context->t,
1586 tfc,NULL,NULL)) {
1587 return FALSE;
1588 }
1589
1590 sd->event_count++;
1591 if(sd->event_count >= sd->n)
1592 return TRUE;
1593 }
1594
1595 /* Seek back n events forward from the current position (1 to n)
1596 * 0 is ok too, but it will actually do nothing.
1597 *
1598 * Parameters :
1599 * @self the trace set context
1600 * @n number of events to jump over
1601 * @filter filter to call.
1602 *
1603 * returns : the number of events jumped over (may be less than requested if end
1604 * of traceset reached) */
1605 guint lttv_process_traceset_seek_n_forward(LttvTracesetContext *self,
1606 guint n,
1607 check_handler *check,
1608 gboolean *stop_flag,
1609 LttvFilter *filter1,
1610 LttvFilter *filter2,
1611 LttvFilter *filter3,
1612 gpointer data)
1613 {
1614 struct seek_forward_data sd;
1615 sd.event_count = 0;
1616 sd.n = n;
1617 sd.filter1 = filter1;
1618 sd.filter2 = filter2;
1619 sd.filter3 = filter3;
1620 sd.data = data;
1621 sd.check = check;
1622 sd.stop_flag = stop_flag;
1623 sd.raw_event_count = 0;
1624
1625 if(sd.event_count >= sd.n) return sd.event_count;
1626
1627 LttvHooks *hooks = lttv_hooks_new();
1628
1629 lttv_hooks_add(hooks, seek_forward_event_hook, &sd, LTTV_PRIO_DEFAULT);
1630
1631 lttv_process_traceset_begin(self, NULL, NULL, NULL, hooks, NULL);
1632
1633 /* it will end on the end of traceset, or the fact that the
1634 * hook returns TRUE.
1635 */
1636 lttv_process_traceset_middle(self, ltt_time_infinite,
1637 G_MAXUINT, NULL);
1638
1639 /* Here, our position is either the end of traceset, or the exact position
1640 * after n events : leave it like this. This might be placed on an event that
1641 * will be filtered out, we don't care : all we know is that the following
1642 * event filtered in will be the right one. */
1643
1644 lttv_process_traceset_end(self, NULL, NULL, NULL, hooks, NULL);
1645
1646 lttv_hooks_destroy(hooks);
1647
1648 return sd.event_count;
1649 }
1650
1651
This page took 0.071174 seconds and 4 git commands to generate.