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