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
)
58 struct _LttvTracesetContextPosition
{
59 GArray
*ep
; /* Array of LttEventPosition */
60 GArray
*tfc
; /* Array of corresponding
62 LttTime timestamp
; /* Current time at the saved position */
65 void lttv_context_init(LttvTracesetContext
*self
, LttvTraceset
*ts
)
67 LTTV_TRACESET_CONTEXT_GET_CLASS(self
)->init(self
, ts
);
71 void lttv_context_fini(LttvTracesetContext
*self
)
73 LTTV_TRACESET_CONTEXT_GET_CLASS(self
)->fini(self
);
78 lttv_context_new_traceset_context(LttvTracesetContext
*self
)
80 return LTTV_TRACESET_CONTEXT_GET_CLASS(self
)->new_traceset_context(self
);
87 lttv_context_new_trace_context(LttvTracesetContext
*self
)
89 return LTTV_TRACESET_CONTEXT_GET_CLASS(self
)->new_trace_context(self
);
93 LttvTracefileContext
*
94 lttv_context_new_tracefile_context(LttvTracesetContext
*self
)
96 return LTTV_TRACESET_CONTEXT_GET_CLASS(self
)->new_tracefile_context(self
);
99 /****************************************************************************
100 * lttv_traceset_context_compute_time_span
102 * Keep the time span is sync with on the fly addition and removal of traces
103 * in a trace set. It must be called each time a trace is added/removed from
104 * the traceset. It could be more efficient to call it only once a bunch
105 * of traces are loaded, but the calculation is not long, so it's not
108 * Author : Xang Xiu Yang
109 ***************************************************************************/
110 static void lttv_traceset_context_compute_time_span(
111 LttvTracesetContext
*self
,
112 TimeInterval
*time_span
)
114 LttvTraceset
* traceset
= self
->ts
;
115 int numTraces
= lttv_traceset_number(traceset
);
118 LttvTraceContext
*tc
;
121 time_span
->start_time
.tv_sec
= 0;
122 time_span
->start_time
.tv_nsec
= 0;
123 time_span
->end_time
.tv_sec
= 0;
124 time_span
->end_time
.tv_nsec
= 0;
126 for(i
=0; i
<numTraces
;i
++){
127 tc
= self
->traces
[i
];
130 ltt_trace_time_span_get(trace
, &s
, &e
);
133 time_span
->start_time
= s
;
134 time_span
->end_time
= e
;
136 if(s
.tv_sec
< time_span
->start_time
.tv_sec
137 || (s
.tv_sec
== time_span
->start_time
.tv_sec
138 && s
.tv_nsec
< time_span
->start_time
.tv_nsec
))
139 time_span
->start_time
= s
;
140 if(e
.tv_sec
> time_span
->end_time
.tv_sec
141 || (e
.tv_sec
== time_span
->end_time
.tv_sec
142 && e
.tv_nsec
> time_span
->end_time
.tv_nsec
))
143 time_span
->end_time
= e
;
148 static void init_tracefile_context(LttTracefile
*tracefile
,
149 LttvTraceContext
*tc
)
151 LttvTracefileContext
*tfc
;
152 LttvTracesetContext
*tsc
= tc
->ts_context
;
154 tfc
= LTTV_TRACESET_CONTEXT_GET_CLASS(tsc
)->new_tracefile_context(tsc
);
156 tfc
->index
= tc
->tracefiles
->len
;
157 tc
->tracefiles
= g_array_append_val(tc
->tracefiles
, tfc
);
162 tfc
->event
= lttv_hooks_new();
163 tfc
->event_by_id
= lttv_hooks_by_id_new();
164 tfc
->a
= g_object_new(LTTV_ATTRIBUTE_TYPE
, NULL
);
169 init(LttvTracesetContext
*self
, LttvTraceset
*ts
)
171 guint i
, j
, nb_trace
, nb_control
, nb_per_cpu
, nb_tracefile
;
173 LttvTraceContext
*tc
;
175 GData
**tracefiles_groups
;
177 struct compute_tracefile_group_args args
;
179 nb_trace
= lttv_traceset_number(ts
);
181 self
->traces
= g_new(LttvTraceContext
*, nb_trace
);
182 self
->a
= g_object_new(LTTV_ATTRIBUTE_TYPE
, NULL
);
183 self
->ts_a
= lttv_traceset_attribute(ts
);
184 for(i
= 0 ; i
< nb_trace
; i
++) {
185 tc
= LTTV_TRACESET_CONTEXT_GET_CLASS(self
)->new_trace_context(self
);
186 self
->traces
[i
] = tc
;
188 tc
->ts_context
= self
;
190 tc
->vt
= lttv_traceset_get(ts
, i
);
191 tc
->t
= lttv_trace(tc
->vt
);
192 tc
->a
= g_object_new(LTTV_ATTRIBUTE_TYPE
, NULL
);
193 tc
->t_a
= lttv_trace_attribute(tc
->vt
);
194 tc
->tracefiles
= g_array_sized_new(FALSE
, TRUE
,
195 sizeof(LttvTracefileContext
*), 10);
197 tracefiles_groups
= ltt_trace_get_tracefiles_groups(tc
->t
);
199 args
.func
= (ForEachTraceFileFunc
)init_tracefile_context
;
202 g_datalist_foreach(tracefiles_groups
,
203 (GDataForeachFunc
)compute_tracefile_group
,
207 nb_control
= ltt_trace_control_tracefile_number(tc
->t
);
208 nb_per_cpu
= ltt_trace_per_cpu_tracefile_number(tc
->t
);
209 nb_tracefile
= nb_control
+ nb_per_cpu
;
210 tc
->tracefiles
= g_new(LttvTracefileContext
*, nb_tracefile
);
212 for(j
= 0 ; j
< nb_tracefile
; j
++) {
213 tfc
= LTTV_TRACESET_CONTEXT_GET_CLASS(self
)->new_tracefile_context(self
);
214 tc
->tracefiles
[j
] = tfc
;
219 tfc
->tf
= ltt_trace_control_tracefile_get(tc
->t
, j
);
222 tfc
->control
= FALSE
;
223 tfc
->tf
= ltt_trace_per_cpu_tracefile_get(tc
->t
, j
- nb_control
);
227 tfc
->e
= ltt_event_new();
228 tfc
->event
= lttv_hooks_new();
229 tfc
->event_by_id
= lttv_hooks_by_id_new();
230 tfc
->a
= g_object_new(LTTV_ATTRIBUTE_TYPE
, NULL
);
235 self
->pqueue
= g_tree_new(compare_tracefile
);
236 lttv_process_traceset_seek_time(self
, ltt_time_zero
);
237 lttv_traceset_context_compute_time_span(self
, &self
->time_span
);
242 void fini(LttvTracesetContext
*self
)
244 guint i
, j
, nb_trace
, nb_tracefile
;
246 LttvTraceContext
*tc
;
248 LttvTracefileContext
*tfc
;
250 LttvTraceset
*ts
= self
->ts
;
254 g_tree_destroy(self
->pqueue
);
255 g_object_unref(self
->a
);
257 nb_trace
= lttv_traceset_number(ts
);
259 for(i
= 0 ; i
< nb_trace
; i
++) {
260 tc
= self
->traces
[i
];
262 g_object_unref(tc
->a
);
264 nb_tracefile
= tc
->tracefiles
->len
;
266 for(j
= 0 ; j
< nb_tracefile
; j
++) {
267 tfc
= g_array_index(tc
->tracefiles
, LttvTracefileContext
*, j
);
268 lttv_hooks_destroy(tfc
->event
);
269 lttv_hooks_by_id_destroy(tfc
->event_by_id
);
270 g_object_unref(tfc
->a
);
273 g_array_free(tc
->tracefiles
, TRUE
);
276 g_free(self
->traces
);
280 void lttv_traceset_context_add_hooks(LttvTracesetContext
*self
,
281 LttvHooks
*before_traceset
,
282 LttvHooks
*before_trace
,
283 LttvHooks
*before_tracefile
,
285 LttvHooksById
*event_by_id
)
287 LttvTraceset
*ts
= self
->ts
;
291 LttvTraceContext
*tc
;
293 lttv_hooks_call(before_traceset
, self
);
295 nb_trace
= lttv_traceset_number(ts
);
297 for(i
= 0 ; i
< nb_trace
; i
++) {
298 tc
= self
->traces
[i
];
299 lttv_trace_context_add_hooks(tc
,
308 void lttv_traceset_context_remove_hooks(LttvTracesetContext
*self
,
309 LttvHooks
*after_traceset
,
310 LttvHooks
*after_trace
,
311 LttvHooks
*after_tracefile
,
313 LttvHooksById
*event_by_id
)
316 LttvTraceset
*ts
= self
->ts
;
320 LttvTraceContext
*tc
;
322 nb_trace
= lttv_traceset_number(ts
);
324 for(i
= 0 ; i
< nb_trace
; i
++) {
325 tc
= self
->traces
[i
];
326 lttv_trace_context_remove_hooks(tc
,
333 lttv_hooks_call(after_traceset
, self
);
338 void lttv_trace_context_add_hooks(LttvTraceContext
*self
,
339 LttvHooks
*before_trace
,
340 LttvHooks
*before_tracefile
,
342 LttvHooksById
*event_by_id
)
344 guint i
, nb_tracefile
;
346 LttvTracefileContext
*tfc
;
348 lttv_hooks_call(before_trace
, self
);
350 nb_tracefile
= self
->tracefiles
->len
;
352 for(i
= 0 ; i
< nb_tracefile
; i
++) {
353 tfc
= g_array_index(self
->tracefiles
, LttvTracefileContext
*, i
);
354 lttv_tracefile_context_add_hooks(tfc
,
363 void lttv_trace_context_remove_hooks(LttvTraceContext
*self
,
364 LttvHooks
*after_trace
,
365 LttvHooks
*after_tracefile
,
367 LttvHooksById
*event_by_id
)
369 guint i
, nb_tracefile
;
371 LttvTracefileContext
*tfc
;
373 nb_tracefile
= self
->tracefiles
->len
;
375 for(i
= 0 ; i
< nb_tracefile
; i
++) {
376 tfc
= g_array_index(self
->tracefiles
, LttvTracefileContext
*, i
);
377 lttv_tracefile_context_remove_hooks(tfc
,
383 lttv_hooks_call(after_trace
, self
);
386 void lttv_tracefile_context_add_hooks(LttvTracefileContext
*self
,
387 LttvHooks
*before_tracefile
,
389 LttvHooksById
*event_by_id
)
395 lttv_hooks_call(before_tracefile
, self
);
396 lttv_hooks_add_list(self
->event
, event
);
397 if(event_by_id
!= NULL
) {
398 for(i
= 0; i
< event_by_id
->array
->len
; i
++) {
399 index
= g_array_index(event_by_id
->array
, guint
, i
);
400 hook
= lttv_hooks_by_id_find(self
->event_by_id
, index
);
401 lttv_hooks_add_list(hook
, lttv_hooks_by_id_get(event_by_id
, index
));
406 void lttv_tracefile_context_remove_hooks(LttvTracefileContext
*self
,
407 LttvHooks
*after_tracefile
,
409 LttvHooksById
*event_by_id
)
415 lttv_hooks_remove_list(self
->event
, event
);
416 if(event_by_id
!= NULL
) {
417 for(i
= 0; i
< event_by_id
->array
->len
; i
++) {
418 index
= g_array_index(event_by_id
->array
, guint
, i
);
419 hook
= lttv_hooks_by_id_get(self
->event_by_id
, index
);
421 lttv_hooks_remove_list(hook
, lttv_hooks_by_id_get(event_by_id
, index
));
425 lttv_hooks_call(after_tracefile
, self
);
430 void lttv_tracefile_context_add_hooks_by_id(LttvTracefileContext
*tfc
,
432 LttvHooks
*event_by_id
)
435 h
= lttv_hooks_by_id_find(tfc
->event_by_id
, i
);
436 lttv_hooks_add_list(h
, event_by_id
);
439 void lttv_tracefile_context_remove_hooks_by_id(LttvTracefileContext
*tfc
,
442 lttv_hooks_by_id_remove(tfc
->event_by_id
, i
);
445 static LttvTracesetContext
*
446 new_traceset_context(LttvTracesetContext
*self
)
448 return g_object_new(LTTV_TRACESET_CONTEXT_TYPE
, NULL
);
452 static LttvTraceContext
*
453 new_trace_context(LttvTracesetContext
*self
)
455 return g_object_new(LTTV_TRACE_CONTEXT_TYPE
, NULL
);
459 static LttvTracefileContext
*
460 new_tracefile_context(LttvTracesetContext
*self
)
462 return g_object_new(LTTV_TRACEFILE_CONTEXT_TYPE
, NULL
);
467 traceset_context_instance_init (GTypeInstance
*instance
, gpointer g_class
)
469 /* Be careful of anything which would not work well with shallow copies */
474 traceset_context_finalize (LttvTracesetContext
*self
)
476 G_OBJECT_CLASS(g_type_class_peek(g_type_parent(LTTV_TRACESET_CONTEXT_TYPE
)))
477 ->finalize(G_OBJECT(self
));
482 traceset_context_class_init (LttvTracesetContextClass
*klass
)
484 GObjectClass
*gobject_class
= G_OBJECT_CLASS(klass
);
486 gobject_class
->finalize
= (void (*)(GObject
*self
))traceset_context_finalize
;
489 klass
->new_traceset_context
= new_traceset_context
;
490 klass
->new_trace_context
= new_trace_context
;
491 klass
->new_tracefile_context
= new_tracefile_context
;
496 lttv_traceset_context_get_type(void)
498 static GType type
= 0;
500 static const GTypeInfo info
= {
501 sizeof (LttvTracesetContextClass
),
502 NULL
, /* base_init */
503 NULL
, /* base_finalize */
504 (GClassInitFunc
) traceset_context_class_init
, /* class_init */
505 NULL
, /* class_finalize */
506 NULL
, /* class_data */
507 sizeof (LttvTracesetContext
),
509 (GInstanceInitFunc
) traceset_context_instance_init
, /* instance_init */
510 NULL
/* Value handling */
513 type
= g_type_register_static (G_TYPE_OBJECT
, "LttvTracesetContextType",
521 trace_context_instance_init (GTypeInstance
*instance
, gpointer g_class
)
523 /* Be careful of anything which would not work well with shallow copies */
528 trace_context_finalize (LttvTraceContext
*self
)
530 G_OBJECT_CLASS(g_type_class_peek(g_type_parent(LTTV_TRACE_CONTEXT_TYPE
)))->
531 finalize(G_OBJECT(self
));
536 trace_context_class_init (LttvTraceContextClass
*klass
)
538 GObjectClass
*gobject_class
= G_OBJECT_CLASS(klass
);
540 gobject_class
->finalize
= (void (*)(GObject
*self
)) trace_context_finalize
;
545 lttv_trace_context_get_type(void)
547 static GType type
= 0;
549 static const GTypeInfo info
= {
550 sizeof (LttvTraceContextClass
),
551 NULL
, /* base_init */
552 NULL
, /* base_finalize */
553 (GClassInitFunc
) trace_context_class_init
, /* class_init */
554 NULL
, /* class_finalize */
555 NULL
, /* class_data */
556 sizeof (LttvTraceContext
),
558 (GInstanceInitFunc
) trace_context_instance_init
, /* instance_init */
559 NULL
/* Value handling */
562 type
= g_type_register_static (G_TYPE_OBJECT
, "LttvTraceContextType",
570 tracefile_context_instance_init (GTypeInstance
*instance
, gpointer g_class
)
572 /* Be careful of anything which would not work well with shallow copies */
577 tracefile_context_finalize (LttvTracefileContext
*self
)
579 G_OBJECT_CLASS(g_type_class_peek(g_type_parent(LTTV_TRACEFILE_CONTEXT_TYPE
)))
580 ->finalize(G_OBJECT(self
));
585 tracefile_context_class_init (LttvTracefileContextClass
*klass
)
587 GObjectClass
*gobject_class
= G_OBJECT_CLASS(klass
);
589 gobject_class
->finalize
= (void (*)(GObject
*self
))tracefile_context_finalize
;
594 lttv_tracefile_context_get_type(void)
596 static GType type
= 0;
598 static const GTypeInfo info
= {
599 sizeof (LttvTracefileContextClass
),
600 NULL
, /* base_init */
601 NULL
, /* base_finalize */
602 (GClassInitFunc
) tracefile_context_class_init
, /* class_init */
603 NULL
, /* class_finalize */
604 NULL
, /* class_data */
605 sizeof (LttvTracefileContext
),
607 (GInstanceInitFunc
) tracefile_context_instance_init
, /* instance_init */
608 NULL
/* Value handling */
611 type
= g_type_register_static (G_TYPE_OBJECT
, "LttvTracefileContextType",
619 static gboolean
get_first(gpointer key
, gpointer value
, gpointer user_data
) {
620 *((LttvTracefileContext
**)user_data
) = (LttvTracefileContext
*)value
;
624 static gboolean
test_tree(gpointer key
, gpointer value
, gpointer user_data
) {
626 g_assert(((LttvTracefileContext
*)user_data
) != (LttvTracefileContext
*)value
);
632 void lttv_process_traceset_begin(LttvTracesetContext
*self
,
633 LttvHooks
*before_traceset
,
634 LttvHooks
*before_trace
,
635 LttvHooks
*before_tracefile
,
637 LttvHooksById
*event_by_id
)
640 /* simply add hooks in context. _before hooks are called by add_hooks. */
641 /* It calls all before_traceset, before_trace, and before_tracefile hooks. */
642 lttv_traceset_context_add_hooks(self
,
651 /* Note : a _middle must be preceded from a _seek or another middle */
652 guint
lttv_process_traceset_middle(LttvTracesetContext
*self
,
655 const LttvTracesetContextPosition
*end_position
)
657 GTree
*pqueue
= self
->pqueue
;
659 guint fac_id
, ev_id
, id
;
661 LttvTracefileContext
*tfc
;
667 guint read_ret
= FALSE
;
669 gboolean last_ret
= FALSE
; /* return value of the last hook list called */
671 /* Get the next event from the pqueue, call its hooks,
672 reinsert in the pqueue the following event from the same tracefile
673 unless the tracefile is finished or the event is later than the
678 g_tree_foreach(pqueue
, get_first
, &tfc
);
679 /* End of traceset : tfc is NULL */
680 if(unlikely(tfc
== NULL
))
686 * - the maximum number of events specified?
687 * - the end position ?
689 * then the read is finished. We leave the queue in the same state and
693 if(unlikely(last_ret
== TRUE
||
694 ((count
>= nb_events
) && (nb_events
!= G_MAXULONG
)) ||
695 (end_position
!=NULL
&<tv_traceset_context_ctx_pos_compare(self
,
696 end_position
) == 0)||
697 ltt_time_compare(end
, tfc
->timestamp
) <= 0))
702 /* Get the tracefile with an event for the smallest time found. If two
703 or more tracefiles have events for the same time, hope that lookup
704 and remove are consistent. */
706 g_tree_remove(pqueue
, tfc
);
708 g_tree_foreach(pqueue
, test_tree
, tfc
);
711 e
= ltt_tracefile_get_event(tfc
->tf
);
712 fac_id
= ltt_event_facility_id(e
);
713 ev_id
= ltt_event_eventtype_id(e
);
714 id
= GET_HOOK_ID(fac_id
, ev_id
);
715 last_ret
= lttv_hooks_call_merge(tfc
->event
, tfc
,
716 lttv_hooks_by_id_get(tfc
->event_by_id
, id
), tfc
);
718 read_ret
= ltt_tracefile_read(tfc
->tf
);
720 if(likely(!read_ret
)) {
721 g_debug("got someting");
722 tfc
->timestamp
= ltt_event_time(e
);
723 g_tree_insert(pqueue
, tfc
, tfc
);
725 if(read_ret
== ERANGE
)
726 g_debug("End of trace");
728 g_error("Error happened in lttv_process_traceset_middle");
734 void lttv_process_traceset_end(LttvTracesetContext
*self
,
735 LttvHooks
*after_traceset
,
736 LttvHooks
*after_trace
,
737 LttvHooks
*after_tracefile
,
739 LttvHooksById
*event_by_id
)
741 /* Remove hooks from context. _after hooks are called by remove_hooks. */
742 /* It calls all after_traceset, after_trace, and after_tracefile hooks. */
743 lttv_traceset_context_remove_hooks(self
,
751 /* Subtile modification :
752 * if tracefile has no event at or after the time requested, it is not put in
753 * the queue, as the next read would fail. */
754 void lttv_process_trace_seek_time(LttvTraceContext
*self
, LttTime start
)
756 guint i
, nb_tracefile
;
760 LttvTracefileContext
*tfc
;
762 GTree
*pqueue
= self
->ts_context
->pqueue
;
764 nb_tracefile
= self
->tracefiles
->len
;
766 for(i
= 0 ; i
< nb_tracefile
; i
++) {
767 tfc
= g_array_index(self
->tracefiles
, LttvTracefileContext
*, i
);
768 ret
= ltt_tracefile_seek_time(tfc
->tf
, start
);
769 if(ret
== EPERM
) g_error("error in lttv_process_trace_seek_time seek");
770 g_tree_remove(pqueue
, tfc
);
772 if(ret
== 0) { /* not ERANGE especially */
773 tfc
->timestamp
= ltt_event_time(ltt_tracefile_get_event(tfc
->tf
));
774 g_tree_insert(pqueue
, tfc
, tfc
);
780 void lttv_process_traceset_seek_time(LttvTracesetContext
*self
, LttTime start
)
784 LttvTraceContext
*tc
;
786 nb_trace
= lttv_traceset_number(self
->ts
);
787 for(i
= 0 ; i
< nb_trace
; i
++) {
788 tc
= self
->traces
[i
];
789 lttv_process_trace_seek_time(tc
, start
);
794 gboolean
lttv_process_traceset_seek_position(LttvTracesetContext
*self
,
795 const LttvTracesetContextPosition
*pos
)
798 LttvTraceContext
*tc
;
799 LttvTracefileContext
*tfc
;
801 g_tree_destroy(self
->pqueue
);
802 self
->pqueue
= g_tree_new(compare_tracefile
);
804 for(i
=0;i
<pos
->ep
->len
; i
++) {
805 LttEventPosition
*ep
= g_array_index(pos
->ep
, LttEventPosition
*, i
);
806 LttvTracefileContext
*tfc
=
807 g_array_index(pos
->tfc
, LttvTracefileContext
*, i
);
808 g_assert(ltt_tracefile_seek_position(tfc
->tf
, ep
) == 0);
809 tfc
->timestamp
= ltt_event_time(ltt_tracefile_get_event(tfc
->tf
));
810 g_tree_insert(self
->pqueue
, tfc
, tfc
);
818 find_field(LttEventType
*et
, const GQuark field
)
829 if(field
== 0) return NULL
;
831 f
= ltt_eventtype_field(et
);
832 t
= ltt_eventtype_type(et
);
833 g_assert(ltt_type_class(t
) == LTT_STRUCT
);
834 nb
= ltt_type_member_number(t
);
835 for(i
= 0 ; i
< nb
; i
++) {
836 ltt_type_member_type(t
, i
, &name
);
837 if(name
== field
) break;
840 return ltt_field_member(f
, i
);
843 LttvTraceHookByFacility
*lttv_trace_hook_get_fac(LttvTraceHook
*th
,
846 return &g_array_index(th
->fac_index
, LttvTraceHookByFacility
, facility_id
);
849 /* Get the first facility corresponding to the name. As the types must be
850 * compatible, it is relevant to use the field name and sizes of the first
851 * facility to create data structures and assume the data will be compatible
852 * thorough the trace */
853 LttvTraceHookByFacility
*lttv_trace_hook_get_first(LttvTraceHook
*th
)
855 g_assert(th
->fac_list
->len
> 0);
856 return g_array_index(th
->fac_list
, LttvTraceHookByFacility
*, 0);
860 /* Returns 0 on success, -1 if fails. */
862 lttv_trace_find_hook(LttTrace
*t
, GQuark facility
, GQuark event
,
863 GQuark field1
, GQuark field2
, GQuark field3
, LttvHook h
, LttvTraceHook
*th
)
867 LttEventType
*et
, *first_et
;
871 guint i
, fac_id
, ev_id
;
873 LttvTraceHookByFacility
*thf
, *first_thf
;
875 facilities
= ltt_trace_facility_get_by_name(t
, facility
);
877 if(unlikely(facilities
== NULL
)) goto facility_error
;
879 th
->fac_index
= g_array_sized_new(FALSE
, TRUE
,
880 sizeof(LttvTraceHookByFacility
),
882 th
->fac_index
= g_array_set_size(th
->fac_index
, NUM_FACILITIES
);
884 th
->fac_list
= g_array_sized_new(FALSE
, TRUE
,
885 sizeof(LttvTraceHookByFacility
*),
887 th
->fac_list
= g_array_set_size(th
->fac_list
, facilities
->len
);
889 fac_id
= g_array_index(facilities
, guint
, 0);
890 f
= ltt_trace_get_facility_by_num(t
, fac_id
);
892 et
= ltt_facility_eventtype_get_by_name(f
, event
);
893 if(unlikely(et
== NULL
)) goto event_error
;
895 thf
= &g_array_index(th
->fac_index
, LttvTraceHookByFacility
, fac_id
);
896 g_array_index(th
->fac_list
, LttvTraceHookByFacility
*, 0)
899 ev_id
= ltt_eventtype_id(et
);
902 thf
->id
= GET_HOOK_ID(fac_id
, ev_id
);
903 thf
->f1
= find_field(et
, field1
);
904 thf
->f2
= find_field(et
, field2
);
905 thf
->f3
= find_field(et
, field3
);
909 /* Check for type compatibility too */
910 for(i
=1;i
<facilities
->len
;i
++) {
911 fac_id
= g_array_index(facilities
, guint
, i
);
912 f
= ltt_trace_get_facility_by_num(t
, fac_id
);
914 et
= ltt_facility_eventtype_get_by_name(f
, ltt_eventtype_name(et
));
915 if(unlikely(et
== NULL
)) goto event_error
;
917 thf
= &g_array_index(th
->fac_index
, LttvTraceHookByFacility
, fac_id
);
918 g_array_index(th
->fac_list
, LttvTraceHookByFacility
*, i
)
920 ev_id
= ltt_eventtype_id(et
);
922 thf
->id
= GET_HOOK_ID(fac_id
, ev_id
);
923 thf
->f1
= find_field(et
, field1
);
924 if(check_fields_compatibility(first_et
, et
,
925 first_thf
->f1
, thf
->f1
))
928 thf
->f2
= find_field(et
, field2
);
929 if(check_fields_compatibility(first_et
, et
,
930 first_thf
->f2
, thf
->f2
))
933 thf
->f3
= find_field(et
, field3
);
934 if(check_fields_compatibility(first_et
, et
,
935 first_thf
->f3
, thf
->f3
))
944 g_error("Event type %s does not exist",
945 g_quark_to_string(ltt_eventtype_name(et
)));
948 g_error("No %s facility", g_quark_to_string(facility
));
951 g_array_free(th
->fac_index
, TRUE
);
952 g_array_free(th
->fac_list
, TRUE
);
953 th
->fac_index
= NULL
;
958 void lttv_trace_hook_destroy(LttvTraceHook
*th
)
960 g_array_free(th
->fac_index
, TRUE
);
961 g_array_free(th
->fac_list
, TRUE
);
965 LttvTracesetContextPosition
*lttv_traceset_context_position_new()
967 LttvTracesetContextPosition
*pos
= g_new(LttvTracesetContextPosition
,1);
968 pos
->ep
= g_array_sized_new(FALSE
, TRUE
, sizeof(LttEventPosition
*),
970 pos
->tfc
= g_array_sized_new(FALSE
, TRUE
, sizeof(LttvTracefileContext
*),
972 pos
->timestamp
= ltt_time_infinite
;
976 gboolean
traverse_get_tfc(gpointer key
, gpointer value
, gpointer data
)
978 LttvTracefileContext
*tfc
= (LttvTracefileContext
*)value
;
979 LttvTracesetContextPosition
*pos
= (LttvTracesetContextPosition
*)data
;
981 LttEvent
*event
= ltt_tracefile_get_event(tfc
->tf
);
982 LttEventPosition
*ep
= ltt_event_position_new();
984 ltt_event_position(event
, ep
);
986 g_array_append_val(pos
->ep
, ep
);
987 g_array_append_val(pos
->tfc
, tfc
);
989 if(ltt_time_compare(tfc
->timestamp
, pos
->timestamp
) < 0)
990 pos
->timestamp
= tfc
->timestamp
;
995 /* Subtile modification :
996 * only save the tracefiles that are loaded in the pqueue */
997 void lttv_traceset_context_position_save(const LttvTracesetContext
*self
,
998 LttvTracesetContextPosition
*pos
)
1000 g_tree_foreach(self
->pqueue
, traverse_get_tfc
, pos
);
1003 void lttv_traceset_context_position_destroy(LttvTracesetContextPosition
*pos
)
1006 for(i
=0;i
<pos
->ep
->len
;i
++)
1007 g_free(g_array_index(pos
->ep
, LttEventPosition
*, i
));
1008 g_array_free(pos
->ep
, TRUE
);
1009 g_array_free(pos
->tfc
, TRUE
);
1013 void lttv_traceset_context_position_copy(LttvTracesetContextPosition
*dest
,
1014 const LttvTracesetContextPosition
*src
)
1018 g_array_set_size(dest
->ep
, src
->ep
->len
);
1019 g_array_set_size(dest
->tfc
, src
->tfc
->len
);
1021 for(i
=0;i
<src
->ep
->len
;i
++) {
1022 g_array_index(dest
->ep
, LttEventPosition
*, i
) = ltt_event_position_new();
1023 ltt_event_position_copy(
1024 g_array_index(dest
->ep
, LttEventPosition
*, i
),
1025 g_array_index(src
->ep
, LttEventPosition
*, i
));
1027 for(i
=0;i
<src
->tfc
->len
;i
++) {
1028 g_array_index(dest
->tfc
, LttvTracefileContext
*, i
) =
1029 g_array_index(src
->tfc
, LttvTracefileContext
*, i
);
1031 dest
->timestamp
= src
->timestamp
;
1034 gint
lttv_traceset_context_ctx_pos_compare(const LttvTracesetContext
*self
,
1035 const LttvTracesetContextPosition
*pos
)
1040 for(i
=0;i
<pos
->ep
->len
;i
++) {
1041 LttEventPosition
*ep
= g_array_index(pos
->ep
, LttEventPosition
*, i
);
1042 LttvTracefileContext
*tfc
=
1043 g_array_index(pos
->tfc
, LttvTracefileContext
*, i
);
1045 LttEvent
*event
= ltt_tracefile_get_event(tfc
->tf
);
1047 ret
= ltt_event_position_compare((LttEventPosition
*)event
,
1049 if(ret
!= 0) return ret
;
1056 gint
lttv_traceset_context_pos_pos_compare(
1057 const LttvTracesetContextPosition
*pos1
,
1058 const LttvTracesetContextPosition
*pos2
)
1063 for(i
=0;i
<pos1
->ep
->len
;i
++) {
1064 LttEventPosition
*ep1
= g_array_index(pos1
->ep
, LttEventPosition
*, i
);
1065 LttTracefile
*tf1
= ltt_event_position_tracefile(ep1
);
1067 for(j
=0;j
<pos2
->ep
->len
;j
++) {
1068 LttEventPosition
*ep2
= g_array_index(pos2
->ep
, LttEventPosition
*, j
);
1069 LttTracefile
*tf2
= ltt_event_position_tracefile(ep2
);
1072 ret
= ltt_event_position_compare(ep1
, ep2
);
1073 if(ret
!= 0) return ret
;
1082 LttTime
lttv_traceset_context_position_get_time(
1083 const LttvTracesetContextPosition
*pos
)
1085 return pos
->timestamp
;
1089 LttvTracefileContext
*lttv_traceset_context_get_current_tfc(LttvTracesetContext
*self
)
1091 GTree
*pqueue
= self
->pqueue
;
1092 LttvTracefileContext
*tfc
= NULL
;
1094 g_tree_foreach(pqueue
, get_first
, &tfc
);