Logging: standardize logging tags
[babeltrace.git] / src / lib / trace-ir / clock-snapshot.c
CommitLineData
605e1019
PP
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
350ad6c1 23#define BT_LOG_TAG "LIB/CLOCK-SNAPSHOT"
578e048b 24#include "lib/lib-logging.h"
605e1019 25
578e048b
MJ
26#include "lib/assert-pre.h"
27#include "compat/uuid.h"
28#include "clock-class.h"
29#include "clock-snapshot.h"
3fadfbc0 30#include <babeltrace2/trace-ir/clock-snapshot-const.h>
578e048b 31#include "compat/compiler.h"
3fadfbc0 32#include <babeltrace2/types.h>
578e048b 33#include "compat/string.h"
605e1019 34#include <inttypes.h>
578e048b
MJ
35#include "lib/object.h"
36#include "common/assert.h"
605e1019
PP
37
38BT_HIDDEN
39void bt_clock_snapshot_destroy(struct bt_clock_snapshot *clock_snapshot)
40{
41 BT_LIB_LOGD("Destroying clock snapshot: %!+k", clock_snapshot);
42 BT_OBJECT_PUT_REF_AND_RESET(clock_snapshot->clock_class);
43 g_free(clock_snapshot);
44}
45
46BT_HIDDEN
58085ca4
PP
47struct bt_clock_snapshot *bt_clock_snapshot_new(
48 struct bt_clock_class *clock_class)
605e1019
PP
49{
50 struct bt_clock_snapshot *ret = NULL;
51
52 BT_ASSERT(clock_class);
53 BT_LIB_LOGD("Creating clock snapshot object: %![cc-]+K=",
54 clock_class);
55 ret = g_new0(struct bt_clock_snapshot, 1);
56 if (!ret) {
57 BT_LOGE_STR("Failed to allocate one clock snapshot.");
58 goto end;
59 }
60
61 bt_object_init_unique(&ret->base);
62 ret->clock_class = clock_class;
63 bt_object_get_no_null_check(clock_class);
64 bt_clock_class_freeze(clock_class);
65 BT_LIB_LOGD("Created clock snapshot object: %!+k", ret);
66
67end:
68 return ret;
69}
70
71BT_HIDDEN
58085ca4
PP
72struct bt_clock_snapshot *bt_clock_snapshot_create(
73 struct bt_clock_class *clock_class)
605e1019
PP
74{
75 struct bt_clock_snapshot *clock_snapshot = NULL;
76
77 BT_ASSERT(clock_class);
78 clock_snapshot = bt_object_pool_create_object(&clock_class->cs_pool);
79 if (!clock_snapshot) {
80 BT_LIB_LOGE("Cannot allocate one clock snapshot from clock class's clock snapshot pool: "
81 "%![cc-]+K", clock_class);
82 goto error;
83 }
84
91d81473 85 if (G_LIKELY(!clock_snapshot->clock_class)) {
605e1019
PP
86 clock_snapshot->clock_class = clock_class;
87 bt_object_get_no_null_check(clock_class);
88 }
89
90 goto end;
91
92error:
93 if (clock_snapshot) {
94 bt_clock_snapshot_recycle(clock_snapshot);
95 clock_snapshot = NULL;
96 }
97
98end:
99 return clock_snapshot;
100}
101
102BT_HIDDEN
103void bt_clock_snapshot_recycle(struct bt_clock_snapshot *clock_snapshot)
104{
105 struct bt_clock_class *clock_class;
106
107 BT_ASSERT(clock_snapshot);
108 BT_LIB_LOGD("Recycling clock snapshot: %!+k", clock_snapshot);
109
110 /*
111 * Those are the important ordered steps:
112 *
113 * 1. Reset the clock snapshot object, but do NOT put its clock
114 * class's reference. This clock class contains the pool to
115 * which we're about to recycle this clock snapshot object,
116 * so we must guarantee its existence thanks to this existing
117 * reference.
118 *
119 * 2. Move the clock class reference to our `clock_class`
120 * variable so that we can set the clock snapshot's clock
121 * class member to NULL before recycling it. We CANNOT do
122 * this after we put the clock class reference because this
123 * bt_object_put_ref() could destroy the clock class, also
124 * destroying its clock snapshot pool, thus also destroying
125 * our clock snapshot object (this would result in an invalid
126 * write access).
127 *
128 * 3. Recycle the clock snapshot object.
129 *
130 * 4. Put our clock class reference.
131 */
132 bt_clock_snapshot_reset(clock_snapshot);
133 clock_class = clock_snapshot->clock_class;
134 BT_ASSERT(clock_class);
135 clock_snapshot->clock_class = NULL;
136 bt_object_pool_recycle_object(&clock_class->cs_pool, clock_snapshot);
137 bt_object_put_ref(clock_class);
138}
139
58085ca4
PP
140uint64_t bt_clock_snapshot_get_value(
141 const struct bt_clock_snapshot *clock_snapshot)
605e1019
PP
142{
143 BT_ASSERT_PRE_NON_NULL(clock_snapshot, "Clock snapshot");
144 BT_ASSERT_PRE(clock_snapshot->is_set,
145 "Clock snapshot is not set: %!+k", clock_snapshot);
146 return clock_snapshot->value_cycles;
147}
148
dc68f16d
PP
149enum bt_clock_snapshot_status bt_clock_snapshot_get_ns_from_origin(
150 const struct bt_clock_snapshot *clock_snapshot,
605e1019
PP
151 int64_t *ret_value_ns)
152{
dc68f16d
PP
153 int ret = BT_CLOCK_SNAPSHOT_STATUS_OK;
154
605e1019
PP
155 BT_ASSERT_PRE_NON_NULL(clock_snapshot, "Clock snapshot");
156 BT_ASSERT_PRE_NON_NULL(ret_value_ns, "Value (ns) (output)");
157 BT_ASSERT_PRE(clock_snapshot->is_set,
158 "Clock snapshot is not set: %!+k", clock_snapshot);
159
160 if (clock_snapshot->ns_from_origin_overflows) {
161 BT_LIB_LOGD("Clock snapshot, once converted to nanoseconds from origin, "
162 "overflows the signed 64-bit integer range: "
163 "%![cs-]+k", clock_snapshot);
dc68f16d 164 ret = BT_CLOCK_SNAPSHOT_STATUS_OVERFLOW;
605e1019
PP
165 goto end;
166 }
167
168 *ret_value_ns = clock_snapshot->ns_from_origin;
169
170end:
171 return ret;
172}
173
174const struct bt_clock_class *bt_clock_snapshot_borrow_clock_class_const(
175 const struct bt_clock_snapshot *clock_snapshot)
176{
177 BT_ASSERT_PRE_NON_NULL(clock_snapshot, "Clock snapshot");
178 return clock_snapshot->clock_class;
179}
This page took 0.03464 seconds and 4 git commands to generate.