Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 11efebdf authored by Michael Wachenschwanz's avatar Michael Wachenschwanz Committed by Dmitri Plotnikov
Browse files

Lock access to PowerStatsLogger data

Also add exceptions to error logs in PowerStatsDataStorage.

Bug: 237470734
Bug: 245423154 (cherry-pick request)
Test: atest FrameworksServicesTests:PowerStatsServiceTest
Change-Id: I37933fc44a3b85d2469f02bb39bad6d6dffbd818
(cherry picked from commit df49541a)
parent 33c34d0a
Loading
Loading
Loading
Loading
+21 −12
Original line number Diff line number Diff line
@@ -220,33 +220,39 @@ public class PowerStatsDataStorage {
    public void write(byte[] data) {
        if (data != null && data.length > 0) {
            mLock.lock();

            long currentTimeMillis = System.currentTimeMillis();
            try {
                long currentTimeMillis = System.currentTimeMillis();
                DataElement dataElement = new DataElement(data);
                mFileRotator.rewriteActive(new DataRewriter(dataElement.toByteArray()),
                        currentTimeMillis);
                mFileRotator.maybeRotate(currentTimeMillis);
            } catch (IOException e) {
                Slog.e(TAG, "Failed to write to on-device storage: " + e);
            }

            } finally {
                mLock.unlock();
            }
        }
    }

    /**
     * Reads all DataElements stored in on-device storage.  For each
     * DataElement retrieved from on-device storage, callback is called.
     */
    public void read(DataElementReadCallback callback) throws IOException {
        mLock.lock();
        try {
            mFileRotator.readMatching(new DataReader(callback), Long.MIN_VALUE, Long.MAX_VALUE);
        } finally {
            mLock.unlock();
        }
    }

    /**
     * Deletes all stored log data.
     */
    public void deleteLogs() {
        mLock.lock();
        try {
            File[] files = mDataStorageDir.listFiles();
            for (int i = 0; i < files.length; i++) {
                int versionDot = mDataStorageFilename.lastIndexOf('.');
@@ -256,5 +262,8 @@ public class PowerStatsDataStorage {
                    files[i].delete();
                }
            }
        } finally {
            mLock.unlock();
        }
    }
}
+8 −8
Original line number Diff line number Diff line
@@ -159,12 +159,12 @@ public final class PowerStatsLogger extends Handler {
                        EnergyMeasurementUtils.packProtoMessage(energyMeasurement, pos);
                        if (DEBUG) EnergyMeasurementUtils.print(energyMeasurement);
                    } 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) {
            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();
@@ -200,12 +200,12 @@ public final class PowerStatsLogger extends Handler {
                        EnergyConsumerResultUtils.packProtoMessage(energyConsumerResult, pos, true);
                        if (DEBUG) EnergyConsumerResultUtils.print(energyConsumerResult);
                    } 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) {
            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();
@@ -241,12 +241,12 @@ public final class PowerStatsLogger extends Handler {
                        StateResidencyResultUtils.packProtoMessage(stateResidencyResult, pos);
                        if (DEBUG) StateResidencyResultUtils.print(stateResidencyResult);
                    } 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) {
            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();
@@ -267,7 +267,7 @@ public final class PowerStatsLogger extends Handler {
                    final FileInputStream fis = new FileInputStream(cachedFile.getPath());
                    fis.read(dataCached);
                } 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.
@@ -291,7 +291,7 @@ public final class PowerStatsLogger extends Handler {
            fos.write(data);
            atomicCachedFile.finishWrite(fos);
        } 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);
        }
    }