2 * SPDX-License-Identifier: MIT
4 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
7 #define BT_LOG_TAG "LIB/CLOCK-SNAPSHOT"
8 #include "lib/logging.h"
10 #include "lib/assert-cond.h"
11 #include "common/uuid.h"
12 #include "clock-class.h"
13 #include "clock-snapshot.h"
14 #include <babeltrace2/trace-ir/clock-snapshot.h>
15 #include "compat/compiler.h"
16 #include <babeltrace2/types.h>
17 #include "compat/string.h"
19 #include "lib/object.h"
20 #include "common/assert.h"
21 #include "lib/func-status.h"
24 void bt_clock_snapshot_destroy(struct bt_clock_snapshot
*clock_snapshot
)
26 BT_ASSERT(clock_snapshot
);
27 BT_LIB_LOGD("Destroying clock snapshot: %!+k", clock_snapshot
);
28 BT_OBJECT_PUT_REF_AND_RESET(clock_snapshot
->clock_class
);
29 g_free(clock_snapshot
);
33 struct bt_clock_snapshot
*bt_clock_snapshot_new(
34 struct bt_clock_class
*clock_class
)
36 struct bt_clock_snapshot
*ret
= NULL
;
38 BT_ASSERT(clock_class
);
39 BT_LIB_LOGD("Creating clock snapshot object: %![cc-]+K=",
41 ret
= g_new0(struct bt_clock_snapshot
, 1);
43 BT_LIB_LOGE_APPEND_CAUSE(
44 "Failed to allocate one clock snapshot.");
48 bt_object_init_unique(&ret
->base
);
49 ret
->clock_class
= clock_class
;
50 bt_object_get_ref_no_null_check(clock_class
);
51 bt_clock_class_freeze(clock_class
);
52 BT_LIB_LOGD("Created clock snapshot object: %!+k", ret
);
59 struct bt_clock_snapshot
*bt_clock_snapshot_create(
60 struct bt_clock_class
*clock_class
)
62 struct bt_clock_snapshot
*clock_snapshot
= NULL
;
64 BT_ASSERT_DBG(clock_class
);
65 clock_snapshot
= bt_object_pool_create_object(&clock_class
->cs_pool
);
66 if (!clock_snapshot
) {
67 BT_LIB_LOGE_APPEND_CAUSE(
68 "Cannot allocate one clock snapshot from clock class's clock snapshot pool: "
69 "%![cc-]+K", clock_class
);
73 if (G_LIKELY(!clock_snapshot
->clock_class
)) {
74 clock_snapshot
->clock_class
= clock_class
;
75 bt_object_get_ref_no_null_check(clock_class
);
79 return clock_snapshot
;
83 void bt_clock_snapshot_recycle(struct bt_clock_snapshot
*clock_snapshot
)
85 struct bt_clock_class
*clock_class
;
87 BT_ASSERT_DBG(clock_snapshot
);
88 BT_LIB_LOGD("Recycling clock snapshot: %!+k", clock_snapshot
);
91 * Those are the important ordered steps:
93 * 1. Reset the clock snapshot object, but do NOT put its clock
94 * class's reference. This clock class contains the pool to
95 * which we're about to recycle this clock snapshot object,
96 * so we must guarantee its existence thanks to this existing
99 * 2. Move the clock class reference to our `clock_class`
100 * variable so that we can set the clock snapshot's clock
101 * class member to NULL before recycling it. We CANNOT do
102 * this after we put the clock class reference because this
103 * bt_object_put_ref() could destroy the clock class, also
104 * destroying its clock snapshot pool, thus also destroying
105 * our clock snapshot object (this would result in an invalid
108 * 3. Recycle the clock snapshot object.
110 * 4. Put our clock class reference.
112 bt_clock_snapshot_reset(clock_snapshot
);
113 clock_class
= clock_snapshot
->clock_class
;
114 BT_ASSERT_DBG(clock_class
);
115 clock_snapshot
->clock_class
= NULL
;
116 bt_object_pool_recycle_object(&clock_class
->cs_pool
, clock_snapshot
);
117 bt_object_put_ref(clock_class
);
120 uint64_t bt_clock_snapshot_get_value(
121 const struct bt_clock_snapshot
*clock_snapshot
)
123 BT_ASSERT_PRE_DEV_CS_NON_NULL(clock_snapshot
);
124 BT_ASSERT_DBG(clock_snapshot
->is_set
);
125 return clock_snapshot
->value_cycles
;
128 enum bt_clock_snapshot_get_ns_from_origin_status
129 bt_clock_snapshot_get_ns_from_origin(
130 const struct bt_clock_snapshot
*clock_snapshot
,
131 int64_t *ret_value_ns
)
133 int ret
= BT_FUNC_STATUS_OK
;
135 BT_ASSERT_PRE_DEV_NO_ERROR();
136 BT_ASSERT_PRE_DEV_CS_NON_NULL(clock_snapshot
);
137 BT_ASSERT_PRE_DEV_NON_NULL("value-ns-output", ret_value_ns
,
138 "Value (ns) (output)");
139 BT_ASSERT_DBG(clock_snapshot
->is_set
);
141 if (clock_snapshot
->ns_from_origin_overflows
) {
142 BT_LIB_LOGE_APPEND_CAUSE(
143 "Clock snapshot, once converted to nanoseconds from origin, "
144 "overflows the signed 64-bit integer range: "
145 "%![cs-]+k", clock_snapshot
);
146 ret
= BT_FUNC_STATUS_OVERFLOW_ERROR
;
150 *ret_value_ns
= clock_snapshot
->ns_from_origin
;
156 const struct bt_clock_class
*bt_clock_snapshot_borrow_clock_class_const(
157 const struct bt_clock_snapshot
*clock_snapshot
)
159 BT_ASSERT_PRE_DEV_CS_NON_NULL(clock_snapshot
);
160 return clock_snapshot
->clock_class
;