Commit 66456ff8 authored by yfriedman's avatar yfriedman Committed by Commit bot

Upstream MemoryUma and use it for all activities.

This achieves a couple of goals:
1) Upstreams MemoryUma
2) Pulls it up from ChromeTabbedActivity (internal) to
AsyncInitializationActivity. This ensures that other activities (e.g.
DocumentActivity) also report memory warnings.
3) Instantiate MemoryUma!! This was broken in
https://chrome-internal-review.googlesource.com/161577 so low-memory
warnings haven't been reported since M35! :(

BUG=428903

Review URL: https://codereview.chromium.org/961323003

Cr-Commit-Position: refs/heads/master@{#318696}
parent 81ab6111
......@@ -15,6 +15,8 @@ import android.view.View;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnPreDrawListener;
import org.chromium.chrome.browser.metrics.MemoryUma;
/**
* An activity that talks with application and activity level delegates for async initialization.
*/
......@@ -30,6 +32,7 @@ public abstract class AsyncInitializationActivity extends ActionBarActivity impl
private Bundle mSavedInstanceState;
private boolean mDestroyed;
private NativeInitializationController mNativeInitializationController;
private MemoryUma mMemoryUma;
public AsyncInitializationActivity() {
mHandler = new Handler();
......@@ -71,6 +74,7 @@ public abstract class AsyncInitializationActivity extends ActionBarActivity impl
@Override
public void finishNativeInitialization() {
mMemoryUma = new MemoryUma();
mNativeInitializationController.onNativeInitializationComplete();
}
......@@ -146,6 +150,7 @@ public abstract class AsyncInitializationActivity extends ActionBarActivity impl
@Override
public void onStop() {
super.onStop();
if (mMemoryUma != null) mMemoryUma.onStop();
mNativeInitializationController.onStop();
}
......@@ -196,6 +201,19 @@ public abstract class AsyncInitializationActivity extends ActionBarActivity impl
return false;
}
@Override
public void onLowMemory() {
super.onLowMemory();
if (mMemoryUma != null) mMemoryUma.onLowMemory();
}
@Override
public void onTrimMemory(int level) {
super.onTrimMemory(level);
if (mMemoryUma != null) mMemoryUma.onTrimMemory(level);
}
/**
* Extending classes should implement this and call {@link Activity#setContentView(int)} in it.
*/
......
// Copyright 2015 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.chrome.browser.metrics;
import static android.content.ComponentCallbacks2.TRIM_MEMORY_BACKGROUND;
import static android.content.ComponentCallbacks2.TRIM_MEMORY_COMPLETE;
import static android.content.ComponentCallbacks2.TRIM_MEMORY_MODERATE;
import static android.content.ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL;
import static android.content.ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW;
import static android.content.ComponentCallbacks2.TRIM_MEMORY_RUNNING_MODERATE;
import static android.content.ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN;
import android.os.Build;
import android.os.SystemClock;
import org.chromium.base.metrics.RecordHistogram;
import java.util.concurrent.TimeUnit;
/**
* Centralizes UMA data collection for Android-specific memory conditions.
*/
public class MemoryUma {
// AndroidMemoryNotificationBackground defined in tools/metrics/histograms/histograms.xml.
private static final int BACKGROUND_TRIM_UI_HIDDEN = 0;
private static final int BACKGROUND_TRIM_BACKGROUND = 1;
private static final int BACKGROUND_TRIM_MODERATE = 2;
private static final int BACKGROUND_TRIM_COMPLETE = 3;
private static final int BACKGROUND_MAX = 4;
// AndroidMemoryNotificationForeground defined in tools/metrics/histograms/histograms.xml.
private static final int FOREGROUND_TRIM_MODERATE = 0;
private static final int FOREGROUND_TRIM_LOW = 1;
private static final int FOREGROUND_TRIM_CRITICAL = 2;
private static final int FOREGROUND_LOW = 3;
private static final int FOREGROUND_MAX = 4;
// Timestamp of the last time we received a LowMemory call since Chrome is in foreground.
private long mLastLowMemoryMsec = -1;
public void onStop() {
mLastLowMemoryMsec = -1;
}
public void onLowMemory() {
memoryNotificationForeground(FOREGROUND_LOW);
long now = SystemClock.elapsedRealtime();
if (mLastLowMemoryMsec >= 0) {
RecordHistogram.recordCustomTimesHistogram("MemoryAndroid.LowMemoryTimeBetween",
now - mLastLowMemoryMsec, 0, TimeUnit.MINUTES.toMillis(10),
TimeUnit.MILLISECONDS, 50);
}
mLastLowMemoryMsec = now;
}
public void onTrimMemory(int level) {
if (level >= TRIM_MEMORY_COMPLETE) {
memoryNotificationBackground(BACKGROUND_TRIM_COMPLETE);
} else if (level >= TRIM_MEMORY_MODERATE) {
memoryNotificationBackground(BACKGROUND_TRIM_MODERATE);
} else if (level >= TRIM_MEMORY_BACKGROUND) {
memoryNotificationBackground(BACKGROUND_TRIM_BACKGROUND);
} else if (level >= TRIM_MEMORY_UI_HIDDEN) {
memoryNotificationBackground(BACKGROUND_TRIM_UI_HIDDEN);
} else if (level >= TRIM_MEMORY_RUNNING_CRITICAL) {
memoryNotificationForeground(FOREGROUND_TRIM_CRITICAL);
} else if (level >= TRIM_MEMORY_RUNNING_LOW) {
memoryNotificationForeground(FOREGROUND_TRIM_LOW);
} else if (level >= TRIM_MEMORY_RUNNING_MODERATE) {
memoryNotificationForeground(FOREGROUND_TRIM_MODERATE);
}
}
private static void memoryNotificationForeground(int notification) {
assert notification >= 0 && notification < FOREGROUND_MAX;
// Before Jelly Bean we have only LowMemory foreground notification.
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) return;
RecordHistogram.recordEnumeratedHistogram("MemoryAndroid.NotificationForeground",
notification, FOREGROUND_MAX);
}
private static void memoryNotificationBackground(int notification) {
assert notification >= 0 && notification < BACKGROUND_MAX;
RecordHistogram.recordEnumeratedHistogram("MemoryAndroid.NotificationBackground",
notification, BACKGROUND_MAX);
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment