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 | 16 | #include "compat/string.h" |
578e048b MJ |
17 | #include "lib/object.h" |
18 | #include "common/assert.h" | |
d24d5663 | 19 | #include "lib/func-status.h" |
605e1019 | 20 | |
605e1019 PP |
21 | void bt_clock_snapshot_destroy(struct bt_clock_snapshot *clock_snapshot) |
22 | { | |
9f807a00 | 23 | BT_ASSERT(clock_snapshot); |
605e1019 PP |
24 | BT_LIB_LOGD("Destroying clock snapshot: %!+k", clock_snapshot); |
25 | BT_OBJECT_PUT_REF_AND_RESET(clock_snapshot->clock_class); | |
26 | g_free(clock_snapshot); | |
27 | } | |
28 | ||
58085ca4 PP |
29 | struct bt_clock_snapshot *bt_clock_snapshot_new( |
30 | struct bt_clock_class *clock_class) | |
605e1019 PP |
31 | { |
32 | struct bt_clock_snapshot *ret = NULL; | |
33 | ||
34 | BT_ASSERT(clock_class); | |
35 | BT_LIB_LOGD("Creating clock snapshot object: %![cc-]+K=", | |
36 | clock_class); | |
37 | ret = g_new0(struct bt_clock_snapshot, 1); | |
38 | if (!ret) { | |
870631a2 PP |
39 | BT_LIB_LOGE_APPEND_CAUSE( |
40 | "Failed to allocate one clock snapshot."); | |
605e1019 PP |
41 | goto end; |
42 | } | |
43 | ||
44 | bt_object_init_unique(&ret->base); | |
45 | ret->clock_class = clock_class; | |
6871026b | 46 | bt_object_get_ref_no_null_check(clock_class); |
605e1019 PP |
47 | bt_clock_class_freeze(clock_class); |
48 | BT_LIB_LOGD("Created clock snapshot object: %!+k", ret); | |
49 | ||
50 | end: | |
51 | return ret; | |
52 | } | |
53 | ||
58085ca4 PP |
54 | struct bt_clock_snapshot *bt_clock_snapshot_create( |
55 | struct bt_clock_class *clock_class) | |
605e1019 PP |
56 | { |
57 | struct bt_clock_snapshot *clock_snapshot = NULL; | |
58 | ||
98b15851 | 59 | BT_ASSERT_DBG(clock_class); |
605e1019 PP |
60 | clock_snapshot = bt_object_pool_create_object(&clock_class->cs_pool); |
61 | if (!clock_snapshot) { | |
870631a2 PP |
62 | BT_LIB_LOGE_APPEND_CAUSE( |
63 | "Cannot allocate one clock snapshot from clock class's clock snapshot pool: " | |
605e1019 | 64 | "%![cc-]+K", clock_class); |
5faa7131 | 65 | goto end; |
605e1019 PP |
66 | } |
67 | ||
91d81473 | 68 | if (G_LIKELY(!clock_snapshot->clock_class)) { |
605e1019 | 69 | clock_snapshot->clock_class = clock_class; |
6871026b | 70 | bt_object_get_ref_no_null_check(clock_class); |
605e1019 PP |
71 | } |
72 | ||
605e1019 PP |
73 | end: |
74 | return clock_snapshot; | |
75 | } | |
76 | ||
605e1019 PP |
77 | void bt_clock_snapshot_recycle(struct bt_clock_snapshot *clock_snapshot) |
78 | { | |
79 | struct bt_clock_class *clock_class; | |
80 | ||
98b15851 | 81 | BT_ASSERT_DBG(clock_snapshot); |
605e1019 PP |
82 | BT_LIB_LOGD("Recycling clock snapshot: %!+k", clock_snapshot); |
83 | ||
84 | /* | |
85 | * Those are the important ordered steps: | |
86 | * | |
87 | * 1. Reset the clock snapshot object, but do NOT put its clock | |
88 | * class's reference. This clock class contains the pool to | |
89 | * which we're about to recycle this clock snapshot object, | |
90 | * so we must guarantee its existence thanks to this existing | |
91 | * reference. | |
92 | * | |
93 | * 2. Move the clock class reference to our `clock_class` | |
94 | * variable so that we can set the clock snapshot's clock | |
95 | * class member to NULL before recycling it. We CANNOT do | |
96 | * this after we put the clock class reference because this | |
97 | * bt_object_put_ref() could destroy the clock class, also | |
98 | * destroying its clock snapshot pool, thus also destroying | |
99 | * our clock snapshot object (this would result in an invalid | |
100 | * write access). | |
101 | * | |
102 | * 3. Recycle the clock snapshot object. | |
103 | * | |
104 | * 4. Put our clock class reference. | |
105 | */ | |
106 | bt_clock_snapshot_reset(clock_snapshot); | |
107 | clock_class = clock_snapshot->clock_class; | |
98b15851 | 108 | BT_ASSERT_DBG(clock_class); |
605e1019 PP |
109 | clock_snapshot->clock_class = NULL; |
110 | bt_object_pool_recycle_object(&clock_class->cs_pool, clock_snapshot); | |
111 | bt_object_put_ref(clock_class); | |
112 | } | |
113 | ||
1353b066 | 114 | BT_EXPORT |
58085ca4 PP |
115 | uint64_t bt_clock_snapshot_get_value( |
116 | const struct bt_clock_snapshot *clock_snapshot) | |
605e1019 | 117 | { |
d5b13b9b | 118 | BT_ASSERT_PRE_DEV_CS_NON_NULL(clock_snapshot); |
1778c2a4 | 119 | BT_ASSERT_DBG(clock_snapshot->is_set); |
605e1019 PP |
120 | return clock_snapshot->value_cycles; |
121 | } | |
122 | ||
1353b066 | 123 | BT_EXPORT |
d24d5663 PP |
124 | enum bt_clock_snapshot_get_ns_from_origin_status |
125 | bt_clock_snapshot_get_ns_from_origin( | |
dc68f16d | 126 | const struct bt_clock_snapshot *clock_snapshot, |
605e1019 PP |
127 | int64_t *ret_value_ns) |
128 | { | |
d24d5663 | 129 | int ret = BT_FUNC_STATUS_OK; |
dc68f16d | 130 | |
17f3083a | 131 | BT_ASSERT_PRE_DEV_NO_ERROR(); |
d5b13b9b | 132 | BT_ASSERT_PRE_DEV_CS_NON_NULL(clock_snapshot); |
1778c2a4 PP |
133 | BT_ASSERT_PRE_DEV_NON_NULL("value-ns-output", ret_value_ns, |
134 | "Value (ns) (output)"); | |
135 | BT_ASSERT_DBG(clock_snapshot->is_set); | |
605e1019 PP |
136 | |
137 | if (clock_snapshot->ns_from_origin_overflows) { | |
bc3d9692 PP |
138 | BT_LIB_LOGE_APPEND_CAUSE( |
139 | "Clock snapshot, once converted to nanoseconds from origin, " | |
605e1019 PP |
140 | "overflows the signed 64-bit integer range: " |
141 | "%![cs-]+k", clock_snapshot); | |
520cdc82 | 142 | ret = BT_FUNC_STATUS_OVERFLOW_ERROR; |
605e1019 PP |
143 | goto end; |
144 | } | |
145 | ||
146 | *ret_value_ns = clock_snapshot->ns_from_origin; | |
147 | ||
148 | end: | |
149 | return ret; | |
150 | } | |
151 | ||
1353b066 | 152 | BT_EXPORT |
605e1019 PP |
153 | const struct bt_clock_class *bt_clock_snapshot_borrow_clock_class_const( |
154 | const struct bt_clock_snapshot *clock_snapshot) | |
155 | { | |
d5b13b9b | 156 | BT_ASSERT_PRE_DEV_CS_NON_NULL(clock_snapshot); |
605e1019 PP |
157 | return clock_snapshot->clock_class; |
158 | } |