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 "lib/object.h"
17 #include "common/assert.h"
18 #include "lib/func-status.h"
20 void bt_clock_snapshot_destroy(struct bt_clock_snapshot
*clock_snapshot
)
22 BT_ASSERT(clock_snapshot
);
23 BT_LIB_LOGD("Destroying clock snapshot: %!+k", clock_snapshot
);
24 BT_OBJECT_PUT_REF_AND_RESET(clock_snapshot
->clock_class
);
25 g_free(clock_snapshot
);
28 struct bt_clock_snapshot
*bt_clock_snapshot_new(
29 struct bt_clock_class
*clock_class
)
31 struct bt_clock_snapshot
*ret
= NULL
;
33 BT_ASSERT(clock_class
);
34 BT_LIB_LOGD("Creating clock snapshot object: %![cc-]+K=",
36 ret
= g_new0(struct bt_clock_snapshot
, 1);
38 BT_LIB_LOGE_APPEND_CAUSE(
39 "Failed to allocate one clock snapshot.");
43 bt_object_init_unique(&ret
->base
);
44 ret
->clock_class
= clock_class
;
45 bt_object_get_ref_no_null_check(clock_class
);
46 bt_clock_class_freeze(clock_class
);
47 BT_LIB_LOGD("Created clock snapshot object: %!+k", ret
);
53 struct bt_clock_snapshot
*bt_clock_snapshot_create(
54 struct bt_clock_class
*clock_class
)
56 struct bt_clock_snapshot
*clock_snapshot
= NULL
;
58 BT_ASSERT_DBG(clock_class
);
59 clock_snapshot
= bt_object_pool_create_object(&clock_class
->cs_pool
);
60 if (!clock_snapshot
) {
61 BT_LIB_LOGE_APPEND_CAUSE(
62 "Cannot allocate one clock snapshot from clock class's clock snapshot pool: "
63 "%![cc-]+K", clock_class
);
67 if (G_LIKELY(!clock_snapshot
->clock_class
)) {
68 clock_snapshot
->clock_class
= clock_class
;
69 bt_object_get_ref_no_null_check(clock_class
);
73 return clock_snapshot
;
76 void bt_clock_snapshot_recycle(struct bt_clock_snapshot
*clock_snapshot
)
78 struct bt_clock_class
*clock_class
;
80 BT_ASSERT_DBG(clock_snapshot
);
81 BT_LIB_LOGD("Recycling clock snapshot: %!+k", clock_snapshot
);
84 * Those are the important ordered steps:
86 * 1. Reset the clock snapshot object, but do NOT put its clock
87 * class's reference. This clock class contains the pool to
88 * which we're about to recycle this clock snapshot object,
89 * so we must guarantee its existence thanks to this existing
92 * 2. Move the clock class reference to our `clock_class`
93 * variable so that we can set the clock snapshot's clock
94 * class member to NULL before recycling it. We CANNOT do
95 * this after we put the clock class reference because this
96 * bt_object_put_ref() could destroy the clock class, also
97 * destroying its clock snapshot pool, thus also destroying
98 * our clock snapshot object (this would result in an invalid
101 * 3. Recycle the clock snapshot object.
103 * 4. Put our clock class reference.
105 bt_clock_snapshot_reset(clock_snapshot
);
106 clock_class
= clock_snapshot
->clock_class
;
107 BT_ASSERT_DBG(clock_class
);
108 clock_snapshot
->clock_class
= NULL
;
109 bt_object_pool_recycle_object(&clock_class
->cs_pool
, clock_snapshot
);
110 bt_object_put_ref(clock_class
);
114 uint64_t bt_clock_snapshot_get_value(
115 const struct bt_clock_snapshot
*clock_snapshot
)
117 BT_ASSERT_PRE_DEV_CS_NON_NULL(clock_snapshot
);
118 BT_ASSERT_DBG(clock_snapshot
->is_set
);
119 return clock_snapshot
->value_cycles
;
123 enum bt_clock_snapshot_get_ns_from_origin_status
124 bt_clock_snapshot_get_ns_from_origin(
125 const struct bt_clock_snapshot
*clock_snapshot
,
126 int64_t *ret_value_ns
)
128 int ret
= BT_FUNC_STATUS_OK
;
130 BT_ASSERT_PRE_DEV_NO_ERROR();
131 BT_ASSERT_PRE_DEV_CS_NON_NULL(clock_snapshot
);
132 BT_ASSERT_PRE_DEV_NON_NULL("value-ns-output", ret_value_ns
,
133 "Value (ns) (output)");
134 BT_ASSERT_DBG(clock_snapshot
->is_set
);
136 if (clock_snapshot
->ns_from_origin_overflows
) {
137 BT_LIB_LOGE_APPEND_CAUSE(
138 "Clock snapshot, once converted to nanoseconds from origin, "
139 "overflows the signed 64-bit integer range: "
140 "%![cs-]+k", clock_snapshot
);
141 ret
= BT_FUNC_STATUS_OVERFLOW_ERROR
;
145 *ret_value_ns
= clock_snapshot
->ns_from_origin
;
152 const struct bt_clock_class
*bt_clock_snapshot_borrow_clock_class_const(
153 const struct bt_clock_snapshot
*clock_snapshot
)
155 BT_ASSERT_PRE_DEV_CS_NON_NULL(clock_snapshot
);
156 return clock_snapshot
->clock_class
;