diff options
author | Hector Tellez <htellez@google.com> | 2016-03-04 19:56:20 -0800 |
---|---|---|
committer | Hector Tellez <htellez@google.com> | 2016-03-09 17:07:51 -0800 |
commit | e952089725398c8fa8b617ca7bdde840a38849fd (patch) | |
tree | 99028f4322d29b98ebc4dd40962484f4b98d2d7d /src/com/android | |
parent | 61a8a07e6770a403ebdb2d55dffcb33409573357 (diff) | |
download | loganalysis-e952089725398c8fa8b617ca7bdde840a38849fd.tar.gz |
Adds projected battery life analysis.
Change-Id: I25bcb84f5d62802dcb51d9a3ceae60929eb89606
Diffstat (limited to 'src/com/android')
4 files changed, 263 insertions, 3 deletions
diff --git a/src/com/android/loganalysis/item/BatteryDischargeStatsInfoItem.java b/src/com/android/loganalysis/item/BatteryDischargeStatsInfoItem.java new file mode 100644 index 0000000..8c15b92 --- /dev/null +++ b/src/com/android/loganalysis/item/BatteryDischargeStatsInfoItem.java @@ -0,0 +1,119 @@ +/* + * Copyright (C) 2016 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.loganalysis.item; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +/** + * An {@link IItem} used to store information of the battery discharge. + */ +public class BatteryDischargeStatsInfoItem extends GenericItem { + + /** Constant for JSON output */ + public static final String MAX_PERCENTAGE = "MAX_PERCENTAGE"; + /** Constant for JSON output */ + public static final String MIN_PERCENTAGE = "MIN_PERCENTAGE"; + /** Constant for JSON output */ + public static final String DISCHARGE_PERCENTAGE = "DISCHARGE_PERCENTAGE"; + /** Constant for JSON output */ + public static final String DISCHARGE_DURATION = "DISCHARGE_DURATION"; + /** Constant for JSON output */ + public static final String PROJECTED_BATTERY_LIFE = "PROJECTED_BATTERY_LIFE"; + + private static final Set<String> ATTRIBUTES = new HashSet<>(Arrays.asList(MAX_PERCENTAGE, + MIN_PERCENTAGE, DISCHARGE_PERCENTAGE, DISCHARGE_DURATION, PROJECTED_BATTERY_LIFE)); + + /** + * The constructor for {@link BatteryDischargeStatsInfoItem}. + */ + public BatteryDischargeStatsInfoItem() { + super(ATTRIBUTES); + } + + /** + * Set the maximum percentage. + */ + public void setMaxPercentage(int percentage) { + setAttribute(MAX_PERCENTAGE, percentage); + } + + /** + * Set the minimum percentage. + */ + public void setMinPercentage(int percentage) { + setAttribute(MIN_PERCENTAGE, percentage); + } + + /** + * Set the discharge percentage. + */ + public void setDischargePercentage(int dischargePercentage) { + setAttribute(DISCHARGE_PERCENTAGE, dischargePercentage); + } + + /** + * Set the discharge duration. + */ + public void setDischargeDuration(long dischargeDuration) { + setAttribute(DISCHARGE_DURATION, dischargeDuration); + } + + /** + * Set the projected battery life. + */ + public void setProjectedBatteryLife(long projectedBatteryLife) { + setAttribute(PROJECTED_BATTERY_LIFE, projectedBatteryLife); + } + + /** + * Get the maximum percentage. + */ + public int getMaxPercentage() { + return (int) getAttribute(MAX_PERCENTAGE); + } + + /** + * Get the minimum percentage. + */ + public int getMinPercentage() { + return (int) getAttribute(MIN_PERCENTAGE); + } + + /** + * Get the discharge percentage. + */ + public int getDischargePercentage() { + return (int) getAttribute(DISCHARGE_PERCENTAGE); + } + + /** + * Get the discharge duration. + */ + public long getDischargeDuration() { + return (long) getAttribute(DISCHARGE_DURATION); + } + + /** + * Get the projected battery life. + */ + public long getProjectedBatteryLife() { + return (long) getAttribute(PROJECTED_BATTERY_LIFE); + } + +} diff --git a/src/com/android/loganalysis/item/DumpsysBatteryStatsItem.java b/src/com/android/loganalysis/item/DumpsysBatteryStatsItem.java index 1039324..8b7a800 100644 --- a/src/com/android/loganalysis/item/DumpsysBatteryStatsItem.java +++ b/src/com/android/loganalysis/item/DumpsysBatteryStatsItem.java @@ -27,9 +27,12 @@ public class DumpsysBatteryStatsItem implements IItem { public static final String SUMMARY = "SUMMARY"; /** Constant for JSON output */ public static final String DETAILED_STATS = "DETAILED_STATS"; + /** Constant for JSON output */ + public static final String DISCHARGE_STATS = "DISCHARGE_STATS"; private BatteryStatsSummaryInfoItem mBatteryStatsSummaryItem; private BatteryStatsDetailedInfoItem mDetailedBatteryStatsItem; + private BatteryDischargeStatsInfoItem mDischargeStatsItem; /** * Set the battery stats summary {@link BatteryStatsSummaryInfoItem} @@ -46,6 +49,13 @@ public class DumpsysBatteryStatsItem implements IItem { } /** + * Set the battery steps info item {@link BatteryDischargeStatsInfoItem} + */ + public void setBatteryDischargeStatsItem(BatteryDischargeStatsInfoItem item){ + mDischargeStatsItem = item; + } + + /** * Get the battery stats summary {@link BatteryStatsSummaryInfoItem} */ public BatteryStatsSummaryInfoItem getBatteryStatsSummaryItem() { @@ -59,6 +69,12 @@ public class DumpsysBatteryStatsItem implements IItem { return mDetailedBatteryStatsItem; } + /** + * Get the battery steps info item {@link BatteryDischargeStatsInfoItem} + */ + public BatteryDischargeStatsInfoItem getBatteryDischargeStatsItem() { + return mDischargeStatsItem; + } /** * {@inheritDoc} @@ -89,6 +105,9 @@ public class DumpsysBatteryStatsItem implements IItem { if (mDetailedBatteryStatsItem != null) { batteryStatsComponent.put(DETAILED_STATS, mDetailedBatteryStatsItem.toJson()); } + if (mDischargeStatsItem != null) { + batteryStatsComponent.put(DISCHARGE_STATS, mDischargeStatsItem.toJson()); + } } catch (JSONException e) { // ignore } diff --git a/src/com/android/loganalysis/parser/BatteryDischargeStatsInfoParser.java b/src/com/android/loganalysis/parser/BatteryDischargeStatsInfoParser.java new file mode 100644 index 0000000..00e0527 --- /dev/null +++ b/src/com/android/loganalysis/parser/BatteryDischargeStatsInfoParser.java @@ -0,0 +1,112 @@ +/* + * Copyright (C) 2016 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.loganalysis.parser; + +import com.android.loganalysis.item.BatteryDischargeStatsInfoItem; +import com.android.loganalysis.item.BatteryStatsSummaryInfoItem; +import com.android.loganalysis.util.NumberFormattingUtil; + +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * A {@link IParser} to parse the battery discharge section. + */ +public class BatteryDischargeStatsInfoParser extends AbstractSectionParser { + + /** + * Matches; #47: +5m5s105ms to 47 (screen-on, power-save-off, device-idle-off) + */ + private static Pattern DISCHARGE_STEP_PATTERN = Pattern + .compile("^.*: \\+((\\d+)h)?((\\d+)m)?((\\d+)s)?(\\d+)ms.* to (\\d+).*"); + + /** + * {@inheritDoc} + * + * @return The {@link BatteryDischargeStatsInfoItem}. + */ + @Override + public BatteryDischargeStatsInfoItem parse(List<String> lines) { + long totalDuration = 0; + long projectionDuration = 0; + Integer minPercent = null; + Integer maxPercent = null; + Integer minProjectionPercent = null; + Integer maxProjectionPercent = null; + + for (String line : lines) { + Matcher m = DISCHARGE_STEP_PATTERN.matcher(line); + + if (m.matches()) { + int percent = Integer.parseInt(m.group(8)); + + if (minPercent == null || percent < minPercent) { + minPercent = percent; + } + + if (maxPercent == null || maxPercent < percent) { + maxPercent = percent; + } + + long duration = NumberFormattingUtil.getMs( + NumberFormattingUtil.parseIntOrZero(m.group(2)), + NumberFormattingUtil.parseIntOrZero(m.group(4)), + NumberFormattingUtil.parseIntOrZero(m.group(6)), + NumberFormattingUtil.parseIntOrZero(m.group(7))); + + totalDuration += duration; + + // For computing the projected battery life we drop the first 5% of the battery + // charge because these discharge 'slower' and are not reliable for the projection. + if (percent > 94) { + continue; + } + + if (minProjectionPercent == null || percent < minProjectionPercent) { + minProjectionPercent = percent; + } + + if (maxProjectionPercent == null || maxProjectionPercent < percent) { + maxProjectionPercent = percent; + } + + projectionDuration += duration; + } + } + + if (minPercent == null) { + return null; + } + + int dischargePercent = maxPercent - minPercent + 1; + + BatteryDischargeStatsInfoItem item = new BatteryDischargeStatsInfoItem(); + item.setDischargeDuration(totalDuration); + item.setDischargePercentage(dischargePercent); + item.setMaxPercentage(maxPercent); + item.setMinPercentage(minPercent); + + if (minProjectionPercent == null) { + return item; + } + + int projectionDischargePercent = maxProjectionPercent - minProjectionPercent + 1; + item.setProjectedBatteryLife((projectionDuration * 100) / projectionDischargePercent); + return item; + } +} diff --git a/src/com/android/loganalysis/parser/DumpsysBatteryStatsParser.java b/src/com/android/loganalysis/parser/DumpsysBatteryStatsParser.java index 8a995e8..a93c892 100644 --- a/src/com/android/loganalysis/parser/DumpsysBatteryStatsParser.java +++ b/src/com/android/loganalysis/parser/DumpsysBatteryStatsParser.java @@ -16,6 +16,7 @@ package com.android.loganalysis.parser; +import com.android.loganalysis.item.BatteryDischargeStatsInfoItem; import com.android.loganalysis.item.BatteryStatsDetailedInfoItem; import com.android.loganalysis.item.DumpsysBatteryStatsItem; import com.android.loganalysis.item.BatteryStatsSummaryInfoItem; @@ -31,11 +32,17 @@ public class DumpsysBatteryStatsParser extends AbstractSectionParser { private static final String SUMMARY_INFO_SECTION_REGEX = "Battery History \\(\\d+% used, \\d+(KB)? used of \\d+KB, \\d+ strings using " + "\\d+(KB)?\\):$"; + private static final String DISCHARGE_STATS_INFO_SECTION_REGEX = "^Discharge step durations:$"; private static final String DETAILED_INFO_SECTION_REGEX = "^Statistics since last charge:$"; - private static final String NOOP_SECTION_REGEX = "^Statistics since last unplugged:$"; + + // We are not using this sections and will be ignored. + private static final String NOOP_SECTION_REGEX = + "^(Statistics since last unplugged:|Daily stats:)$"; private BatteryStatsSummaryInfoParser mSummaryParser = new BatteryStatsSummaryInfoParser(); private BatteryStatsDetailedInfoParser mDetailedParser = new BatteryStatsDetailedInfoParser(); + private BatteryDischargeStatsInfoParser mDischargeStepsParser = new + BatteryDischargeStatsInfoParser(); private DumpsysBatteryStatsItem mDumpsysBatteryStatsItem = null; private boolean mParsedInput = false; @@ -64,6 +71,7 @@ public class DumpsysBatteryStatsParser extends AbstractSectionParser { protected void setup() { addSectionParser(mSummaryParser, SUMMARY_INFO_SECTION_REGEX); addSectionParser(mDetailedParser, DETAILED_INFO_SECTION_REGEX); + addSectionParser(mDischargeStepsParser, DISCHARGE_STATS_INFO_SECTION_REGEX); addSectionParser(new NoopParser(), NOOP_SECTION_REGEX); } @@ -82,9 +90,11 @@ public class DumpsysBatteryStatsParser extends AbstractSectionParser { if (mDumpsysBatteryStatsItem != null) { mDumpsysBatteryStatsItem.setBatteryStatsSummarytem( - (BatteryStatsSummaryInfoItem) getSection(mSummaryParser)); + (BatteryStatsSummaryInfoItem) getSection(mSummaryParser)); mDumpsysBatteryStatsItem.setDetailedBatteryStatsItem( - (BatteryStatsDetailedInfoItem) getSection(mDetailedParser)); + (BatteryStatsDetailedInfoItem) getSection(mDetailedParser)); + mDumpsysBatteryStatsItem.setBatteryDischargeStatsItem( + (BatteryDischargeStatsInfoItem) getSection(mDischargeStepsParser)); } } } |