Adam Lesinski | e08af19 | 2015-03-25 16:42:59 -0700 | [diff] [blame] | 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 | */ |
| 16 | package com.android.internal.os; |
| 17 | |
| 18 | import android.os.BatteryStats; |
Adam Lesinski | 1739076 | 2015-04-10 13:17:47 -0700 | [diff] [blame] | 19 | import android.util.Log; |
Adam Lesinski | e08af19 | 2015-03-25 16:42:59 -0700 | [diff] [blame] | 20 | |
| 21 | /** |
| 22 | * WiFi power calculator for when BatteryStats supports energy reporting |
| 23 | * from the WiFi controller. |
| 24 | */ |
| 25 | public class WifiPowerCalculator extends PowerCalculator { |
Adam Lesinski | 1739076 | 2015-04-10 13:17:47 -0700 | [diff] [blame] | 26 | private static final boolean DEBUG = BatteryStatsHelper.DEBUG; |
| 27 | private static final String TAG = "WifiPowerCalculator"; |
Adam Lesinski | e08af19 | 2015-03-25 16:42:59 -0700 | [diff] [blame] | 28 | private final double mIdleCurrentMa; |
| 29 | private final double mTxCurrentMa; |
| 30 | private final double mRxCurrentMa; |
| 31 | private double mTotalAppPowerDrain = 0; |
| 32 | |
| 33 | public WifiPowerCalculator(PowerProfile profile) { |
| 34 | mIdleCurrentMa = profile.getAveragePower(PowerProfile.POWER_WIFI_CONTROLLER_IDLE); |
| 35 | mTxCurrentMa = profile.getAveragePower(PowerProfile.POWER_WIFI_CONTROLLER_TX); |
| 36 | mRxCurrentMa = profile.getAveragePower(PowerProfile.POWER_WIFI_CONTROLLER_RX); |
| 37 | } |
| 38 | |
| 39 | @Override |
| 40 | public void calculateApp(BatterySipper app, BatteryStats.Uid u, long rawRealtimeUs, |
| 41 | long rawUptimeUs, int statsType) { |
| 42 | final long idleTime = u.getWifiControllerActivity(BatteryStats.CONTROLLER_IDLE_TIME, |
| 43 | statsType); |
| 44 | final long txTime = u.getWifiControllerActivity(BatteryStats.CONTROLLER_TX_TIME, statsType); |
| 45 | final long rxTime = u.getWifiControllerActivity(BatteryStats.CONTROLLER_RX_TIME, statsType); |
| 46 | app.wifiRunningTimeMs = idleTime + rxTime + txTime; |
| 47 | app.wifiPowerMah = |
| 48 | ((idleTime * mIdleCurrentMa) + (txTime * mTxCurrentMa) + (rxTime * mRxCurrentMa)) |
| 49 | / (1000*60*60); |
| 50 | mTotalAppPowerDrain += app.wifiPowerMah; |
| 51 | |
| 52 | app.wifiRxPackets = u.getNetworkActivityPackets(BatteryStats.NETWORK_WIFI_RX_DATA, |
| 53 | statsType); |
| 54 | app.wifiTxPackets = u.getNetworkActivityPackets(BatteryStats.NETWORK_WIFI_TX_DATA, |
| 55 | statsType); |
| 56 | app.wifiRxBytes = u.getNetworkActivityBytes(BatteryStats.NETWORK_WIFI_RX_DATA, |
| 57 | statsType); |
| 58 | app.wifiTxBytes = u.getNetworkActivityBytes(BatteryStats.NETWORK_WIFI_TX_DATA, |
| 59 | statsType); |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public void calculateRemaining(BatterySipper app, BatteryStats stats, long rawRealtimeUs, |
| 64 | long rawUptimeUs, int statsType) { |
| 65 | final long idleTimeMs = stats.getWifiControllerActivity(BatteryStats.CONTROLLER_IDLE_TIME, |
| 66 | statsType); |
| 67 | final long rxTimeMs = stats.getWifiControllerActivity(BatteryStats.CONTROLLER_RX_TIME, |
| 68 | statsType); |
| 69 | final long txTimeMs = stats.getWifiControllerActivity(BatteryStats.CONTROLLER_TX_TIME, |
| 70 | statsType); |
| 71 | app.wifiRunningTimeMs = idleTimeMs + rxTimeMs + txTimeMs; |
| 72 | |
| 73 | double powerDrain = stats.getWifiControllerActivity(BatteryStats.CONTROLLER_POWER_DRAIN, |
| 74 | statsType) / (1000*60*60); |
| 75 | if (powerDrain == 0) { |
| 76 | // Some controllers do not report power drain, so we can calculate it here. |
| 77 | powerDrain = ((idleTimeMs * mIdleCurrentMa) + (txTimeMs * mTxCurrentMa) |
| 78 | + (rxTimeMs * mRxCurrentMa)) / (1000*60*60); |
| 79 | } |
| 80 | app.wifiPowerMah = Math.max(0, powerDrain - mTotalAppPowerDrain); |
Adam Lesinski | 1739076 | 2015-04-10 13:17:47 -0700 | [diff] [blame] | 81 | |
| 82 | if (DEBUG) { |
| 83 | Log.d(TAG, "left over WiFi power: " + BatteryStatsHelper.makemAh(app.wifiPowerMah)); |
| 84 | } |
Adam Lesinski | e08af19 | 2015-03-25 16:42:59 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public void reset() { |
| 89 | mTotalAppPowerDrain = 0; |
| 90 | } |
| 91 | } |