Home | History | Annotate | Download | only in walt
      1 /*
      2  * Copyright (C) 2016 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package org.chromium.latency.walt;
     18 
     19 /**
     20  * Representation of our best knowledge of the remote clock.
     21  * All time variables here are stored in microseconds.
     22  *
     23  * Which time reporting function is used locally on Android:
     24  * This app uses SystemClock.uptimeMillis() for keeping local time which, up to
     25  * units, is the same time reported by System.nanoTime() and by
     26  * clock_gettime(CLOCK_MONOTONIC, &ts) from time.h and is, roughly, the time
     27  * elapsed since last boot, excluding sleep time.
     28  *
     29  * base_time is the local monotonic clock time when remote clock was zeroed.
     30  *
     31  * micros() is our best available approximation of the current reading of the remote clock.
     32  *
     33  * Immediately after synchronization, minLag is set to zero and the remote clock is guaranteed to
     34  * lag behind what micros() reports by at most maxLag.
     35  *
     36  * Immediately after synchronization or an update of the bounds (minLag, maxLag) the following holds
     37  * t_remote + minLag < micros() < t_remote + maxLag
     38  *
     39  * For more details about clock synchronization refer to
     40  * https://github.com/google/walt/blob/master/android/WALT/app/src/main/jni/README.md
     41  * and sync_clock.c
     42  */
     43 
     44 public class RemoteClockInfo {
     45     public int minLag;
     46     public int maxLag;
     47     public long baseTime;
     48 
     49     public static long microTime() {
     50         return System.nanoTime() / 1000;
     51     }
     52 
     53     public long micros() {
     54         return microTime() - baseTime;
     55     }
     56 
     57     public int getMeanLag() {
     58         return (minLag + maxLag) / 2;
     59     }
     60 
     61     public String toString() {
     62         return "Remote clock [us]: current time = " + micros() + " baseTime = " + baseTime +
     63                 " lagBounds = (" + minLag + ", " + maxLag + ")";
     64     }
     65 }
     66