Commit | Line | Data |
---|---|---|
605e1019 | 1 | /* |
0235b0db | 2 | * SPDX-License-Identifier: MIT |
605e1019 | 3 | * |
0235b0db | 4 | * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com> |
605e1019 PP |
5 | */ |
6 | ||
350ad6c1 | 7 | #define BT_LOG_TAG "LIB/CLOCK-SNAPSHOT" |
c2d9d9cf | 8 | #include "lib/logging.h" |
605e1019 | 9 | |
d98421f2 | 10 | #include "lib/assert-cond.h" |
6162e6b7 | 11 | #include "common/uuid.h" |
578e048b MJ |
12 | #include "clock-class.h" |
13 | #include "clock-snapshot.h" | |
43c59509 | 14 | #include <babeltrace2/trace-ir/clock-snapshot.h> |
578e048b | 15 | #include "compat/compiler.h" |
3fadfbc0 | 16 | #include <babeltrace2/types.h> |
578e048b | 17 | #include "compat/string.h" |
605e1019 | 18 | #include <inttypes.h> |
578e048b MJ |
19 | #include "lib/object.h" |
20 | #include "common/assert.h" | |
d24d5663 | 21 | #include "lib/func-status.h" |
605e1019 PP |
22 | |
23 | BT_HIDDEN | |
24 | void bt_clock_snapshot_destroy(struct bt_clock_snapshot *clock_snapshot) | |
25 | { | |
9f807a00 | 26 | BT_ASSERT(clock_snapshot); |
605e1019 PP |
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); | |
30 | } | |
31 | ||
32 | BT_HIDDEN | |
58085ca4 PP |
33 | struct bt_clock_snapshot *bt_clock_snapshot_new( |
34 | struct bt_clock_class *clock_class) | |
605e1019 PP |
35 | { |
36 | struct bt_clock_snapshot *ret = NULL; | |
37 | ||
38 | BT_ASSERT(clock_class); | |
39 | BT_LIB_LOGD("Creating clock snapshot object: %![cc-]+K=", | |
40 | clock_class); | |
41 | ret = g_new0(struct bt_clock_snapshot, 1); | |
42 | if (!ret) { | |
870631a2 PP |
43 | BT_LIB_LOGE_APPEND_CAUSE( |
44 | "Failed to allocate one clock snapshot."); | |
605e1019 PP |
45 | goto end; |
46 | } | |
47 | ||
48 | bt_object_init_unique(&ret->base); | |
49 | ret->clock_class = clock_class; | |
6871026b | 50 | bt_object_get_ref_no_null_check(clock_class); |
605e1019 PP |
51 | bt_clock_class_freeze(clock_class); |
52 | BT_LIB_LOGD("Created clock snapshot object: %!+k", ret); | |
53 | ||
54 | end: | |
55 | return ret; | |
56 | } | |
57 | ||
58 | BT_HIDDEN | |
58085ca4 PP |
59 | struct bt_clock_snapshot *bt_clock_snapshot_create( |
60 | struct bt_clock_class *clock_class) | |
605e1019 PP |
61 | { |
62 | struct bt_clock_snapshot *clock_snapshot = NULL; | |
63 | ||
98b15851 | 64 | BT_ASSERT_DBG(clock_class); |
605e1019 PP |
65 | clock_snapshot = bt_object_pool_create_object(&clock_class->cs_pool); |
66 | if (!clock_snapshot) { | |
870631a2 PP |
67 | BT_LIB_LOGE_APPEND_CAUSE( |
68 | "Cannot allocate one clock snapshot from clock class's clock snapshot pool: " | |
605e1019 | 69 | "%![cc-]+K", clock_class); |
5faa7131 | 70 | goto end; |
605e1019 PP |
71 | } |
72 | ||
91d81473 | 73 | if (G_LIKELY(!clock_snapshot->clock_class)) { |
605e1019 | 74 | clock_snapshot->clock_class = clock_class; |
6871026b | 75 | bt_object_get_ref_no_null_check(clock_class); |
605e1019 PP |
76 | } |
77 | ||
605e1019 PP |
78 | end: |
79 | return clock_snapshot; | |
80 | } | |
81 | ||
82 | BT_HIDDEN | |
83 | void bt_clock_snapshot_recycle(struct bt_clock_snapshot *clock_snapshot) | |
84 | { | |
85 | struct bt_clock_class *clock_class; | |
86 | ||
98b15851 | 87 | BT_ASSERT_DBG(clock_snapshot); |
605e1019 PP |
88 | BT_LIB_LOGD("Recycling clock snapshot: %!+k", clock_snapshot); |
89 | ||
90 | /* | |
91 | * Those are the important ordered steps: | |
92 | * | |
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 | |
97 | * reference. | |
98 | * | |
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 | |
106 | * write access). | |
107 | * | |
108 | * 3. Recycle the clock snapshot object. | |
109 | * | |
110 | * 4. Put our clock class reference. | |
111 | */ | |
112 | bt_clock_snapshot_reset(clock_snapshot); | |
113 | clock_class = clock_snapshot->clock_class; | |
98b15851 | 114 | BT_ASSERT_DBG(clock_class); |
605e1019 PP |
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); | |
118 | } | |
119 | ||
58085ca4 PP |
120 | uint64_t bt_clock_snapshot_get_value( |
121 | const struct bt_clock_snapshot *clock_snapshot) | |
605e1019 | 122 | { |
d5b13b9b | 123 | BT_ASSERT_PRE_DEV_CS_NON_NULL(clock_snapshot); |
bdb288b3 | 124 | BT_ASSERT_PRE_DEV(clock_snapshot->is_set, |
605e1019 PP |
125 | "Clock snapshot is not set: %!+k", clock_snapshot); |
126 | return clock_snapshot->value_cycles; | |
127 | } | |
128 | ||
d24d5663 PP |
129 | enum bt_clock_snapshot_get_ns_from_origin_status |
130 | bt_clock_snapshot_get_ns_from_origin( | |
dc68f16d | 131 | const struct bt_clock_snapshot *clock_snapshot, |
605e1019 PP |
132 | int64_t *ret_value_ns) |
133 | { | |
d24d5663 | 134 | int ret = BT_FUNC_STATUS_OK; |
dc68f16d | 135 | |
17f3083a | 136 | BT_ASSERT_PRE_DEV_NO_ERROR(); |
d5b13b9b | 137 | BT_ASSERT_PRE_DEV_CS_NON_NULL(clock_snapshot); |
bdb288b3 PP |
138 | BT_ASSERT_PRE_DEV_NON_NULL(ret_value_ns, "Value (ns) (output)"); |
139 | BT_ASSERT_PRE_DEV(clock_snapshot->is_set, | |
605e1019 PP |
140 | "Clock snapshot is not set: %!+k", clock_snapshot); |
141 | ||
142 | if (clock_snapshot->ns_from_origin_overflows) { | |
bc3d9692 PP |
143 | BT_LIB_LOGE_APPEND_CAUSE( |
144 | "Clock snapshot, once converted to nanoseconds from origin, " | |
605e1019 PP |
145 | "overflows the signed 64-bit integer range: " |
146 | "%![cs-]+k", clock_snapshot); | |
520cdc82 | 147 | ret = BT_FUNC_STATUS_OVERFLOW_ERROR; |
605e1019 PP |
148 | goto end; |
149 | } | |
150 | ||
151 | *ret_value_ns = clock_snapshot->ns_from_origin; | |
152 | ||
153 | end: | |
154 | return ret; | |
155 | } | |
156 | ||
157 | const struct bt_clock_class *bt_clock_snapshot_borrow_clock_class_const( | |
158 | const struct bt_clock_snapshot *clock_snapshot) | |
159 | { | |
d5b13b9b | 160 | BT_ASSERT_PRE_DEV_CS_NON_NULL(clock_snapshot); |
605e1019 PP |
161 | return clock_snapshot->clock_class; |
162 | } |