| 1 | /* |
| 2 | * clock.c |
| 3 | * |
| 4 | * Babeltrace CTF IR - Clock |
| 5 | * |
| 6 | * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 7 | * Copyright 2017 Philippe Proulx <pproulx@efficios.com> |
| 8 | * |
| 9 | * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 10 | * |
| 11 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 12 | * of this software and associated documentation files (the "Software"), to deal |
| 13 | * in the Software without restriction, including without limitation the rights |
| 14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 15 | * copies of the Software, and to permit persons to whom the Software is |
| 16 | * furnished to do so, subject to the following conditions: |
| 17 | * |
| 18 | * The above copyright notice and this permission notice shall be included in |
| 19 | * all copies or substantial portions of the Software. |
| 20 | * |
| 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 24 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 26 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 27 | * SOFTWARE. |
| 28 | */ |
| 29 | |
| 30 | #define BT_LOG_TAG "CTF-WRITER-CLOCK" |
| 31 | #include <babeltrace/lib-logging-internal.h> |
| 32 | |
| 33 | #include <babeltrace/ctf-writer/clock-internal.h> |
| 34 | #include <babeltrace/ctf-ir/clock-class.h> |
| 35 | #include <babeltrace/ctf-ir/clock-class-internal.h> |
| 36 | #include <babeltrace/ctf-ir/utils.h> |
| 37 | #include <babeltrace/compat/uuid-internal.h> |
| 38 | #include <babeltrace/ref.h> |
| 39 | #include <babeltrace/object-internal.h> |
| 40 | #include <babeltrace/compiler-internal.h> |
| 41 | #include <babeltrace/assert-internal.h> |
| 42 | #include <babeltrace/assert-pre-internal.h> |
| 43 | #include <inttypes.h> |
| 44 | |
| 45 | static |
| 46 | void bt_ctf_clock_destroy(struct bt_object *obj); |
| 47 | |
| 48 | struct bt_ctf_clock *bt_ctf_clock_create(const char *name) |
| 49 | { |
| 50 | int ret; |
| 51 | struct bt_ctf_clock *clock = NULL; |
| 52 | unsigned char cc_uuid[BABELTRACE_UUID_LEN]; |
| 53 | |
| 54 | BT_ASSERT_PRE_NON_NULL(name, "Name"); |
| 55 | clock = g_new0(struct bt_ctf_clock, 1); |
| 56 | if (!clock) { |
| 57 | goto error; |
| 58 | } |
| 59 | |
| 60 | bt_object_init(clock, bt_ctf_clock_destroy); |
| 61 | clock->value = 0; |
| 62 | |
| 63 | /* Pre-2.0.0 backward compatibility: default frequency is 1 GHz */ |
| 64 | clock->clock_class = bt_clock_class_create(name, 1000000000); |
| 65 | if (!clock->clock_class) { |
| 66 | goto error; |
| 67 | } |
| 68 | |
| 69 | /* Automatically set clock class's UUID. */ |
| 70 | ret = bt_uuid_generate(cc_uuid); |
| 71 | if (ret) { |
| 72 | goto error; |
| 73 | } |
| 74 | |
| 75 | ret = bt_clock_class_set_uuid(clock->clock_class, cc_uuid); |
| 76 | BT_ASSERT(ret == 0); |
| 77 | return clock; |
| 78 | |
| 79 | error: |
| 80 | BT_PUT(clock); |
| 81 | return clock; |
| 82 | } |
| 83 | |
| 84 | const char *bt_ctf_clock_get_name(struct bt_ctf_clock *clock) |
| 85 | { |
| 86 | BT_ASSERT_PRE_NON_NULL(clock, "CTF writer clock"); |
| 87 | return bt_clock_class_get_name(clock->clock_class); |
| 88 | } |
| 89 | |
| 90 | const char *bt_ctf_clock_get_description(struct bt_ctf_clock *clock) |
| 91 | { |
| 92 | BT_ASSERT_PRE_NON_NULL(clock, "CTF writer clock"); |
| 93 | return bt_clock_class_get_description(clock->clock_class); |
| 94 | } |
| 95 | |
| 96 | int bt_ctf_clock_set_description(struct bt_ctf_clock *clock, const char *desc) |
| 97 | { |
| 98 | BT_ASSERT_PRE_NON_NULL(clock, "CTF writer clock"); |
| 99 | return bt_clock_class_set_description(clock->clock_class, desc); |
| 100 | } |
| 101 | |
| 102 | uint64_t bt_ctf_clock_get_frequency(struct bt_ctf_clock *clock) |
| 103 | { |
| 104 | BT_ASSERT_PRE_NON_NULL(clock, "CTF writer clock"); |
| 105 | return bt_clock_class_get_frequency(clock->clock_class); |
| 106 | } |
| 107 | |
| 108 | int bt_ctf_clock_set_frequency(struct bt_ctf_clock *clock, uint64_t freq) |
| 109 | { |
| 110 | BT_ASSERT_PRE_NON_NULL(clock, "CTF writer clock"); |
| 111 | return bt_clock_class_set_frequency(clock->clock_class, freq); |
| 112 | } |
| 113 | |
| 114 | uint64_t bt_ctf_clock_get_precision(struct bt_ctf_clock *clock) |
| 115 | { |
| 116 | BT_ASSERT_PRE_NON_NULL(clock, "CTF writer clock"); |
| 117 | return bt_clock_class_get_precision(clock->clock_class); |
| 118 | } |
| 119 | |
| 120 | int bt_ctf_clock_set_precision(struct bt_ctf_clock *clock, uint64_t precision) |
| 121 | { |
| 122 | BT_ASSERT_PRE_NON_NULL(clock, "CTF writer clock"); |
| 123 | return bt_clock_class_set_precision(clock->clock_class, precision); |
| 124 | } |
| 125 | |
| 126 | int bt_ctf_clock_get_offset_s(struct bt_ctf_clock *clock, int64_t *offset_s) |
| 127 | { |
| 128 | BT_ASSERT_PRE_NON_NULL(clock, "CTF writer clock"); |
| 129 | return bt_clock_class_get_offset_s(clock->clock_class, offset_s); |
| 130 | } |
| 131 | |
| 132 | int bt_ctf_clock_set_offset_s(struct bt_ctf_clock *clock, int64_t offset_s) |
| 133 | { |
| 134 | BT_ASSERT_PRE_NON_NULL(clock, "CTF writer clock"); |
| 135 | return bt_clock_class_set_offset_s(clock->clock_class, offset_s); |
| 136 | } |
| 137 | |
| 138 | int bt_ctf_clock_get_offset(struct bt_ctf_clock *clock, int64_t *offset) |
| 139 | { |
| 140 | BT_ASSERT_PRE_NON_NULL(clock, "CTF writer clock"); |
| 141 | return bt_clock_class_get_offset_cycles(clock->clock_class, offset); |
| 142 | } |
| 143 | |
| 144 | int bt_ctf_clock_set_offset(struct bt_ctf_clock *clock, int64_t offset) |
| 145 | { |
| 146 | BT_ASSERT_PRE_NON_NULL(clock, "CTF writer clock"); |
| 147 | return bt_clock_class_set_offset_cycles(clock->clock_class, offset); |
| 148 | } |
| 149 | |
| 150 | int bt_ctf_clock_get_is_absolute(struct bt_ctf_clock *clock) |
| 151 | { |
| 152 | BT_ASSERT_PRE_NON_NULL(clock, "CTF writer clock"); |
| 153 | return bt_clock_class_is_absolute(clock->clock_class); |
| 154 | } |
| 155 | |
| 156 | int bt_ctf_clock_set_is_absolute(struct bt_ctf_clock *clock, int is_absolute) |
| 157 | { |
| 158 | BT_ASSERT_PRE_NON_NULL(clock, "CTF writer clock"); |
| 159 | return bt_clock_class_set_is_absolute(clock->clock_class, is_absolute); |
| 160 | } |
| 161 | |
| 162 | const unsigned char *bt_ctf_clock_get_uuid(struct bt_ctf_clock *clock) |
| 163 | { |
| 164 | BT_ASSERT_PRE_NON_NULL(clock, "CTF writer clock"); |
| 165 | return bt_clock_class_get_uuid(clock->clock_class); |
| 166 | } |
| 167 | |
| 168 | int bt_ctf_clock_set_uuid(struct bt_ctf_clock *clock, const unsigned char *uuid) |
| 169 | { |
| 170 | BT_ASSERT_PRE_NON_NULL(clock, "CTF writer clock"); |
| 171 | return bt_clock_class_set_uuid(clock->clock_class, uuid); |
| 172 | } |
| 173 | |
| 174 | int bt_ctf_clock_set_time(struct bt_ctf_clock *clock, int64_t time) |
| 175 | { |
| 176 | int64_t value; |
| 177 | |
| 178 | BT_ASSERT_PRE_NON_NULL(clock, "CTF writer clock"); |
| 179 | |
| 180 | /* Common case where cycles are actually nanoseconds */ |
| 181 | if (clock->clock_class->frequency == 1000000000) { |
| 182 | value = time; |
| 183 | } else { |
| 184 | value = (uint64_t) (((double) time * |
| 185 | (double) clock->clock_class->frequency) / 1e9); |
| 186 | } |
| 187 | |
| 188 | BT_ASSERT_PRE(clock->value <= value, |
| 189 | "CTF writer clock value must be updated monotonically: " |
| 190 | "prev-value=%" PRId64 ", new-value=%" PRId64, |
| 191 | clock->value, value); |
| 192 | clock->value = value; |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | void bt_ctf_clock_get(struct bt_ctf_clock *clock) |
| 197 | { |
| 198 | bt_get(clock); |
| 199 | } |
| 200 | |
| 201 | void bt_ctf_clock_put(struct bt_ctf_clock *clock) |
| 202 | { |
| 203 | bt_put(clock); |
| 204 | } |
| 205 | |
| 206 | BT_HIDDEN |
| 207 | int bt_ctf_clock_get_value(struct bt_ctf_clock *clock, uint64_t *value) |
| 208 | { |
| 209 | BT_ASSERT_PRE_NON_NULL(clock, "CTF writer clock"); |
| 210 | BT_ASSERT_PRE_NON_NULL(value, "Value"); |
| 211 | *value = clock->value; |
| 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | static |
| 216 | void bt_ctf_clock_destroy(struct bt_object *obj) |
| 217 | { |
| 218 | struct bt_ctf_clock *clock; |
| 219 | |
| 220 | clock = container_of(obj, struct bt_ctf_clock, base); |
| 221 | bt_put(clock->clock_class); |
| 222 | g_free(clock); |
| 223 | } |