1 /* This file is part of the Linux Trace Toolkit viewer
2 * Copyright (C) 2003-2004 Michel Dagenais
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License Version 2 as
6 * published by the Free Software Foundation;
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
24 #include <lttv/tracecontext.h>
25 #include <ltt/event.h>
26 #include <ltt/facility.h>
27 #include <ltt/trace.h>
34 gint
compare_tracefile(gconstpointer a
, gconstpointer b
)
38 const LttvTracefileContext
*trace_a
= (const LttvTracefileContext
*)a
;
39 const LttvTracefileContext
*trace_b
= (const LttvTracefileContext
*)b
;
41 if(likely(trace_a
!= trace_b
)) {
42 comparison
= ltt_time_compare(trace_a
->timestamp
, trace_b
->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
)
48 else if(trace_a
->t_context
->index
> trace_b
->t_context
->index
)
55 struct _LttvTracesetContextPosition
{
56 GArray
*ep
; /* Array of LttEventPosition */
57 GArray
*tfc
; /* Array of corresponding
59 LttTime timestamp
; /* Current time at the saved position */
62 void lttv_context_init(LttvTracesetContext
*self
, LttvTraceset
*ts
)
64 LTTV_TRACESET_CONTEXT_GET_CLASS(self
)->init(self
, ts
);
68 void lttv_context_fini(LttvTracesetContext
*self
)
70 LTTV_TRACESET_CONTEXT_GET_CLASS(self
)->fini(self
);
75 lttv_context_new_traceset_context(LttvTracesetContext
*self
)
77 return LTTV_TRACESET_CONTEXT_GET_CLASS(self
)->new_traceset_context(self
);
84 lttv_context_new_trace_context(LttvTracesetContext
*self
)
86 return LTTV_TRACESET_CONTEXT_GET_CLASS(self
)->new_trace_context(self
);
90 LttvTracefileContext
*
91 lttv_context_new_tracefile_context(LttvTracesetContext
*self
)
93 return LTTV_TRACESET_CONTEXT_GET_CLASS(self
)->new_tracefile_context(self
);
96 /****************************************************************************
97 * lttv_traceset_context_compute_time_span
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
105 * Author : Xang Xiu Yang
106 ***************************************************************************/
107 static void lttv_traceset_context_compute_time_span(
108 LttvTracesetContext
*self
,
109 TimeInterval
*time_span
)
111 LttvTraceset
* traceset
= self
->ts
;
112 int numTraces
= lttv_traceset_number(traceset
);
115 LttvTraceContext
*tc
;
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;
123 for(i
=0; i
<numTraces
;i
++){
124 tc
= self
->traces
[i
];
127 ltt_trace_time_span_get(trace
, &s
, &e
);
130 time_span
->start_time
= s
;
131 time_span
->end_time
= e
;
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
;
145 static void init_tracefile_context(LttTracefile
*tracefile
,
146 LttvTraceContext
*tc
)
148 LttvTracefileContext
*tfc
;
149 LttvTracesetContext
*tsc
= tc
->ts_context
;
151 tfc
= LTTV_TRACESET_CONTEXT_GET_CLASS(tsc
)->new_tracefile_context(tsc
);
153 tfc
->index
= tc
->tracefiles
->len
;
154 tc
->tracefiles
= g_array_append_val(tc
->tracefiles
, tfc
);
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
);
166 init(LttvTracesetContext
*self
, LttvTraceset
*ts
)
170 LttvTraceContext
*tc
;
172 GData
**tracefiles_groups
;
174 struct compute_tracefile_group_args args
;
176 nb_trace
= lttv_traceset_number(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 // self->sync_position = lttv_traceset_context_position_new();
182 for(i
= 0 ; i
< nb_trace
; i
++) {
183 tc
= LTTV_TRACESET_CONTEXT_GET_CLASS(self
)->new_trace_context(self
);
184 self
->traces
[i
] = tc
;
186 tc
->ts_context
= self
;
188 tc
->vt
= lttv_traceset_get(ts
, i
);
189 tc
->t
= lttv_trace(tc
->vt
);
190 tc
->a
= g_object_new(LTTV_ATTRIBUTE_TYPE
, NULL
);
191 tc
->t_a
= lttv_trace_attribute(tc
->vt
);
192 tc
->tracefiles
= g_array_sized_new(FALSE
, TRUE
,
193 sizeof(LttvTracefileContext
*), 10);
195 tracefiles_groups
= ltt_trace_get_tracefiles_groups(tc
->t
);
196 if(tracefiles_groups
!= NULL
) {
197 args
.func
= (ForEachTraceFileFunc
)init_tracefile_context
;
200 g_datalist_foreach(tracefiles_groups
,
201 (GDataForeachFunc
)compute_tracefile_group
,
206 nb_control
= ltt_trace_control_tracefile_number(tc
->t
);
207 nb_per_cpu
= ltt_trace_per_cpu_tracefile_number(tc
->t
);
208 nb_tracefile
= nb_control
+ nb_per_cpu
;
209 tc
->tracefiles
= g_new(LttvTracefileContext
*, nb_tracefile
);
211 for(j
= 0 ; j
< nb_tracefile
; j
++) {
212 tfc
= LTTV_TRACESET_CONTEXT_GET_CLASS(self
)->new_tracefile_context(self
);
213 tc
->tracefiles
[j
] = tfc
;
218 tfc
->tf
= ltt_trace_control_tracefile_get(tc
->t
, j
);
221 tfc
->control
= FALSE
;
222 tfc
->tf
= ltt_trace_per_cpu_tracefile_get(tc
->t
, j
- nb_control
);
226 tfc
->e
= ltt_event_new();
227 tfc
->event
= lttv_hooks_new();
228 tfc
->event_by_id
= lttv_hooks_by_id_new();
229 tfc
->a
= g_object_new(LTTV_ATTRIBUTE_TYPE
, NULL
);
234 self
->pqueue
= g_tree_new(compare_tracefile
);
235 lttv_process_traceset_seek_time(self
, ltt_time_zero
);
236 lttv_traceset_context_compute_time_span(self
, &self
->time_span
);
241 void fini(LttvTracesetContext
*self
)
243 guint i
, j
, nb_trace
, nb_tracefile
;
245 LttvTraceContext
*tc
;
247 LttvTracefileContext
**tfc
;
249 LttvTraceset
*ts
= self
->ts
;
251 g_tree_destroy(self
->pqueue
);
252 g_object_unref(self
->a
);
253 // lttv_traceset_context_position_destroy(self->sync_position);
255 nb_trace
= lttv_traceset_number(ts
);
257 for(i
= 0 ; i
< nb_trace
; i
++) {
258 tc
= self
->traces
[i
];
260 g_object_unref(tc
->a
);
262 nb_tracefile
= tc
->tracefiles
->len
;
264 for(j
= 0 ; j
< nb_tracefile
; j
++) {
265 tfc
= &g_array_index(tc
->tracefiles
, LttvTracefileContext
*, j
);
266 lttv_hooks_destroy((*tfc
)->event
);
267 lttv_hooks_by_id_destroy((*tfc
)->event_by_id
);
268 g_object_unref((*tfc
)->a
);
269 g_object_unref(*tfc
);
271 g_array_free(tc
->tracefiles
, TRUE
);
274 g_free(self
->traces
);
278 void lttv_traceset_context_add_hooks(LttvTracesetContext
*self
,
279 LttvHooks
*before_traceset
,
280 LttvHooks
*before_trace
,
281 LttvHooks
*before_tracefile
,
283 LttvHooksById
*event_by_id
)
285 LttvTraceset
*ts
= self
->ts
;
289 LttvTraceContext
*tc
;
291 lttv_hooks_call(before_traceset
, self
);
293 nb_trace
= lttv_traceset_number(ts
);
295 for(i
= 0 ; i
< nb_trace
; i
++) {
296 tc
= self
->traces
[i
];
297 lttv_trace_context_add_hooks(tc
,
306 void lttv_traceset_context_remove_hooks(LttvTracesetContext
*self
,
307 LttvHooks
*after_traceset
,
308 LttvHooks
*after_trace
,
309 LttvHooks
*after_tracefile
,
311 LttvHooksById
*event_by_id
)
314 LttvTraceset
*ts
= self
->ts
;
318 LttvTraceContext
*tc
;
320 nb_trace
= lttv_traceset_number(ts
);
322 for(i
= 0 ; i
< nb_trace
; i
++) {
323 tc
= self
->traces
[i
];
324 lttv_trace_context_remove_hooks(tc
,
331 lttv_hooks_call(after_traceset
, self
);
336 void lttv_trace_context_add_hooks(LttvTraceContext
*self
,
337 LttvHooks
*before_trace
,
338 LttvHooks
*before_tracefile
,
340 LttvHooksById
*event_by_id
)
342 guint i
, nb_tracefile
;
344 LttvTracefileContext
**tfc
;
346 lttv_hooks_call(before_trace
, self
);
348 nb_tracefile
= self
->tracefiles
->len
;
350 for(i
= 0 ; i
< nb_tracefile
; i
++) {
351 tfc
= &g_array_index(self
->tracefiles
, LttvTracefileContext
*, i
);
352 lttv_tracefile_context_add_hooks(*tfc
,
361 void lttv_trace_context_remove_hooks(LttvTraceContext
*self
,
362 LttvHooks
*after_trace
,
363 LttvHooks
*after_tracefile
,
365 LttvHooksById
*event_by_id
)
367 guint i
, nb_tracefile
;
369 LttvTracefileContext
**tfc
;
371 nb_tracefile
= self
->tracefiles
->len
;
373 for(i
= 0 ; i
< nb_tracefile
; i
++) {
374 tfc
= &g_array_index(self
->tracefiles
, LttvTracefileContext
*, i
);
375 lttv_tracefile_context_remove_hooks(*tfc
,
381 lttv_hooks_call(after_trace
, self
);
384 void lttv_tracefile_context_add_hooks(LttvTracefileContext
*self
,
385 LttvHooks
*before_tracefile
,
387 LttvHooksById
*event_by_id
)
393 lttv_hooks_call(before_tracefile
, self
);
394 lttv_hooks_add_list(self
->event
, event
);
395 if(event_by_id
!= NULL
) {
396 for(i
= 0; i
< event_by_id
->array
->len
; i
++) {
397 index
= g_array_index(event_by_id
->array
, guint
, i
);
398 hook
= lttv_hooks_by_id_find(self
->event_by_id
, index
);
399 lttv_hooks_add_list(hook
, lttv_hooks_by_id_get(event_by_id
, index
));
404 void lttv_tracefile_context_remove_hooks(LttvTracefileContext
*self
,
405 LttvHooks
*after_tracefile
,
407 LttvHooksById
*event_by_id
)
413 lttv_hooks_remove_list(self
->event
, event
);
414 if(event_by_id
!= NULL
) {
415 for(i
= 0; i
< event_by_id
->array
->len
; i
++) {
416 index
= g_array_index(event_by_id
->array
, guint
, i
);
417 hook
= lttv_hooks_by_id_get(self
->event_by_id
, index
);
419 lttv_hooks_remove_list(hook
, lttv_hooks_by_id_get(event_by_id
, index
));
423 lttv_hooks_call(after_tracefile
, self
);
428 void lttv_tracefile_context_add_hooks_by_id(LttvTracefileContext
*tfc
,
430 LttvHooks
*event_by_id
)
433 h
= lttv_hooks_by_id_find(tfc
->event_by_id
, i
);
434 lttv_hooks_add_list(h
, event_by_id
);
437 void lttv_tracefile_context_remove_hooks_by_id(LttvTracefileContext
*tfc
,
440 lttv_hooks_by_id_remove(tfc
->event_by_id
, i
);
443 static LttvTracesetContext
*
444 new_traceset_context(LttvTracesetContext
*self
)
446 return g_object_new(LTTV_TRACESET_CONTEXT_TYPE
, NULL
);
450 static LttvTraceContext
*
451 new_trace_context(LttvTracesetContext
*self
)
453 return g_object_new(LTTV_TRACE_CONTEXT_TYPE
, NULL
);
457 static LttvTracefileContext
*
458 new_tracefile_context(LttvTracesetContext
*self
)
460 return g_object_new(LTTV_TRACEFILE_CONTEXT_TYPE
, NULL
);
465 traceset_context_instance_init (GTypeInstance
*instance
, gpointer g_class
)
467 /* Be careful of anything which would not work well with shallow copies */
472 traceset_context_finalize (LttvTracesetContext
*self
)
474 G_OBJECT_CLASS(g_type_class_peek(g_type_parent(LTTV_TRACESET_CONTEXT_TYPE
)))
475 ->finalize(G_OBJECT(self
));
480 traceset_context_class_init (LttvTracesetContextClass
*klass
)
482 GObjectClass
*gobject_class
= G_OBJECT_CLASS(klass
);
484 gobject_class
->finalize
= (void (*)(GObject
*self
))traceset_context_finalize
;
487 klass
->new_traceset_context
= new_traceset_context
;
488 klass
->new_trace_context
= new_trace_context
;
489 klass
->new_tracefile_context
= new_tracefile_context
;
494 lttv_traceset_context_get_type(void)
496 static GType type
= 0;
498 static const GTypeInfo info
= {
499 sizeof (LttvTracesetContextClass
),
500 NULL
, /* base_init */
501 NULL
, /* base_finalize */
502 (GClassInitFunc
) traceset_context_class_init
, /* class_init */
503 NULL
, /* class_finalize */
504 NULL
, /* class_data */
505 sizeof (LttvTracesetContext
),
507 (GInstanceInitFunc
) traceset_context_instance_init
, /* instance_init */
508 NULL
/* Value handling */
511 type
= g_type_register_static (G_TYPE_OBJECT
, "LttvTracesetContextType",
519 trace_context_instance_init (GTypeInstance
*instance
, gpointer g_class
)
521 /* Be careful of anything which would not work well with shallow copies */
526 trace_context_finalize (LttvTraceContext
*self
)
528 G_OBJECT_CLASS(g_type_class_peek(g_type_parent(LTTV_TRACE_CONTEXT_TYPE
)))->
529 finalize(G_OBJECT(self
));
534 trace_context_class_init (LttvTraceContextClass
*klass
)
536 GObjectClass
*gobject_class
= G_OBJECT_CLASS(klass
);
538 gobject_class
->finalize
= (void (*)(GObject
*self
)) trace_context_finalize
;
543 lttv_trace_context_get_type(void)
545 static GType type
= 0;
547 static const GTypeInfo info
= {
548 sizeof (LttvTraceContextClass
),
549 NULL
, /* base_init */
550 NULL
, /* base_finalize */
551 (GClassInitFunc
) trace_context_class_init
, /* class_init */
552 NULL
, /* class_finalize */
553 NULL
, /* class_data */
554 sizeof (LttvTraceContext
),
556 (GInstanceInitFunc
) trace_context_instance_init
, /* instance_init */
557 NULL
/* Value handling */
560 type
= g_type_register_static (G_TYPE_OBJECT
, "LttvTraceContextType",
568 tracefile_context_instance_init (GTypeInstance
*instance
, gpointer g_class
)
570 /* Be careful of anything which would not work well with shallow copies */
575 tracefile_context_finalize (LttvTracefileContext
*self
)
577 G_OBJECT_CLASS(g_type_class_peek(g_type_parent(LTTV_TRACEFILE_CONTEXT_TYPE
)))
578 ->finalize(G_OBJECT(self
));
583 tracefile_context_class_init (LttvTracefileContextClass
*klass
)
585 GObjectClass
*gobject_class
= G_OBJECT_CLASS(klass
);
587 gobject_class
->finalize
= (void (*)(GObject
*self
))tracefile_context_finalize
;
592 lttv_tracefile_context_get_type(void)
594 static GType type
= 0;
596 static const GTypeInfo info
= {
597 sizeof (LttvTracefileContextClass
),
598 NULL
, /* base_init */
599 NULL
, /* base_finalize */
600 (GClassInitFunc
) tracefile_context_class_init
, /* class_init */
601 NULL
, /* class_finalize */
602 NULL
, /* class_data */
603 sizeof (LttvTracefileContext
),
605 (GInstanceInitFunc
) tracefile_context_instance_init
, /* instance_init */
606 NULL
/* Value handling */
609 type
= g_type_register_static (G_TYPE_OBJECT
, "LttvTracefileContextType",
617 static gboolean
get_first(gpointer key
, gpointer value
, gpointer user_data
) {
618 g_assert(key
== value
);
619 *((LttvTracefileContext
**)user_data
) = (LttvTracefileContext
*)value
;
623 static gboolean
test_tree(gpointer key
, gpointer value
, gpointer user_data
) {
625 LttvTracefileContext
*tfc
= (LttvTracefileContext
*)key
;
627 g_debug("Tracefile name %s, time %lu.%lu, tfi %u, ti %u",
628 g_quark_to_string(ltt_tracefile_name(tfc
->tf
)),
629 tfc
->timestamp
.tv_sec
, tfc
->timestamp
.tv_nsec
,
630 tfc
->index
, tfc
->t_context
->index
);
632 if(((LttvTracefileContext
*)user_data
) == (LttvTracefileContext
*)value
) {
633 g_assert(compare_tracefile(user_data
, value
) == 0);
635 g_assert(compare_tracefile(user_data
, value
) != 0);
637 //g_assert(((LttvTracefileContext *)user_data) != (LttvTracefileContext *)value);
643 void lttv_process_traceset_begin(LttvTracesetContext
*self
,
644 LttvHooks
*before_traceset
,
645 LttvHooks
*before_trace
,
646 LttvHooks
*before_tracefile
,
648 LttvHooksById
*event_by_id
)
651 /* simply add hooks in context. _before hooks are called by add_hooks. */
652 /* It calls all before_traceset, before_trace, and before_tracefile hooks. */
653 lttv_traceset_context_add_hooks(self
,
662 enum read_state
{ LAST_NONE
, LAST_OK
, LAST_EMPTY
};
664 /* Note : a _middle must be preceded from a _seek or another middle */
665 guint
lttv_process_traceset_middle(LttvTracesetContext
*self
,
668 const LttvTracesetContextPosition
*end_position
)
670 GTree
*pqueue
= self
->pqueue
;
672 guint fac_id
, ev_id
, id
;
674 LttvTracefileContext
*tfc
;
682 enum read_state last_read_state
= LAST_NONE
;
684 gboolean last_ret
= FALSE
; /* return value of the last hook list called */
686 /* Get the next event from the pqueue, call its hooks,
687 reinsert in the pqueue the following event from the same tracefile
688 unless the tracefile is finished or the event is later than the
693 g_tree_foreach(pqueue
, get_first
, &tfc
);
694 /* End of traceset : tfc is NULL */
695 if(unlikely(tfc
== NULL
))
701 * - the maximum number of events specified?
702 * - the end position ?
704 * then the read is finished. We leave the queue in the same state and
708 if(unlikely(last_ret
== TRUE
||
709 ((count
>= nb_events
) && (nb_events
!= G_MAXULONG
)) ||
710 (end_position
!=NULL
&<tv_traceset_context_ctx_pos_compare(self
,
711 end_position
) == 0)||
712 ltt_time_compare(end
, tfc
->timestamp
) <= 0))
717 /* Get the tracefile with an event for the smallest time found. If two
718 or more tracefiles have events for the same time, hope that lookup
719 and remove are consistent. */
722 g_debug("test tree before remove");
723 g_tree_foreach(pqueue
, test_tree
, tfc
);
725 g_tree_remove(pqueue
, tfc
);
728 g_debug("test tree after remove");
729 g_tree_foreach(pqueue
, test_tree
, tfc
);
732 e
= ltt_tracefile_get_event(tfc
->tf
);
734 if(last_read_state
!= LAST_EMPTY
) {
735 /* Only call hooks if the last read has given an event or if we are at the
736 * first pass (not if last read returned end of tracefile) */
739 fac_id
= ltt_event_facility_id(e
);
740 ev_id
= ltt_event_eventtype_id(e
);
741 id
= GET_HOOK_ID(fac_id
, ev_id
);
742 last_ret
= lttv_hooks_call_merge(tfc
->event
, tfc
,
743 lttv_hooks_by_id_get(tfc
->event_by_id
, id
), tfc
);
746 read_ret
= ltt_tracefile_read(tfc
->tf
);
748 if(likely(!read_ret
)) {
749 g_debug("An event is ready");
750 tfc
->timestamp
= ltt_event_time(e
);
752 g_tree_insert(pqueue
, tfc
, tfc
);
753 last_read_state
= LAST_OK
;
755 tfc
->timestamp
= ltt_time_infinite
;
757 if(read_ret
== ERANGE
) {
758 last_read_state
= LAST_EMPTY
;
759 g_debug("End of trace");
761 g_error("Error happened in lttv_process_traceset_middle");
767 void lttv_process_traceset_end(LttvTracesetContext
*self
,
768 LttvHooks
*after_traceset
,
769 LttvHooks
*after_trace
,
770 LttvHooks
*after_tracefile
,
772 LttvHooksById
*event_by_id
)
774 /* Remove hooks from context. _after hooks are called by remove_hooks. */
775 /* It calls all after_traceset, after_trace, and after_tracefile hooks. */
776 lttv_traceset_context_remove_hooks(self
,
784 /* Subtile modification :
785 * if tracefile has no event at or after the time requested, it is not put in
786 * the queue, as the next read would fail. */
787 void lttv_process_trace_seek_time(LttvTraceContext
*self
, LttTime start
)
789 guint i
, nb_tracefile
;
793 LttvTracefileContext
**tfc
;
795 GTree
*pqueue
= self
->ts_context
->pqueue
;
797 nb_tracefile
= self
->tracefiles
->len
;
799 for(i
= 0 ; i
< nb_tracefile
; i
++) {
800 tfc
= &g_array_index(self
->tracefiles
, LttvTracefileContext
*, i
);
802 g_tree_remove(pqueue
, *tfc
);
804 ret
= ltt_tracefile_seek_time((*tfc
)->tf
, start
);
805 if(ret
== EPERM
) g_error("error in lttv_process_trace_seek_time seek");
807 if(ret
== 0) { /* not ERANGE especially */
808 (*tfc
)->timestamp
= ltt_event_time(ltt_tracefile_get_event((*tfc
)->tf
));
809 g_tree_insert(pqueue
, (*tfc
), (*tfc
));
811 (*tfc
)->timestamp
= ltt_time_infinite
;
817 void lttv_process_traceset_seek_time(LttvTracesetContext
*self
, LttTime start
)
821 LttvTraceContext
*tc
;
823 nb_trace
= lttv_traceset_number(self
->ts
);
824 for(i
= 0 ; i
< nb_trace
; i
++) {
825 tc
= self
->traces
[i
];
826 lttv_process_trace_seek_time(tc
, start
);
831 gboolean
lttv_process_traceset_seek_position(LttvTracesetContext
*self
,
832 const LttvTracesetContextPosition
*pos
)
836 g_tree_destroy(self
->pqueue
);
837 self
->pqueue
= g_tree_new(compare_tracefile
);
839 for(i
=0;i
<pos
->ep
->len
; i
++) {
840 LttEventPosition
**ep
= &g_array_index(pos
->ep
, LttEventPosition
*, i
);
841 LttvTracefileContext
**tfc
=
842 &g_array_index(pos
->tfc
, LttvTracefileContext
*, i
);
844 if(ltt_tracefile_seek_position((*tfc
)->tf
, *ep
) != 0)
846 (*tfc
)->timestamp
= ltt_event_time(ltt_tracefile_get_event((*tfc
)->tf
));
847 g_tree_insert(self
->pqueue
, (*tfc
), (*tfc
));
849 (*tfc
)->timestamp
= ltt_time_infinite
;
858 find_field(LttEventType
*et
, const GQuark field
)
869 if(field
== 0) return NULL
;
871 f
= ltt_eventtype_field(et
);
872 t
= ltt_eventtype_type(et
);
873 g_assert(ltt_type_class(t
) == LTT_STRUCT
);
874 nb
= ltt_type_member_number(t
);
875 for(i
= 0 ; i
< nb
; i
++) {
876 ltt_type_member_type(t
, i
, &name
);
877 if(name
== field
) break;
880 return ltt_field_member(f
, i
);
883 LttvTraceHookByFacility
*lttv_trace_hook_get_fac(LttvTraceHook
*th
,
886 return &g_array_index(th
->fac_index
, LttvTraceHookByFacility
, facility_id
);
889 /* Get the first facility corresponding to the name. As the types must be
890 * compatible, it is relevant to use the field name and sizes of the first
891 * facility to create data structures and assume the data will be compatible
892 * thorough the trace */
893 LttvTraceHookByFacility
*lttv_trace_hook_get_first(LttvTraceHook
*th
)
895 g_assert(th
->fac_list
->len
> 0);
896 return g_array_index(th
->fac_list
, LttvTraceHookByFacility
*, 0);
900 /* Returns 0 on success, -1 if fails. */
902 lttv_trace_find_hook(LttTrace
*t
, GQuark facility
, GQuark event
,
903 GQuark field1
, GQuark field2
, GQuark field3
, LttvHook h
, gpointer hook_data
,
908 LttEventType
*et
, *first_et
;
912 guint i
, fac_id
, ev_id
;
914 LttvTraceHookByFacility
*thf
, *first_thf
;
916 facilities
= ltt_trace_facility_get_by_name(t
, facility
);
918 if(unlikely(facilities
== NULL
)) goto facility_error
;
920 th
->fac_index
= g_array_sized_new(FALSE
, TRUE
,
921 sizeof(LttvTraceHookByFacility
),
923 th
->fac_index
= g_array_set_size(th
->fac_index
, NUM_FACILITIES
);
925 th
->fac_list
= g_array_sized_new(FALSE
, TRUE
,
926 sizeof(LttvTraceHookByFacility
*),
928 th
->fac_list
= g_array_set_size(th
->fac_list
, facilities
->len
);
930 fac_id
= g_array_index(facilities
, guint
, 0);
931 f
= ltt_trace_get_facility_by_num(t
, fac_id
);
933 et
= ltt_facility_eventtype_get_by_name(f
, event
);
934 if(unlikely(et
== NULL
)) goto event_error
;
936 thf
= &g_array_index(th
->fac_index
, LttvTraceHookByFacility
, fac_id
);
937 g_array_index(th
->fac_list
, LttvTraceHookByFacility
*, 0) = thf
;
939 ev_id
= ltt_eventtype_id(et
);
942 thf
->id
= GET_HOOK_ID(fac_id
, ev_id
);
943 thf
->f1
= find_field(et
, field1
);
944 thf
->f2
= find_field(et
, field2
);
945 thf
->f3
= find_field(et
, field3
);
946 thf
->hook_data
= hook_data
;
951 /* Check for type compatibility too */
952 for(i
=1;i
<facilities
->len
;i
++) {
953 fac_id
= g_array_index(facilities
, guint
, i
);
954 f
= ltt_trace_get_facility_by_num(t
, fac_id
);
956 et
= ltt_facility_eventtype_get_by_name(f
, ltt_eventtype_name(et
));
957 if(unlikely(et
== NULL
)) goto event_error
;
959 thf
= &g_array_index(th
->fac_index
, LttvTraceHookByFacility
, fac_id
);
960 g_array_index(th
->fac_list
, LttvTraceHookByFacility
*, i
) = thf
;
961 ev_id
= ltt_eventtype_id(et
);
963 thf
->id
= GET_HOOK_ID(fac_id
, ev_id
);
964 thf
->f1
= find_field(et
, field1
);
965 if(check_fields_compatibility(first_et
, et
,
966 first_thf
->f1
, thf
->f1
))
969 thf
->f2
= find_field(et
, field2
);
970 if(check_fields_compatibility(first_et
, et
,
971 first_thf
->f2
, thf
->f2
))
974 thf
->f3
= find_field(et
, field3
);
975 if(check_fields_compatibility(first_et
, et
,
976 first_thf
->f3
, thf
->f3
))
978 thf
->hook_data
= hook_data
;
986 g_error("Event type %s does not exist",
987 g_quark_to_string(ltt_eventtype_name(et
)));
990 g_error("No %s facility", g_quark_to_string(facility
));
993 g_array_free(th
->fac_index
, TRUE
);
994 g_array_free(th
->fac_list
, TRUE
);
995 th
->fac_index
= NULL
;
1000 void lttv_trace_hook_destroy(LttvTraceHook
*th
)
1002 g_array_free(th
->fac_index
, TRUE
);
1003 g_array_free(th
->fac_list
, TRUE
);
1007 LttvTracesetContextPosition
*lttv_traceset_context_position_new()
1009 LttvTracesetContextPosition
*pos
= g_new(LttvTracesetContextPosition
,1);
1010 pos
->ep
= g_array_sized_new(FALSE
, TRUE
, sizeof(LttEventPosition
*),
1012 pos
->tfc
= g_array_sized_new(FALSE
, TRUE
, sizeof(LttvTracefileContext
*),
1014 pos
->timestamp
= ltt_time_infinite
;
1018 /* Save all positions, the ones not in the pqueue will have NULL
1020 void lttv_traceset_context_position_save(const LttvTracesetContext
*self
,
1021 LttvTracesetContextPosition
*pos
)
1024 guint num_traces
= lttv_traceset_number(self
->ts
);
1026 for(i
=0; i
<num_traces
;i
++) {
1027 GArray
* tracefiles
= self
->traces
[i
]->tracefiles
;
1029 guint num_tracefiles
= tracefiles
->len
;
1031 for(j
=0;j
<num_tracefiles
;j
++) {
1032 LttvTracefileContext
**tfc
= &g_array_index(tracefiles
,
1033 LttvTracefileContext
*, j
);
1035 LttEvent
*event
= ltt_tracefile_get_event((*tfc
)->tf
);
1036 LttEventPosition
*ep
;
1038 if(ltt_time_compare((*tfc
)->timestamp
, ltt_time_infinite
) != 0) {
1039 ep
= ltt_event_position_new();
1040 ltt_event_position(event
, ep
);
1041 if(ltt_time_compare((*tfc
)->timestamp
, pos
->timestamp
) < 0)
1042 pos
->timestamp
= (*tfc
)->timestamp
;
1046 g_array_append_val(pos
->tfc
, *tfc
);
1047 g_array_append_val(pos
->ep
, ep
);
1053 void lttv_traceset_context_position_destroy(LttvTracesetContextPosition
*pos
)
1056 LttEventPosition
**ep
;
1058 for(i
=0;i
<pos
->ep
->len
;i
++) {
1059 ep
= &g_array_index(pos
->ep
, LttEventPosition
*, i
);
1063 g_array_free(pos
->ep
, TRUE
);
1064 g_array_free(pos
->tfc
, TRUE
);
1068 void lttv_traceset_context_position_copy(LttvTracesetContextPosition
*dest
,
1069 const LttvTracesetContextPosition
*src
)
1072 LttEventPosition
**src_ep
, **dest_ep
;
1074 g_array_set_size(dest
->ep
, src
->ep
->len
);
1075 g_array_set_size(dest
->tfc
, src
->tfc
->len
);
1077 for(i
=0;i
<src
->ep
->len
;i
++) {
1078 src_ep
= &g_array_index(src
->ep
, LttEventPosition
*, i
);
1079 dest_ep
= &g_array_index(dest
->ep
, LttEventPosition
*, i
);
1080 if(*src_ep
!= NULL
) {
1081 *dest_ep
= ltt_event_position_new();
1082 ltt_event_position_copy(
1088 for(i
=0;i
<src
->tfc
->len
;i
++) {
1089 g_array_index(dest
->tfc
, LttvTracefileContext
*, i
) =
1090 g_array_index(src
->tfc
, LttvTracefileContext
*, i
);
1092 dest
->timestamp
= src
->timestamp
;
1095 gint
lttv_traceset_context_ctx_pos_compare(const LttvTracesetContext
*self
,
1096 const LttvTracesetContextPosition
*pos
)
1101 if(pos
->ep
->len
== 0) {
1102 if(lttv_traceset_number(self
->ts
) == 0) return 0;
1105 if(lttv_traceset_number(self
->ts
) == 0)
1108 for(i
=0;i
<pos
->ep
->len
;i
++) {
1109 LttEventPosition
*ep
= g_array_index(pos
->ep
, LttEventPosition
*, i
);
1110 LttvTracefileContext
*tfc
=
1111 g_array_index(pos
->tfc
, LttvTracefileContext
*, i
);
1114 if(ltt_time_compare(tfc
->timestamp
, ltt_time_infinite
) != 0) {
1118 if(ltt_time_compare(tfc
->timestamp
, ltt_time_infinite
) == 0) {
1121 LttEvent
*event
= ltt_tracefile_get_event(tfc
->tf
);
1123 ret
= ltt_event_position_compare((LttEventPosition
*)event
,
1127 if(ret
!= 0) return ret
;
1134 gint
lttv_traceset_context_pos_pos_compare(
1135 const LttvTracesetContextPosition
*pos1
,
1136 const LttvTracesetContextPosition
*pos2
)
1141 if(pos1
->ep
->len
== 0) {
1142 if(pos2
->ep
->len
== 0) return 0;
1145 if(pos2
->ep
->len
== 0)
1148 for(i
=0;i
<pos1
->ep
->len
;i
++) {
1149 LttEventPosition
*ep1
= g_array_index(pos1
->ep
, LttEventPosition
*, i
);
1150 LttvTracefileContext
*tfc1
= g_array_index(pos1
->tfc
,
1151 LttvTracefileContext
*, i
);
1154 for(j
=0;j
<pos2
->ep
->len
;j
++) {
1155 LttEventPosition
*ep2
= g_array_index(pos2
->ep
, LttEventPosition
*, j
);
1156 LttvTracefileContext
*tfc2
= g_array_index(pos2
->tfc
,
1157 LttvTracefileContext
*, j
);
1160 ret
= ltt_event_position_compare(ep1
, ep2
);
1164 if(ret
!= 0) return ret
;
1168 for(j
=0;j
<pos2
->ep
->len
;j
++) {
1169 LttEventPosition
*ep2
= g_array_index(pos2
->ep
, LttEventPosition
*, j
);
1170 LttvTracefileContext
*tfc2
= g_array_index(pos2
->tfc
,
1171 LttvTracefileContext
*, j
);
1173 if(ep2
!= NULL
) ret
= 1;
1182 LttTime
lttv_traceset_context_position_get_time(
1183 const LttvTracesetContextPosition
*pos
)
1185 return pos
->timestamp
;
1189 LttvTracefileContext
*lttv_traceset_context_get_current_tfc(LttvTracesetContext
*self
)
1191 GTree
*pqueue
= self
->pqueue
;
1192 LttvTracefileContext
*tfc
= NULL
;
1194 g_tree_foreach(pqueue
, get_first
, &tfc
);
1199 /* lttv_process_traceset_synchronize_tracefiles
1201 * Use the sync_position field of the trace set context to synchronize each
1202 * tracefile with the previously saved position.
1204 * If no previous position has been saved, it simply does nothing.
1206 void lttv_process_traceset_synchronize_tracefiles(LttvTracesetContext
*tsc
)
1208 g_assert(lttv_process_traceset_seek_position(tsc
, tsc
->sync_position
) == 0);
1214 void lttv_process_traceset_get_sync_data(LttvTracesetContext
*tsc
)
1216 lttv_traceset_context_position_save(tsc
, tsc
->sync_position
);