1 package com.android.internal.os;
2 
3 import android.os.BatteryStats;
4 import android.util.Log;
5 import android.util.LongSparseArray;
6 
7 public class MemoryPowerCalculator extends PowerCalculator {
8 
9     public static final String TAG = "MemoryPowerCalculator";
10     private static final boolean DEBUG = BatteryStatsHelper.DEBUG;
11     private final double[] powerAverages;
12 
MemoryPowerCalculator(PowerProfile profile)13     public MemoryPowerCalculator(PowerProfile profile) {
14         int numBuckets = profile.getNumElements(PowerProfile.POWER_MEMORY);
15         powerAverages = new double[numBuckets];
16         for (int i = 0; i < numBuckets; i++) {
17             powerAverages[i] = profile.getAveragePower(PowerProfile.POWER_MEMORY, i);
18             if (powerAverages[i] == 0 && DEBUG) {
19                 Log.d(TAG, "Problem with PowerProfile. Received 0 value in MemoryPowerCalculator");
20             }
21         }
22     }
23 
24     @Override
calculateApp(BatterySipper app, BatteryStats.Uid u, long rawRealtimeUs, long rawUptimeUs, int statsType)25     public void calculateApp(BatterySipper app, BatteryStats.Uid u, long rawRealtimeUs,
26             long rawUptimeUs, int statsType) {}
27 
28     @Override
calculateRemaining(BatterySipper app, BatteryStats stats, long rawRealtimeUs, long rawUptimeUs, int statsType)29     public void calculateRemaining(BatterySipper app, BatteryStats stats, long rawRealtimeUs,
30             long rawUptimeUs, int statsType) {
31         double totalMah = 0;
32         long totalTimeMs = 0;
33         LongSparseArray<? extends BatteryStats.Timer> timers = stats.getKernelMemoryStats();
34         for (int i = 0; i < timers.size() && i < powerAverages.length; i++) {
35             double mAatRail = powerAverages[(int) timers.keyAt(i)];
36             long timeMs = timers.valueAt(i).getTotalTimeLocked(rawRealtimeUs, statsType);
37             double mAm = (mAatRail * timeMs) / (1000*60);
38             if(DEBUG) {
39                 Log.d(TAG, "Calculating mAh for bucket " + timers.keyAt(i) + " while unplugged");
40                 Log.d(TAG, "Converted power profile number from "
41                         + powerAverages[(int) timers.keyAt(i)] + " into " + mAatRail);
42                 Log.d(TAG, "Calculated mAm " + mAm);
43             }
44             totalMah += mAm/60;
45             totalTimeMs += timeMs;
46         }
47         app.usagePowerMah = totalMah;
48         app.usageTimeMs = totalTimeMs;
49         if (DEBUG) {
50             Log.d(TAG, String.format("Calculated total mAh for memory %f while unplugged %d ",
51                     totalMah, totalTimeMs));
52         }
53     }
54 }
55