| 1 | /* |
| 2 | * clock.c |
| 3 | * |
| 4 | * Babeltrace CTF IR - Clock |
| 5 | * |
| 6 | * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 7 | * |
| 8 | * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 9 | * |
| 10 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 11 | * of this software and associated documentation files (the "Software"), to deal |
| 12 | * in the Software without restriction, including without limitation the rights |
| 13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 14 | * copies of the Software, and to permit persons to whom the Software is |
| 15 | * furnished to do so, subject to the following conditions: |
| 16 | * |
| 17 | * The above copyright notice and this permission notice shall be included in |
| 18 | * all copies or substantial portions of the Software. |
| 19 | * |
| 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 26 | * SOFTWARE. |
| 27 | */ |
| 28 | |
| 29 | #include <babeltrace/ctf-ir/clock-internal.h> |
| 30 | #include <babeltrace/ctf-writer/writer-internal.h> |
| 31 | #include <babeltrace/compiler.h> |
| 32 | #include <inttypes.h> |
| 33 | |
| 34 | static |
| 35 | void bt_ctf_clock_destroy(struct bt_ctf_ref *ref); |
| 36 | |
| 37 | BT_HIDDEN |
| 38 | struct bt_ctf_clock *_bt_ctf_clock_create(void) |
| 39 | { |
| 40 | struct bt_ctf_clock *clock = g_new0( |
| 41 | struct bt_ctf_clock, 1); |
| 42 | |
| 43 | if (!clock) { |
| 44 | goto end; |
| 45 | } |
| 46 | |
| 47 | clock->precision = 1; |
| 48 | clock->frequency = 1000000000; |
| 49 | bt_ctf_ref_init(&clock->ref_count); |
| 50 | end: |
| 51 | return clock; |
| 52 | } |
| 53 | |
| 54 | BT_HIDDEN |
| 55 | int bt_ctf_clock_set_name(struct bt_ctf_clock *clock, |
| 56 | const char *name) |
| 57 | { |
| 58 | int ret = 0; |
| 59 | |
| 60 | if (validate_identifier(name)) { |
| 61 | ret = -1; |
| 62 | goto end; |
| 63 | } |
| 64 | |
| 65 | if (clock->name) { |
| 66 | g_string_assign(clock->name, name); |
| 67 | } else { |
| 68 | clock->name = g_string_new(name); |
| 69 | if (!clock->name) { |
| 70 | ret = -1; |
| 71 | goto end; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | end: |
| 76 | return ret; |
| 77 | } |
| 78 | |
| 79 | struct bt_ctf_clock *bt_ctf_clock_create(const char *name) |
| 80 | { |
| 81 | int ret; |
| 82 | struct bt_ctf_clock *clock = NULL; |
| 83 | |
| 84 | clock = _bt_ctf_clock_create(); |
| 85 | if (!clock) { |
| 86 | goto error; |
| 87 | } |
| 88 | |
| 89 | ret = bt_ctf_clock_set_name(clock, name); |
| 90 | if (ret) { |
| 91 | goto error_destroy; |
| 92 | } |
| 93 | |
| 94 | ret = babeltrace_uuid_generate(clock->uuid); |
| 95 | if (ret) { |
| 96 | goto error_destroy; |
| 97 | } |
| 98 | |
| 99 | clock->uuid_set = 1; |
| 100 | return clock; |
| 101 | error_destroy: |
| 102 | bt_ctf_clock_destroy(&clock->ref_count); |
| 103 | error: |
| 104 | return NULL; |
| 105 | } |
| 106 | |
| 107 | const char *bt_ctf_clock_get_name(struct bt_ctf_clock *clock) |
| 108 | { |
| 109 | const char *ret = NULL; |
| 110 | |
| 111 | if (!clock) { |
| 112 | goto end; |
| 113 | } |
| 114 | |
| 115 | if (clock->name) { |
| 116 | ret = clock->name->str; |
| 117 | } |
| 118 | |
| 119 | end: |
| 120 | return ret; |
| 121 | } |
| 122 | |
| 123 | const char *bt_ctf_clock_get_description(struct bt_ctf_clock *clock) |
| 124 | { |
| 125 | const char *ret = NULL; |
| 126 | |
| 127 | if (!clock) { |
| 128 | goto end; |
| 129 | } |
| 130 | |
| 131 | if (clock->description) { |
| 132 | ret = clock->description->str; |
| 133 | } |
| 134 | end: |
| 135 | return ret; |
| 136 | } |
| 137 | |
| 138 | int bt_ctf_clock_set_description(struct bt_ctf_clock *clock, const char *desc) |
| 139 | { |
| 140 | int ret = 0; |
| 141 | |
| 142 | if (!clock || !desc || clock->frozen) { |
| 143 | ret = -1; |
| 144 | goto end; |
| 145 | } |
| 146 | |
| 147 | clock->description = g_string_new(desc); |
| 148 | ret = clock->description ? 0 : -1; |
| 149 | end: |
| 150 | return ret; |
| 151 | } |
| 152 | |
| 153 | uint64_t bt_ctf_clock_get_frequency(struct bt_ctf_clock *clock) |
| 154 | { |
| 155 | uint64_t ret = -1ULL; |
| 156 | |
| 157 | if (!clock) { |
| 158 | goto end; |
| 159 | } |
| 160 | |
| 161 | ret = clock->frequency; |
| 162 | end: |
| 163 | return ret; |
| 164 | } |
| 165 | |
| 166 | int bt_ctf_clock_set_frequency(struct bt_ctf_clock *clock, uint64_t freq) |
| 167 | { |
| 168 | int ret = 0; |
| 169 | |
| 170 | if (!clock || clock->frozen) { |
| 171 | ret = -1; |
| 172 | goto end; |
| 173 | } |
| 174 | |
| 175 | clock->frequency = freq; |
| 176 | end: |
| 177 | return ret; |
| 178 | } |
| 179 | |
| 180 | uint64_t bt_ctf_clock_get_precision(struct bt_ctf_clock *clock) |
| 181 | { |
| 182 | uint64_t ret = -1ULL; |
| 183 | |
| 184 | if (!clock) { |
| 185 | goto end; |
| 186 | } |
| 187 | |
| 188 | ret = clock->precision; |
| 189 | end: |
| 190 | return ret; |
| 191 | } |
| 192 | |
| 193 | int bt_ctf_clock_set_precision(struct bt_ctf_clock *clock, uint64_t precision) |
| 194 | { |
| 195 | int ret = 0; |
| 196 | |
| 197 | if (!clock || clock->frozen) { |
| 198 | ret = -1; |
| 199 | goto end; |
| 200 | } |
| 201 | |
| 202 | clock->precision = precision; |
| 203 | end: |
| 204 | return ret; |
| 205 | } |
| 206 | |
| 207 | uint64_t bt_ctf_clock_get_offset_s(struct bt_ctf_clock *clock) |
| 208 | { |
| 209 | uint64_t ret = -1ULL; |
| 210 | |
| 211 | if (!clock) { |
| 212 | goto end; |
| 213 | } |
| 214 | |
| 215 | ret = clock->offset_s; |
| 216 | end: |
| 217 | return ret; |
| 218 | } |
| 219 | |
| 220 | int bt_ctf_clock_set_offset_s(struct bt_ctf_clock *clock, uint64_t offset_s) |
| 221 | { |
| 222 | int ret = 0; |
| 223 | |
| 224 | if (!clock || clock->frozen) { |
| 225 | ret = -1; |
| 226 | goto end; |
| 227 | } |
| 228 | |
| 229 | clock->offset_s = offset_s; |
| 230 | end: |
| 231 | return ret; |
| 232 | } |
| 233 | |
| 234 | uint64_t bt_ctf_clock_get_offset(struct bt_ctf_clock *clock) |
| 235 | { |
| 236 | uint64_t ret = -1ULL; |
| 237 | |
| 238 | if (!clock) { |
| 239 | goto end; |
| 240 | } |
| 241 | |
| 242 | ret = clock->offset; |
| 243 | end: |
| 244 | return ret; |
| 245 | } |
| 246 | |
| 247 | int bt_ctf_clock_set_offset(struct bt_ctf_clock *clock, uint64_t offset) |
| 248 | { |
| 249 | int ret = 0; |
| 250 | |
| 251 | if (!clock || clock->frozen) { |
| 252 | ret = -1; |
| 253 | goto end; |
| 254 | } |
| 255 | |
| 256 | clock->offset = offset; |
| 257 | end: |
| 258 | return ret; |
| 259 | } |
| 260 | |
| 261 | int bt_ctf_clock_get_is_absolute(struct bt_ctf_clock *clock) |
| 262 | { |
| 263 | int ret = -1; |
| 264 | |
| 265 | if (!clock) { |
| 266 | goto end; |
| 267 | } |
| 268 | |
| 269 | ret = clock->absolute; |
| 270 | end: |
| 271 | return ret; |
| 272 | } |
| 273 | |
| 274 | int bt_ctf_clock_set_is_absolute(struct bt_ctf_clock *clock, int is_absolute) |
| 275 | { |
| 276 | int ret = 0; |
| 277 | |
| 278 | if (!clock || clock->frozen) { |
| 279 | ret = -1; |
| 280 | goto end; |
| 281 | } |
| 282 | |
| 283 | clock->absolute = !!is_absolute; |
| 284 | end: |
| 285 | return ret; |
| 286 | } |
| 287 | |
| 288 | const unsigned char *bt_ctf_clock_get_uuid(struct bt_ctf_clock *clock) |
| 289 | { |
| 290 | const unsigned char *ret; |
| 291 | |
| 292 | if (!clock || !clock->uuid_set) { |
| 293 | ret = NULL; |
| 294 | goto end; |
| 295 | } |
| 296 | |
| 297 | ret = clock->uuid; |
| 298 | end: |
| 299 | return ret; |
| 300 | } |
| 301 | |
| 302 | int bt_ctf_clock_set_uuid(struct bt_ctf_clock *clock, const unsigned char *uuid) |
| 303 | { |
| 304 | int ret = 0; |
| 305 | |
| 306 | if (!clock || !uuid || clock->frozen) { |
| 307 | ret = -1; |
| 308 | goto end; |
| 309 | } |
| 310 | |
| 311 | memcpy(clock->uuid, uuid, sizeof(uuid_t)); |
| 312 | clock->uuid_set = 1; |
| 313 | end: |
| 314 | return ret; |
| 315 | } |
| 316 | |
| 317 | uint64_t bt_ctf_clock_get_time(struct bt_ctf_clock *clock) |
| 318 | { |
| 319 | uint64_t ret = -1ULL; |
| 320 | |
| 321 | if (!clock) { |
| 322 | goto end; |
| 323 | } |
| 324 | |
| 325 | ret = clock->time; |
| 326 | end: |
| 327 | return ret; |
| 328 | } |
| 329 | |
| 330 | int bt_ctf_clock_set_time(struct bt_ctf_clock *clock, uint64_t time) |
| 331 | { |
| 332 | int ret = 0; |
| 333 | |
| 334 | /* Timestamps are strictly monotonic */ |
| 335 | if (!clock || time < clock->time) { |
| 336 | ret = -1; |
| 337 | goto end; |
| 338 | } |
| 339 | |
| 340 | clock->time = time; |
| 341 | end: |
| 342 | return ret; |
| 343 | } |
| 344 | |
| 345 | void bt_ctf_clock_get(struct bt_ctf_clock *clock) |
| 346 | { |
| 347 | if (!clock) { |
| 348 | return; |
| 349 | } |
| 350 | |
| 351 | bt_ctf_ref_get(&clock->ref_count); |
| 352 | } |
| 353 | |
| 354 | void bt_ctf_clock_put(struct bt_ctf_clock *clock) |
| 355 | { |
| 356 | if (!clock) { |
| 357 | return; |
| 358 | } |
| 359 | |
| 360 | bt_ctf_ref_put(&clock->ref_count, bt_ctf_clock_destroy); |
| 361 | } |
| 362 | |
| 363 | BT_HIDDEN |
| 364 | void bt_ctf_clock_freeze(struct bt_ctf_clock *clock) |
| 365 | { |
| 366 | if (!clock) { |
| 367 | return; |
| 368 | } |
| 369 | |
| 370 | clock->frozen = 1; |
| 371 | } |
| 372 | |
| 373 | BT_HIDDEN |
| 374 | void bt_ctf_clock_serialize(struct bt_ctf_clock *clock, |
| 375 | struct metadata_context *context) |
| 376 | { |
| 377 | unsigned char *uuid; |
| 378 | |
| 379 | if (!clock || !context) { |
| 380 | return; |
| 381 | } |
| 382 | |
| 383 | uuid = clock->uuid; |
| 384 | g_string_append(context->string, "clock {\n"); |
| 385 | g_string_append_printf(context->string, "\tname = %s;\n", |
| 386 | clock->name->str); |
| 387 | g_string_append_printf(context->string, |
| 388 | "\tuuid = \"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\";\n", |
| 389 | uuid[0], uuid[1], uuid[2], uuid[3], |
| 390 | uuid[4], uuid[5], uuid[6], uuid[7], |
| 391 | uuid[8], uuid[9], uuid[10], uuid[11], |
| 392 | uuid[12], uuid[13], uuid[14], uuid[15]); |
| 393 | if (clock->description) { |
| 394 | g_string_append_printf(context->string, "\tdescription = \"%s\";\n", |
| 395 | clock->description->str); |
| 396 | } |
| 397 | |
| 398 | g_string_append_printf(context->string, "\tfreq = %" PRIu64 ";\n", |
| 399 | clock->frequency); |
| 400 | g_string_append_printf(context->string, "\tprecision = %" PRIu64 ";\n", |
| 401 | clock->precision); |
| 402 | g_string_append_printf(context->string, "\toffset_s = %" PRIu64 ";\n", |
| 403 | clock->offset_s); |
| 404 | g_string_append_printf(context->string, "\toffset = %" PRIu64 ";\n", |
| 405 | clock->offset); |
| 406 | g_string_append_printf(context->string, "\tabsolute = %s;\n", |
| 407 | clock->absolute ? "TRUE" : "FALSE"); |
| 408 | g_string_append(context->string, "};\n\n"); |
| 409 | } |
| 410 | |
| 411 | static |
| 412 | void bt_ctf_clock_destroy(struct bt_ctf_ref *ref) |
| 413 | { |
| 414 | struct bt_ctf_clock *clock; |
| 415 | |
| 416 | if (!ref) { |
| 417 | return; |
| 418 | } |
| 419 | |
| 420 | clock = container_of(ref, struct bt_ctf_clock, ref_count); |
| 421 | if (clock->name) { |
| 422 | g_string_free(clock->name, TRUE); |
| 423 | } |
| 424 | |
| 425 | if (clock->description) { |
| 426 | g_string_free(clock->description, TRUE); |
| 427 | } |
| 428 | |
| 429 | g_free(clock); |
| 430 | } |