2012-04-12 14:19:57 -07:00
|
|
|
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
package org.mozilla.gecko;
|
|
|
|
|
2013-12-10 10:41:34 -08:00
|
|
|
import org.mozilla.gecko.mozglue.RobocopTarget;
|
|
|
|
|
2012-07-27 17:53:54 -07:00
|
|
|
import android.os.SystemClock;
|
|
|
|
import android.util.Log;
|
|
|
|
|
2013-12-10 10:41:34 -08:00
|
|
|
/**
|
|
|
|
* All telemetry times are relative to one of two clocks:
|
|
|
|
*
|
|
|
|
* * Real time since the device was booted, including deep sleep. Use this
|
|
|
|
* as a substitute for wall clock.
|
|
|
|
* * Uptime since the device was booted, excluding deep sleep. Use this to
|
|
|
|
* avoid timing a user activity when their phone is in their pocket!
|
|
|
|
*
|
|
|
|
* The majority of methods in this class are defined in terms of real time.
|
|
|
|
*/
|
|
|
|
@RobocopTarget
|
2012-04-12 14:19:57 -07:00
|
|
|
public class Telemetry {
|
|
|
|
private static final String LOGTAG = "Telemetry";
|
|
|
|
|
2013-12-10 10:41:34 -08:00
|
|
|
public static long uptime() {
|
|
|
|
return SystemClock.uptimeMillis();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static long realtime() {
|
|
|
|
return SystemClock.elapsedRealtime();
|
|
|
|
}
|
|
|
|
|
2012-04-12 14:19:57 -07:00
|
|
|
// Define new histograms in:
|
2012-10-01 05:43:38 -07:00
|
|
|
// toolkit/components/telemetry/Histograms.json
|
2013-12-10 10:41:34 -08:00
|
|
|
public static void HistogramAdd(String name, int value) {
|
|
|
|
GeckoEvent event = GeckoEvent.createTelemetryHistogramAddEvent(name, value);
|
2013-08-15 12:03:47 -07:00
|
|
|
GeckoAppShell.sendEventToGecko(event);
|
2012-04-12 14:19:57 -07:00
|
|
|
}
|
2012-04-12 14:19:57 -07:00
|
|
|
|
2013-12-10 10:41:34 -08:00
|
|
|
public abstract static class Timer {
|
|
|
|
private final long mStartTime;
|
|
|
|
private final String mName;
|
|
|
|
|
|
|
|
private volatile boolean mHasFinished = false;
|
2013-06-04 17:16:57 -07:00
|
|
|
private volatile long mElapsed = -1;
|
2012-04-12 14:19:57 -07:00
|
|
|
|
2013-12-10 10:41:34 -08:00
|
|
|
protected abstract long now();
|
|
|
|
|
2012-04-12 14:19:57 -07:00
|
|
|
public Timer(String name) {
|
|
|
|
mName = name;
|
2013-12-10 10:41:34 -08:00
|
|
|
mStartTime = now();
|
2012-04-12 14:19:57 -07:00
|
|
|
}
|
|
|
|
|
2012-11-26 14:57:11 -08:00
|
|
|
public void cancel() {
|
|
|
|
mHasFinished = true;
|
|
|
|
}
|
|
|
|
|
2013-06-04 17:16:57 -07:00
|
|
|
public long getElapsed() {
|
|
|
|
return mElapsed;
|
|
|
|
}
|
|
|
|
|
2012-04-12 14:19:57 -07:00
|
|
|
public void stop() {
|
2012-10-01 13:57:00 -07:00
|
|
|
// Only the first stop counts.
|
|
|
|
if (mHasFinished) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-10 10:41:34 -08:00
|
|
|
mHasFinished = true;
|
|
|
|
|
|
|
|
final long elapsed = now() - mStartTime;
|
|
|
|
if (elapsed < 0) {
|
|
|
|
Log.e(LOGTAG, "Current time less than start time -- clock shenanigans?");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-04 17:16:57 -07:00
|
|
|
mElapsed = elapsed;
|
2013-12-10 10:41:34 -08:00
|
|
|
if (elapsed > Integer.MAX_VALUE) {
|
|
|
|
Log.e(LOGTAG, "Duration of " + elapsed + "ms is too great to add to histogram.");
|
|
|
|
return;
|
2012-04-12 14:19:57 -07:00
|
|
|
}
|
2013-12-10 10:41:34 -08:00
|
|
|
|
|
|
|
HistogramAdd(mName, (int)(elapsed));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static class RealtimeTimer extends Timer {
|
|
|
|
public RealtimeTimer(String name) {
|
|
|
|
super(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected long now() {
|
|
|
|
return Telemetry.realtime();
|
2012-04-12 14:19:57 -07:00
|
|
|
}
|
|
|
|
}
|
2013-12-10 10:41:34 -08:00
|
|
|
|
|
|
|
public static class UptimeTimer extends Timer {
|
|
|
|
public UptimeTimer(String name) {
|
|
|
|
super(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected long now() {
|
|
|
|
return Telemetry.uptime();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void startUISession(String sessionName) {
|
|
|
|
GeckoEvent event = GeckoEvent.createTelemetryUISessionStartEvent(sessionName, realtime());
|
|
|
|
GeckoAppShell.sendEventToGecko(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void stopUISession(String sessionName, String reason) {
|
|
|
|
GeckoEvent event = GeckoEvent.createTelemetryUISessionStopEvent(sessionName, reason, realtime());
|
|
|
|
GeckoAppShell.sendEventToGecko(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void sendUIEvent(String action, String method, long timestamp, String extras) {
|
|
|
|
GeckoEvent event = GeckoEvent.createTelemetryUIEvent(action, method, timestamp, extras);
|
|
|
|
GeckoAppShell.sendEventToGecko(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void sendUIEvent(String action, String method, long timestamp) {
|
|
|
|
sendUIEvent(action, method, timestamp, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void sendUIEvent(String action, String method, String extras) {
|
|
|
|
sendUIEvent(action, method, realtime(), extras);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void sendUIEvent(String action, String method) {
|
|
|
|
sendUIEvent(action, method, realtime(), null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void sendUIEvent(String action) {
|
|
|
|
sendUIEvent(action, null, realtime(), null);
|
|
|
|
}
|
2012-04-12 14:19:57 -07:00
|
|
|
}
|