Fix: API: remove unsupported BT_SEEK_END from API
[babeltrace.git] / lib / trace-collection.c
1 /*
2 * trace-collection.c
3 *
4 * Babeltrace Library
5 *
6 * Copyright 2012 EfficiOS Inc. and Linux Foundation
7 *
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 */
20 #include <babeltrace/babeltrace.h>
21 #include <babeltrace/format.h>
22 #include <babeltrace/context.h>
23 #include <babeltrace/ctf/types.h>
24 #include <babeltrace/ctf-text/types.h>
25 #include <babeltrace/trace-collection.h>
26 #include <babeltrace/ctf-ir/metadata.h> /* for clocks */
27 #include <babeltrace/clock-internal.h>
28
29 #include <inttypes.h>
30
31 struct clock_match {
32 GHashTable *clocks;
33 struct ctf_clock *clock_match;
34 struct trace_collection *tc;
35 };
36
37 static void check_clock_match(gpointer key, gpointer value, gpointer user_data)
38 {
39 struct clock_match *match = user_data;
40 struct ctf_clock *clock_a = value, *clock_b;
41
42 if (clock_a->uuid != 0) {
43 /*
44 * Lookup the the trace clocks into the collection
45 * clocks.
46 */
47 clock_b = g_hash_table_lookup(match->clocks,
48 (gpointer) (unsigned long) clock_a->uuid);
49 if (clock_b) {
50 match->clock_match = clock_b;
51 return;
52 }
53 } else if (clock_a->absolute) {
54 /*
55 * Absolute time references, such as NTP, are looked up
56 * by clock name.
57 */
58 clock_b = g_hash_table_lookup(match->clocks,
59 (gpointer) (unsigned long) clock_a->name);
60 if (clock_b) {
61 match->clock_match = clock_b;
62 return;
63 }
64 }
65 }
66
67 /*
68 * Note: if using a frequency different from 1GHz for clock->offset, it
69 * is recommended to express the seconds in offset_s, otherwise there
70 * will be a loss of precision caused by the limited size of the double
71 * mantissa.
72 */
73 static
74 uint64_t clock_offset_ns(struct ctf_clock *clock)
75 {
76 return clock->offset_s * 1000000000ULL
77 + clock_cycles_to_ns(clock, clock->offset);
78 }
79
80 static void clock_add(gpointer key, gpointer value, gpointer user_data)
81 {
82 struct clock_match *clock_match = user_data;
83 GHashTable *tc_clocks = clock_match->clocks;
84 struct ctf_clock *t_clock = value;
85 GQuark v;
86
87 if (t_clock->absolute)
88 v = t_clock->name;
89 else
90 v = t_clock->uuid;
91 if (v) {
92 struct ctf_clock *tc_clock;
93
94 tc_clock = g_hash_table_lookup(tc_clocks,
95 (gpointer) (unsigned long) v);
96 if (!tc_clock) {
97 /*
98 * For now, we only support CTF that has one
99 * single clock uuid or name (absolute ref).
100 */
101 if (g_hash_table_size(tc_clocks) > 0) {
102 fprintf(stderr, "[error] Only CTF traces with a single clock description are supported by this babeltrace version.\n");
103 }
104 if (!clock_match->tc->offset_nr) {
105 clock_match->tc->offset_first = clock_offset_ns(t_clock);
106 clock_match->tc->delta_offset_first_sum = 0;
107 clock_match->tc->offset_nr++;
108 clock_match->tc->single_clock_offset_avg =
109 clock_match->tc->offset_first;
110 }
111 g_hash_table_insert(tc_clocks,
112 (gpointer) (unsigned long) v,
113 value);
114 } else {
115 int64_t diff_ns;
116
117 /*
118 * Check that the offsets match. If not, warn
119 * the user that we do an arbitrary choice.
120 */
121 diff_ns = clock_offset_ns(tc_clock) - clock_offset_ns(t_clock);
122 printf_debug("Clock \"%s\" offset between traces has a delta of %" PRIu64 " ns.",
123 g_quark_to_string(tc_clock->name),
124 diff_ns < 0 ? -diff_ns : diff_ns);
125 if (diff_ns > 10000 || diff_ns < -10000) {
126 fprintf(stderr, "[warning] Clock \"%s\" offset differs between traces (delta %" PRIu64 " ns). Using average.\n",
127 g_quark_to_string(tc_clock->name),
128 diff_ns < 0 ? -diff_ns : diff_ns);
129 }
130 /* Compute average */
131 clock_match->tc->delta_offset_first_sum +=
132 clock_offset_ns(t_clock) - clock_match->tc->offset_first;
133 clock_match->tc->offset_nr++;
134 clock_match->tc->single_clock_offset_avg =
135 clock_match->tc->offset_first
136 + (clock_match->tc->delta_offset_first_sum / clock_match->tc->offset_nr);
137 }
138 }
139 }
140
141 /*
142 * Whenever we add a trace to the trace collection, check that we can
143 * correlate this trace with at least one other clock in the trace and
144 * convert the index from cycles to real time.
145 */
146 int trace_collection_add(struct trace_collection *tc,
147 struct trace_descriptor *td)
148 {
149 struct ctf_trace *trace;
150
151 if (!tc || !td)
152 return -EINVAL;
153
154 trace = container_of(td, struct ctf_trace, parent);
155 g_ptr_array_add(tc->array, td);
156 trace->collection = tc;
157
158 if (tc->array->len > 1) {
159 struct clock_match clock_match = {
160 .clocks = tc->clocks,
161 .clock_match = NULL,
162 .tc = NULL,
163 };
164
165 /*
166 * With two or more traces, we need correlation info
167 * avalable.
168 */
169 g_hash_table_foreach(trace->clocks,
170 check_clock_match,
171 &clock_match);
172 if (!clock_match.clock_match) {
173 fprintf(stderr, "[error] No clocks can be correlated and multiple traces are added to the collection. If you are certain those traces can be correlated, try using \"--clock-force-correlate\".\n");
174 goto error;
175 }
176 }
177
178 {
179 struct clock_match clock_match = {
180 .clocks = tc->clocks,
181 .clock_match = NULL,
182 .tc = tc,
183 };
184
185 /*
186 * Add each clock from the trace clocks into the trace
187 * collection clocks.
188 */
189 g_hash_table_foreach(trace->clocks,
190 clock_add,
191 &clock_match);
192 }
193
194 return 0;
195 error:
196 return -EPERM;
197 }
198
199 int trace_collection_remove(struct trace_collection *tc,
200 struct trace_descriptor *td)
201 {
202 if (!tc || !td)
203 return -EINVAL;
204
205 if (g_ptr_array_remove(tc->array, td)) {
206 return 0;
207 } else {
208 return -1;
209 }
210
211 }
212
213 void init_trace_collection(struct trace_collection *tc)
214 {
215 assert(tc);
216 tc->array = g_ptr_array_new();
217 tc->clocks = g_hash_table_new(g_direct_hash, g_direct_equal);
218 tc->single_clock_offset_avg = 0;
219 tc->offset_first = 0;
220 tc->delta_offset_first_sum = 0;
221 tc->offset_nr = 0;
222 }
223
224 /*
225 * finalize_trace_collection() closes the opened traces for read
226 * and free the memory allocated for trace collection
227 */
228 void finalize_trace_collection(struct trace_collection *tc)
229 {
230 assert(tc);
231 g_ptr_array_free(tc->array, TRUE);
232 g_hash_table_destroy(tc->clocks);
233 }
This page took 0.033693 seconds and 4 git commands to generate.