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