|
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/. */ |
|
5 |
|
6 package org.mozilla.gecko; |
|
7 |
|
8 import org.mozilla.gecko.mozglue.RobocopTarget; |
|
9 |
|
10 import android.os.SystemClock; |
|
11 import android.util.Log; |
|
12 |
|
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"; |
|
26 |
|
27 public static long uptime() { |
|
28 return SystemClock.uptimeMillis(); |
|
29 } |
|
30 |
|
31 public static long realtime() { |
|
32 return SystemClock.elapsedRealtime(); |
|
33 } |
|
34 |
|
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 } |
|
41 |
|
42 public abstract static class Timer { |
|
43 private final long mStartTime; |
|
44 private final String mName; |
|
45 |
|
46 private volatile boolean mHasFinished = false; |
|
47 private volatile long mElapsed = -1; |
|
48 |
|
49 protected abstract long now(); |
|
50 |
|
51 public Timer(String name) { |
|
52 mName = name; |
|
53 mStartTime = now(); |
|
54 } |
|
55 |
|
56 public void cancel() { |
|
57 mHasFinished = true; |
|
58 } |
|
59 |
|
60 public long getElapsed() { |
|
61 return mElapsed; |
|
62 } |
|
63 |
|
64 public void stop() { |
|
65 // Only the first stop counts. |
|
66 if (mHasFinished) { |
|
67 return; |
|
68 } |
|
69 |
|
70 mHasFinished = true; |
|
71 |
|
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 } |
|
77 |
|
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 } |
|
83 |
|
84 HistogramAdd(mName, (int)(elapsed)); |
|
85 } |
|
86 } |
|
87 |
|
88 public static class RealtimeTimer extends Timer { |
|
89 public RealtimeTimer(String name) { |
|
90 super(name); |
|
91 } |
|
92 |
|
93 @Override |
|
94 protected long now() { |
|
95 return Telemetry.realtime(); |
|
96 } |
|
97 } |
|
98 |
|
99 public static class UptimeTimer extends Timer { |
|
100 public UptimeTimer(String name) { |
|
101 super(name); |
|
102 } |
|
103 |
|
104 @Override |
|
105 protected long now() { |
|
106 return Telemetry.uptime(); |
|
107 } |
|
108 } |
|
109 |
|
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 } |
|
115 |
|
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 } |
|
121 |
|
122 public static void stopUISession(String sessionName) { |
|
123 stopUISession(sessionName, null); |
|
124 } |
|
125 |
|
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 } |
|
131 |
|
132 public static void sendUIEvent(String action, String method, long timestamp) { |
|
133 sendUIEvent(action, method, timestamp, null); |
|
134 } |
|
135 |
|
136 public static void sendUIEvent(String action, String method, String extras) { |
|
137 sendUIEvent(action, method, realtime(), extras); |
|
138 } |
|
139 |
|
140 public static void sendUIEvent(String action, String method) { |
|
141 sendUIEvent(action, method, realtime(), null); |
|
142 } |
|
143 |
|
144 public static void sendUIEvent(String action) { |
|
145 sendUIEvent(action, null, realtime(), null); |
|
146 } |
|
147 } |