Skip to content

Commit

Permalink
Merge branch 'main' into patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
sunnylqm authored Dec 2, 2023
2 parents d5fcc31 + 1066ce0 commit 17142ad
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,10 @@
* A clock that returns number of milliseconds since boot. It guarantees that every next call to
* now() will return a value that is not less that was returned from previous call to now(). This
* happens regardless system time changes, time zone changes, daylight saving changes etc.
*
* <p>DO USE THIS CLOCK FOR PERFORMANCE MEASUREMENT. IT STOPS TICKING WHILE THE DEVICE SLEEPS, THAT
* IS, WHILE THE DEVICE CANNOT RUN THE CODE WE ARE PURPORTEDLY MEASURING.
*/
@Nullsafe(Nullsafe.Mode.LOCAL)
@DoNotStrip
public class AwakeTimeSinceBootClock implements MonotonicClock, MonotonicNanoClock {
public class AwakeTimeSinceBootClock implements MonotonicNanoClock {
@DoNotStrip private static final AwakeTimeSinceBootClock INSTANCE = new AwakeTimeSinceBootClock();

private AwakeTimeSinceBootClock() {}
Expand All @@ -35,16 +32,10 @@ public static AwakeTimeSinceBootClock get() {
return INSTANCE;
}

@Override
@DoNotStrip
public long now() {
// Guaranteed to be monotonic according to documentation.
return android.os.SystemClock.uptimeMillis();
}

@Override
@DoNotStrip
public long nowNanos() {
// Guaranteed to be monotonic according to documentation.
return java.lang.System.nanoTime();
}
}
49 changes: 47 additions & 2 deletions fbcore/src/main/java/com/facebook/common/time/MonotonicClock.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package com.facebook.common.time;

import com.facebook.common.internal.DoNotStrip;
import java.util.concurrent.TimeUnit;
import javax.annotation.concurrent.ThreadSafe;

/** A clock that is guaranteed not to go backward. */
Expand All @@ -19,10 +20,54 @@ public interface MonotonicClock {
* returned from this clock in this process. They have no meaning outside of this process and
* should not be written to disk.
*
* <p>The difference between two timestamps is an interval, in nanoseconds.
*
* @return A timestamp for the current time, in nanoseconds.
*/
@DoNotStrip
long nowNanos();

/**
* Produce a timestamp. Values returned from this method may only be compared to other values
* returned from this clock in this process. They have no meaning outside of this process and
* should not be written to disk. This method uses the same timesource as {@link #nowNanos()}
*
* <p>The difference between two timestamps is an interval, in milliseconds.
*
* @return A timestamp for the current time, in ms.
* @return A timestamp for the current time, in milliseconds.
*/
@DoNotStrip
long now();
default long now() {
return TimeUnit.NANOSECONDS.toMillis(nowNanos());
}

/**
* Produce a non-singleton MonotonicClock instance wrapping the given Clock.
*
* @return A MonotonicClock that uses the provided Clock.
*/
static MonotonicClock of(Clock provider) {
return new MonotonicClockWrapper(provider);
}

public static final class MonotonicClockWrapper implements MonotonicClock {
private final Clock provider;
private long mLast;

private MonotonicClockWrapper(Clock provider) {
this.provider = provider;
mLast = provider.now();
}

@Override
public long nowNanos() {
return TimeUnit.MILLISECONDS.toNanos(now());
}

@Override
public long now() {
mLast = Math.max(mLast, provider.now());
return mLast;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,8 @@

package com.facebook.common.time;

import com.facebook.common.internal.DoNotStrip;
import javax.annotation.concurrent.ThreadSafe;

/** A clock that is guaranteed not to go backward. */
@ThreadSafe
public interface MonotonicNanoClock {

/**
* Produce a timestamp. Values returned from this method may only be compared to other values
* returned from this clock in this process. They have no meaning outside of this process and
* should not be written to disk.
*
* <p>The difference between two timestamps is an interval, in nanoseconds.
*
* @return A timestamp for the current time, in nanoseconds.
*/
@DoNotStrip
long nowNanos();
}
public interface MonotonicNanoClock extends MonotonicClock {}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import com.facebook.common.internal.DoNotStrip;
import com.facebook.infer.annotation.Nullsafe;
import java.util.concurrent.TimeUnit;

/**
* A clock that returns number of milliseconds since boot. It guarantees that every next call to
Expand Down Expand Up @@ -38,6 +39,12 @@ public static RealtimeSinceBootClock get() {
@Override
public long now() {
// Guaranteed to be monotonic according to documentation.
return android.os.SystemClock.elapsedRealtime /*sic*/();
return android.os.SystemClock.elapsedRealtime();
}

@Override
public long nowNanos() {
// Guaranteed to be monotonic according to documentation.
return TimeUnit.MILLISECONDS.toNanos(now());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.facebook.infer.annotation.Nullsafe;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;

/** */
@Nullsafe(Nullsafe.Mode.STRICT)
Expand Down Expand Up @@ -80,6 +81,11 @@ public synchronized long now() {
return now;
}

@Override
public long nowNanos() {
return TimeUnit.MILLISECONDS.toNanos(now());
}

/**
* Add a clock that will get called before {@link #listeners}, in order to guarantee call order.
*/
Expand Down

0 comments on commit 17142ad

Please sign in to comment.