Commit | Line | Data |
---|---|---|
ecbb78c0 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 | ||
23 | #define BT_LOG_TAG "CLOCK-SNAPSHOT" | |
24 | #include <babeltrace/lib-logging-internal.h> | |
25 | ||
26 | #include <babeltrace/assert-pre-internal.h> | |
27 | #include <babeltrace/compat/uuid-internal.h> | |
28 | #include <babeltrace/trace-ir/clock-class-internal.h> | |
29 | #include <babeltrace/trace-ir/clock-snapshot-internal.h> | |
30 | #include <babeltrace/compiler-internal.h> | |
31 | #include <babeltrace/types.h> | |
32 | #include <babeltrace/compat/string-internal.h> | |
33 | #include <inttypes.h> | |
34 | #include <babeltrace/object-internal.h> | |
35 | #include <babeltrace/assert-internal.h> | |
36 | ||
37 | BT_HIDDEN | |
38 | void bt_clock_snapshot_destroy(struct bt_clock_snapshot *clock_snapshot) | |
39 | { | |
40 | BT_LIB_LOGD("Destroying clock snapshot: %!+k", clock_snapshot); | |
41 | BT_OBJECT_PUT_REF_AND_RESET(clock_snapshot->clock_class); | |
42 | g_free(clock_snapshot); | |
43 | } | |
44 | ||
45 | BT_HIDDEN | |
46 | struct bt_clock_snapshot *bt_clock_snapshot_new(struct bt_clock_class *clock_class) | |
47 | { | |
48 | struct bt_clock_snapshot *ret = NULL; | |
49 | ||
50 | BT_ASSERT(clock_class); | |
51 | BT_LIB_LOGD("Creating clock snapshot object: %![cc-]+K=", | |
52 | clock_class); | |
53 | ret = g_new0(struct bt_clock_snapshot, 1); | |
54 | if (!ret) { | |
55 | BT_LOGE_STR("Failed to allocate one clock snapshot."); | |
56 | goto end; | |
57 | } | |
58 | ||
59 | bt_object_init_unique(&ret->base); | |
60 | ret->clock_class = clock_class; | |
61 | bt_object_get_no_null_check(clock_class); | |
62 | bt_clock_class_freeze(clock_class); | |
63 | BT_LIB_LOGD("Created clock snapshot object: %!+k", ret); | |
64 | ||
65 | end: | |
66 | return ret; | |
67 | } | |
68 | ||
69 | BT_HIDDEN | |
70 | struct bt_clock_snapshot *bt_clock_snapshot_create(struct bt_clock_class *clock_class) | |
71 | { | |
72 | struct bt_clock_snapshot *clock_snapshot = NULL; | |
73 | ||
74 | BT_ASSERT(clock_class); | |
75 | clock_snapshot = bt_object_pool_create_object(&clock_class->cs_pool); | |
76 | if (!clock_snapshot) { | |
77 | BT_LIB_LOGE("Cannot allocate one clock snapshot from clock class's clock snapshot pool: " | |
78 | "%![cc-]+K", clock_class); | |
79 | goto error; | |
80 | } | |
81 | ||
82 | if (likely(!clock_snapshot->clock_class)) { | |
83 | clock_snapshot->clock_class = clock_class; | |
84 | bt_object_get_no_null_check(clock_class); | |
85 | } | |
86 | ||
87 | goto end; | |
88 | ||
89 | error: | |
90 | if (clock_snapshot) { | |
91 | bt_clock_snapshot_recycle(clock_snapshot); | |
92 | clock_snapshot = NULL; | |
93 | } | |
94 | ||
95 | end: | |
96 | return clock_snapshot; | |
97 | } | |
98 | ||
99 | BT_HIDDEN | |
100 | void bt_clock_snapshot_recycle(struct bt_clock_snapshot *clock_snapshot) | |
101 | { | |
102 | struct bt_clock_class *clock_class; | |
103 | ||
104 | BT_ASSERT(clock_snapshot); | |
105 | BT_LIB_LOGD("Recycling clock snapshot: %!+k", clock_snapshot); | |
106 | ||
107 | /* | |
108 | * Those are the important ordered steps: | |
109 | * | |
110 | * 1. Reset the clock snapshot object, but do NOT put its clock | |
111 | * class's reference. This clock class contains the pool to | |
112 | * which we're about to recycle this clock snapshot object, | |
113 | * so we must guarantee its existence thanks to this existing | |
114 | * reference. | |
115 | * | |
116 | * 2. Move the clock class reference to our `clock_class` | |
117 | * variable so that we can set the clock snapshot's clock | |
118 | * class member to NULL before recycling it. We CANNOT do | |
119 | * this after we put the clock class reference because this | |
120 | * bt_object_put_ref() could destroy the clock class, also | |
121 | * destroying its clock snapshot pool, thus also destroying | |
122 | * our clock snapshot object (this would result in an invalid | |
123 | * write access). | |
124 | * | |
125 | * 3. Recycle the clock snapshot object. | |
126 | * | |
127 | * 4. Put our clock class reference. | |
128 | */ | |
129 | bt_clock_snapshot_reset(clock_snapshot); | |
130 | clock_class = clock_snapshot->clock_class; | |
131 | BT_ASSERT(clock_class); | |
132 | clock_snapshot->clock_class = NULL; | |
133 | bt_object_pool_recycle_object(&clock_class->cs_pool, clock_snapshot); | |
134 | bt_object_put_ref(clock_class); | |
135 | } | |
136 | ||
137 | uint64_t bt_clock_snapshot_get_value(const struct bt_clock_snapshot *clock_snapshot) | |
138 | { | |
139 | BT_ASSERT_PRE_NON_NULL(clock_snapshot, "Clock snapshot"); | |
140 | BT_ASSERT_PRE(clock_snapshot->is_set, | |
141 | "Clock snapshot is not set: %!+k", clock_snapshot); | |
142 | return clock_snapshot->value_cycles; | |
143 | } | |
144 | ||
145 | int bt_clock_snapshot_get_ns_from_origin(const struct bt_clock_snapshot *clock_snapshot, | |
146 | int64_t *ret_value_ns) | |
147 | { | |
148 | int ret = 0; | |
149 | BT_ASSERT_PRE_NON_NULL(clock_snapshot, "Clock snapshot"); | |
150 | BT_ASSERT_PRE_NON_NULL(ret_value_ns, "Value (ns) (output)"); | |
151 | BT_ASSERT_PRE(clock_snapshot->is_set, | |
152 | "Clock snapshot is not set: %!+k", clock_snapshot); | |
153 | ||
154 | if (clock_snapshot->ns_from_origin_overflows) { | |
155 | BT_LIB_LOGD("Clock snapshot, once converted to nanoseconds from origin, " | |
156 | "overflows the signed 64-bit integer range: " | |
157 | "%![cs-]+k", clock_snapshot); | |
158 | ret = -1; | |
159 | goto end; | |
160 | } | |
161 | ||
162 | *ret_value_ns = clock_snapshot->ns_from_origin; | |
163 | ||
164 | end: | |
165 | return ret; | |
166 | } | |
167 | ||
168 | const struct bt_clock_class *bt_clock_snapshot_borrow_clock_class_const( | |
169 | const struct bt_clock_snapshot *clock_snapshot) | |
170 | { | |
171 | BT_ASSERT_PRE_NON_NULL(clock_snapshot, "Clock snapshot"); | |
172 | return clock_snapshot->clock_class; | |
173 | } |