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