| 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | package org.chromium.content.browser; |
| 6 | |
| 7 | import android.content.BroadcastReceiver; |
| 8 | import android.content.Context; |
| 9 | import android.content.Intent; |
| 10 | import android.content.IntentFilter; |
| 11 | import android.os.Debug; |
| 12 | import android.util.Log; |
| 13 | |
| 14 | import org.chromium.content.common.CommandLine; |
| 15 | |
| 16 | // Logs Heap stats, such as gc count, alloc count, etc. |
| 17 | // It's enabled by CommandLine.ENABLE_TEST_INTENTS, and logs whenever broadcast |
| 18 | // intent ACTION_LOG is received, e.g.: |
| 19 | // adb shell am broadcast -a com.google.android.apps.chrome.LOG_HEAP_STATS |
| 20 | public class HeapStatsLogger { |
| 21 | private static final String TAG = "HeapStatsLogger"; |
| 22 | private static final String ACTION_LOG = "com.google.android.apps.chrome.LOG_HEAP_STATS"; |
| 23 | |
| 24 | private static HeapStatsLogger sHeapStats; |
| 25 | |
| 26 | private HeapStatsLoggerReceiver mBroadcastReceiver; |
| 27 | private HeapStatsLoggerIntentFilter mIntentFilter; |
| 28 | |
| 29 | public static void init(Context context) { |
| 30 | if (CommandLine.getInstance().hasSwitch(CommandLine.ENABLE_TEST_INTENTS)) { |
| 31 | sHeapStats = new HeapStatsLogger(context); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | private HeapStatsLogger(Context context) { |
| 36 | Debug.startAllocCounting(); |
| 37 | mBroadcastReceiver = new HeapStatsLoggerReceiver(); |
| 38 | mIntentFilter = new HeapStatsLoggerIntentFilter(); |
| 39 | context.registerReceiver(mBroadcastReceiver, mIntentFilter); |
| 40 | } |
| 41 | |
| 42 | private static class HeapStatsLoggerIntentFilter extends IntentFilter { |
| 43 | HeapStatsLoggerIntentFilter() { |
| 44 | addAction(ACTION_LOG); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | private class HeapStatsLoggerReceiver extends BroadcastReceiver { |
| 49 | @Override |
| 50 | public void onReceive(Context context, Intent intent) { |
| 51 | if (ACTION_LOG.equals(intent.getAction())) { |
| 52 | log(); |
| 53 | } else { |
| 54 | Log.e(TAG, "Unexpected intent: " + intent); |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | private void log() { |
| 60 | Log.i(TAG, "heap_stats " + |
| 61 | // Format is "key=value unit", and it'll be parsed by the test |
| 62 | // runner in order to be added to the bot graphs. |
| 63 | "gc_count=" + Debug.getGlobalGcInvocationCount() + " times " + |
| 64 | "alloc_count=" + Debug.getGlobalAllocCount() + " times " + |
| 65 | "alloc_size=" + Debug.getGlobalAllocSize() + " bytes " + |
| 66 | "freed_count=" + Debug.getGlobalFreedCount() + " times " + |
| 67 | "freed_size=" + Debug.getGlobalFreedSize() + " bytes" |
| 68 | ); |
| 69 | } |
| 70 | } |