1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/Telemetry.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,147 @@ 1.4 +/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- 1.5 + * This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.7 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +package org.mozilla.gecko; 1.10 + 1.11 +import org.mozilla.gecko.mozglue.RobocopTarget; 1.12 + 1.13 +import android.os.SystemClock; 1.14 +import android.util.Log; 1.15 + 1.16 +/** 1.17 + * All telemetry times are relative to one of two clocks: 1.18 + * 1.19 + * * Real time since the device was booted, including deep sleep. Use this 1.20 + * as a substitute for wall clock. 1.21 + * * Uptime since the device was booted, excluding deep sleep. Use this to 1.22 + * avoid timing a user activity when their phone is in their pocket! 1.23 + * 1.24 + * The majority of methods in this class are defined in terms of real time. 1.25 + */ 1.26 +@RobocopTarget 1.27 +public class Telemetry { 1.28 + private static final String LOGTAG = "Telemetry"; 1.29 + 1.30 + public static long uptime() { 1.31 + return SystemClock.uptimeMillis(); 1.32 + } 1.33 + 1.34 + public static long realtime() { 1.35 + return SystemClock.elapsedRealtime(); 1.36 + } 1.37 + 1.38 + // Define new histograms in: 1.39 + // toolkit/components/telemetry/Histograms.json 1.40 + public static void HistogramAdd(String name, int value) { 1.41 + GeckoEvent event = GeckoEvent.createTelemetryHistogramAddEvent(name, value); 1.42 + GeckoAppShell.sendEventToGecko(event); 1.43 + } 1.44 + 1.45 + public abstract static class Timer { 1.46 + private final long mStartTime; 1.47 + private final String mName; 1.48 + 1.49 + private volatile boolean mHasFinished = false; 1.50 + private volatile long mElapsed = -1; 1.51 + 1.52 + protected abstract long now(); 1.53 + 1.54 + public Timer(String name) { 1.55 + mName = name; 1.56 + mStartTime = now(); 1.57 + } 1.58 + 1.59 + public void cancel() { 1.60 + mHasFinished = true; 1.61 + } 1.62 + 1.63 + public long getElapsed() { 1.64 + return mElapsed; 1.65 + } 1.66 + 1.67 + public void stop() { 1.68 + // Only the first stop counts. 1.69 + if (mHasFinished) { 1.70 + return; 1.71 + } 1.72 + 1.73 + mHasFinished = true; 1.74 + 1.75 + final long elapsed = now() - mStartTime; 1.76 + if (elapsed < 0) { 1.77 + Log.e(LOGTAG, "Current time less than start time -- clock shenanigans?"); 1.78 + return; 1.79 + } 1.80 + 1.81 + mElapsed = elapsed; 1.82 + if (elapsed > Integer.MAX_VALUE) { 1.83 + Log.e(LOGTAG, "Duration of " + elapsed + "ms is too great to add to histogram."); 1.84 + return; 1.85 + } 1.86 + 1.87 + HistogramAdd(mName, (int)(elapsed)); 1.88 + } 1.89 + } 1.90 + 1.91 + public static class RealtimeTimer extends Timer { 1.92 + public RealtimeTimer(String name) { 1.93 + super(name); 1.94 + } 1.95 + 1.96 + @Override 1.97 + protected long now() { 1.98 + return Telemetry.realtime(); 1.99 + } 1.100 + } 1.101 + 1.102 + public static class UptimeTimer extends Timer { 1.103 + public UptimeTimer(String name) { 1.104 + super(name); 1.105 + } 1.106 + 1.107 + @Override 1.108 + protected long now() { 1.109 + return Telemetry.uptime(); 1.110 + } 1.111 + } 1.112 + 1.113 + public static void startUISession(String sessionName) { 1.114 + Log.d(LOGTAG, "StartUISession: " + sessionName); 1.115 + GeckoEvent event = GeckoEvent.createTelemetryUISessionStartEvent(sessionName, realtime()); 1.116 + GeckoAppShell.sendEventToGecko(event); 1.117 + } 1.118 + 1.119 + public static void stopUISession(String sessionName, String reason) { 1.120 + Log.d(LOGTAG, "StopUISession: " + sessionName + ", reason=" + reason); 1.121 + GeckoEvent event = GeckoEvent.createTelemetryUISessionStopEvent(sessionName, reason, realtime()); 1.122 + GeckoAppShell.sendEventToGecko(event); 1.123 + } 1.124 + 1.125 + public static void stopUISession(String sessionName) { 1.126 + stopUISession(sessionName, null); 1.127 + } 1.128 + 1.129 + public static void sendUIEvent(String action, String method, long timestamp, String extras) { 1.130 + Log.d(LOGTAG, "SendUIEvent: action = " + action + " method = " + method + " timestamp = " + timestamp + " extras = " + extras); 1.131 + GeckoEvent event = GeckoEvent.createTelemetryUIEvent(action, method, timestamp, extras); 1.132 + GeckoAppShell.sendEventToGecko(event); 1.133 + } 1.134 + 1.135 + public static void sendUIEvent(String action, String method, long timestamp) { 1.136 + sendUIEvent(action, method, timestamp, null); 1.137 + } 1.138 + 1.139 + public static void sendUIEvent(String action, String method, String extras) { 1.140 + sendUIEvent(action, method, realtime(), extras); 1.141 + } 1.142 + 1.143 + public static void sendUIEvent(String action, String method) { 1.144 + sendUIEvent(action, method, realtime(), null); 1.145 + } 1.146 + 1.147 + public static void sendUIEvent(String action) { 1.148 + sendUIEvent(action, null, realtime(), null); 1.149 + } 1.150 +}