ir: disable clock value accessors in non-writer mode
[babeltrace.git] / formats / ctf / ir / clock.c
index 723b860dd6e9d377ccf6e7e0f442358720caa4e4..2e765430f66d8ad590e8dfbff01777fc62a3963f 100644 (file)
@@ -94,11 +94,18 @@ struct bt_ctf_clock *bt_ctf_clock_create(const char *name)
                goto error;
        }
 
-       ret = babeltrace_uuid_generate(clock->uuid);
+       ret = bt_uuid_generate(clock->uuid);
        if (ret) {
                goto error;
        }
 
+       /*
+        * For backward compatibility reasons, a fresh clock can have
+        * a value because it could be added to a trace created by a
+        * CTF writer. As soon as this clock is added to a non-writer
+        * trace, then its value/time functions will be disabled.
+        */
+       clock->has_value = 1;
        clock->uuid_set = 1;
        return clock;
 error:
@@ -206,20 +213,21 @@ end:
        return ret;
 }
 
-uint64_t bt_ctf_clock_get_offset_s(struct bt_ctf_clock *clock)
+int bt_ctf_clock_get_offset_s(struct bt_ctf_clock *clock, int64_t *offset_s)
 {
-       uint64_t ret = -1ULL;
+       int ret = 0;
 
-       if (!clock) {
+       if (!clock || !offset_s) {
+               ret = -1;
                goto end;
        }
 
-       ret = clock->offset_s;
+       *offset_s = clock->offset_s;
 end:
        return ret;
 }
 
-int bt_ctf_clock_set_offset_s(struct bt_ctf_clock *clock, uint64_t offset_s)
+int bt_ctf_clock_set_offset_s(struct bt_ctf_clock *clock, int64_t offset_s)
 {
        int ret = 0;
 
@@ -233,20 +241,21 @@ end:
        return ret;
 }
 
-uint64_t bt_ctf_clock_get_offset(struct bt_ctf_clock *clock)
+int bt_ctf_clock_get_offset(struct bt_ctf_clock *clock, int64_t *offset)
 {
-       uint64_t ret = -1ULL;
+       int ret = 0;
 
-       if (!clock) {
+       if (!clock || !offset) {
+               ret = -1;
                goto end;
        }
 
-       ret = clock->offset;
+       *offset = clock->offset;
 end:
        return ret;
 }
 
-int bt_ctf_clock_set_offset(struct bt_ctf_clock *clock, uint64_t offset)
+int bt_ctf_clock_set_offset(struct bt_ctf_clock *clock, int64_t offset)
 {
        int ret = 0;
 
@@ -316,7 +325,70 @@ end:
        return ret;
 }
 
-uint64_t bt_ctf_clock_get_time(struct bt_ctf_clock *clock)
+int bt_ctf_clock_get_time(struct bt_ctf_clock *clock, int64_t *time)
+{
+       int ret = 0;
+
+       if (!clock || !time) {
+               ret = -1;
+               goto end;
+       }
+
+
+       if (!clock->has_value) {
+               /*
+                * Clock belongs to a non-writer mode trace and thus
+                * this function is disabled.
+                */
+               goto end;
+       }
+
+       /* Common case where cycles are actually nanoseconds */
+       if (clock->frequency == 1000000000) {
+               *time = (int64_t) clock->value;
+       } else {
+               *time = (int64_t) ((1e9 * (double) clock->value) /
+                       (double) clock->frequency);
+       }
+
+end:
+       return ret;
+}
+
+int bt_ctf_clock_set_time(struct bt_ctf_clock *clock, int64_t time)
+{
+       int ret = 0;
+
+       /* Timestamps are strictly monotonic */
+       if (!clock) {
+               ret = -1;
+               goto end;
+       }
+
+
+       if (!clock->has_value) {
+               /*
+                * Clock belongs to a non-writer mode trace and thus
+                * this function is disabled.
+                */
+               ret = -1;
+               goto end;
+       }
+
+       /* Common case where cycles are actually nanoseconds */
+       if (clock->frequency == 1000000000) {
+               clock->value = time;
+               goto end;
+       }
+
+       ret = bt_ctf_clock_set_value(clock,
+               (uint64_t) (((double) time * (double) clock->frequency) / 1e9));
+
+end:
+       return ret;
+}
+
+uint64_t bt_ctf_clock_get_value(struct bt_ctf_clock *clock)
 {
        uint64_t ret = -1ULL;
 
@@ -324,22 +396,44 @@ uint64_t bt_ctf_clock_get_time(struct bt_ctf_clock *clock)
                goto end;
        }
 
-       ret = clock->time;
+       if (!clock->has_value) {
+               /*
+                * Clock belongs to a non-writer mode trace and thus
+                * this function is disabled.
+                */
+               goto end;
+       }
+
+       ret = clock->value;
 end:
        return ret;
 }
 
-int bt_ctf_clock_set_time(struct bt_ctf_clock *clock, uint64_t time)
+int bt_ctf_clock_set_value(struct bt_ctf_clock *clock, uint64_t value)
 {
        int ret = 0;
 
+       if (!clock) {
+               ret = -1;
+               goto end;
+       }
+
+       if (!clock->has_value) {
+               /*
+                * Clock belongs to a non-writer mode trace and thus
+                * this function is disabled.
+                */
+               ret = -1;
+               goto end;
+       }
+
        /* Timestamps are strictly monotonic */
-       if (!clock || time < clock->time) {
+       if (value < clock->value) {
                ret = -1;
                goto end;
        }
 
-       clock->time = time;
+       clock->value = value;
 end:
        return ret;
 }
This page took 0.027651 seconds and 4 git commands to generate.