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" |
578e048b MJ |
11 | #include "clock-class.h" |
12 | #include "clock-snapshot.h" | |
43c59509 | 13 | #include <babeltrace2/trace-ir/clock-snapshot.h> |
578e048b | 14 | #include "compat/compiler.h" |
3fadfbc0 | 15 | #include <babeltrace2/types.h> |
578e048b MJ |
16 | #include "lib/object.h" |
17 | #include "common/assert.h" | |
d24d5663 | 18 | #include "lib/func-status.h" |
605e1019 | 19 | |
605e1019 PP |
20 | void bt_clock_snapshot_destroy(struct bt_clock_snapshot *clock_snapshot) |
21 | { | |
9f807a00 | 22 | BT_ASSERT(clock_snapshot); |
605e1019 PP |
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 | ||
58085ca4 PP |
28 | struct bt_clock_snapshot *bt_clock_snapshot_new( |
29 | struct bt_clock_class *clock_class) | |
605e1019 PP |
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) { | |
870631a2 PP |
38 | BT_LIB_LOGE_APPEND_CAUSE( |
39 | "Failed to allocate one clock snapshot."); | |
605e1019 PP |
40 | goto end; |
41 | } | |
42 | ||
43 | bt_object_init_unique(&ret->base); | |
44 | ret->clock_class = clock_class; | |
6871026b | 45 | bt_object_get_ref_no_null_check(clock_class); |
605e1019 PP |
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 | ||
58085ca4 PP |
53 | struct bt_clock_snapshot *bt_clock_snapshot_create( |
54 | struct bt_clock_class *clock_class) | |
605e1019 PP |
55 | { |
56 | struct bt_clock_snapshot *clock_snapshot = NULL; | |
57 | ||
98b15851 | 58 | BT_ASSERT_DBG(clock_class); |
605e1019 PP |
59 | clock_snapshot = bt_object_pool_create_object(&clock_class->cs_pool); |
60 | if (!clock_snapshot) { | |
870631a2 PP |
61 | BT_LIB_LOGE_APPEND_CAUSE( |
62 | "Cannot allocate one clock snapshot from clock class's clock snapshot pool: " | |
605e1019 | 63 | "%![cc-]+K", clock_class); |
5faa7131 | 64 | goto end; |
605e1019 PP |
65 | } |
66 | ||
91d81473 | 67 | if (G_LIKELY(!clock_snapshot->clock_class)) { |
605e1019 | 68 | clock_snapshot->clock_class = clock_class; |
6871026b | 69 | bt_object_get_ref_no_null_check(clock_class); |
605e1019 PP |
70 | } |
71 | ||
605e1019 PP |
72 | end: |
73 | return clock_snapshot; | |
74 | } | |
75 | ||
605e1019 PP |
76 | void bt_clock_snapshot_recycle(struct bt_clock_snapshot *clock_snapshot) |
77 | { | |
78 | struct bt_clock_class *clock_class; | |
79 | ||
98b15851 | 80 | BT_ASSERT_DBG(clock_snapshot); |
605e1019 PP |
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; | |
98b15851 | 107 | BT_ASSERT_DBG(clock_class); |
605e1019 PP |
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 | ||
1353b066 | 113 | BT_EXPORT |
58085ca4 PP |
114 | uint64_t bt_clock_snapshot_get_value( |
115 | const struct bt_clock_snapshot *clock_snapshot) | |
605e1019 | 116 | { |
d5b13b9b | 117 | BT_ASSERT_PRE_DEV_CS_NON_NULL(clock_snapshot); |
1778c2a4 | 118 | BT_ASSERT_DBG(clock_snapshot->is_set); |
605e1019 PP |
119 | return clock_snapshot->value_cycles; |
120 | } | |
121 | ||
1353b066 | 122 | BT_EXPORT |
d24d5663 PP |
123 | enum bt_clock_snapshot_get_ns_from_origin_status |
124 | bt_clock_snapshot_get_ns_from_origin( | |
dc68f16d | 125 | const struct bt_clock_snapshot *clock_snapshot, |
605e1019 PP |
126 | int64_t *ret_value_ns) |
127 | { | |
d24d5663 | 128 | int ret = BT_FUNC_STATUS_OK; |
dc68f16d | 129 | |
17f3083a | 130 | BT_ASSERT_PRE_DEV_NO_ERROR(); |
d5b13b9b | 131 | BT_ASSERT_PRE_DEV_CS_NON_NULL(clock_snapshot); |
1778c2a4 PP |
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); | |
605e1019 PP |
135 | |
136 | if (clock_snapshot->ns_from_origin_overflows) { | |
bc3d9692 PP |
137 | BT_LIB_LOGE_APPEND_CAUSE( |
138 | "Clock snapshot, once converted to nanoseconds from origin, " | |
605e1019 PP |
139 | "overflows the signed 64-bit integer range: " |
140 | "%![cs-]+k", clock_snapshot); | |
520cdc82 | 141 | ret = BT_FUNC_STATUS_OVERFLOW_ERROR; |
605e1019 PP |
142 | goto end; |
143 | } | |
144 | ||
145 | *ret_value_ns = clock_snapshot->ns_from_origin; | |
146 | ||
147 | end: | |
148 | return ret; | |
149 | } | |
150 | ||
1353b066 | 151 | BT_EXPORT |
605e1019 PP |
152 | const struct bt_clock_class *bt_clock_snapshot_borrow_clock_class_const( |
153 | const struct bt_clock_snapshot *clock_snapshot) | |
154 | { | |
d5b13b9b | 155 | BT_ASSERT_PRE_DEV_CS_NON_NULL(clock_snapshot); |
605e1019 PP |
156 | return clock_snapshot->clock_class; |
157 | } |