Commit | Line | Data |
---|---|---|
605e1019 PP |
1 | /* |
2 | * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com> | |
3 | * | |
4 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
5 | * of this software and associated documentation files (the "Software"), to deal | |
6 | * in the Software without restriction, including without limitation the rights | |
7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
8 | * copies of the Software, and to permit persons to whom the Software is | |
9 | * furnished to do so, subject to the following conditions: | |
10 | * | |
11 | * The above copyright notice and this permission notice shall be included in | |
12 | * all copies or substantial portions of the Software. | |
13 | * | |
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
20 | * SOFTWARE. | |
21 | */ | |
22 | ||
350ad6c1 | 23 | #define BT_LOG_TAG "LIB/CLOCK-SNAPSHOT" |
c2d9d9cf | 24 | #include "lib/logging.h" |
605e1019 | 25 | |
578e048b MJ |
26 | #include "lib/assert-pre.h" |
27 | #include "compat/uuid.h" | |
28 | #include "clock-class.h" | |
29 | #include "clock-snapshot.h" | |
3fadfbc0 | 30 | #include <babeltrace2/trace-ir/clock-snapshot-const.h> |
578e048b | 31 | #include "compat/compiler.h" |
3fadfbc0 | 32 | #include <babeltrace2/types.h> |
578e048b | 33 | #include "compat/string.h" |
605e1019 | 34 | #include <inttypes.h> |
578e048b MJ |
35 | #include "lib/object.h" |
36 | #include "common/assert.h" | |
d24d5663 | 37 | #include "lib/func-status.h" |
605e1019 PP |
38 | |
39 | BT_HIDDEN | |
40 | void bt_clock_snapshot_destroy(struct bt_clock_snapshot *clock_snapshot) | |
41 | { | |
42 | BT_LIB_LOGD("Destroying clock snapshot: %!+k", clock_snapshot); | |
43 | BT_OBJECT_PUT_REF_AND_RESET(clock_snapshot->clock_class); | |
44 | g_free(clock_snapshot); | |
45 | } | |
46 | ||
47 | BT_HIDDEN | |
58085ca4 PP |
48 | struct bt_clock_snapshot *bt_clock_snapshot_new( |
49 | struct bt_clock_class *clock_class) | |
605e1019 PP |
50 | { |
51 | struct bt_clock_snapshot *ret = NULL; | |
52 | ||
53 | BT_ASSERT(clock_class); | |
54 | BT_LIB_LOGD("Creating clock snapshot object: %![cc-]+K=", | |
55 | clock_class); | |
56 | ret = g_new0(struct bt_clock_snapshot, 1); | |
57 | if (!ret) { | |
870631a2 PP |
58 | BT_LIB_LOGE_APPEND_CAUSE( |
59 | "Failed to allocate one clock snapshot."); | |
605e1019 PP |
60 | goto end; |
61 | } | |
62 | ||
63 | bt_object_init_unique(&ret->base); | |
64 | ret->clock_class = clock_class; | |
65 | bt_object_get_no_null_check(clock_class); | |
66 | bt_clock_class_freeze(clock_class); | |
67 | BT_LIB_LOGD("Created clock snapshot object: %!+k", ret); | |
68 | ||
69 | end: | |
70 | return ret; | |
71 | } | |
72 | ||
73 | BT_HIDDEN | |
58085ca4 PP |
74 | struct bt_clock_snapshot *bt_clock_snapshot_create( |
75 | struct bt_clock_class *clock_class) | |
605e1019 PP |
76 | { |
77 | struct bt_clock_snapshot *clock_snapshot = NULL; | |
78 | ||
79 | BT_ASSERT(clock_class); | |
80 | clock_snapshot = bt_object_pool_create_object(&clock_class->cs_pool); | |
81 | if (!clock_snapshot) { | |
870631a2 PP |
82 | BT_LIB_LOGE_APPEND_CAUSE( |
83 | "Cannot allocate one clock snapshot from clock class's clock snapshot pool: " | |
605e1019 PP |
84 | "%![cc-]+K", clock_class); |
85 | goto error; | |
86 | } | |
87 | ||
91d81473 | 88 | if (G_LIKELY(!clock_snapshot->clock_class)) { |
605e1019 PP |
89 | clock_snapshot->clock_class = clock_class; |
90 | bt_object_get_no_null_check(clock_class); | |
91 | } | |
92 | ||
93 | goto end; | |
94 | ||
95 | error: | |
96 | if (clock_snapshot) { | |
97 | bt_clock_snapshot_recycle(clock_snapshot); | |
98 | clock_snapshot = NULL; | |
99 | } | |
100 | ||
101 | end: | |
102 | return clock_snapshot; | |
103 | } | |
104 | ||
105 | BT_HIDDEN | |
106 | void bt_clock_snapshot_recycle(struct bt_clock_snapshot *clock_snapshot) | |
107 | { | |
108 | struct bt_clock_class *clock_class; | |
109 | ||
110 | BT_ASSERT(clock_snapshot); | |
111 | BT_LIB_LOGD("Recycling clock snapshot: %!+k", clock_snapshot); | |
112 | ||
113 | /* | |
114 | * Those are the important ordered steps: | |
115 | * | |
116 | * 1. Reset the clock snapshot object, but do NOT put its clock | |
117 | * class's reference. This clock class contains the pool to | |
118 | * which we're about to recycle this clock snapshot object, | |
119 | * so we must guarantee its existence thanks to this existing | |
120 | * reference. | |
121 | * | |
122 | * 2. Move the clock class reference to our `clock_class` | |
123 | * variable so that we can set the clock snapshot's clock | |
124 | * class member to NULL before recycling it. We CANNOT do | |
125 | * this after we put the clock class reference because this | |
126 | * bt_object_put_ref() could destroy the clock class, also | |
127 | * destroying its clock snapshot pool, thus also destroying | |
128 | * our clock snapshot object (this would result in an invalid | |
129 | * write access). | |
130 | * | |
131 | * 3. Recycle the clock snapshot object. | |
132 | * | |
133 | * 4. Put our clock class reference. | |
134 | */ | |
135 | bt_clock_snapshot_reset(clock_snapshot); | |
136 | clock_class = clock_snapshot->clock_class; | |
137 | BT_ASSERT(clock_class); | |
138 | clock_snapshot->clock_class = NULL; | |
139 | bt_object_pool_recycle_object(&clock_class->cs_pool, clock_snapshot); | |
140 | bt_object_put_ref(clock_class); | |
141 | } | |
142 | ||
58085ca4 PP |
143 | uint64_t bt_clock_snapshot_get_value( |
144 | const struct bt_clock_snapshot *clock_snapshot) | |
605e1019 PP |
145 | { |
146 | BT_ASSERT_PRE_NON_NULL(clock_snapshot, "Clock snapshot"); | |
147 | BT_ASSERT_PRE(clock_snapshot->is_set, | |
148 | "Clock snapshot is not set: %!+k", clock_snapshot); | |
149 | return clock_snapshot->value_cycles; | |
150 | } | |
151 | ||
d24d5663 PP |
152 | enum bt_clock_snapshot_get_ns_from_origin_status |
153 | bt_clock_snapshot_get_ns_from_origin( | |
dc68f16d | 154 | const struct bt_clock_snapshot *clock_snapshot, |
605e1019 PP |
155 | int64_t *ret_value_ns) |
156 | { | |
d24d5663 | 157 | int ret = BT_FUNC_STATUS_OK; |
dc68f16d | 158 | |
605e1019 PP |
159 | BT_ASSERT_PRE_NON_NULL(clock_snapshot, "Clock snapshot"); |
160 | BT_ASSERT_PRE_NON_NULL(ret_value_ns, "Value (ns) (output)"); | |
161 | BT_ASSERT_PRE(clock_snapshot->is_set, | |
162 | "Clock snapshot is not set: %!+k", clock_snapshot); | |
163 | ||
164 | if (clock_snapshot->ns_from_origin_overflows) { | |
165 | BT_LIB_LOGD("Clock snapshot, once converted to nanoseconds from origin, " | |
166 | "overflows the signed 64-bit integer range: " | |
167 | "%![cs-]+k", clock_snapshot); | |
d24d5663 | 168 | ret = BT_FUNC_STATUS_OVERFLOW; |
605e1019 PP |
169 | goto end; |
170 | } | |
171 | ||
172 | *ret_value_ns = clock_snapshot->ns_from_origin; | |
173 | ||
174 | end: | |
175 | return ret; | |
176 | } | |
177 | ||
178 | const struct bt_clock_class *bt_clock_snapshot_borrow_clock_class_const( | |
179 | const struct bt_clock_snapshot *clock_snapshot) | |
180 | { | |
181 | BT_ASSERT_PRE_NON_NULL(clock_snapshot, "Clock snapshot"); | |
182 | return clock_snapshot->clock_class; | |
183 | } |