cpp-common/bt2c/fmt.hpp: use `wise_enum::string_type` in `EnableIfIsWiseEnum` definition
[babeltrace.git] / src / lib / trace-ir / clock-snapshot.c
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
5 */
6
7 #define BT_LOG_TAG "LIB/CLOCK-SNAPSHOT"
8 #include "lib/logging.h"
9
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"
19
20 void bt_clock_snapshot_destroy(struct bt_clock_snapshot *clock_snapshot)
21 {
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);
26 }
27
28 struct bt_clock_snapshot *bt_clock_snapshot_new(
29 struct bt_clock_class *clock_class)
30 {
31 struct bt_clock_snapshot *ret = NULL;
32
33 BT_ASSERT(clock_class);
34 BT_LIB_LOGD("Creating clock snapshot object: %![cc-]+K=",
35 clock_class);
36 ret = g_new0(struct bt_clock_snapshot, 1);
37 if (!ret) {
38 BT_LIB_LOGE_APPEND_CAUSE(
39 "Failed to allocate one clock snapshot.");
40 goto end;
41 }
42
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);
48
49 end:
50 return ret;
51 }
52
53 struct bt_clock_snapshot *bt_clock_snapshot_create(
54 struct bt_clock_class *clock_class)
55 {
56 struct bt_clock_snapshot *clock_snapshot = NULL;
57
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);
64 goto end;
65 }
66
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);
70 }
71
72 end:
73 return clock_snapshot;
74 }
75
76 void bt_clock_snapshot_recycle(struct bt_clock_snapshot *clock_snapshot)
77 {
78 struct bt_clock_class *clock_class;
79
80 BT_ASSERT_DBG(clock_snapshot);
81 BT_LIB_LOGD("Recycling clock snapshot: %!+k", clock_snapshot);
82
83 /*
84 * Those are the important ordered steps:
85 *
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
90 * reference.
91 *
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
99 * write access).
100 *
101 * 3. Recycle the clock snapshot object.
102 *
103 * 4. Put our clock class reference.
104 */
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);
111 }
112
113 BT_EXPORT
114 uint64_t bt_clock_snapshot_get_value(
115 const struct bt_clock_snapshot *clock_snapshot)
116 {
117 BT_ASSERT_PRE_DEV_CS_NON_NULL(clock_snapshot);
118 BT_ASSERT_DBG(clock_snapshot->is_set);
119 return clock_snapshot->value_cycles;
120 }
121
122 BT_EXPORT
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)
127 {
128 int ret = BT_FUNC_STATUS_OK;
129
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);
135
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;
142 goto end;
143 }
144
145 *ret_value_ns = clock_snapshot->ns_from_origin;
146
147 end:
148 return ret;
149 }
150
151 BT_EXPORT
152 const struct bt_clock_class *bt_clock_snapshot_borrow_clock_class_const(
153 const struct bt_clock_snapshot *clock_snapshot)
154 {
155 BT_ASSERT_PRE_DEV_CS_NON_NULL(clock_snapshot);
156 return clock_snapshot->clock_class;
157 }
This page took 0.033255 seconds and 4 git commands to generate.