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:
return ret;
}
+uint64_t ns_from_value(uint64_t frequency, uint64_t value)
+{
+ uint64_t ns;
+
+ if (frequency == 1000000000) {
+ ns = value;
+ } else {
+ ns = (uint64_t) ((1e9 * (double) value) / (double) frequency);
+ }
+
+ return ns;
+}
+
int bt_ctf_clock_get_time(struct bt_ctf_clock *clock, int64_t *time)
{
int ret = 0;
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);
+
+ if (!clock->has_value) {
+ /*
+ * Clock belongs to a non-writer mode trace and thus
+ * this function is disabled.
+ */
+ goto end;
}
+ *time = (int64_t) ns_from_value(clock->frequency, clock->value);
+
end:
return ret;
}
int bt_ctf_clock_set_time(struct bt_ctf_clock *clock, int64_t time)
{
int ret = 0;
+ int64_t value;
/* Timestamps are strictly monotonic */
if (!clock) {
goto end;
}
- /* Common case where cycles are actually nanoseconds */
- if (clock->frequency == 1000000000) {
- clock->value = time;
+
+ if (!clock->has_value) {
+ /*
+ * Clock belongs to a non-writer mode trace and thus
+ * this function is disabled.
+ */
+ ret = -1;
goto end;
}
- ret = bt_ctf_clock_set_value(clock,
- (uint64_t) (((double) time * (double) clock->frequency) / 1e9));
+ /* Common case where cycles are actually nanoseconds */
+ if (clock->frequency == 1000000000) {
+ value = time;
+ } else {
+ value = (uint64_t) (((double) time *
+ (double) clock->frequency) / 1e9);
+ }
+ ret = bt_ctf_clock_set_value(clock, value);
end:
return ret;
}
goto end;
}
+ 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 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 || value < clock->value) {
+ if (value < clock->value) {
ret = -1;
goto end;
}
g_free(clock);
}
+
+int64_t bt_ctf_clock_ns_from_value(struct bt_ctf_clock *clock, uint64_t value)
+{
+ int64_t ns = -1ULL;
+
+ if (!clock) {
+ goto end;
+ }
+
+ /* Initialize nanosecond timestamp to clock's offset in seconds */
+ ns = clock->offset_s * 1000000000;
+
+ /* Add offset in cycles, converted to nanoseconds */
+ ns += ns_from_value(clock->frequency, clock->offset);
+
+ /* Add given value, converter to nanoseconds */
+ ns += ns_from_value(clock->frequency, value);
+
+end:
+ return ns;
+}