Home | History | Annotate | Download | only in os
      1 /*
      2  * Copyright (C) 2009 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 
     17 package com.android.internal.os;
     18 
     19 
     20 import android.content.Context;
     21 import android.content.res.Resources;
     22 import android.content.res.XmlResourceParser;
     23 
     24 import com.android.internal.util.XmlUtils;
     25 
     26 import org.xmlpull.v1.XmlPullParser;
     27 import org.xmlpull.v1.XmlPullParserException;
     28 
     29 import java.io.IOException;
     30 import java.util.ArrayList;
     31 import java.util.HashMap;
     32 
     33 /**
     34  * Reports power consumption values for various device activities. Reads values from an XML file.
     35  * Customize the XML file for different devices.
     36  * [hidden]
     37  */
     38 public class PowerProfile {
     39 
     40     /**
     41      * No power consumption, or accounted for elsewhere.
     42      */
     43     public static final String POWER_NONE = "none";
     44 
     45     /**
     46      * Power consumption when CPU is in power collapse mode.
     47      */
     48     public static final String POWER_CPU_IDLE = "cpu.idle";
     49 
     50     /**
     51      * Power consumption when CPU is awake (when a wake lock is held).  This
     52      * should be 0 on devices that can go into full CPU power collapse even
     53      * when a wake lock is held.  Otherwise, this is the power consumption in
     54      * addition to POWER_CPU_IDLE due to a wake lock being held but with no
     55      * CPU activity.
     56      */
     57     public static final String POWER_CPU_AWAKE = "cpu.awake";
     58 
     59     /**
     60      * Power consumption when CPU is in power collapse mode.
     61      */
     62     @Deprecated
     63     public static final String POWER_CPU_ACTIVE = "cpu.active";
     64 
     65     /**
     66      * Power consumption when WiFi driver is scanning for networks.
     67      */
     68     public static final String POWER_WIFI_SCAN = "wifi.scan";
     69 
     70     /**
     71      * Power consumption when WiFi driver is on.
     72      */
     73     public static final String POWER_WIFI_ON = "wifi.on";
     74 
     75     /**
     76      * Power consumption when WiFi driver is transmitting/receiving.
     77      */
     78     public static final String POWER_WIFI_ACTIVE = "wifi.active";
     79 
     80     //
     81     // Updated power constants. These are not estimated, they are real world
     82     // currents and voltages for the underlying bluetooth and wifi controllers.
     83     //
     84 
     85     public static final String POWER_WIFI_CONTROLLER_IDLE = "wifi.controller.idle";
     86     public static final String POWER_WIFI_CONTROLLER_RX = "wifi.controller.rx";
     87     public static final String POWER_WIFI_CONTROLLER_TX = "wifi.controller.tx";
     88     public static final String POWER_WIFI_CONTROLLER_TX_LEVELS = "wifi.controller.tx_levels";
     89     public static final String POWER_WIFI_CONTROLLER_OPERATING_VOLTAGE = "wifi.controller.voltage";
     90 
     91     public static final String POWER_BLUETOOTH_CONTROLLER_IDLE = "bluetooth.controller.idle";
     92     public static final String POWER_BLUETOOTH_CONTROLLER_RX = "bluetooth.controller.rx";
     93     public static final String POWER_BLUETOOTH_CONTROLLER_TX = "bluetooth.controller.tx";
     94     public static final String POWER_BLUETOOTH_CONTROLLER_OPERATING_VOLTAGE =
     95             "bluetooth.controller.voltage";
     96 
     97     public static final String POWER_MODEM_CONTROLLER_IDLE = "modem.controller.idle";
     98     public static final String POWER_MODEM_CONTROLLER_RX = "modem.controller.rx";
     99     public static final String POWER_MODEM_CONTROLLER_TX = "modem.controller.tx";
    100     public static final String POWER_MODEM_CONTROLLER_OPERATING_VOLTAGE =
    101             "modem.controller.voltage";
    102 
    103     /**
    104      * Power consumption when GPS is on.
    105      */
    106     public static final String POWER_GPS_ON = "gps.on";
    107 
    108     /**
    109      * Power consumption when Bluetooth driver is on.
    110      * @deprecated
    111      */
    112     @Deprecated
    113     public static final String POWER_BLUETOOTH_ON = "bluetooth.on";
    114 
    115     /**
    116      * Power consumption when Bluetooth driver is transmitting/receiving.
    117      * @deprecated
    118      */
    119     @Deprecated
    120     public static final String POWER_BLUETOOTH_ACTIVE = "bluetooth.active";
    121 
    122     /**
    123      * Power consumption when Bluetooth driver gets an AT command.
    124      * @deprecated
    125      */
    126     @Deprecated
    127     public static final String POWER_BLUETOOTH_AT_CMD = "bluetooth.at";
    128 
    129 
    130     /**
    131      * Power consumption when screen is on, not including the backlight power.
    132      */
    133     public static final String POWER_SCREEN_ON = "screen.on";
    134 
    135     /**
    136      * Power consumption when cell radio is on but not on a call.
    137      */
    138     public static final String POWER_RADIO_ON = "radio.on";
    139 
    140     /**
    141      * Power consumption when cell radio is hunting for a signal.
    142      */
    143     public static final String POWER_RADIO_SCANNING = "radio.scanning";
    144 
    145     /**
    146      * Power consumption when talking on the phone.
    147      */
    148     public static final String POWER_RADIO_ACTIVE = "radio.active";
    149 
    150     /**
    151      * Power consumption at full backlight brightness. If the backlight is at
    152      * 50% brightness, then this should be multiplied by 0.5
    153      */
    154     public static final String POWER_SCREEN_FULL = "screen.full";
    155 
    156     /**
    157      * Power consumed by the audio hardware when playing back audio content. This is in addition
    158      * to the CPU power, probably due to a DSP and / or amplifier.
    159      */
    160     public static final String POWER_AUDIO = "dsp.audio";
    161 
    162     /**
    163      * Power consumed by any media hardware when playing back video content. This is in addition
    164      * to the CPU power, probably due to a DSP.
    165      */
    166     public static final String POWER_VIDEO = "dsp.video";
    167 
    168     /**
    169      * Average power consumption when camera flashlight is on.
    170      */
    171     public static final String POWER_FLASHLIGHT = "camera.flashlight";
    172 
    173     /**
    174      * Average power consumption when the camera is on over all standard use cases.
    175      *
    176      * TODO: Add more fine-grained camera power metrics.
    177      */
    178     public static final String POWER_CAMERA = "camera.avg";
    179 
    180     @Deprecated
    181     public static final String POWER_CPU_SPEEDS = "cpu.speeds";
    182 
    183     /**
    184      * Power consumed by wif batched scaning.  Broken down into bins by
    185      * Channels Scanned per Hour.  May do 1-720 scans per hour of 1-100 channels
    186      * for a range of 1-72,000.  Going logrithmic (1-8, 9-64, 65-512, 513-4096, 4097-)!
    187      */
    188     public static final String POWER_WIFI_BATCHED_SCAN = "wifi.batchedscan";
    189 
    190     /**
    191      * Battery capacity in milliAmpHour (mAh).
    192      */
    193     public static final String POWER_BATTERY_CAPACITY = "battery.capacity";
    194 
    195     static final HashMap<String, Object> sPowerMap = new HashMap<>();
    196 
    197     private static final String TAG_DEVICE = "device";
    198     private static final String TAG_ITEM = "item";
    199     private static final String TAG_ARRAY = "array";
    200     private static final String TAG_ARRAYITEM = "value";
    201     private static final String ATTR_NAME = "name";
    202 
    203     public PowerProfile(Context context) {
    204         // Read the XML file for the given profile (normally only one per
    205         // device)
    206         if (sPowerMap.size() == 0) {
    207             readPowerValuesFromXml(context);
    208         }
    209         initCpuClusters();
    210     }
    211 
    212     private void readPowerValuesFromXml(Context context) {
    213         int id = com.android.internal.R.xml.power_profile;
    214         final Resources resources = context.getResources();
    215         XmlResourceParser parser = resources.getXml(id);
    216         boolean parsingArray = false;
    217         ArrayList<Double> array = new ArrayList<Double>();
    218         String arrayName = null;
    219 
    220         try {
    221             XmlUtils.beginDocument(parser, TAG_DEVICE);
    222 
    223             while (true) {
    224                 XmlUtils.nextElement(parser);
    225 
    226                 String element = parser.getName();
    227                 if (element == null) break;
    228 
    229                 if (parsingArray && !element.equals(TAG_ARRAYITEM)) {
    230                     // Finish array
    231                     sPowerMap.put(arrayName, array.toArray(new Double[array.size()]));
    232                     parsingArray = false;
    233                 }
    234                 if (element.equals(TAG_ARRAY)) {
    235                     parsingArray = true;
    236                     array.clear();
    237                     arrayName = parser.getAttributeValue(null, ATTR_NAME);
    238                 } else if (element.equals(TAG_ITEM) || element.equals(TAG_ARRAYITEM)) {
    239                     String name = null;
    240                     if (!parsingArray) name = parser.getAttributeValue(null, ATTR_NAME);
    241                     if (parser.next() == XmlPullParser.TEXT) {
    242                         String power = parser.getText();
    243                         double value = 0;
    244                         try {
    245                             value = Double.valueOf(power);
    246                         } catch (NumberFormatException nfe) {
    247                         }
    248                         if (element.equals(TAG_ITEM)) {
    249                             sPowerMap.put(name, value);
    250                         } else if (parsingArray) {
    251                             array.add(value);
    252                         }
    253                     }
    254                 }
    255             }
    256             if (parsingArray) {
    257                 sPowerMap.put(arrayName, array.toArray(new Double[array.size()]));
    258             }
    259         } catch (XmlPullParserException e) {
    260             throw new RuntimeException(e);
    261         } catch (IOException e) {
    262             throw new RuntimeException(e);
    263         } finally {
    264             parser.close();
    265         }
    266 
    267         // Now collect other config variables.
    268         int[] configResIds = new int[]{
    269                 com.android.internal.R.integer.config_bluetooth_idle_cur_ma,
    270                 com.android.internal.R.integer.config_bluetooth_rx_cur_ma,
    271                 com.android.internal.R.integer.config_bluetooth_tx_cur_ma,
    272                 com.android.internal.R.integer.config_bluetooth_operating_voltage_mv,
    273                 com.android.internal.R.integer.config_wifi_idle_receive_cur_ma,
    274                 com.android.internal.R.integer.config_wifi_active_rx_cur_ma,
    275                 com.android.internal.R.integer.config_wifi_tx_cur_ma,
    276                 com.android.internal.R.integer.config_wifi_operating_voltage_mv,
    277         };
    278 
    279         String[] configResIdKeys = new String[]{
    280                 POWER_BLUETOOTH_CONTROLLER_IDLE,
    281                 POWER_BLUETOOTH_CONTROLLER_RX,
    282                 POWER_BLUETOOTH_CONTROLLER_TX,
    283                 POWER_BLUETOOTH_CONTROLLER_OPERATING_VOLTAGE,
    284                 POWER_WIFI_CONTROLLER_IDLE,
    285                 POWER_WIFI_CONTROLLER_RX,
    286                 POWER_WIFI_CONTROLLER_TX,
    287                 POWER_WIFI_CONTROLLER_OPERATING_VOLTAGE,
    288         };
    289 
    290         for (int i = 0; i < configResIds.length; i++) {
    291             String key = configResIdKeys[i];
    292             // if we already have some of these parameters in power_profile.xml, ignore the
    293             // value in config.xml
    294             if ((sPowerMap.containsKey(key) && (Double) sPowerMap.get(key) > 0)) {
    295                 continue;
    296             }
    297             int value = resources.getInteger(configResIds[i]);
    298             if (value > 0) {
    299                 sPowerMap.put(key, (double) value);
    300             }
    301         }
    302     }
    303 
    304     private CpuClusterKey[] mCpuClusters;
    305 
    306     private static final String POWER_CPU_CLUSTER_CORE_COUNT = "cpu.clusters.cores";
    307     private static final String POWER_CPU_CLUSTER_SPEED_PREFIX = "cpu.speeds.cluster";
    308     private static final String POWER_CPU_CLUSTER_ACTIVE_PREFIX = "cpu.active.cluster";
    309 
    310     @SuppressWarnings("deprecated")
    311     private void initCpuClusters() {
    312         // Figure out how many CPU clusters we're dealing with
    313         final Object obj = sPowerMap.get(POWER_CPU_CLUSTER_CORE_COUNT);
    314         if (obj == null || !(obj instanceof Double[])) {
    315             // Default to single.
    316             mCpuClusters = new CpuClusterKey[1];
    317             mCpuClusters[0] = new CpuClusterKey(POWER_CPU_SPEEDS, POWER_CPU_ACTIVE, 1);
    318 
    319         } else {
    320             final Double[] array = (Double[]) obj;
    321             mCpuClusters = new CpuClusterKey[array.length];
    322             for (int cluster = 0; cluster < array.length; cluster++) {
    323                 int numCpusInCluster = (int) Math.round(array[cluster]);
    324                 mCpuClusters[cluster] = new CpuClusterKey(
    325                         POWER_CPU_CLUSTER_SPEED_PREFIX + cluster,
    326                         POWER_CPU_CLUSTER_ACTIVE_PREFIX + cluster,
    327                         numCpusInCluster);
    328             }
    329         }
    330     }
    331 
    332     public static class CpuClusterKey {
    333         private final String timeKey;
    334         private final String powerKey;
    335         private final int numCpus;
    336 
    337         private CpuClusterKey(String timeKey, String powerKey, int numCpus) {
    338             this.timeKey = timeKey;
    339             this.powerKey = powerKey;
    340             this.numCpus = numCpus;
    341         }
    342     }
    343 
    344     public int getNumCpuClusters() {
    345         return mCpuClusters.length;
    346     }
    347 
    348     public int getNumCoresInCpuCluster(int index) {
    349         return mCpuClusters[index].numCpus;
    350     }
    351 
    352     public int getNumSpeedStepsInCpuCluster(int index) {
    353         Object value = sPowerMap.get(mCpuClusters[index].timeKey);
    354         if (value != null && value instanceof Double[]) {
    355             return ((Double[])value).length;
    356         }
    357         return 1; // Only one speed
    358     }
    359 
    360     public double getAveragePowerForCpu(int cluster, int step) {
    361         if (cluster >= 0 && cluster < mCpuClusters.length) {
    362             return getAveragePower(mCpuClusters[cluster].powerKey, step);
    363         }
    364         return 0;
    365     }
    366 
    367     /**
    368      * Returns the average current in mA consumed by the subsystem, or the given
    369      * default value if the subsystem has no recorded value.
    370      * @param type the subsystem type
    371      * @param defaultValue the value to return if the subsystem has no recorded value.
    372      * @return the average current in milliAmps.
    373      */
    374     public double getAveragePowerOrDefault(String type, double defaultValue) {
    375         if (sPowerMap.containsKey(type)) {
    376             Object data = sPowerMap.get(type);
    377             if (data instanceof Double[]) {
    378                 return ((Double[])data)[0];
    379             } else {
    380                 return (Double) sPowerMap.get(type);
    381             }
    382         } else {
    383             return defaultValue;
    384         }
    385     }
    386 
    387     /**
    388      * Returns the average current in mA consumed by the subsystem
    389      * @param type the subsystem type
    390      * @return the average current in milliAmps.
    391      */
    392     public double getAveragePower(String type) {
    393         return getAveragePowerOrDefault(type, 0);
    394     }
    395 
    396     /**
    397      * Returns the average current in mA consumed by the subsystem for the given level.
    398      * @param type the subsystem type
    399      * @param level the level of power at which the subsystem is running. For instance, the
    400      *  signal strength of the cell network between 0 and 4 (if there are 4 bars max.)
    401      *  If there is no data for multiple levels, the level is ignored.
    402      * @return the average current in milliAmps.
    403      */
    404     public double getAveragePower(String type, int level) {
    405         if (sPowerMap.containsKey(type)) {
    406             Object data = sPowerMap.get(type);
    407             if (data instanceof Double[]) {
    408                 final Double[] values = (Double[]) data;
    409                 if (values.length > level && level >= 0) {
    410                     return values[level];
    411                 } else if (level < 0 || values.length == 0) {
    412                     return 0;
    413                 } else {
    414                     return values[values.length - 1];
    415                 }
    416             } else {
    417                 return (Double) data;
    418             }
    419         } else {
    420             return 0;
    421         }
    422     }
    423 
    424     /**
    425      * Returns the battery capacity, if available, in milli Amp Hours. If not available,
    426      * it returns zero.
    427      * @return the battery capacity in mAh
    428      */
    429     public double getBatteryCapacity() {
    430         return getAveragePower(POWER_BATTERY_CAPACITY);
    431     }
    432 }
    433