Bug 1437551 - [1.4] Add GeckoRuntime telemetry API. r=snorp,jchen
authorEugen Sawin <esawin@mozilla.com>
Tue, 24 Apr 2018 16:50:40 +0200
changeset 471470 a6aad40b3d02f3e71074b8fbcf7705af243cf7f4
parent 471469 0f0c781e4886f4d1ee667033b38f653405569759
child 471471 db1371bac45da2bf0ea5d635be5fa30fb3a5856e
push id1728
push userjlund@mozilla.com
push dateMon, 18 Jun 2018 21:12:27 +0000
treeherdermozilla-release@c296fde26f5f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssnorp, jchen
bugs1437551
milestone61.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1437551 - [1.4] Add GeckoRuntime telemetry API. r=snorp,jchen
mobile/android/geckoview/src/main/java/org/mozilla/geckoview/GeckoRuntime.java
mobile/android/geckoview/src/main/java/org/mozilla/geckoview/RuntimeTelemetry.java
--- a/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/GeckoRuntime.java
+++ b/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/GeckoRuntime.java
@@ -15,16 +15,17 @@ import android.util.Log;
 
 import org.mozilla.gecko.EventDispatcher;
 import org.mozilla.gecko.GeckoAppShell;
 import org.mozilla.gecko.GeckoThread;
 import org.mozilla.gecko.PrefsHelper;
 import org.mozilla.gecko.util.BundleEventListener;
 import org.mozilla.gecko.util.EventCallback;
 import org.mozilla.gecko.util.GeckoBundle;
+import org.mozilla.gecko.util.ThreadUtils;
 
 public final class GeckoRuntime implements Parcelable {
     private static final String LOGTAG = "GeckoRuntime";
     private static final boolean DEBUG = false;
 
     private static GeckoRuntime sDefaultRuntime;
 
     /**
@@ -46,16 +47,17 @@ public final class GeckoRuntime implemen
             sDefaultRuntime.init(new GeckoRuntimeSettings());
         }
 
         return sDefaultRuntime;
     }
 
     private GeckoRuntimeSettings mSettings;
     private Delegate mDelegate;
+    private RuntimeTelemetry mTelemetry;
 
     /**
      * Attach the runtime to the given context.
      *
      * @param context The new context to attach to.
      */
     public void attachTo(final @NonNull Context context) {
         if (DEBUG) {
@@ -190,16 +192,31 @@ public final class GeckoRuntime implemen
     public GeckoRuntimeSettings getSettings() {
         return mSettings;
     }
 
     /* package */ void setPref(final String name, final Object value) {
         PrefsHelper.setPref(name, value, /* flush */ false);
     }
 
+    /**
+     * Return the telemetry object for this runtime.
+     *
+     * @return The telemetry object.
+     */
+    public RuntimeTelemetry getTelemetry() {
+        ThreadUtils.assertOnUiThread();
+
+        if (mTelemetry == null) {
+            mTelemetry = new RuntimeTelemetry(this);
+        }
+        return mTelemetry;
+
+    }
+
     @Override // Parcelable
     public int describeContents() {
         return 0;
     }
 
     @Override // Parcelable
     public void writeToParcel(Parcel out, int flags) {
         out.writeParcelable(mSettings, flags);
new file mode 100644
--- /dev/null
+++ b/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/RuntimeTelemetry.java
@@ -0,0 +1,130 @@
+/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
+ * vim: ts=4 sw=4 expandtab:
+ * 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.geckoview;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+
+import org.json.JSONException;
+import org.json.JSONObject;
+
+import android.support.annotation.IntDef;
+import android.support.annotation.NonNull;
+import android.support.annotation.Nullable;
+import android.util.Log;
+
+import org.mozilla.gecko.EventDispatcher;
+import org.mozilla.gecko.util.GeckoBundle;
+import org.mozilla.gecko.util.EventCallback;
+
+/**
+ * The telemetry API gives access to telemetry data of the Gecko runtime.
+ */
+public final class RuntimeTelemetry {
+    private final static String LOGTAG = "GeckoViewTelemetry";
+    private final static boolean DEBUG = false;
+
+    private final GeckoRuntime mRuntime;
+    private final EventDispatcher mEventDispatcher;
+
+    @Retention(RetentionPolicy.SOURCE)
+    @IntDef({ DATASET_BASE, DATASET_EXTENDED })
+    public @interface DatasetType {}
+
+    // Match with nsITelemetry.
+    /**
+     * The base dataset suitable for release builds.
+     */
+    public static final int DATASET_BASE = 0;
+    /**
+     * The extended dataset suitable for pre-release builds.
+     */
+    public static final int DATASET_EXTENDED = 1;
+
+    @IntDef(flag = true,
+            value = { SNAPSHOT_HISTOGRAMS, SNAPSHOT_KEYED_HISTOGRAMS,
+                      SNAPSHOT_SCALARS, SNAPSHOT_KEYED_SCALARS,
+                      SNAPSHOT_ALL })
+    public @interface SnapshotType {}
+
+    // Match with GeckoViewTelemetryController.
+    /**
+     * Adds a "histogram" object entry to the snapshot response.
+     */
+    public static final int SNAPSHOT_HISTOGRAMS = 1 << 0;
+    /**
+     * Adds a "keyedHistogram" object entry to the snapshot response.
+     */
+    public static final int SNAPSHOT_KEYED_HISTOGRAMS = 1 << 1;
+    /**
+     * Adds a "scalars" object entry to the snapshot response.
+     */
+    public static final int SNAPSHOT_SCALARS = 1 << 2;
+    /**
+     * Adds a "keyedScalars" object entry to the snapshot response.
+     */
+    public static final int SNAPSHOT_KEYED_SCALARS = 1 << 3;
+    /**
+     * Adds all snapshot types to the response.
+     */
+    public static final int SNAPSHOT_ALL = (1 << 4) - 1;
+
+    /* package */ RuntimeTelemetry(final @NonNull GeckoRuntime runtime) {
+        mRuntime = runtime;
+        mEventDispatcher = EventDispatcher.getInstance();
+    }
+
+    /**
+     * Retrieve all telemetry snapshots.
+     *
+     * @param dataset The dataset type to retreive.
+     *                One of {@link #RuntimeTelemetry.DATASET_BASE DATASET_*} flags.
+     * @param clear Whether the retrieved snapshots should be cleared.
+     * @param response Used to return the async response.
+     */
+    public void getSnapshots(
+          final @DatasetType int dataset,
+          final boolean clear,
+          final @NonNull GeckoResponse<GeckoBundle> response) {
+        getSnapshots(SNAPSHOT_ALL, dataset, clear, response);
+    }
+
+    /**
+     * Retrieve the requested telemetry snapshots.
+     *
+     * @param types The requested snapshot types.
+     *              One or more of {@link #RuntimeTelemetry.SNAPSHOT_HISTOGRAMS SNAPSHOT_*} flags.
+     * @param dataset The dataset type to retreive.
+     *                One of {@link #RuntimeTelemetry.DATASET_BASE DATASET_*} flags.
+     * @param clear Whether the retrieved snapshots should be cleared.
+     * @param response Used to return the async response.
+     */
+    public void getSnapshots(
+          final @SnapshotType int types,
+          final @DatasetType int dataset,
+          final boolean clear,
+          final @NonNull GeckoResponse<GeckoBundle> response) {
+        final GeckoBundle msg = new GeckoBundle(3);
+        msg.putInt("types", types);
+        msg.putInt("dataset", dataset);
+        msg.putBoolean("clear", clear);
+
+        mEventDispatcher.dispatch("GeckoView:TelemetrySnapshots", msg,
+            new EventCallback() {
+                @Override
+                public void sendSuccess(final Object result) {
+                    response.respond((GeckoBundle) result);
+                }
+
+                @Override
+                public void sendError(final Object error) {
+                    Log.e(LOGTAG, "getSnapshots failed: " + error);
+                    response.respond(null);
+                }
+            });
+    }
+}