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