Some battery improvements:
- New API for iterating over history that will allow a better implementation
in the future.
- Now do writes asynchronously.
Also improve the documentation for Activity.onRetainNonInstanceState().
Change-Id: Idf67f2796a8868eb62f288bcbb2bad29876c8554
diff --git a/core/java/com/android/internal/os/BatteryStatsImpl.java b/core/java/com/android/internal/os/BatteryStatsImpl.java
index c2d003e..6e5c47f 100644
--- a/core/java/com/android/internal/os/BatteryStatsImpl.java
+++ b/core/java/com/android/internal/os/BatteryStatsImpl.java
@@ -51,6 +51,7 @@
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.locks.ReentrantLock;
/**
* All information we are collecting about things that can happen that impact
@@ -3834,6 +3835,22 @@
}
}
+ private HistoryItem mHistoryIterator;
+
+ public boolean startIteratingHistoryLocked() {
+ return (mHistoryIterator = mHistory) != null;
+ }
+
+ public boolean getNextHistoryLocked(HistoryItem out) {
+ HistoryItem cur = mHistoryIterator;
+ if (cur == null) {
+ return false;
+ }
+ out.setTo(cur);
+ mHistoryIterator = cur.next;
+ return true;
+ }
+
@Override
public HistoryItem getHistory() {
return mHistory;
@@ -3960,7 +3977,7 @@
}
if (doWrite || (mLastWriteTime + (60 * 1000)) < mSecRealtime) {
if (mFile != null) {
- writeLocked();
+ writeAsyncLocked();
}
}
}
@@ -4356,11 +4373,22 @@
}
public void shutdownLocked() {
- writeLocked();
+ writeSyncLocked();
mShuttingDown = true;
}
- public void writeLocked() {
+ Parcel mPendingWrite = null;
+ final ReentrantLock mWriteLock = new ReentrantLock();
+
+ public void writeAsyncLocked() {
+ writeLocked(false);
+ }
+
+ public void writeSyncLocked() {
+ writeLocked(true);
+ }
+
+ void writeLocked(boolean sync) {
if (mFile == null) {
Slog.w("BatteryStats", "writeLocked: no file associated with this instance");
return;
@@ -4370,23 +4398,51 @@
return;
}
+ Parcel out = Parcel.obtain();
+ writeSummaryToParcel(out);
+ mLastWriteTime = SystemClock.elapsedRealtime();
+
+ if (mPendingWrite != null) {
+ mPendingWrite.recycle();
+ }
+ mPendingWrite = out;
+
+ if (sync) {
+ commitPendingDataToDisk();
+ } else {
+ Thread thr = new Thread("BatteryStats-Write") {
+ @Override
+ public void run() {
+ Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
+ commitPendingDataToDisk();
+ }
+ };
+ thr.start();
+ }
+ }
+
+ public void commitPendingDataToDisk() {
+ Parcel next;
+ synchronized (this) {
+ next = mPendingWrite;
+ mPendingWrite = null;
+
+ mWriteLock.lock();
+ }
+
try {
FileOutputStream stream = new FileOutputStream(mFile.chooseForWrite());
- Parcel out = Parcel.obtain();
- writeSummaryToParcel(out);
- stream.write(out.marshall());
- out.recycle();
-
+ stream.write(next.marshall());
stream.flush();
stream.close();
mFile.commit();
-
- mLastWriteTime = SystemClock.elapsedRealtime();
- return;
} catch (IOException e) {
Slog.w("BatteryStats", "Error writing battery statistics", e);
+ mFile.rollback();
+ } finally {
+ next.recycle();
+ mWriteLock.unlock();
}
- mFile.rollback();
}
static byte[] readFully(FileInputStream stream) throws java.io.IOException {