mobile/android/base/Telemetry.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

     1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
     2  * This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     4  * You can obtain one at http://mozilla.org/MPL/2.0/. */
     6 package org.mozilla.gecko;
     8 import org.mozilla.gecko.mozglue.RobocopTarget;
    10 import android.os.SystemClock;
    11 import android.util.Log;
    13 /**
    14  * All telemetry times are relative to one of two clocks:
    15  * 
    16  * * Real time since the device was booted, including deep sleep. Use this
    17  *   as a substitute for wall clock.
    18  * * Uptime since the device was booted, excluding deep sleep. Use this to
    19  *   avoid timing a user activity when their phone is in their pocket!
    20  *
    21  * The majority of methods in this class are defined in terms of real time.
    22  */
    23 @RobocopTarget
    24 public class Telemetry {
    25     private static final String LOGTAG = "Telemetry";
    27     public static long uptime() {
    28         return SystemClock.uptimeMillis();
    29     }
    31     public static long realtime() {
    32         return SystemClock.elapsedRealtime();
    33     }
    35     // Define new histograms in:
    36     // toolkit/components/telemetry/Histograms.json
    37     public static void HistogramAdd(String name, int value) {
    38         GeckoEvent event = GeckoEvent.createTelemetryHistogramAddEvent(name, value);
    39         GeckoAppShell.sendEventToGecko(event);
    40     }
    42     public abstract static class Timer {
    43         private final long mStartTime;
    44         private final String mName;
    46         private volatile boolean mHasFinished = false;
    47         private volatile long mElapsed = -1;
    49         protected abstract long now();
    51         public Timer(String name) {
    52             mName = name;
    53             mStartTime = now();
    54         }
    56         public void cancel() {
    57             mHasFinished = true;
    58         }
    60         public long getElapsed() {
    61           return mElapsed;
    62         }
    64         public void stop() {
    65             // Only the first stop counts.
    66             if (mHasFinished) {
    67                 return;
    68             }
    70             mHasFinished = true;
    72             final long elapsed = now() - mStartTime;
    73             if (elapsed < 0) {
    74                 Log.e(LOGTAG, "Current time less than start time -- clock shenanigans?");
    75                 return;
    76             }
    78             mElapsed = elapsed;
    79             if (elapsed > Integer.MAX_VALUE) {
    80                 Log.e(LOGTAG, "Duration of " + elapsed + "ms is too great to add to histogram.");
    81                 return;
    82             }
    84             HistogramAdd(mName, (int)(elapsed));
    85         }
    86     }
    88     public static class RealtimeTimer extends Timer {
    89         public RealtimeTimer(String name) {
    90             super(name);
    91         }
    93         @Override
    94         protected long now() {
    95             return Telemetry.realtime();
    96         }
    97     }
    99     public static class UptimeTimer extends Timer {
   100         public UptimeTimer(String name) {
   101             super(name);
   102         }
   104         @Override
   105         protected long now() {
   106             return Telemetry.uptime();
   107         }
   108     }
   110     public static void startUISession(String sessionName) {
   111         Log.d(LOGTAG, "StartUISession: " + sessionName);
   112         GeckoEvent event = GeckoEvent.createTelemetryUISessionStartEvent(sessionName, realtime());
   113         GeckoAppShell.sendEventToGecko(event);
   114     }
   116     public static void stopUISession(String sessionName, String reason) {
   117         Log.d(LOGTAG, "StopUISession: " + sessionName + ", reason=" + reason);
   118         GeckoEvent event = GeckoEvent.createTelemetryUISessionStopEvent(sessionName, reason, realtime());
   119         GeckoAppShell.sendEventToGecko(event);
   120     }
   122     public static void stopUISession(String sessionName) {
   123         stopUISession(sessionName, null);
   124     }
   126     public static void sendUIEvent(String action, String method, long timestamp, String extras) {
   127         Log.d(LOGTAG, "SendUIEvent: action = " + action + " method = " + method + " timestamp = " + timestamp + " extras = " + extras);
   128         GeckoEvent event = GeckoEvent.createTelemetryUIEvent(action, method, timestamp, extras);
   129         GeckoAppShell.sendEventToGecko(event);
   130     }
   132     public static void sendUIEvent(String action, String method, long timestamp) {
   133         sendUIEvent(action, method, timestamp, null);
   134     }
   136     public static void sendUIEvent(String action, String method, String extras) {
   137         sendUIEvent(action, method, realtime(), extras);
   138     }
   140     public static void sendUIEvent(String action, String method) {
   141         sendUIEvent(action, method, realtime(), null);
   142     }
   144     public static void sendUIEvent(String action) {
   145         sendUIEvent(action, null, realtime(), null);
   146     }
   147 }

mercurial