android通过广播打印RAM信息

通过广播打印ram相关log
参数说明:
广播:com.android.settings.action.RAM_INFO
int型参数index:0 - 3h, 1 - 6h, 2 - 12h, 3 - 24h
代表过去时间app使用ram情况(平均/最大占用)

Index: frameworks/base/services/core/java/com/android/server/am/BroadcastQueue.java
===================================================================
--- frameworks/base/services/core/java/com/android/server/am/BroadcastQueue.java	(版本 10810)
+++ frameworks/base/services/core/java/com/android/server/am/BroadcastQueue.java	(版本 10827)
@@ -121,6 +121,7 @@
         

+        sBackgroundActionWhiteListArraySet.add("com.android.settings.action.RAM_INFO");
     }

 
Index: packages/apps/Settings/src/com/android/settings/applications/RamInfo.java
===================================================================
--- packages/apps/Settings/src/com/android/settings/applications/RamInfo.java	(不存在的)
+++ packages/apps/Settings/src/com/android/settings/applications/RamInfo.java	(版本 10827)
@@ -0,0 +1,120 @@
+package com.android.settings.applications;
+
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.pm.PackageManager;
+import android.text.format.Formatter;
+import android.text.format.Formatter.BytesResult;
+import android.util.Log;
+
+import com.android.settings.R;
+import com.android.settings.Utils;
+import com.android.settings.applications.ProcStatsData.MemInfo;
+
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+
+public class RamInfo extends BroadcastReceiver {
+
+    private static String TAG ="RamInfo";
+
+    private PackageManager mPm;
+
+    @Override
+    public void onReceive(Context context, Intent intent) {
+        Log.d(TAG, "onReceive:" + intent.getAction());
+
+        int index = intent.getIntExtra("index", 3); // 0 - 3h, 1 - 6h, 2 - 12h, 3 - 24h
+
+        mPm = context.getPackageManager();
+
+        ProcStatsData statsManager = new ProcStatsData(context, false);
+        statsManager.setDuration(ProcessStatsBase.sDurations[index]);
+
+        MemInfo memInfo = statsManager.getMemInfo();
+        double usedRam = memInfo.realUsedRam;
+        double totalRam = memInfo.realTotalRam;
+        double freeRam = memInfo.realFreeRam;
+        BytesResult usedResult = Formatter.formatBytes(context.getResources(), (long) usedRam,
+                Formatter.FLAG_SHORTER);
+        String totalString = Formatter.formatShortFileSize(context, (long) totalRam);
+        String freeString = Formatter.formatShortFileSize(context, (long) freeRam);
+        CharSequence memString;
+        CharSequence[] memStatesStr = context.getResources().getTextArray(R.array.ram_states);
+        int memState = statsManager.getMemState();
+        if (memState >= 0 && memState < memStatesStr.length - 1) {
+            memString = memStatesStr[memState];
+        } else {
+            memString = memStatesStr[memStatesStr.length - 1];
+        }
+        String durationString = context.getResources().getString(ProcessStatsBase.sDurationLabels[index]);
+        int numApps = statsManager.getEntries().size();
+
+        Log.d(TAG, "********** RAM INFO (" + durationString + ") **********");
+        // Average
+        Log.d(TAG, context.getResources().getString(R.string.memory_avg_use) + ": " + usedResult.value + usedResult.units);
+        // Performance
+        Log.d(TAG, context.getResources().getString(R.string.memory_performance) + ": " + memString);
+        // Total memory
+        Log.d(TAG, context.getResources().getString(R.string.total_memory) + ": " + totalString);
+        // Average used
+        Log.d(TAG, context.getResources().getString(R.string.average_used) + ": " + (Utils.formatPercentage((long) usedRam, (long) totalRam)));
+        // Free
+        Log.d(TAG, context.getResources().getString(R.string.free_memory) + ": " + freeString);
+        // Memory used by apps
+        Log.d(TAG, context.getResources().getString(R.string.memory_usage_apps)+ ": " + context.getResources().getQuantityString(
+                R.plurals.memory_usage_apps_summary, numApps, numApps, durationString));
+
+        boolean mShowMax = false;
+
+        List pkgEntries = statsManager.getEntries();
+
+        // Update everything and get the absolute maximum of memory usage for scaling.
+        for (int i = 0, N = pkgEntries.size(); i < N; i++) {
+            ProcStatsPackageEntry pkg = pkgEntries.get(i);
+            pkg.updateMetrics();
+        }
+
+        Collections.sort(pkgEntries, mShowMax ? sMaxPackageEntryCompare : sPackageEntryCompare);
+
+        for (int i = 0; i < pkgEntries.size(); i++) {
+            ProcStatsPackageEntry pkg = pkgEntries.get(i);
+            pkg.retrieveUiData(context, mPm);
+            boolean statsForeground = pkg.mRunWeight > pkg.mBgWeight;
+            double avgAmount = (statsForeground ? pkg.mRunWeight : pkg.mBgWeight) * memInfo.weightToRam;
+            double maxAmount = (statsForeground ? pkg.mMaxRunMem : pkg.mMaxBgMem) * memInfo.totalScale * 1024;
+            Log.d(TAG, pkg.mUiLabel + "(" + pkg.mPackage + ")" + 
+                    " {" + context.getResources().getString(R.string.memory_avg_use) + ":" + Formatter.formatShortFileSize(context, (long) avgAmount) + " " +
+                    context.getResources().getString(R.string.memory_max_use) + ":" + Formatter.formatShortFileSize(context, (long) maxAmount) + "}");
+        }
+        Log.d(TAG, "********** RAM INFO END **********");
+    }
+
+    final static Comparator sPackageEntryCompare
+            = new Comparator() {
+        @Override
+        public int compare(ProcStatsPackageEntry lhs, ProcStatsPackageEntry rhs) {
+            double rhsWeight = Math.max(rhs.mRunWeight, rhs.mBgWeight);
+            double lhsWeight = Math.max(lhs.mRunWeight, lhs.mBgWeight);
+            if (lhsWeight == rhsWeight) {
+                return 0;
+            }
+            return lhsWeight < rhsWeight ? 1 : -1;
+        }
+    };
+
+    final static Comparator sMaxPackageEntryCompare
+            = new Comparator() {
+        @Override
+        public int compare(ProcStatsPackageEntry lhs, ProcStatsPackageEntry rhs) {
+            double rhsMax = Math.max(rhs.mMaxBgMem, rhs.mMaxRunMem);
+            double lhsMax = Math.max(lhs.mMaxBgMem, lhs.mMaxRunMem);
+            if (lhsMax == rhsMax) {
+                return 0;
+            }
+            return lhsMax < rhsMax ? 1 : -1;
+        }
+    };
+}
\ No newline at end of file
Index: packages/apps/Settings/AndroidManifest.xml
===================================================================
--- packages/apps/Settings/AndroidManifest.xml	(版本 10810)
+++ packages/apps/Settings/AndroidManifest.xml	(版本 10827)
@@ -3603,5 +3603,11 @@
             
          

+
+        
+                
+                
+            
+         
     
 

你可能感兴趣的:(android_system,android)