blob: b4470397c67bc2580b51f4ca5965145df1ac70b2 [file] [log] [blame]
Adam Lesinskie08af192015-03-25 16:42:59 -07001/*
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;
Adam Lesinski17390762015-04-10 13:17:47 -070019import android.util.Log;
Adam Lesinskie08af192015-03-25 16:42:59 -070020
21/**
22 * WiFi power calculator for when BatteryStats supports energy reporting
23 * from the WiFi controller.
24 */
25public class WifiPowerCalculator extends PowerCalculator {
Adam Lesinski17390762015-04-10 13:17:47 -070026 private static final boolean DEBUG = BatteryStatsHelper.DEBUG;
27 private static final String TAG = "WifiPowerCalculator";
Adam Lesinskie08af192015-03-25 16:42:59 -070028 private final double mIdleCurrentMa;
29 private final double mTxCurrentMa;
30 private final double mRxCurrentMa;
31 private double mTotalAppPowerDrain = 0;
Adam Lesinski9f55cc72016-01-27 20:42:14 -080032 private long mTotalAppRunningTime = 0;
Adam Lesinskie08af192015-03-25 16:42:59 -070033
34 public WifiPowerCalculator(PowerProfile profile) {
35 mIdleCurrentMa = profile.getAveragePower(PowerProfile.POWER_WIFI_CONTROLLER_IDLE);
36 mTxCurrentMa = profile.getAveragePower(PowerProfile.POWER_WIFI_CONTROLLER_TX);
37 mRxCurrentMa = profile.getAveragePower(PowerProfile.POWER_WIFI_CONTROLLER_RX);
38 }
39
40 @Override
41 public void calculateApp(BatterySipper app, BatteryStats.Uid u, long rawRealtimeUs,
42 long rawUptimeUs, int statsType) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -080043 final BatteryStats.ControllerActivityCounter counter = u.getWifiControllerActivity();
44 if (counter == null) {
45 return;
46 }
47
48 final long idleTime = counter.getIdleTimeCounter().getCountLocked(statsType);
49 final long txTime = counter.getTxTimeCounters()[0].getCountLocked(statsType);
50 final long rxTime = counter.getRxTimeCounter().getCountLocked(statsType);
Adam Lesinskie08af192015-03-25 16:42:59 -070051 app.wifiRunningTimeMs = idleTime + rxTime + txTime;
Adam Lesinski9f55cc72016-01-27 20:42:14 -080052 mTotalAppRunningTime += app.wifiRunningTimeMs;
53
Adam Lesinskie08af192015-03-25 16:42:59 -070054 app.wifiPowerMah =
55 ((idleTime * mIdleCurrentMa) + (txTime * mTxCurrentMa) + (rxTime * mRxCurrentMa))
56 / (1000*60*60);
57 mTotalAppPowerDrain += app.wifiPowerMah;
58
59 app.wifiRxPackets = u.getNetworkActivityPackets(BatteryStats.NETWORK_WIFI_RX_DATA,
60 statsType);
61 app.wifiTxPackets = u.getNetworkActivityPackets(BatteryStats.NETWORK_WIFI_TX_DATA,
62 statsType);
63 app.wifiRxBytes = u.getNetworkActivityBytes(BatteryStats.NETWORK_WIFI_RX_DATA,
64 statsType);
65 app.wifiTxBytes = u.getNetworkActivityBytes(BatteryStats.NETWORK_WIFI_TX_DATA,
66 statsType);
Mitchell Wills083e1d92015-07-30 10:46:05 -070067
68 if (DEBUG && app.wifiPowerMah != 0) {
69 Log.d(TAG, "UID " + u.getUid() + ": idle=" + idleTime + "ms rx=" + rxTime + "ms tx=" +
70 txTime + "ms power=" + BatteryStatsHelper.makemAh(app.wifiPowerMah));
71 }
Adam Lesinskie08af192015-03-25 16:42:59 -070072 }
73
74 @Override
75 public void calculateRemaining(BatterySipper app, BatteryStats stats, long rawRealtimeUs,
76 long rawUptimeUs, int statsType) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -080077 final BatteryStats.ControllerActivityCounter counter = stats.getWifiControllerActivity();
78
79 final long idleTimeMs = counter.getIdleTimeCounter().getCountLocked(statsType);
80 final long txTimeMs = counter.getTxTimeCounters()[0].getCountLocked(statsType);
81 final long rxTimeMs = counter.getRxTimeCounter().getCountLocked(statsType);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080082
83 app.wifiRunningTimeMs = Math.max(0,
84 (idleTimeMs + rxTimeMs + txTimeMs) - mTotalAppRunningTime);
Adam Lesinskie08af192015-03-25 16:42:59 -070085
Adam Lesinski21f76aa2016-01-25 12:27:06 -080086 double powerDrainMah = counter.getPowerCounter().getCountLocked(statsType)
87 / (double)(1000*60*60);
Adam Lesinski8576cf92015-06-09 12:48:25 -070088 if (powerDrainMah == 0) {
Adam Lesinskie08af192015-03-25 16:42:59 -070089 // Some controllers do not report power drain, so we can calculate it here.
Adam Lesinski8576cf92015-06-09 12:48:25 -070090 powerDrainMah = ((idleTimeMs * mIdleCurrentMa) + (txTimeMs * mTxCurrentMa)
Adam Lesinskie08af192015-03-25 16:42:59 -070091 + (rxTimeMs * mRxCurrentMa)) / (1000*60*60);
92 }
Adam Lesinski8576cf92015-06-09 12:48:25 -070093 app.wifiPowerMah = Math.max(0, powerDrainMah - mTotalAppPowerDrain);
Adam Lesinski17390762015-04-10 13:17:47 -070094
95 if (DEBUG) {
96 Log.d(TAG, "left over WiFi power: " + BatteryStatsHelper.makemAh(app.wifiPowerMah));
97 }
Adam Lesinskie08af192015-03-25 16:42:59 -070098 }
99
100 @Override
101 public void reset() {
102 mTotalAppPowerDrain = 0;
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800103 mTotalAppRunningTime = 0;
Adam Lesinskie08af192015-03-25 16:42:59 -0700104 }
105}