-/*
- * The offset between monotonic and realtime clock can be negative if
- * the system sets the REALTIME clock to 0 after boot.
- */
-static
-int measure_single_clock_offset(struct offset_sample *sample)
-{
- uint64_t monotonic_avg, monotonic[2], measure_delta, realtime;
- uint64_t tcf = trace_clock_freq();
- struct timespec rts = { 0, 0 };
- int ret;
-
- monotonic[0] = trace_clock_read64();
- ret = lttng_clock_gettime(CLOCK_REALTIME, &rts);
- if (ret < 0) {
- return ret;
- }
- monotonic[1] = trace_clock_read64();
- measure_delta = monotonic[1] - monotonic[0];
- if (measure_delta > sample->measure_delta) {
- /*
- * Discard value if it took longer to read than the best
- * sample so far.
- */
- return 0;
- }
- monotonic_avg = (monotonic[0] + monotonic[1]) >> 1;
- realtime = (uint64_t) rts.tv_sec * tcf;
- if (tcf == NSEC_PER_SEC) {
- realtime += rts.tv_nsec;
- } else {
- realtime += (uint64_t) rts.tv_nsec * tcf / NSEC_PER_SEC;
- }
- sample->offset = (int64_t) realtime - monotonic_avg;
- sample->measure_delta = measure_delta;
- return 0;
-}
-
-/*
- * Approximation of NTP time of day to clock monotonic correlation,
- * taken at start of trace. Keep the measurement that took the less time
- * to complete, thus removing imprecision caused by preemption.
- * May return a negative offset.
- */
-static
-int64_t measure_clock_offset(void)
-{
- int i;
- struct offset_sample offset_best_sample = {
- .offset = 0,
- .measure_delta = UINT64_MAX,
- };
-
- for (i = 0; i < NR_CLOCK_OFFSET_SAMPLES; i++) {
- if (measure_single_clock_offset(&offset_best_sample)) {
- return 0;
- }
- }
- return offset_best_sample.offset;
-}
-