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 "clock-class.h"
12 #include "clock-snapshot.h"
13 #include <babeltrace2/trace-ir/clock-snapshot.h>
14 #include "compat/compiler.h"
15 #include <babeltrace2/types.h>
16 #include "compat/string.h"
17 #include "lib/object.h"
18 #include "common/assert.h"
19 #include "lib/func-status.h"
21 void bt_clock_snapshot_destroy(struct bt_clock_snapshot
*clock_snapshot
)
23 BT_ASSERT(clock_snapshot
);
24 BT_LIB_LOGD("Destroying clock snapshot: %!+k", clock_snapshot
);
25 BT_OBJECT_PUT_REF_AND_RESET(clock_snapshot
->clock_class
);
26 g_free(clock_snapshot
);
29 struct bt_clock_snapshot
*bt_clock_snapshot_new(
30 struct bt_clock_class
*clock_class
)
32 struct bt_clock_snapshot
*ret
= NULL
;
34 BT_ASSERT(clock_class
);
35 BT_LIB_LOGD("Creating clock snapshot object: %![cc-]+K=",
37 ret
= g_new0(struct bt_clock_snapshot
, 1);
39 BT_LIB_LOGE_APPEND_CAUSE(
40 "Failed to allocate one clock snapshot.");
44 bt_object_init_unique(&ret
->base
);
45 ret
->clock_class
= clock_class
;
46 bt_object_get_ref_no_null_check(clock_class
);
47 bt_clock_class_freeze(clock_class
);
48 BT_LIB_LOGD("Created clock snapshot object: %!+k", ret
);
54 struct bt_clock_snapshot
*bt_clock_snapshot_create(
55 struct bt_clock_class
*clock_class
)
57 struct bt_clock_snapshot
*clock_snapshot
= NULL
;
59 BT_ASSERT_DBG(clock_class
);
60 clock_snapshot
= bt_object_pool_create_object(&clock_class
->cs_pool
);
61 if (!clock_snapshot
) {
62 BT_LIB_LOGE_APPEND_CAUSE(
63 "Cannot allocate one clock snapshot from clock class's clock snapshot pool: "
64 "%![cc-]+K", clock_class
);
68 if (G_LIKELY(!clock_snapshot
->clock_class
)) {
69 clock_snapshot
->clock_class
= clock_class
;
70 bt_object_get_ref_no_null_check(clock_class
);
74 return clock_snapshot
;
77 void bt_clock_snapshot_recycle(struct bt_clock_snapshot
*clock_snapshot
)
79 struct bt_clock_class
*clock_class
;
81 BT_ASSERT_DBG(clock_snapshot
);
82 BT_LIB_LOGD("Recycling clock snapshot: %!+k", clock_snapshot
);
85 * Those are the important ordered steps:
87 * 1. Reset the clock snapshot object, but do NOT put its clock
88 * class's reference. This clock class contains the pool to
89 * which we're about to recycle this clock snapshot object,
90 * so we must guarantee its existence thanks to this existing
93 * 2. Move the clock class reference to our `clock_class`
94 * variable so that we can set the clock snapshot's clock
95 * class member to NULL before recycling it. We CANNOT do
96 * this after we put the clock class reference because this
97 * bt_object_put_ref() could destroy the clock class, also
98 * destroying its clock snapshot pool, thus also destroying
99 * our clock snapshot object (this would result in an invalid
102 * 3. Recycle the clock snapshot object.
104 * 4. Put our clock class reference.
106 bt_clock_snapshot_reset(clock_snapshot
);
107 clock_class
= clock_snapshot
->clock_class
;
108 BT_ASSERT_DBG(clock_class
);
109 clock_snapshot
->clock_class
= NULL
;
110 bt_object_pool_recycle_object(&clock_class
->cs_pool
, clock_snapshot
);
111 bt_object_put_ref(clock_class
);
115 uint64_t bt_clock_snapshot_get_value(
116 const struct bt_clock_snapshot
*clock_snapshot
)
118 BT_ASSERT_PRE_DEV_CS_NON_NULL(clock_snapshot
);
119 BT_ASSERT_DBG(clock_snapshot
->is_set
);
120 return clock_snapshot
->value_cycles
;
124 enum bt_clock_snapshot_get_ns_from_origin_status
125 bt_clock_snapshot_get_ns_from_origin(
126 const struct bt_clock_snapshot
*clock_snapshot
,
127 int64_t *ret_value_ns
)
129 int ret
= BT_FUNC_STATUS_OK
;
131 BT_ASSERT_PRE_DEV_NO_ERROR();
132 BT_ASSERT_PRE_DEV_CS_NON_NULL(clock_snapshot
);
133 BT_ASSERT_PRE_DEV_NON_NULL("value-ns-output", ret_value_ns
,
134 "Value (ns) (output)");
135 BT_ASSERT_DBG(clock_snapshot
->is_set
);
137 if (clock_snapshot
->ns_from_origin_overflows
) {
138 BT_LIB_LOGE_APPEND_CAUSE(
139 "Clock snapshot, once converted to nanoseconds from origin, "
140 "overflows the signed 64-bit integer range: "
141 "%![cs-]+k", clock_snapshot
);
142 ret
= BT_FUNC_STATUS_OVERFLOW_ERROR
;
146 *ret_value_ns
= clock_snapshot
->ns_from_origin
;
153 const struct bt_clock_class
*bt_clock_snapshot_borrow_clock_class_const(
154 const struct bt_clock_snapshot
*clock_snapshot
)
156 BT_ASSERT_PRE_DEV_CS_NON_NULL(clock_snapshot
);
157 return clock_snapshot
->clock_class
;