Merge "Lock access to PowerStatsLogger data" into tm-qpr-dev

This commit is contained in:
TreeHugger Robot
2022-09-13 05:25:41 +00:00
committed by Android (Google) Code Review
2 changed files with 29 additions and 20 deletions

View File

@@ -220,18 +220,17 @@ public class PowerStatsDataStorage {
public void write(byte[] data) { public void write(byte[] data) {
if (data != null && data.length > 0) { if (data != null && data.length > 0) {
mLock.lock(); mLock.lock();
long currentTimeMillis = System.currentTimeMillis();
try { try {
long currentTimeMillis = System.currentTimeMillis();
DataElement dataElement = new DataElement(data); DataElement dataElement = new DataElement(data);
mFileRotator.rewriteActive(new DataRewriter(dataElement.toByteArray()), mFileRotator.rewriteActive(new DataRewriter(dataElement.toByteArray()),
currentTimeMillis); currentTimeMillis);
mFileRotator.maybeRotate(currentTimeMillis); mFileRotator.maybeRotate(currentTimeMillis);
} catch (IOException e) { } catch (IOException e) {
Slog.e(TAG, "Failed to write to on-device storage: " + e); Slog.e(TAG, "Failed to write to on-device storage: " + e);
} finally {
mLock.unlock();
} }
mLock.unlock();
} }
} }
@@ -240,21 +239,31 @@ public class PowerStatsDataStorage {
* DataElement retrieved from on-device storage, callback is called. * DataElement retrieved from on-device storage, callback is called.
*/ */
public void read(DataElementReadCallback callback) throws IOException { public void read(DataElementReadCallback callback) throws IOException {
mFileRotator.readMatching(new DataReader(callback), Long.MIN_VALUE, Long.MAX_VALUE); mLock.lock();
try {
mFileRotator.readMatching(new DataReader(callback), Long.MIN_VALUE, Long.MAX_VALUE);
} finally {
mLock.unlock();
}
} }
/** /**
* Deletes all stored log data. * Deletes all stored log data.
*/ */
public void deleteLogs() { public void deleteLogs() {
File[] files = mDataStorageDir.listFiles(); mLock.lock();
for (int i = 0; i < files.length; i++) { try {
int versionDot = mDataStorageFilename.lastIndexOf('.'); File[] files = mDataStorageDir.listFiles();
String beforeVersionDot = mDataStorageFilename.substring(0, versionDot); for (int i = 0; i < files.length; i++) {
// Check that the stems before the version match. int versionDot = mDataStorageFilename.lastIndexOf('.');
if (files[i].getName().startsWith(beforeVersionDot)) { String beforeVersionDot = mDataStorageFilename.substring(0, versionDot);
files[i].delete(); // Check that the stems before the version match.
if (files[i].getName().startsWith(beforeVersionDot)) {
files[i].delete();
}
} }
} finally {
mLock.unlock();
} }
} }
} }

View File

@@ -159,12 +159,12 @@ public final class PowerStatsLogger extends Handler {
EnergyMeasurementUtils.packProtoMessage(energyMeasurement, pos); EnergyMeasurementUtils.packProtoMessage(energyMeasurement, pos);
if (DEBUG) EnergyMeasurementUtils.print(energyMeasurement); if (DEBUG) EnergyMeasurementUtils.print(energyMeasurement);
} catch (IOException e) { } catch (IOException e) {
Slog.e(TAG, "Failed to write energy meter data to incident report."); Slog.e(TAG, "Failed to write energy meter data to incident report.", e);
} }
} }
}); });
} catch (IOException e) { } catch (IOException e) {
Slog.e(TAG, "Failed to write energy meter info to incident report."); Slog.e(TAG, "Failed to write energy meter info to incident report.", e);
} }
pos.flush(); pos.flush();
@@ -200,12 +200,12 @@ public final class PowerStatsLogger extends Handler {
EnergyConsumerResultUtils.packProtoMessage(energyConsumerResult, pos, true); EnergyConsumerResultUtils.packProtoMessage(energyConsumerResult, pos, true);
if (DEBUG) EnergyConsumerResultUtils.print(energyConsumerResult); if (DEBUG) EnergyConsumerResultUtils.print(energyConsumerResult);
} catch (IOException e) { } catch (IOException e) {
Slog.e(TAG, "Failed to write energy model data to incident report."); Slog.e(TAG, "Failed to write energy model data to incident report.", e);
} }
} }
}); });
} catch (IOException e) { } catch (IOException e) {
Slog.e(TAG, "Failed to write energy model info to incident report."); Slog.e(TAG, "Failed to write energy model info to incident report.", e);
} }
pos.flush(); pos.flush();
@@ -241,12 +241,12 @@ public final class PowerStatsLogger extends Handler {
StateResidencyResultUtils.packProtoMessage(stateResidencyResult, pos); StateResidencyResultUtils.packProtoMessage(stateResidencyResult, pos);
if (DEBUG) StateResidencyResultUtils.print(stateResidencyResult); if (DEBUG) StateResidencyResultUtils.print(stateResidencyResult);
} catch (IOException e) { } catch (IOException e) {
Slog.e(TAG, "Failed to write residency data to incident report."); Slog.e(TAG, "Failed to write residency data to incident report.", e);
} }
} }
}); });
} catch (IOException e) { } catch (IOException e) {
Slog.e(TAG, "Failed to write residency data to incident report."); Slog.e(TAG, "Failed to write residency data to incident report.", e);
} }
pos.flush(); pos.flush();
@@ -267,7 +267,7 @@ public final class PowerStatsLogger extends Handler {
final FileInputStream fis = new FileInputStream(cachedFile.getPath()); final FileInputStream fis = new FileInputStream(cachedFile.getPath());
fis.read(dataCached); fis.read(dataCached);
} catch (IOException e) { } catch (IOException e) {
Slog.e(TAG, "Failed to read cached data from file"); Slog.e(TAG, "Failed to read cached data from file", e);
} }
// If the cached and current data are different, delete the data store. // If the cached and current data are different, delete the data store.
@@ -291,7 +291,7 @@ public final class PowerStatsLogger extends Handler {
fos.write(data); fos.write(data);
atomicCachedFile.finishWrite(fos); atomicCachedFile.finishWrite(fos);
} catch (IOException e) { } catch (IOException e) {
Slog.e(TAG, "Failed to write current data to cached file"); Slog.e(TAG, "Failed to write current data to cached file", e);
} }
} }