Skip to content

Commit

Permalink
Move Java CachedMetrics functionality to base/.
Browse files Browse the repository at this point in the history
This is general-purpose functionality that allows
recording UMA metrics before the C++ native library
has been loaded and is useful to have outside of the
Chrome layer. In particular, I will be using it from
components/variations to record information about the
first run variations seed fetch.

No functional changes in this CL.

BUG=632199

Review-Url: https://chromiumcodereview.appspot.com/2435813002
Cr-Commit-Position: refs/heads/master@{#426652}
  • Loading branch information
asvitkine-chromium authored and Commit bot committed Oct 20, 2016
1 parent d25555b commit b7e5cad
Show file tree
Hide file tree
Showing 10 changed files with 187 additions and 165 deletions.
1 change: 1 addition & 0 deletions base/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -2322,6 +2322,7 @@ if (is_android) {
"android/java/src/org/chromium/base/library_loader/ModernLinker.java",
"android/java/src/org/chromium/base/library_loader/NativeLibraryPreloader.java",
"android/java/src/org/chromium/base/library_loader/ProcessInitException.java",
"android/java/src/org/chromium/base/metrics/CachedMetrics.java",
"android/java/src/org/chromium/base/metrics/RecordHistogram.java",
"android/java/src/org/chromium/base/metrics/RecordUserAction.java",
"android/java/src/org/chromium/base/metrics/StatisticsRecorderAndroid.java",
Expand Down
169 changes: 169 additions & 0 deletions base/android/java/src/org/chromium/base/metrics/CachedMetrics.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.chromium.base.metrics;

import org.chromium.base.library_loader.LibraryLoader;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
* Utility classes for recording UMA metrics before the native library
* may have been loaded. Metrics are cached until the library is known
* to be loaded, then committed to the MetricsService all at once.
*/
public class CachedMetrics {
/**
* Creating an instance of a subclass of this class automatically adds it to a list of objects
* that are committed when the native library is available.
*/
private abstract static class CachedHistogram {
private static final List<CachedHistogram> sEvents = new ArrayList<CachedHistogram>();

protected final String mHistogramName;

/**
* @param histogramName Name of the histogram to record.
*/
protected CachedHistogram(String histogramName) {
mHistogramName = histogramName;
sEvents.add(this);
}

/** Commits the histogram. Expects the native library to be loaded. */
protected abstract void commitAndClear();
}

/**
* Caches an action that will be recorded after native side is loaded.
*/
public static class ActionEvent extends CachedHistogram {
private int mCount;

public ActionEvent(String actionName) {
super(actionName);
}

public void record() {
if (LibraryLoader.isInitialized()) {
recordWithNative();
} else {
mCount++;
}
}

private void recordWithNative() {
RecordUserAction.record(mHistogramName);
}

@Override
protected void commitAndClear() {
while (mCount > 0) {
recordWithNative();
mCount--;
}
}
}

/** Caches a set of integer histogram samples. */
public static class SparseHistogramSample extends CachedHistogram {
private final List<Integer> mSamples = new ArrayList<Integer>();

public SparseHistogramSample(String histogramName) {
super(histogramName);
}

public void record(int sample) {
if (LibraryLoader.isInitialized()) {
recordWithNative(sample);
} else {
mSamples.add(sample);
}
}

private void recordWithNative(int sample) {
RecordHistogram.recordSparseSlowlyHistogram(mHistogramName, sample);
}

@Override
protected void commitAndClear() {
for (Integer sample : mSamples) {
recordWithNative(sample);
}
mSamples.clear();
}
}

/** Caches a set of enumerated histogram samples. */
public static class EnumeratedHistogramSample extends CachedHistogram {
private final List<Integer> mSamples = new ArrayList<Integer>();
private final int mMaxValue;

public EnumeratedHistogramSample(String histogramName, int maxValue) {
super(histogramName);
mMaxValue = maxValue;
}

public void record(int sample) {
if (LibraryLoader.isInitialized()) {
recordWithNative(sample);
} else {
mSamples.add(sample);
}
}

private void recordWithNative(int sample) {
RecordHistogram.recordEnumeratedHistogram(mHistogramName, sample, mMaxValue);
}

@Override
protected void commitAndClear() {
for (Integer sample : mSamples) {
recordWithNative(sample);
}
mSamples.clear();
}
}

/** Caches a set of times histogram samples. */
public static class TimesHistogramSample extends CachedHistogram {
private final List<Long> mSamples = new ArrayList<Long>();
private final TimeUnit mTimeUnit;

public TimesHistogramSample(String histogramName, TimeUnit timeUnit) {
super(histogramName);
mTimeUnit = timeUnit;
}

public void record(long sample) {
if (LibraryLoader.isInitialized()) {
recordWithNative(sample);
} else {
mSamples.add(sample);
}
}

private void recordWithNative(long sample) {
RecordHistogram.recordTimesHistogram(mHistogramName, sample, mTimeUnit);
}

@Override
protected void commitAndClear() {
for (Long sample : mSamples) {
recordWithNative(sample);
}
mSamples.clear();
}
}

/**
* Calls out to native code to commit any cached histograms and events.
* Should be called once the native library has been loaded.
*/
public static void commitCachedMetrics() {
for (CachedHistogram event : CachedHistogram.sEvents) event.commitAndClear();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
import android.widget.RemoteViews;

import org.chromium.base.Log;
import org.chromium.base.metrics.CachedMetrics;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.ChromeActivity;
import org.chromium.chrome.browser.metrics.LaunchMetrics;
import org.chromium.chrome.browser.tab.Tab;
import org.chromium.ui.interpolators.BakedBezierInterpolator;

Expand All @@ -35,10 +35,10 @@
*/
class CustomTabBottomBarDelegate {
private static final String TAG = "CustomTab";
private static final LaunchMetrics.ActionEvent REMOTE_VIEWS_SHOWN =
new LaunchMetrics.ActionEvent("CustomTabsRemoteViewsShown");
private static final LaunchMetrics.ActionEvent REMOTE_VIEWS_UPDATED =
new LaunchMetrics.ActionEvent("CustomTabsRemoteViewsUpdated");
private static final CachedMetrics.ActionEvent REMOTE_VIEWS_SHOWN =
new CachedMetrics.ActionEvent("CustomTabsRemoteViewsShown");
private static final CachedMetrics.ActionEvent REMOTE_VIEWS_UPDATED =
new CachedMetrics.ActionEvent("CustomTabsRemoteViewsUpdated");
private static final int SLIDE_ANIMATION_DURATION_MS = 400;
private ChromeActivity mActivity;
private ViewGroup mBottomBarView;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.chromium.base.ContextUtils;
import org.chromium.base.Log;
import org.chromium.base.TraceEvent;
import org.chromium.base.metrics.CachedMetrics;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.ChromeApplication;
import org.chromium.chrome.browser.ChromeSwitches;
Expand All @@ -42,7 +43,6 @@
import org.chromium.chrome.browser.firstrun.FirstRunFlowSequencer;
import org.chromium.chrome.browser.firstrun.LightweightFirstRunActivity;
import org.chromium.chrome.browser.instantapps.InstantAppsHandler;
import org.chromium.chrome.browser.metrics.LaunchMetrics;
import org.chromium.chrome.browser.metrics.MediaNotificationUma;
import org.chromium.chrome.browser.multiwindow.MultiWindowUtils;
import org.chromium.chrome.browser.notifications.NotificationPlatformBridge;
Expand Down Expand Up @@ -87,8 +87,8 @@ public class ChromeLauncherActivity extends Activity
*/
private static final int PARTNER_BROWSER_CUSTOMIZATIONS_TIMEOUT_MS = 10000;

private static final LaunchMetrics.SparseHistogramSample sIntentFlagsHistogram =
new LaunchMetrics.SparseHistogramSample("Launch.IntentFlags");
private static final CachedMetrics.SparseHistogramSample sIntentFlagsHistogram =
new CachedMetrics.SparseHistogramSample("Launch.IntentFlags");

private IntentHandler mIntentHandler;
private boolean mIsInLegacyMultiInstanceMode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
import org.chromium.base.Log;
import org.chromium.base.ThreadUtils;
import org.chromium.base.VisibleForTesting;
import org.chromium.base.metrics.CachedMetrics.SparseHistogramSample;
import org.chromium.base.metrics.CachedMetrics.TimesHistogramSample;
import org.chromium.chrome.browser.ChromeApplication;
import org.chromium.chrome.browser.metrics.LaunchMetrics.SparseHistogramSample;
import org.chromium.chrome.browser.metrics.LaunchMetrics.TimesHistogramSample;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import com.google.android.gms.common.GoogleApiAvailability;

import org.chromium.base.ThreadUtils;
import org.chromium.chrome.browser.metrics.LaunchMetrics.EnumeratedHistogramSample;
import org.chromium.base.metrics.CachedMetrics.EnumeratedHistogramSample;

import java.util.concurrent.atomic.AtomicBoolean;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
import org.chromium.base.ContextUtils;
import org.chromium.base.FieldTrialList;
import org.chromium.base.Log;
import org.chromium.base.metrics.CachedMetrics.TimesHistogramSample;
import org.chromium.chrome.browser.ChromeApplication;
import org.chromium.chrome.browser.ChromeSwitches;
import org.chromium.chrome.browser.IntentHandler;
import org.chromium.chrome.browser.externalnav.ExternalNavigationDelegateImpl;
import org.chromium.chrome.browser.metrics.LaunchMetrics.TimesHistogramSample;
import org.chromium.chrome.browser.preferences.ChromePreferenceManager;
import org.chromium.chrome.browser.util.IntentUtils;
import org.chromium.content_public.browser.WebContents;
Expand Down
Loading

0 comments on commit b7e5cad

Please sign in to comment.