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