lib: add internal BT_LIB_LOG*_APPEND_CAUSE() macros
[babeltrace.git] / src / lib / trace-ir / clock-snapshot.c
1 /*
2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 #define BT_LOG_TAG "LIB/CLOCK-SNAPSHOT"
24 #include "lib/logging.h"
25
26 #include "lib/assert-pre.h"
27 #include "compat/uuid.h"
28 #include "clock-class.h"
29 #include "clock-snapshot.h"
30 #include <babeltrace2/trace-ir/clock-snapshot-const.h>
31 #include "compat/compiler.h"
32 #include <babeltrace2/types.h>
33 #include "compat/string.h"
34 #include <inttypes.h>
35 #include "lib/object.h"
36 #include "common/assert.h"
37 #include "lib/func-status.h"
38
39 BT_HIDDEN
40 void bt_clock_snapshot_destroy(struct bt_clock_snapshot *clock_snapshot)
41 {
42 BT_LIB_LOGD("Destroying clock snapshot: %!+k", clock_snapshot);
43 BT_OBJECT_PUT_REF_AND_RESET(clock_snapshot->clock_class);
44 g_free(clock_snapshot);
45 }
46
47 BT_HIDDEN
48 struct bt_clock_snapshot *bt_clock_snapshot_new(
49 struct bt_clock_class *clock_class)
50 {
51 struct bt_clock_snapshot *ret = NULL;
52
53 BT_ASSERT(clock_class);
54 BT_LIB_LOGD("Creating clock snapshot object: %![cc-]+K=",
55 clock_class);
56 ret = g_new0(struct bt_clock_snapshot, 1);
57 if (!ret) {
58 BT_LOGE_STR("Failed to allocate one clock snapshot.");
59 goto end;
60 }
61
62 bt_object_init_unique(&ret->base);
63 ret->clock_class = clock_class;
64 bt_object_get_no_null_check(clock_class);
65 bt_clock_class_freeze(clock_class);
66 BT_LIB_LOGD("Created clock snapshot object: %!+k", ret);
67
68 end:
69 return ret;
70 }
71
72 BT_HIDDEN
73 struct bt_clock_snapshot *bt_clock_snapshot_create(
74 struct bt_clock_class *clock_class)
75 {
76 struct bt_clock_snapshot *clock_snapshot = NULL;
77
78 BT_ASSERT(clock_class);
79 clock_snapshot = bt_object_pool_create_object(&clock_class->cs_pool);
80 if (!clock_snapshot) {
81 BT_LIB_LOGE("Cannot allocate one clock snapshot from clock class's clock snapshot pool: "
82 "%![cc-]+K", clock_class);
83 goto error;
84 }
85
86 if (G_LIKELY(!clock_snapshot->clock_class)) {
87 clock_snapshot->clock_class = clock_class;
88 bt_object_get_no_null_check(clock_class);
89 }
90
91 goto end;
92
93 error:
94 if (clock_snapshot) {
95 bt_clock_snapshot_recycle(clock_snapshot);
96 clock_snapshot = NULL;
97 }
98
99 end:
100 return clock_snapshot;
101 }
102
103 BT_HIDDEN
104 void bt_clock_snapshot_recycle(struct bt_clock_snapshot *clock_snapshot)
105 {
106 struct bt_clock_class *clock_class;
107
108 BT_ASSERT(clock_snapshot);
109 BT_LIB_LOGD("Recycling clock snapshot: %!+k", clock_snapshot);
110
111 /*
112 * Those are the important ordered steps:
113 *
114 * 1. Reset the clock snapshot object, but do NOT put its clock
115 * class's reference. This clock class contains the pool to
116 * which we're about to recycle this clock snapshot object,
117 * so we must guarantee its existence thanks to this existing
118 * reference.
119 *
120 * 2. Move the clock class reference to our `clock_class`
121 * variable so that we can set the clock snapshot's clock
122 * class member to NULL before recycling it. We CANNOT do
123 * this after we put the clock class reference because this
124 * bt_object_put_ref() could destroy the clock class, also
125 * destroying its clock snapshot pool, thus also destroying
126 * our clock snapshot object (this would result in an invalid
127 * write access).
128 *
129 * 3. Recycle the clock snapshot object.
130 *
131 * 4. Put our clock class reference.
132 */
133 bt_clock_snapshot_reset(clock_snapshot);
134 clock_class = clock_snapshot->clock_class;
135 BT_ASSERT(clock_class);
136 clock_snapshot->clock_class = NULL;
137 bt_object_pool_recycle_object(&clock_class->cs_pool, clock_snapshot);
138 bt_object_put_ref(clock_class);
139 }
140
141 uint64_t bt_clock_snapshot_get_value(
142 const struct bt_clock_snapshot *clock_snapshot)
143 {
144 BT_ASSERT_PRE_NON_NULL(clock_snapshot, "Clock snapshot");
145 BT_ASSERT_PRE(clock_snapshot->is_set,
146 "Clock snapshot is not set: %!+k", clock_snapshot);
147 return clock_snapshot->value_cycles;
148 }
149
150 enum bt_clock_snapshot_get_ns_from_origin_status
151 bt_clock_snapshot_get_ns_from_origin(
152 const struct bt_clock_snapshot *clock_snapshot,
153 int64_t *ret_value_ns)
154 {
155 int ret = BT_FUNC_STATUS_OK;
156
157 BT_ASSERT_PRE_NON_NULL(clock_snapshot, "Clock snapshot");
158 BT_ASSERT_PRE_NON_NULL(ret_value_ns, "Value (ns) (output)");
159 BT_ASSERT_PRE(clock_snapshot->is_set,
160 "Clock snapshot is not set: %!+k", clock_snapshot);
161
162 if (clock_snapshot->ns_from_origin_overflows) {
163 BT_LIB_LOGD("Clock snapshot, once converted to nanoseconds from origin, "
164 "overflows the signed 64-bit integer range: "
165 "%![cs-]+k", clock_snapshot);
166 ret = BT_FUNC_STATUS_OVERFLOW;
167 goto end;
168 }
169
170 *ret_value_ns = clock_snapshot->ns_from_origin;
171
172 end:
173 return ret;
174 }
175
176 const struct bt_clock_class *bt_clock_snapshot_borrow_clock_class_const(
177 const struct bt_clock_snapshot *clock_snapshot)
178 {
179 BT_ASSERT_PRE_NON_NULL(clock_snapshot, "Clock snapshot");
180 return clock_snapshot->clock_class;
181 }
This page took 0.032202 seconds and 4 git commands to generate.