Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e564266c authored by Frank Li's avatar Frank Li Committed by Automerger Merge Worker
Browse files

Merge "Change stopwatch accuracy from milliseconds to microseconds" am: 3f32101a

Change-Id: I1da62467c481fa0e1f9d2b9f6b1fcbc2ed818d63
parents 2902a090 3f32101a
Loading
Loading
Loading
Loading
+12 −12
Original line number Original line Diff line number Diff line
@@ -23,15 +23,15 @@ import android.os.SystemClock;
 * @hide
 * @hide
 */
 */
public class Stopwatch {
public class Stopwatch {
    private long mStartTimeMs;
    private long mStartTimeNs;
    private long mStopTimeMs;
    private long mStopTimeNs;


    public boolean isStarted() {
    public boolean isStarted() {
        return (mStartTimeMs > 0);
        return (mStartTimeNs > 0);
    }
    }


    public boolean isStopped() {
    public boolean isStopped() {
        return (mStopTimeMs > 0);
        return (mStopTimeNs > 0);
    }
    }


    public boolean isRunning() {
    public boolean isRunning() {
@@ -43,31 +43,31 @@ public class Stopwatch {
     */
     */
    public Stopwatch start() {
    public Stopwatch start() {
        if (!isStarted()) {
        if (!isStarted()) {
            mStartTimeMs = SystemClock.elapsedRealtime();
            mStartTimeNs = SystemClock.elapsedRealtimeNanos();
        }
        }
        return this;
        return this;
    }
    }


    /**
    /**
     * Stop the Stopwatch.
     * Stop the Stopwatch.
     * @return the total time recorded, in milliseconds, or 0 if not started.
     * @return the total time recorded, in microseconds, or 0 if not started.
     */
     */
    public long stop() {
    public long stop() {
        if (isRunning()) {
        if (isRunning()) {
            mStopTimeMs = SystemClock.elapsedRealtime();
            mStopTimeNs = SystemClock.elapsedRealtimeNanos();
        }
        }
        // Return either the delta after having stopped, or 0.
        // Return either the delta after having stopped, or 0.
        return (mStopTimeMs - mStartTimeMs);
        return (mStopTimeNs - mStartTimeNs) / 1000;
    }
    }


    /**
    /**
     * Return the total time recorded to date, in milliseconds.
     * Return the total time recorded to date, in microseconds.
     * If the Stopwatch is not running, returns the same value as stop(),
     * If the Stopwatch is not running, returns the same value as stop(),
     * i.e. either the total time recorded before stopping or 0.
     * i.e. either the total time recorded before stopping or 0.
     */
     */
    public long lap() {
    public long lap() {
        if (isRunning()) {
        if (isRunning()) {
            return (SystemClock.elapsedRealtime() - mStartTimeMs);
            return (SystemClock.elapsedRealtimeNanos() - mStartTimeNs) / 1000;
        } else {
        } else {
            return stop();
            return stop();
        }
        }
@@ -77,7 +77,7 @@ public class Stopwatch {
     * Reset the Stopwatch. It will be stopped when this method returns.
     * Reset the Stopwatch. It will be stopped when this method returns.
     */
     */
    public void reset() {
    public void reset() {
        mStartTimeMs = 0;
        mStartTimeNs = 0;
        mStopTimeMs = 0;
        mStopTimeNs = 0;
    }
    }
}
}
+6 −5
Original line number Original line Diff line number Diff line
@@ -147,7 +147,7 @@ public class DnsUtils {
        } catch (TimeoutException | InterruptedException e) {
        } catch (TimeoutException | InterruptedException e) {
            errorMsg = "Timeout";
            errorMsg = "Timeout";
        } finally {
        } finally {
            logDnsResult(result, watch.stop() /* latency */, logger, type, errorMsg);
            logDnsResult(result, watch.stop() / 1000 /* latencyMs */, logger, type, errorMsg);
        }
        }


        if (null != errorMsg) throw new UnknownHostException(host);
        if (null != errorMsg) throw new UnknownHostException(host);
@@ -155,8 +155,9 @@ public class DnsUtils {
        return result.toArray(new InetAddress[0]);
        return result.toArray(new InetAddress[0]);
    }
    }


    private static void logDnsResult(@Nullable final List<InetAddress> results, final long latency,
    private static void logDnsResult(@Nullable final List<InetAddress> results,
            @Nullable final DnsLogFunc logger, int type, @NonNull final String errorMsg) {
            final long latencyMs, @Nullable final DnsLogFunc logger, int type,
            @NonNull final String errorMsg) {
        if (logger == null) {
        if (logger == null) {
            return;
            return;
        }
        }
@@ -166,9 +167,9 @@ public class DnsUtils {
            for (InetAddress address : results) {
            for (InetAddress address : results) {
                builder.append(',').append(address.getHostAddress());
                builder.append(',').append(address.getHostAddress());
            }
            }
            logger.log(String.format("%dms OK %s", latency, builder.substring(1)));
            logger.log(String.format("%dms OK %s", latencyMs, builder.substring(1)));
        } else {
        } else {
            logger.log(String.format("%dms FAIL in type %s %s", latency, dnsTypeToStr(type),
            logger.log(String.format("%dms FAIL in type %s %s", latencyMs, dnsTypeToStr(type),
                    errorMsg));
                    errorMsg));
        }
        }
    }
    }
+5 −5
Original line number Original line Diff line number Diff line
@@ -1398,11 +1398,11 @@ public class NetworkMonitor extends StateMachine {
                time = watch.stop();
                time = watch.stop();
                final String strIps = Arrays.toString(ips);
                final String strIps = Arrays.toString(ips);
                success = (ips != null && ips.length > 0);
                success = (ips != null && ips.length > 0);
                validationLog(PROBE_PRIVDNS, host, String.format("%dms: %s", time, strIps));
                validationLog(PROBE_PRIVDNS, host, String.format("%dus: %s", time, strIps));
            } catch (UnknownHostException uhe) {
            } catch (UnknownHostException uhe) {
                time = watch.stop();
                time = watch.stop();
                validationLog(PROBE_PRIVDNS, host,
                validationLog(PROBE_PRIVDNS, host,
                        String.format("%dms - Error: %s", time, uhe.getMessage()));
                        String.format("%dus - Error: %s", time, uhe.getMessage()));
            }
            }
            logValidationProbe(time, PROBE_PRIVDNS, success ? DNS_SUCCESS : DNS_FAILURE);
            logValidationProbe(time, PROBE_PRIVDNS, success ? DNS_SUCCESS : DNS_FAILURE);
            return success;
            return success;
@@ -2930,18 +2930,18 @@ public class NetworkMonitor extends StateMachine {
        if (mEvaluationTimer.isRunning()) {
        if (mEvaluationTimer.isRunning()) {
            int[] transports = mNetworkCapabilities.getTransportTypes();
            int[] transports = mNetworkCapabilities.getTransportTypes();
            mMetricsLog.log(mCleartextDnsNetwork, transports,
            mMetricsLog.log(mCleartextDnsNetwork, transports,
                    new NetworkEvent(evtype, mEvaluationTimer.stop()));
                    new NetworkEvent(evtype, mEvaluationTimer.stop() / 1000));
            mEvaluationTimer.reset();
            mEvaluationTimer.reset();
        }
        }
    }
    }


    private void logValidationProbe(long durationMs, int probeType, int probeResult) {
    private void logValidationProbe(long durationUs, int probeType, int probeResult) {
        int[] transports = mNetworkCapabilities.getTransportTypes();
        int[] transports = mNetworkCapabilities.getTransportTypes();
        boolean isFirstValidation = validationStage().mIsFirstValidation;
        boolean isFirstValidation = validationStage().mIsFirstValidation;
        ValidationProbeEvent ev = new ValidationProbeEvent.Builder()
        ValidationProbeEvent ev = new ValidationProbeEvent.Builder()
                .setProbeType(probeType, isFirstValidation)
                .setProbeType(probeType, isFirstValidation)
                .setReturnCode(probeResult)
                .setReturnCode(probeResult)
                .setDurationMs(durationMs)
                .setDurationMs(durationUs / 1000)
                .build();
                .build();
        mMetricsLog.log(mCleartextDnsNetwork, transports, ev);
        mMetricsLog.log(mCleartextDnsNetwork, transports, ev);
    }
    }