FINALLY find a bug in compare_tracefile : must use ltt_time_compare in the reversed...
[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/tracecontext.h>
25 #include <ltt/event.h>
26 #include <ltt/facility.h>
27 #include <ltt/trace.h>
28 #include <ltt/type.h>
29 #include <errno.h>
30
31
32
33
34 gint compare_tracefile(gconstpointer a, gconstpointer b)
35 {
36 gint comparison = 0;
37
38 const LttvTracefileContext *trace_a = (const LttvTracefileContext *)a;
39 const LttvTracefileContext *trace_b = (const LttvTracefileContext *)b;
40
41 if(likely(trace_a != trace_b)) {
42 comparison = ltt_time_compare(trace_b->timestamp, trace_a->timestamp);
43 if(unlikely(comparison == 0)) {
44 if(trace_a->index < trace_b->index) comparison = -1;
45 else if(trace_a->index > trace_b->index) comparison = 1;
46 else if(trace_a->t_context->index < trace_b->t_context->index)
47 comparison = -1;
48 else if(trace_a->t_context->index > trace_b->t_context->index)
49 comparison = 1;
50 }
51 }
52 return comparison;
53 }
54
55 struct _LttvTracesetContextPosition {
56 GArray *ep; /* Array of LttEventPosition */
57 GArray *tfc; /* Array of corresponding
58 TracefileContext* */
59 LttTime timestamp; /* Current time at the saved position */
60 };
61
62 void lttv_context_init(LttvTracesetContext *self, LttvTraceset *ts)
63 {
64 LTTV_TRACESET_CONTEXT_GET_CLASS(self)->init(self, ts);
65 }
66
67
68 void lttv_context_fini(LttvTracesetContext *self)
69 {
70 LTTV_TRACESET_CONTEXT_GET_CLASS(self)->fini(self);
71 }
72
73
74 LttvTracesetContext *
75 lttv_context_new_traceset_context(LttvTracesetContext *self)
76 {
77 return LTTV_TRACESET_CONTEXT_GET_CLASS(self)->new_traceset_context(self);
78 }
79
80
81
82
83 LttvTraceContext *
84 lttv_context_new_trace_context(LttvTracesetContext *self)
85 {
86 return LTTV_TRACESET_CONTEXT_GET_CLASS(self)->new_trace_context(self);
87 }
88
89
90 LttvTracefileContext *
91 lttv_context_new_tracefile_context(LttvTracesetContext *self)
92 {
93 return LTTV_TRACESET_CONTEXT_GET_CLASS(self)->new_tracefile_context(self);
94 }
95
96 /****************************************************************************
97 * lttv_traceset_context_compute_time_span
98 *
99 * Keep the time span is sync with on the fly addition and removal of traces
100 * in a trace set. It must be called each time a trace is added/removed from
101 * the traceset. It could be more efficient to call it only once a bunch
102 * of traces are loaded, but the calculation is not long, so it's not
103 * critical.
104 *
105 * Author : Xang Xiu Yang
106 ***************************************************************************/
107 static void lttv_traceset_context_compute_time_span(
108 LttvTracesetContext *self,
109 TimeInterval *time_span)
110 {
111 LttvTraceset * traceset = self->ts;
112 int numTraces = lttv_traceset_number(traceset);
113 int i;
114 LttTime s, e;
115 LttvTraceContext *tc;
116 LttTrace * trace;
117
118 time_span->start_time.tv_sec = 0;
119 time_span->start_time.tv_nsec = 0;
120 time_span->end_time.tv_sec = 0;
121 time_span->end_time.tv_nsec = 0;
122
123 for(i=0; i<numTraces;i++){
124 tc = self->traces[i];
125 trace = tc->t;
126
127 ltt_trace_time_span_get(trace, &s, &e);
128
129 if(i==0){
130 time_span->start_time = s;
131 time_span->end_time = e;
132 }else{
133 if(s.tv_sec < time_span->start_time.tv_sec
134 || (s.tv_sec == time_span->start_time.tv_sec
135 && s.tv_nsec < time_span->start_time.tv_nsec))
136 time_span->start_time = s;
137 if(e.tv_sec > time_span->end_time.tv_sec
138 || (e.tv_sec == time_span->end_time.tv_sec
139 && e.tv_nsec > time_span->end_time.tv_nsec))
140 time_span->end_time = e;
141 }
142 }
143 }
144
145 static void init_tracefile_context(LttTracefile *tracefile,
146 LttvTraceContext *tc)
147 {
148 LttvTracefileContext *tfc;
149 LttvTracesetContext *tsc = tc->ts_context;
150
151 tfc = LTTV_TRACESET_CONTEXT_GET_CLASS(tsc)->new_tracefile_context(tsc);
152
153 tfc->index = tc->tracefiles->len;
154 tc->tracefiles = g_array_append_val(tc->tracefiles, tfc);
155
156 tfc->tf = tracefile;
157
158 tfc->t_context = tc;
159 tfc->event = lttv_hooks_new();
160 tfc->event_by_id = lttv_hooks_by_id_new();
161 tfc->a = g_object_new(LTTV_ATTRIBUTE_TYPE, NULL);
162 }
163
164
165 static void
166 init(LttvTracesetContext *self, LttvTraceset *ts)
167 {
168 guint i, j, nb_trace, nb_control, nb_per_cpu, nb_tracefile;
169
170 LttvTraceContext *tc;
171
172 GData **tracefiles_groups;
173
174 struct compute_tracefile_group_args args;
175
176 nb_trace = lttv_traceset_number(ts);
177 self->ts = ts;
178 self->traces = g_new(LttvTraceContext *, nb_trace);
179 self->a = g_object_new(LTTV_ATTRIBUTE_TYPE, NULL);
180 self->ts_a = lttv_traceset_attribute(ts);
181 for(i = 0 ; i < nb_trace ; i++) {
182 tc = LTTV_TRACESET_CONTEXT_GET_CLASS(self)->new_trace_context(self);
183 self->traces[i] = tc;
184
185 tc->ts_context = self;
186 tc->index = i;
187 tc->vt = lttv_traceset_get(ts, i);
188 tc->t = lttv_trace(tc->vt);
189 tc->a = g_object_new(LTTV_ATTRIBUTE_TYPE, NULL);
190 tc->t_a = lttv_trace_attribute(tc->vt);
191 tc->tracefiles = g_array_sized_new(FALSE, TRUE,
192 sizeof(LttvTracefileContext*), 10);
193
194 tracefiles_groups = ltt_trace_get_tracefiles_groups(tc->t);
195
196 args.func = (ForEachTraceFileFunc)init_tracefile_context;
197 args.func_args = tc;
198
199 g_datalist_foreach(tracefiles_groups,
200 (GDataForeachFunc)compute_tracefile_group,
201 &args);
202
203 #if 0
204 nb_control = ltt_trace_control_tracefile_number(tc->t);
205 nb_per_cpu = ltt_trace_per_cpu_tracefile_number(tc->t);
206 nb_tracefile = nb_control + nb_per_cpu;
207 tc->tracefiles = g_new(LttvTracefileContext *, nb_tracefile);
208
209 for(j = 0 ; j < nb_tracefile ; j++) {
210 tfc = LTTV_TRACESET_CONTEXT_GET_CLASS(self)->new_tracefile_context(self);
211 tc->tracefiles[j] = tfc;
212 tfc->index = j;
213
214 if(j < nb_control) {
215 tfc->control = TRUE;
216 tfc->tf = ltt_trace_control_tracefile_get(tc->t, j);
217 }
218 else {
219 tfc->control = FALSE;
220 tfc->tf = ltt_trace_per_cpu_tracefile_get(tc->t, j - nb_control);
221 }
222
223 tfc->t_context = tc;
224 tfc->e = ltt_event_new();
225 tfc->event = lttv_hooks_new();
226 tfc->event_by_id = lttv_hooks_by_id_new();
227 tfc->a = g_object_new(LTTV_ATTRIBUTE_TYPE, NULL);
228 }
229 #endif //0
230
231 }
232 self->pqueue = g_tree_new(compare_tracefile);
233 lttv_process_traceset_seek_time(self, ltt_time_zero);
234 lttv_traceset_context_compute_time_span(self, &self->time_span);
235
236 }
237
238
239 void fini(LttvTracesetContext *self)
240 {
241 guint i, j, nb_trace, nb_tracefile;
242
243 LttvTraceContext *tc;
244
245 LttvTracefileContext *tfc;
246
247 LttvTraceset *ts = self->ts;
248
249 //FIXME : segfault
250
251 g_tree_destroy(self->pqueue);
252 g_object_unref(self->a);
253
254 nb_trace = lttv_traceset_number(ts);
255
256 for(i = 0 ; i < nb_trace ; i++) {
257 tc = self->traces[i];
258
259 g_object_unref(tc->a);
260
261 nb_tracefile = tc->tracefiles->len;
262
263 for(j = 0 ; j < nb_tracefile ; j++) {
264 tfc = g_array_index(tc->tracefiles, LttvTracefileContext*, j);
265 lttv_hooks_destroy(tfc->event);
266 lttv_hooks_by_id_destroy(tfc->event_by_id);
267 g_object_unref(tfc->a);
268 g_object_unref(tfc);
269 }
270 g_array_free(tc->tracefiles, TRUE);
271 g_object_unref(tc);
272 }
273 g_free(self->traces);
274 }
275
276
277 void lttv_traceset_context_add_hooks(LttvTracesetContext *self,
278 LttvHooks *before_traceset,
279 LttvHooks *before_trace,
280 LttvHooks *before_tracefile,
281 LttvHooks *event,
282 LttvHooksById *event_by_id)
283 {
284 LttvTraceset *ts = self->ts;
285
286 guint i, nb_trace;
287
288 LttvTraceContext *tc;
289
290 lttv_hooks_call(before_traceset, self);
291
292 nb_trace = lttv_traceset_number(ts);
293
294 for(i = 0 ; i < nb_trace ; i++) {
295 tc = self->traces[i];
296 lttv_trace_context_add_hooks(tc,
297 before_trace,
298 before_tracefile,
299 event,
300 event_by_id);
301 }
302 }
303
304
305 void lttv_traceset_context_remove_hooks(LttvTracesetContext *self,
306 LttvHooks *after_traceset,
307 LttvHooks *after_trace,
308 LttvHooks *after_tracefile,
309 LttvHooks *event,
310 LttvHooksById *event_by_id)
311 {
312
313 LttvTraceset *ts = self->ts;
314
315 guint i, nb_trace;
316
317 LttvTraceContext *tc;
318
319 nb_trace = lttv_traceset_number(ts);
320
321 for(i = 0 ; i < nb_trace ; i++) {
322 tc = self->traces[i];
323 lttv_trace_context_remove_hooks(tc,
324 after_trace,
325 after_tracefile,
326 event,
327 event_by_id);
328 }
329
330 lttv_hooks_call(after_traceset, self);
331
332
333 }
334
335 void lttv_trace_context_add_hooks(LttvTraceContext *self,
336 LttvHooks *before_trace,
337 LttvHooks *before_tracefile,
338 LttvHooks *event,
339 LttvHooksById *event_by_id)
340 {
341 guint i, nb_tracefile;
342
343 LttvTracefileContext *tfc;
344
345 lttv_hooks_call(before_trace, self);
346
347 nb_tracefile = self->tracefiles->len;
348
349 for(i = 0 ; i < nb_tracefile ; i++) {
350 tfc = g_array_index(self->tracefiles, LttvTracefileContext*, i);
351 lttv_tracefile_context_add_hooks(tfc,
352 before_tracefile,
353 event,
354 event_by_id);
355 }
356 }
357
358
359
360 void lttv_trace_context_remove_hooks(LttvTraceContext *self,
361 LttvHooks *after_trace,
362 LttvHooks *after_tracefile,
363 LttvHooks *event,
364 LttvHooksById *event_by_id)
365 {
366 guint i, nb_tracefile;
367
368 LttvTracefileContext *tfc;
369
370 nb_tracefile = self->tracefiles->len;
371
372 for(i = 0 ; i < nb_tracefile ; i++) {
373 tfc = g_array_index(self->tracefiles, LttvTracefileContext*, i);
374 lttv_tracefile_context_remove_hooks(tfc,
375 after_tracefile,
376 event,
377 event_by_id);
378 }
379
380 lttv_hooks_call(after_trace, self);
381 }
382
383 void lttv_tracefile_context_add_hooks(LttvTracefileContext *self,
384 LttvHooks *before_tracefile,
385 LttvHooks *event,
386 LttvHooksById *event_by_id)
387 {
388 guint i, index;
389
390 LttvHooks *hook;
391
392 lttv_hooks_call(before_tracefile, self);
393 lttv_hooks_add_list(self->event, event);
394 if(event_by_id != NULL) {
395 for(i = 0; i < event_by_id->array->len; i++) {
396 index = g_array_index(event_by_id->array, guint, i);
397 hook = lttv_hooks_by_id_find(self->event_by_id, index);
398 lttv_hooks_add_list(hook, lttv_hooks_by_id_get(event_by_id, index));
399 }
400 }
401 }
402
403 void lttv_tracefile_context_remove_hooks(LttvTracefileContext *self,
404 LttvHooks *after_tracefile,
405 LttvHooks *event,
406 LttvHooksById *event_by_id)
407 {
408 guint i, index;
409
410 LttvHooks *hook;
411
412 lttv_hooks_remove_list(self->event, event);
413 if(event_by_id != NULL) {
414 for(i = 0; i < event_by_id->array->len; i++) {
415 index = g_array_index(event_by_id->array, guint, i);
416 hook = lttv_hooks_by_id_get(self->event_by_id, index);
417 if(hook != NULL)
418 lttv_hooks_remove_list(hook, lttv_hooks_by_id_get(event_by_id, index));
419 }
420 }
421
422 lttv_hooks_call(after_tracefile, self);
423 }
424
425
426
427 void lttv_tracefile_context_add_hooks_by_id(LttvTracefileContext *tfc,
428 unsigned i,
429 LttvHooks *event_by_id)
430 {
431 LttvHooks * h;
432 h = lttv_hooks_by_id_find(tfc->event_by_id, i);
433 lttv_hooks_add_list(h, event_by_id);
434 }
435
436 void lttv_tracefile_context_remove_hooks_by_id(LttvTracefileContext *tfc,
437 unsigned i)
438 {
439 lttv_hooks_by_id_remove(tfc->event_by_id, i);
440 }
441
442 static LttvTracesetContext *
443 new_traceset_context(LttvTracesetContext *self)
444 {
445 return g_object_new(LTTV_TRACESET_CONTEXT_TYPE, NULL);
446 }
447
448
449 static LttvTraceContext *
450 new_trace_context(LttvTracesetContext *self)
451 {
452 return g_object_new(LTTV_TRACE_CONTEXT_TYPE, NULL);
453 }
454
455
456 static LttvTracefileContext *
457 new_tracefile_context(LttvTracesetContext *self)
458 {
459 return g_object_new(LTTV_TRACEFILE_CONTEXT_TYPE, NULL);
460 }
461
462
463 static void
464 traceset_context_instance_init (GTypeInstance *instance, gpointer g_class)
465 {
466 /* Be careful of anything which would not work well with shallow copies */
467 }
468
469
470 static void
471 traceset_context_finalize (LttvTracesetContext *self)
472 {
473 G_OBJECT_CLASS(g_type_class_peek(g_type_parent(LTTV_TRACESET_CONTEXT_TYPE)))
474 ->finalize(G_OBJECT(self));
475 }
476
477
478 static void
479 traceset_context_class_init (LttvTracesetContextClass *klass)
480 {
481 GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
482
483 gobject_class->finalize = (void (*)(GObject *self))traceset_context_finalize;
484 klass->init = init;
485 klass->fini = fini;
486 klass->new_traceset_context = new_traceset_context;
487 klass->new_trace_context = new_trace_context;
488 klass->new_tracefile_context = new_tracefile_context;
489 }
490
491
492 GType
493 lttv_traceset_context_get_type(void)
494 {
495 static GType type = 0;
496 if (type == 0) {
497 static const GTypeInfo info = {
498 sizeof (LttvTracesetContextClass),
499 NULL, /* base_init */
500 NULL, /* base_finalize */
501 (GClassInitFunc) traceset_context_class_init, /* class_init */
502 NULL, /* class_finalize */
503 NULL, /* class_data */
504 sizeof (LttvTracesetContext),
505 0, /* n_preallocs */
506 (GInstanceInitFunc) traceset_context_instance_init, /* instance_init */
507 NULL /* Value handling */
508 };
509
510 type = g_type_register_static (G_TYPE_OBJECT, "LttvTracesetContextType",
511 &info, 0);
512 }
513 return type;
514 }
515
516
517 static void
518 trace_context_instance_init (GTypeInstance *instance, gpointer g_class)
519 {
520 /* Be careful of anything which would not work well with shallow copies */
521 }
522
523
524 static void
525 trace_context_finalize (LttvTraceContext *self)
526 {
527 G_OBJECT_CLASS(g_type_class_peek(g_type_parent(LTTV_TRACE_CONTEXT_TYPE)))->
528 finalize(G_OBJECT(self));
529 }
530
531
532 static void
533 trace_context_class_init (LttvTraceContextClass *klass)
534 {
535 GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
536
537 gobject_class->finalize = (void (*)(GObject *self)) trace_context_finalize;
538 }
539
540
541 GType
542 lttv_trace_context_get_type(void)
543 {
544 static GType type = 0;
545 if (type == 0) {
546 static const GTypeInfo info = {
547 sizeof (LttvTraceContextClass),
548 NULL, /* base_init */
549 NULL, /* base_finalize */
550 (GClassInitFunc) trace_context_class_init, /* class_init */
551 NULL, /* class_finalize */
552 NULL, /* class_data */
553 sizeof (LttvTraceContext),
554 0, /* n_preallocs */
555 (GInstanceInitFunc) trace_context_instance_init, /* instance_init */
556 NULL /* Value handling */
557 };
558
559 type = g_type_register_static (G_TYPE_OBJECT, "LttvTraceContextType",
560 &info, 0);
561 }
562 return type;
563 }
564
565
566 static void
567 tracefile_context_instance_init (GTypeInstance *instance, gpointer g_class)
568 {
569 /* Be careful of anything which would not work well with shallow copies */
570 }
571
572
573 static void
574 tracefile_context_finalize (LttvTracefileContext *self)
575 {
576 G_OBJECT_CLASS(g_type_class_peek(g_type_parent(LTTV_TRACEFILE_CONTEXT_TYPE)))
577 ->finalize(G_OBJECT(self));
578 }
579
580
581 static void
582 tracefile_context_class_init (LttvTracefileContextClass *klass)
583 {
584 GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
585
586 gobject_class->finalize = (void (*)(GObject *self))tracefile_context_finalize;
587 }
588
589
590 GType
591 lttv_tracefile_context_get_type(void)
592 {
593 static GType type = 0;
594 if (type == 0) {
595 static const GTypeInfo info = {
596 sizeof (LttvTracefileContextClass),
597 NULL, /* base_init */
598 NULL, /* base_finalize */
599 (GClassInitFunc) tracefile_context_class_init, /* class_init */
600 NULL, /* class_finalize */
601 NULL, /* class_data */
602 sizeof (LttvTracefileContext),
603 0, /* n_preallocs */
604 (GInstanceInitFunc) tracefile_context_instance_init, /* instance_init */
605 NULL /* Value handling */
606 };
607
608 type = g_type_register_static (G_TYPE_OBJECT, "LttvTracefileContextType",
609 &info, 0);
610 }
611 return type;
612 }
613
614
615
616 static gboolean get_first(gpointer key, gpointer value, gpointer user_data) {
617 g_assert(key == value);
618 *((LttvTracefileContext **)user_data) = (LttvTracefileContext *)value;
619 return TRUE;
620 }
621
622 static gboolean test_tree(gpointer key, gpointer value, gpointer user_data) {
623
624 LttvTracefileContext *tfc = (LttvTracefileContext *)key;
625
626 g_debug("Tracefile name %s, time %lu.%lu, tfi %u, ti %u",
627 g_quark_to_string(ltt_tracefile_name(tfc->tf)),
628 tfc->timestamp.tv_sec, tfc->timestamp.tv_nsec,
629 tfc->index, tfc->t_context->index);
630
631 if(((LttvTracefileContext *)user_data) == (LttvTracefileContext *)value) {
632 g_assert(compare_tracefile(user_data, value) == 0);
633 } else
634 g_assert(compare_tracefile(user_data, value) != 0);
635
636 //g_assert(((LttvTracefileContext *)user_data) != (LttvTracefileContext *)value);
637 return FALSE;
638 }
639
640
641
642 void lttv_process_traceset_begin(LttvTracesetContext *self,
643 LttvHooks *before_traceset,
644 LttvHooks *before_trace,
645 LttvHooks *before_tracefile,
646 LttvHooks *event,
647 LttvHooksById *event_by_id)
648 {
649
650 /* simply add hooks in context. _before hooks are called by add_hooks. */
651 /* It calls all before_traceset, before_trace, and before_tracefile hooks. */
652 lttv_traceset_context_add_hooks(self,
653 before_traceset,
654 before_trace,
655 before_tracefile,
656 event,
657 event_by_id);
658
659 }
660
661 /* Note : a _middle must be preceded from a _seek or another middle */
662 guint lttv_process_traceset_middle(LttvTracesetContext *self,
663 LttTime end,
664 guint nb_events,
665 const LttvTracesetContextPosition *end_position)
666 {
667 GTree *pqueue = self->pqueue;
668
669 guint fac_id, ev_id, id;
670
671 LttvTracefileContext *tfc;
672
673 LttEvent *e;
674
675 unsigned count = 0;
676
677 guint read_ret = FALSE;
678
679 gboolean last_ret = FALSE; /* return value of the last hook list called */
680
681 /* Get the next event from the pqueue, call its hooks,
682 reinsert in the pqueue the following event from the same tracefile
683 unless the tracefile is finished or the event is later than the
684 end time. */
685
686 while(TRUE) {
687 tfc = NULL;
688 g_tree_foreach(pqueue, get_first, &tfc);
689 /* End of traceset : tfc is NULL */
690 if(unlikely(tfc == NULL))
691 {
692 return count;
693 }
694
695 /* Have we reached :
696 * - the maximum number of events specified?
697 * - the end position ?
698 * - the end time ?
699 * then the read is finished. We leave the queue in the same state and
700 * break the loop.
701 */
702
703 if(unlikely(last_ret == TRUE ||
704 ((count >= nb_events) && (nb_events != G_MAXULONG)) ||
705 (end_position!=NULL&&lttv_traceset_context_ctx_pos_compare(self,
706 end_position) == 0)||
707 ltt_time_compare(end, tfc->timestamp) <= 0))
708 {
709 return count;
710 }
711
712 /* Get the tracefile with an event for the smallest time found. If two
713 or more tracefiles have events for the same time, hope that lookup
714 and remove are consistent. */
715
716 g_debug("test tree before remove");
717 g_tree_foreach(pqueue, test_tree, tfc);
718
719 g_tree_remove(pqueue, tfc);
720
721 g_debug("test tree after remove");
722 g_tree_foreach(pqueue, test_tree, tfc);
723
724 count++;
725
726 e = ltt_tracefile_get_event(tfc->tf);
727 fac_id = ltt_event_facility_id(e);
728 ev_id = ltt_event_eventtype_id(e);
729 id = GET_HOOK_ID(fac_id, ev_id);
730 last_ret = lttv_hooks_call_merge(tfc->event, tfc,
731 lttv_hooks_by_id_get(tfc->event_by_id, id), tfc);
732
733 read_ret = ltt_tracefile_read(tfc->tf);
734
735 if(likely(!read_ret)) {
736 g_debug("An event is ready");
737 tfc->timestamp = ltt_event_time(e);
738
739 g_tree_insert(pqueue, tfc, tfc);
740 } else {
741 if(read_ret == ERANGE)
742 g_debug("End of trace");
743 else
744 g_error("Error happened in lttv_process_traceset_middle");
745 }
746 }
747 }
748
749
750 void lttv_process_traceset_end(LttvTracesetContext *self,
751 LttvHooks *after_traceset,
752 LttvHooks *after_trace,
753 LttvHooks *after_tracefile,
754 LttvHooks *event,
755 LttvHooksById *event_by_id)
756 {
757 /* Remove hooks from context. _after hooks are called by remove_hooks. */
758 /* It calls all after_traceset, after_trace, and after_tracefile hooks. */
759 lttv_traceset_context_remove_hooks(self,
760 after_traceset,
761 after_trace,
762 after_tracefile,
763 event,
764 event_by_id);
765 }
766
767 /* Subtile modification :
768 * if tracefile has no event at or after the time requested, it is not put in
769 * the queue, as the next read would fail. */
770 void lttv_process_trace_seek_time(LttvTraceContext *self, LttTime start)
771 {
772 guint i, nb_tracefile;
773
774 gint ret;
775
776 LttvTracefileContext *tfc;
777
778 GTree *pqueue = self->ts_context->pqueue;
779
780 nb_tracefile = self->tracefiles->len;
781
782 for(i = 0 ; i < nb_tracefile ; i++) {
783 tfc = g_array_index(self->tracefiles, LttvTracefileContext*, i);
784
785 g_tree_remove(pqueue, tfc);
786
787 ret = ltt_tracefile_seek_time(tfc->tf, start);
788 if(ret == EPERM) g_error("error in lttv_process_trace_seek_time seek");
789
790 if(ret == 0) { /* not ERANGE especially */
791 tfc->timestamp = ltt_event_time(ltt_tracefile_get_event(tfc->tf));
792 g_tree_insert(pqueue, tfc, tfc);
793 }
794 }
795 }
796
797
798 void lttv_process_traceset_seek_time(LttvTracesetContext *self, LttTime start)
799 {
800 guint i, nb_trace;
801
802 LttvTraceContext *tc;
803
804 nb_trace = lttv_traceset_number(self->ts);
805 for(i = 0 ; i < nb_trace ; i++) {
806 tc = self->traces[i];
807 lttv_process_trace_seek_time(tc, start);
808 }
809 }
810
811
812 gboolean lttv_process_traceset_seek_position(LttvTracesetContext *self,
813 const LttvTracesetContextPosition *pos)
814 {
815 guint i;
816 LttvTraceContext *tc;
817 LttvTracefileContext *tfc;
818
819 g_tree_destroy(self->pqueue);
820 self->pqueue = g_tree_new(compare_tracefile);
821
822 for(i=0;i<pos->ep->len; i++) {
823 LttEventPosition *ep = g_array_index(pos->ep, LttEventPosition*, i);
824 LttvTracefileContext *tfc =
825 g_array_index(pos->tfc, LttvTracefileContext*, i);
826 g_assert(ltt_tracefile_seek_position(tfc->tf, ep) == 0);
827 tfc->timestamp = ltt_event_time(ltt_tracefile_get_event(tfc->tf));
828 g_tree_insert(self->pqueue, tfc, tfc);
829 }
830 return TRUE;
831 }
832
833
834
835 static LttField *
836 find_field(LttEventType *et, const GQuark field)
837 {
838 LttType *t;
839
840 LttField *f;
841
842 guint i, nb;
843
844 GQuark name;
845
846 /* Field is unset */
847 if(field == 0) return NULL;
848
849 f = ltt_eventtype_field(et);
850 t = ltt_eventtype_type(et);
851 g_assert(ltt_type_class(t) == LTT_STRUCT);
852 nb = ltt_type_member_number(t);
853 for(i = 0 ; i < nb ; i++) {
854 ltt_type_member_type(t, i, &name);
855 if(name == field) break;
856 }
857 g_assert(i < nb);
858 return ltt_field_member(f, i);
859 }
860
861 LttvTraceHookByFacility *lttv_trace_hook_get_fac(LttvTraceHook *th,
862 guint facility_id)
863 {
864 return &g_array_index(th->fac_index, LttvTraceHookByFacility, facility_id);
865 }
866
867 /* Get the first facility corresponding to the name. As the types must be
868 * compatible, it is relevant to use the field name and sizes of the first
869 * facility to create data structures and assume the data will be compatible
870 * thorough the trace */
871 LttvTraceHookByFacility *lttv_trace_hook_get_first(LttvTraceHook *th)
872 {
873 g_assert(th->fac_list->len > 0);
874 return g_array_index(th->fac_list, LttvTraceHookByFacility*, 0);
875 }
876
877
878 /* Returns 0 on success, -1 if fails. */
879 gint
880 lttv_trace_find_hook(LttTrace *t, GQuark facility, GQuark event,
881 GQuark field1, GQuark field2, GQuark field3, LttvHook h, LttvTraceHook *th)
882 {
883 LttFacility *f;
884
885 LttEventType *et, *first_et;
886
887 GArray *facilities;
888
889 guint i, fac_id, ev_id;
890
891 LttvTraceHookByFacility *thf, *first_thf;
892
893 facilities = ltt_trace_facility_get_by_name(t, facility);
894
895 if(unlikely(facilities == NULL)) goto facility_error;
896
897 th->fac_index = g_array_sized_new(FALSE, TRUE,
898 sizeof(LttvTraceHookByFacility),
899 NUM_FACILITIES);
900 th->fac_index = g_array_set_size(th->fac_index, NUM_FACILITIES);
901
902 th->fac_list = g_array_sized_new(FALSE, TRUE,
903 sizeof(LttvTraceHookByFacility*),
904 facilities->len);
905 th->fac_list = g_array_set_size(th->fac_list, facilities->len);
906
907 fac_id = g_array_index(facilities, guint, 0);
908 f = ltt_trace_get_facility_by_num(t, fac_id);
909
910 et = ltt_facility_eventtype_get_by_name(f, event);
911 if(unlikely(et == NULL)) goto event_error;
912
913 thf = &g_array_index(th->fac_index, LttvTraceHookByFacility, fac_id);
914 g_array_index(th->fac_list, LttvTraceHookByFacility*, 0)
915 = thf;
916
917 ev_id = ltt_eventtype_id(et);
918
919 thf->h = h;
920 thf->id = GET_HOOK_ID(fac_id, ev_id);
921 thf->f1 = find_field(et, field1);
922 thf->f2 = find_field(et, field2);
923 thf->f3 = find_field(et, field3);
924
925 first_thf = thf;
926
927 /* Check for type compatibility too */
928 for(i=1;i<facilities->len;i++) {
929 fac_id = g_array_index(facilities, guint, i);
930 f = ltt_trace_get_facility_by_num(t, fac_id);
931
932 et = ltt_facility_eventtype_get_by_name(f, ltt_eventtype_name(et));
933 if(unlikely(et == NULL)) goto event_error;
934
935 thf = &g_array_index(th->fac_index, LttvTraceHookByFacility, fac_id);
936 g_array_index(th->fac_list, LttvTraceHookByFacility*, i)
937 = thf;
938 ev_id = ltt_eventtype_id(et);
939 thf->h = h;
940 thf->id = GET_HOOK_ID(fac_id, ev_id);
941 thf->f1 = find_field(et, field1);
942 if(check_fields_compatibility(first_et, et,
943 first_thf->f1, thf->f1))
944 goto type_error;
945
946 thf->f2 = find_field(et, field2);
947 if(check_fields_compatibility(first_et, et,
948 first_thf->f2, thf->f2))
949 goto type_error;
950
951 thf->f3 = find_field(et, field3);
952 if(check_fields_compatibility(first_et, et,
953 first_thf->f3, thf->f3))
954 goto type_error;
955 }
956
957 return 0;
958
959 type_error:
960 goto free;
961 event_error:
962 g_error("Event type %s does not exist",
963 g_quark_to_string(ltt_eventtype_name(et)));
964 goto free;
965 facility_error:
966 g_error("No %s facility", g_quark_to_string(facility));
967 goto free;
968 free:
969 g_array_free(th->fac_index, TRUE);
970 g_array_free(th->fac_list, TRUE);
971 th->fac_index = NULL;
972 th->fac_list = NULL;
973 return -1;
974 }
975
976 void lttv_trace_hook_destroy(LttvTraceHook *th)
977 {
978 g_array_free(th->fac_index, TRUE);
979 g_array_free(th->fac_list, TRUE);
980 }
981
982
983 LttvTracesetContextPosition *lttv_traceset_context_position_new()
984 {
985 LttvTracesetContextPosition *pos = g_new(LttvTracesetContextPosition,1);
986 pos->ep = g_array_sized_new(FALSE, TRUE, sizeof(LttEventPosition*),
987 10);
988 pos->tfc = g_array_sized_new(FALSE, TRUE, sizeof(LttvTracefileContext*),
989 10);
990 pos->timestamp = ltt_time_infinite;
991 return pos;
992 }
993
994 gboolean traverse_get_tfc(gpointer key, gpointer value, gpointer data)
995 {
996 LttvTracefileContext *tfc = (LttvTracefileContext *)value;
997 LttvTracesetContextPosition *pos = (LttvTracesetContextPosition *)data;
998
999 LttEvent *event = ltt_tracefile_get_event(tfc->tf);
1000 LttEventPosition *ep = ltt_event_position_new();
1001
1002 ltt_event_position(event, ep);
1003
1004 g_array_append_val(pos->ep, ep);
1005 g_array_append_val(pos->tfc, tfc);
1006
1007 if(ltt_time_compare(tfc->timestamp, pos->timestamp) < 0)
1008 pos->timestamp = tfc->timestamp;
1009
1010 return 0;
1011 }
1012
1013 /* Subtile modification :
1014 * only save the tracefiles that are loaded in the pqueue */
1015 void lttv_traceset_context_position_save(const LttvTracesetContext *self,
1016 LttvTracesetContextPosition *pos)
1017 {
1018 g_tree_foreach(self->pqueue, traverse_get_tfc, pos);
1019 }
1020
1021 void lttv_traceset_context_position_destroy(LttvTracesetContextPosition *pos)
1022 {
1023 int i;
1024 for(i=0;i<pos->ep->len;i++)
1025 g_free(g_array_index(pos->ep, LttEventPosition*, i));
1026 g_array_free(pos->ep, TRUE);
1027 g_array_free(pos->tfc, TRUE);
1028 g_free(pos);
1029 }
1030
1031 void lttv_traceset_context_position_copy(LttvTracesetContextPosition *dest,
1032 const LttvTracesetContextPosition *src)
1033 {
1034 int i;
1035
1036 g_array_set_size(dest->ep, src->ep->len);
1037 g_array_set_size(dest->tfc, src->tfc->len);
1038
1039 for(i=0;i<src->ep->len;i++) {
1040 g_array_index(dest->ep, LttEventPosition*, i) = ltt_event_position_new();
1041 ltt_event_position_copy(
1042 g_array_index(dest->ep, LttEventPosition*, i),
1043 g_array_index(src->ep, LttEventPosition*, i));
1044 }
1045 for(i=0;i<src->tfc->len;i++) {
1046 g_array_index(dest->tfc, LttvTracefileContext*, i) =
1047 g_array_index(src->tfc, LttvTracefileContext*, i);
1048 }
1049 dest->timestamp = src->timestamp;
1050 }
1051
1052 gint lttv_traceset_context_ctx_pos_compare(const LttvTracesetContext *self,
1053 const LttvTracesetContextPosition *pos)
1054 {
1055 int i;
1056 int ret;
1057
1058 for(i=0;i<pos->ep->len;i++) {
1059 LttEventPosition *ep = g_array_index(pos->ep, LttEventPosition*, i);
1060 LttvTracefileContext *tfc =
1061 g_array_index(pos->tfc, LttvTracefileContext*, i);
1062
1063 LttEvent *event = ltt_tracefile_get_event(tfc->tf);
1064
1065 ret = ltt_event_position_compare((LttEventPosition*)event,
1066 ep);
1067 if(ret != 0) return ret;
1068
1069 }
1070 return 0;
1071 }
1072
1073
1074 gint lttv_traceset_context_pos_pos_compare(
1075 const LttvTracesetContextPosition *pos1,
1076 const LttvTracesetContextPosition *pos2)
1077 {
1078 int i, j;
1079 int ret;
1080
1081 for(i=0;i<pos1->ep->len;i++) {
1082 LttEventPosition *ep1 = g_array_index(pos1->ep, LttEventPosition*, i);
1083 LttTracefile *tf1 = ltt_event_position_tracefile(ep1);
1084
1085 for(j=0;j<pos2->ep->len;j++) {
1086 LttEventPosition *ep2 = g_array_index(pos2->ep, LttEventPosition*, j);
1087 LttTracefile *tf2 = ltt_event_position_tracefile(ep2);
1088
1089 if(tf1 == tf2) {
1090 ret = ltt_event_position_compare(ep1, ep2);
1091 if(ret != 0) return ret;
1092 }
1093 }
1094 }
1095 return 0;
1096
1097 }
1098
1099
1100 LttTime lttv_traceset_context_position_get_time(
1101 const LttvTracesetContextPosition *pos)
1102 {
1103 return pos->timestamp;
1104 }
1105
1106
1107 LttvTracefileContext *lttv_traceset_context_get_current_tfc(LttvTracesetContext *self)
1108 {
1109 GTree *pqueue = self->pqueue;
1110 LttvTracefileContext *tfc = NULL;
1111
1112 g_tree_foreach(pqueue, get_first, &tfc);
1113
1114 return tfc;
1115 }
This page took 0.054721 seconds and 4 git commands to generate.