Fix: emf uri: surround by " "
[babeltrace.git] / lib / trace-collection.c
CommitLineData
6cba487f
MD
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 */
75d9ab4b 27#include <babeltrace/clock-internal.h>
6cba487f
MD
28
29#include <inttypes.h>
30
31struct clock_match {
32 GHashTable *clocks;
33 struct ctf_clock *clock_match;
34 struct trace_collection *tc;
35};
36
37static 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
50052405 42 if (clock_a->absolute) {
6cba487f 43 /*
50052405
MD
44 * Absolute time references, such as NTP, are looked up
45 * by clock name.
6cba487f
MD
46 */
47 clock_b = g_hash_table_lookup(match->clocks,
50052405 48 (gpointer) (unsigned long) clock_a->name);
6cba487f
MD
49 if (clock_b) {
50 match->clock_match = clock_b;
51 return;
52 }
50052405 53 } else if (clock_a->uuid != 0) {
6cba487f 54 /*
50052405
MD
55 * Lookup the the trace clocks into the collection
56 * clocks.
6cba487f
MD
57 */
58 clock_b = g_hash_table_lookup(match->clocks,
50052405 59 (gpointer) (unsigned long) clock_a->uuid);
6cba487f
MD
60 if (clock_b) {
61 match->clock_match = clock_b;
62 return;
63 }
64 }
65}
66
75d9ab4b
MD
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 */
73static
74uint64_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
6cba487f
MD
80static 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 /*
50052405
MD
98 * For now we only support CTF that has one
99 * single clock uuid or name (absolute ref) per
100 * trace.
6cba487f
MD
101 */
102 if (g_hash_table_size(tc_clocks) > 0) {
103 fprintf(stderr, "[error] Only CTF traces with a single clock description are supported by this babeltrace version.\n");
104 }
105 if (!clock_match->tc->offset_nr) {
75d9ab4b 106 clock_match->tc->offset_first = clock_offset_ns(t_clock);
6cba487f
MD
107 clock_match->tc->delta_offset_first_sum = 0;
108 clock_match->tc->offset_nr++;
109 clock_match->tc->single_clock_offset_avg =
110 clock_match->tc->offset_first;
111 }
112 g_hash_table_insert(tc_clocks,
113 (gpointer) (unsigned long) v,
114 value);
50052405 115 } else if (!t_clock->absolute) {
6cba487f
MD
116 int64_t diff_ns;
117
118 /*
50052405
MD
119 * For non-absolute clocks, check that the
120 * offsets match. If not, warn the user that we
121 * do an arbitrary choice.
6cba487f 122 */
75d9ab4b 123 diff_ns = clock_offset_ns(tc_clock) - clock_offset_ns(t_clock);
6cba487f
MD
124 printf_debug("Clock \"%s\" offset between traces has a delta of %" PRIu64 " ns.",
125 g_quark_to_string(tc_clock->name),
126 diff_ns < 0 ? -diff_ns : diff_ns);
75d9ab4b 127 if (diff_ns > 10000 || diff_ns < -10000) {
6cba487f
MD
128 fprintf(stderr, "[warning] Clock \"%s\" offset differs between traces (delta %" PRIu64 " ns). Using average.\n",
129 g_quark_to_string(tc_clock->name),
130 diff_ns < 0 ? -diff_ns : diff_ns);
131 }
132 /* Compute average */
133 clock_match->tc->delta_offset_first_sum +=
75d9ab4b 134 clock_offset_ns(t_clock) - clock_match->tc->offset_first;
6cba487f
MD
135 clock_match->tc->offset_nr++;
136 clock_match->tc->single_clock_offset_avg =
137 clock_match->tc->offset_first
138 + (clock_match->tc->delta_offset_first_sum / clock_match->tc->offset_nr);
50052405
MD
139 /* Time need to use offset average */
140 clock_match->tc->clock_use_offset_avg = 1;
6cba487f
MD
141 }
142 }
143}
144
145/*
146 * Whenever we add a trace to the trace collection, check that we can
03798a93
JD
147 * correlate this trace with at least one other clock in the trace and
148 * convert the index from cycles to real time.
6cba487f
MD
149 */
150int trace_collection_add(struct trace_collection *tc,
151 struct trace_descriptor *td)
152{
7f89ddce 153 struct ctf_trace *trace;
6cba487f 154
7f89ddce
MD
155 if (!tc || !td)
156 return -EINVAL;
157
158 trace = container_of(td, struct ctf_trace, parent);
6cba487f
MD
159 g_ptr_array_add(tc->array, td);
160 trace->collection = tc;
161
162 if (tc->array->len > 1) {
163 struct clock_match clock_match = {
164 .clocks = tc->clocks,
165 .clock_match = NULL,
166 .tc = NULL,
167 };
168
169 /*
170 * With two or more traces, we need correlation info
171 * avalable.
172 */
173 g_hash_table_foreach(trace->clocks,
174 check_clock_match,
175 &clock_match);
176 if (!clock_match.clock_match) {
17d37c4d 177 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");
6cba487f
MD
178 goto error;
179 }
180 }
181
182 {
183 struct clock_match clock_match = {
184 .clocks = tc->clocks,
185 .clock_match = NULL,
186 .tc = tc,
187 };
188
189 /*
190 * Add each clock from the trace clocks into the trace
191 * collection clocks.
192 */
193 g_hash_table_foreach(trace->clocks,
194 clock_add,
195 &clock_match);
196 }
03798a93 197
6cba487f
MD
198 return 0;
199error:
200 return -EPERM;
201}
202
203int trace_collection_remove(struct trace_collection *tc,
204 struct trace_descriptor *td)
205{
7f89ddce
MD
206 if (!tc || !td)
207 return -EINVAL;
208
6cba487f
MD
209 if (g_ptr_array_remove(tc->array, td)) {
210 return 0;
211 } else {
212 return -1;
213 }
214
215}
216
217void init_trace_collection(struct trace_collection *tc)
218{
7f89ddce 219 assert(tc);
6cba487f
MD
220 tc->array = g_ptr_array_new();
221 tc->clocks = g_hash_table_new(g_direct_hash, g_direct_equal);
222 tc->single_clock_offset_avg = 0;
223 tc->offset_first = 0;
224 tc->delta_offset_first_sum = 0;
225 tc->offset_nr = 0;
226}
227
228/*
229 * finalize_trace_collection() closes the opened traces for read
230 * and free the memory allocated for trace collection
231 */
232void finalize_trace_collection(struct trace_collection *tc)
233{
7f89ddce 234 assert(tc);
6cba487f
MD
235 g_ptr_array_free(tc->array, TRUE);
236 g_hash_table_destroy(tc->clocks);
237}
This page took 0.032004 seconds and 4 git commands to generate.