Replace assert() -> BT_ASSERT() and some preconditions with BT_ASSERT_PRE()
[babeltrace.git] / lib / ctf-writer / clock.c
CommitLineData
ac0c6bdd
PP
1/*
2 * clock.c
3 *
4 * Babeltrace CTF IR - Clock
5 *
6 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 * Copyright 2017 Philippe Proulx <pproulx@efficios.com>
8 *
9 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 * SOFTWARE.
28 */
29
f6ccaed9
PP
30#define BT_LOG_TAG "CTF-WRITER-CLOCK"
31#include <babeltrace/lib-logging-internal.h>
32
ac0c6bdd
PP
33#include <babeltrace/ctf-writer/clock-internal.h>
34#include <babeltrace/ctf-ir/clock-class.h>
35#include <babeltrace/ctf-ir/clock-class-internal.h>
36#include <babeltrace/ctf-ir/utils.h>
75c3fca1 37#include <babeltrace/compat/uuid-internal.h>
ac0c6bdd
PP
38#include <babeltrace/ref.h>
39#include <babeltrace/object-internal.h>
3d9990ac 40#include <babeltrace/compiler-internal.h>
f6ccaed9
PP
41#include <babeltrace/assert-internal.h>
42#include <babeltrace/assert-pre-internal.h>
ac0c6bdd
PP
43#include <inttypes.h>
44
45static
46void bt_ctf_clock_destroy(struct bt_object *obj);
47
48struct bt_ctf_clock *bt_ctf_clock_create(const char *name)
49{
75c3fca1 50 int ret;
ac0c6bdd 51 struct bt_ctf_clock *clock = NULL;
75c3fca1 52 unsigned char cc_uuid[BABELTRACE_UUID_LEN];
ac0c6bdd 53
f6ccaed9 54 BT_ASSERT_PRE_NON_NULL(name, "Name");
ac0c6bdd 55 clock = g_new0(struct bt_ctf_clock, 1);
ac0c6bdd
PP
56 if (!clock) {
57 goto error;
58 }
59
60 bt_object_init(clock, bt_ctf_clock_destroy);
61 clock->value = 0;
f3534905
PP
62
63 /* Pre-2.0.0 backward compatibility: default frequency is 1 GHz */
50842bdc 64 clock->clock_class = bt_clock_class_create(name, 1000000000);
ac0c6bdd
PP
65 if (!clock->clock_class) {
66 goto error;
67 }
75c3fca1
PP
68
69 /* Automatically set clock class's UUID. */
70 ret = bt_uuid_generate(cc_uuid);
71 if (ret) {
72 goto error;
73 }
74
50842bdc 75 ret = bt_clock_class_set_uuid(clock->clock_class, cc_uuid);
f6ccaed9 76 BT_ASSERT(ret == 0);
ac0c6bdd
PP
77 return clock;
78
79error:
80 BT_PUT(clock);
81 return clock;
82}
83
84const char *bt_ctf_clock_get_name(struct bt_ctf_clock *clock)
85{
f6ccaed9
PP
86 BT_ASSERT_PRE_NON_NULL(clock, "CTF writer clock");
87 return bt_clock_class_get_name(clock->clock_class);
ac0c6bdd
PP
88}
89
90const char *bt_ctf_clock_get_description(struct bt_ctf_clock *clock)
91{
f6ccaed9
PP
92 BT_ASSERT_PRE_NON_NULL(clock, "CTF writer clock");
93 return bt_clock_class_get_description(clock->clock_class);
ac0c6bdd
PP
94}
95
96int bt_ctf_clock_set_description(struct bt_ctf_clock *clock, const char *desc)
97{
f6ccaed9
PP
98 BT_ASSERT_PRE_NON_NULL(clock, "CTF writer clock");
99 return bt_clock_class_set_description(clock->clock_class, desc);
ac0c6bdd
PP
100}
101
102uint64_t bt_ctf_clock_get_frequency(struct bt_ctf_clock *clock)
103{
f6ccaed9
PP
104 BT_ASSERT_PRE_NON_NULL(clock, "CTF writer clock");
105 return bt_clock_class_get_frequency(clock->clock_class);
ac0c6bdd
PP
106}
107
108int bt_ctf_clock_set_frequency(struct bt_ctf_clock *clock, uint64_t freq)
109{
f6ccaed9
PP
110 BT_ASSERT_PRE_NON_NULL(clock, "CTF writer clock");
111 return bt_clock_class_set_frequency(clock->clock_class, freq);
ac0c6bdd
PP
112}
113
114uint64_t bt_ctf_clock_get_precision(struct bt_ctf_clock *clock)
115{
f6ccaed9
PP
116 BT_ASSERT_PRE_NON_NULL(clock, "CTF writer clock");
117 return bt_clock_class_get_precision(clock->clock_class);
ac0c6bdd
PP
118}
119
120int bt_ctf_clock_set_precision(struct bt_ctf_clock *clock, uint64_t precision)
121{
f6ccaed9
PP
122 BT_ASSERT_PRE_NON_NULL(clock, "CTF writer clock");
123 return bt_clock_class_set_precision(clock->clock_class, precision);
ac0c6bdd
PP
124}
125
126int bt_ctf_clock_get_offset_s(struct bt_ctf_clock *clock, int64_t *offset_s)
127{
f6ccaed9
PP
128 BT_ASSERT_PRE_NON_NULL(clock, "CTF writer clock");
129 return bt_clock_class_get_offset_s(clock->clock_class, offset_s);
ac0c6bdd
PP
130}
131
132int bt_ctf_clock_set_offset_s(struct bt_ctf_clock *clock, int64_t offset_s)
133{
f6ccaed9
PP
134 BT_ASSERT_PRE_NON_NULL(clock, "CTF writer clock");
135 return bt_clock_class_set_offset_s(clock->clock_class, offset_s);
ac0c6bdd
PP
136}
137
138int bt_ctf_clock_get_offset(struct bt_ctf_clock *clock, int64_t *offset)
139{
f6ccaed9
PP
140 BT_ASSERT_PRE_NON_NULL(clock, "CTF writer clock");
141 return bt_clock_class_get_offset_cycles(clock->clock_class, offset);
ac0c6bdd
PP
142}
143
144int bt_ctf_clock_set_offset(struct bt_ctf_clock *clock, int64_t offset)
145{
f6ccaed9
PP
146 BT_ASSERT_PRE_NON_NULL(clock, "CTF writer clock");
147 return bt_clock_class_set_offset_cycles(clock->clock_class, offset);
ac0c6bdd
PP
148}
149
150int bt_ctf_clock_get_is_absolute(struct bt_ctf_clock *clock)
151{
f6ccaed9
PP
152 BT_ASSERT_PRE_NON_NULL(clock, "CTF writer clock");
153 return bt_clock_class_is_absolute(clock->clock_class);
ac0c6bdd
PP
154}
155
156int bt_ctf_clock_set_is_absolute(struct bt_ctf_clock *clock, int is_absolute)
157{
f6ccaed9
PP
158 BT_ASSERT_PRE_NON_NULL(clock, "CTF writer clock");
159 return bt_clock_class_set_is_absolute(clock->clock_class, is_absolute);
ac0c6bdd
PP
160}
161
162const unsigned char *bt_ctf_clock_get_uuid(struct bt_ctf_clock *clock)
163{
f6ccaed9
PP
164 BT_ASSERT_PRE_NON_NULL(clock, "CTF writer clock");
165 return bt_clock_class_get_uuid(clock->clock_class);
ac0c6bdd
PP
166}
167
168int bt_ctf_clock_set_uuid(struct bt_ctf_clock *clock, const unsigned char *uuid)
169{
f6ccaed9
PP
170 BT_ASSERT_PRE_NON_NULL(clock, "CTF writer clock");
171 return bt_clock_class_set_uuid(clock->clock_class, uuid);
ac0c6bdd
PP
172}
173
174int bt_ctf_clock_set_time(struct bt_ctf_clock *clock, int64_t time)
175{
ac0c6bdd
PP
176 int64_t value;
177
f6ccaed9 178 BT_ASSERT_PRE_NON_NULL(clock, "CTF writer clock");
ac0c6bdd
PP
179
180 /* Common case where cycles are actually nanoseconds */
181 if (clock->clock_class->frequency == 1000000000) {
182 value = time;
183 } else {
184 value = (uint64_t) (((double) time *
185 (double) clock->clock_class->frequency) / 1e9);
186 }
187
f6ccaed9
PP
188 BT_ASSERT_PRE(clock->value <= value,
189 "CTF writer clock value must be updated monotonically: "
190 "prev-value=%" PRId64 ", new-value=%" PRId64,
191 clock->value, value);
ac0c6bdd 192 clock->value = value;
f6ccaed9 193 return 0;
ac0c6bdd
PP
194}
195
196void bt_ctf_clock_get(struct bt_ctf_clock *clock)
197{
198 bt_get(clock);
199}
200
201void bt_ctf_clock_put(struct bt_ctf_clock *clock)
202{
203 bt_put(clock);
204}
205
206BT_HIDDEN
207int bt_ctf_clock_get_value(struct bt_ctf_clock *clock, uint64_t *value)
208{
f6ccaed9
PP
209 BT_ASSERT_PRE_NON_NULL(clock, "CTF writer clock");
210 BT_ASSERT_PRE_NON_NULL(value, "Value");
ac0c6bdd 211 *value = clock->value;
f6ccaed9 212 return 0;
ac0c6bdd
PP
213}
214
215static
216void bt_ctf_clock_destroy(struct bt_object *obj)
217{
218 struct bt_ctf_clock *clock;
219
220 clock = container_of(obj, struct bt_ctf_clock, base);
221 bt_put(clock->clock_class);
222 g_free(clock);
223}
This page took 0.06043 seconds and 4 git commands to generate.