1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.android.internal.os;
17
18import android.os.BatteryStats;
19import android.util.ArrayMap;
20import android.util.Log;
21
22public class WakelockPowerCalculator extends PowerCalculator {
23    private static final String TAG = "WakelockPowerCalculator";
24    private static final boolean DEBUG = BatteryStatsHelper.DEBUG;
25    private final double mPowerWakelock;
26    private long mTotalAppWakelockTimeMs = 0;
27
28    public WakelockPowerCalculator(PowerProfile profile) {
29        mPowerWakelock = profile.getAveragePower(PowerProfile.POWER_CPU_AWAKE);
30    }
31
32    @Override
33    public void calculateApp(BatterySipper app, BatteryStats.Uid u, long rawRealtimeUs,
34                             long rawUptimeUs, int statsType) {
35        long wakeLockTimeUs = 0;
36        final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelockStats =
37                u.getWakelockStats();
38        final int wakelockStatsCount = wakelockStats.size();
39        for (int i = 0; i < wakelockStatsCount; i++) {
40            final BatteryStats.Uid.Wakelock wakelock = wakelockStats.valueAt(i);
41
42            // Only care about partial wake locks since full wake locks
43            // are canceled when the user turns the screen off.
44            BatteryStats.Timer timer = wakelock.getWakeTime(BatteryStats.WAKE_TYPE_PARTIAL);
45            if (timer != null) {
46                wakeLockTimeUs += timer.getTotalTimeLocked(rawRealtimeUs, statsType);
47            }
48        }
49        app.wakeLockTimeMs = wakeLockTimeUs / 1000; // convert to millis
50        mTotalAppWakelockTimeMs += app.wakeLockTimeMs;
51
52        // Add cost of holding a wake lock.
53        app.wakeLockPowerMah = (app.wakeLockTimeMs * mPowerWakelock) / (1000*60*60);
54        if (DEBUG && app.wakeLockPowerMah != 0) {
55            Log.d(TAG, "UID " + u.getUid() + ": wake " + app.wakeLockTimeMs
56                    + " power=" + BatteryStatsHelper.makemAh(app.wakeLockPowerMah));
57        }
58    }
59
60    @Override
61    public void calculateRemaining(BatterySipper app, BatteryStats stats, long rawRealtimeUs,
62                                   long rawUptimeUs, int statsType) {
63        long wakeTimeMillis = stats.getBatteryUptime(rawUptimeUs) / 1000;
64        wakeTimeMillis -= mTotalAppWakelockTimeMs
65                + (stats.getScreenOnTime(rawRealtimeUs, statsType) / 1000);
66        if (wakeTimeMillis > 0) {
67            final double power = (wakeTimeMillis * mPowerWakelock) / (1000*60*60);
68            if (DEBUG) {
69                Log.d(TAG, "OS wakeLockTime " + wakeTimeMillis + " power "
70                        + BatteryStatsHelper.makemAh(power));
71            }
72            app.wakeLockTimeMs += wakeTimeMillis;
73            app.wakeLockPowerMah += power;
74        }
75    }
76
77    @Override
78    public void reset() {
79        mTotalAppWakelockTimeMs = 0;
80    }
81}
82