From 114a2c09165b0c27a8c4385183daa6b59fe00465 Mon Sep 17 00:00:00 2001 From: David Anderson Date: Wed, 3 Feb 2021 15:39:44 -0800 Subject: [PATCH] libsnapshot: Communicate compression status to update_engine. Bug: 178732971 Test: manual with statsd_testdrive Change-Id: If480e5ef1f6fcdde7ccf6f5060f12d021f75b9a1 --- fs_mgr/libsnapshot/android/snapshot/snapshot.proto | 5 ++++- .../libsnapshot/include/libsnapshot/mock_snapshot.h | 1 + fs_mgr/libsnapshot/include/libsnapshot/snapshot.h | 8 ++++++++ .../include/libsnapshot/snapshot_stats.h | 4 ++-- .../libsnapshot/include/libsnapshot/snapshot_stub.h | 1 + fs_mgr/libsnapshot/snapshot.cpp | 13 ++++++++++++- fs_mgr/libsnapshot/snapshot_stats.cpp | 3 ++- fs_mgr/libsnapshot/snapshot_stub.cpp | 7 ++++++- 8 files changed, 36 insertions(+), 6 deletions(-) diff --git a/fs_mgr/libsnapshot/android/snapshot/snapshot.proto b/fs_mgr/libsnapshot/android/snapshot/snapshot.proto index 36e116943..42bff14bc 100644 --- a/fs_mgr/libsnapshot/android/snapshot/snapshot.proto +++ b/fs_mgr/libsnapshot/android/snapshot/snapshot.proto @@ -156,7 +156,7 @@ message SnapshotUpdateStatus { MergePhase merge_phase = 6; } -// Next: 4 +// Next: 5 message SnapshotMergeReport { // Status of the update after the merge attempts. UpdateState state = 1; @@ -167,4 +167,7 @@ message SnapshotMergeReport { // Total size of all the COW images before the update. uint64 cow_file_size = 3; + + // Whether compression/dm-user was used for any snapshots. + bool compression_enabled = 4; } diff --git a/fs_mgr/libsnapshot/include/libsnapshot/mock_snapshot.h b/fs_mgr/libsnapshot/include/libsnapshot/mock_snapshot.h index 92e79107a..1e420cbbe 100644 --- a/fs_mgr/libsnapshot/include/libsnapshot/mock_snapshot.h +++ b/fs_mgr/libsnapshot/include/libsnapshot/mock_snapshot.h @@ -32,6 +32,7 @@ class MockSnapshotManager : public ISnapshotManager { (const std::function& callback, const std::function& before_cancel), (override)); MOCK_METHOD(UpdateState, GetUpdateState, (double* progress), (override)); + MOCK_METHOD(bool, UpdateUsesCompression, (), (override)); MOCK_METHOD(Return, CreateUpdateSnapshots, (const chromeos_update_engine::DeltaArchiveManifest& manifest), (override)); MOCK_METHOD(bool, MapUpdateSnapshot, diff --git a/fs_mgr/libsnapshot/include/libsnapshot/snapshot.h b/fs_mgr/libsnapshot/include/libsnapshot/snapshot.h index ff7a72756..0d90f6cd6 100644 --- a/fs_mgr/libsnapshot/include/libsnapshot/snapshot.h +++ b/fs_mgr/libsnapshot/include/libsnapshot/snapshot.h @@ -170,6 +170,10 @@ class ISnapshotManager { // Other: 0 virtual UpdateState GetUpdateState(double* progress = nullptr) = 0; + // Returns true if compression is enabled for the current update. This always returns false if + // UpdateState is None, or no snapshots have been created. + virtual bool UpdateUsesCompression() = 0; + // Create necessary COW device / files for OTA clients. New logical partitions will be added to // group "cow" in target_metadata. Regions of partitions of current_metadata will be // "write-protected" and snapshotted. @@ -326,6 +330,7 @@ class SnapshotManager final : public ISnapshotManager { UpdateState ProcessUpdateState(const std::function& callback = {}, const std::function& before_cancel = {}) override; UpdateState GetUpdateState(double* progress = nullptr) override; + bool UpdateUsesCompression() override; Return CreateUpdateSnapshots(const DeltaArchiveManifest& manifest) override; bool MapUpdateSnapshot(const CreateLogicalPartitionParams& params, std::string* snapshot_path) override; @@ -720,6 +725,9 @@ class SnapshotManager final : public ISnapshotManager { SnapuserdClient* snapuserd_client() const { return snapuserd_client_.get(); } + // Helper of UpdateUsesCompression + bool UpdateUsesCompression(LockedFile* lock); + std::string gsid_dir_; std::string metadata_dir_; std::unique_ptr device_; diff --git a/fs_mgr/libsnapshot/include/libsnapshot/snapshot_stats.h b/fs_mgr/libsnapshot/include/libsnapshot/snapshot_stats.h index d691d4f31..96d2deb0c 100644 --- a/fs_mgr/libsnapshot/include/libsnapshot/snapshot_stats.h +++ b/fs_mgr/libsnapshot/include/libsnapshot/snapshot_stats.h @@ -28,7 +28,7 @@ class ISnapshotMergeStats { virtual ~ISnapshotMergeStats() = default; // Called when merge starts or resumes. virtual bool Start() = 0; - virtual void set_state(android::snapshot::UpdateState state) = 0; + virtual void set_state(android::snapshot::UpdateState state, bool using_compression) = 0; virtual void set_cow_file_size(uint64_t cow_file_size) = 0; virtual uint64_t cow_file_size() = 0; @@ -51,7 +51,7 @@ class SnapshotMergeStats : public ISnapshotMergeStats { // ISnapshotMergeStats overrides bool Start() override; - void set_state(android::snapshot::UpdateState state) override; + void set_state(android::snapshot::UpdateState state, bool using_compression) override; void set_cow_file_size(uint64_t cow_file_size) override; uint64_t cow_file_size() override; std::unique_ptr Finish() override; diff --git a/fs_mgr/libsnapshot/include/libsnapshot/snapshot_stub.h b/fs_mgr/libsnapshot/include/libsnapshot/snapshot_stub.h index cba356005..3365ceb73 100644 --- a/fs_mgr/libsnapshot/include/libsnapshot/snapshot_stub.h +++ b/fs_mgr/libsnapshot/include/libsnapshot/snapshot_stub.h @@ -32,6 +32,7 @@ class SnapshotManagerStub : public ISnapshotManager { UpdateState ProcessUpdateState(const std::function& callback = {}, const std::function& before_cancel = {}) override; UpdateState GetUpdateState(double* progress = nullptr) override; + bool UpdateUsesCompression() override; Return CreateUpdateSnapshots( const chromeos_update_engine::DeltaArchiveManifest& manifest) override; bool MapUpdateSnapshot(const android::fs_mgr::CreateLogicalPartitionParams& params, diff --git a/fs_mgr/libsnapshot/snapshot.cpp b/fs_mgr/libsnapshot/snapshot.cpp index 932972576..d9218f4ed 100644 --- a/fs_mgr/libsnapshot/snapshot.cpp +++ b/fs_mgr/libsnapshot/snapshot.cpp @@ -1683,6 +1683,17 @@ UpdateState SnapshotManager::GetUpdateState(double* progress) { return state; } +bool SnapshotManager::UpdateUsesCompression() { + auto lock = LockShared(); + if (!lock) return false; + return UpdateUsesCompression(lock.get()); +} + +bool SnapshotManager::UpdateUsesCompression(LockedFile* lock) { + SnapshotUpdateStatus update_status = ReadSnapshotUpdateStatus(lock); + return update_status.compression_enabled(); +} + bool SnapshotManager::ListSnapshots(LockedFile* lock, std::vector* snapshots) { CHECK(lock); @@ -2109,7 +2120,7 @@ bool SnapshotManager::UnmapCowDevices(LockedFile* lock, const std::string& name) auto& dm = DeviceMapper::Instance(); - if (IsCompressionEnabled() && !UnmapDmUserDevice(name)) { + if (UpdateUsesCompression(lock) && !UnmapDmUserDevice(name)) { return false; } diff --git a/fs_mgr/libsnapshot/snapshot_stats.cpp b/fs_mgr/libsnapshot/snapshot_stats.cpp index 372373086..513700ddc 100644 --- a/fs_mgr/libsnapshot/snapshot_stats.cpp +++ b/fs_mgr/libsnapshot/snapshot_stats.cpp @@ -84,8 +84,9 @@ bool SnapshotMergeStats::Start() { return WriteState(); } -void SnapshotMergeStats::set_state(android::snapshot::UpdateState state) { +void SnapshotMergeStats::set_state(android::snapshot::UpdateState state, bool using_compression) { report_.set_state(state); + report_.set_compression_enabled(using_compression); } void SnapshotMergeStats::set_cow_file_size(uint64_t cow_file_size) { diff --git a/fs_mgr/libsnapshot/snapshot_stub.cpp b/fs_mgr/libsnapshot/snapshot_stub.cpp index 26b9129b8..8a254c99e 100644 --- a/fs_mgr/libsnapshot/snapshot_stub.cpp +++ b/fs_mgr/libsnapshot/snapshot_stub.cpp @@ -116,9 +116,14 @@ std::unique_ptr SnapshotManagerStub::EnsureMetadataMounted() { return nullptr; } +bool SnapshotManagerStub::UpdateUsesCompression() { + LOG(ERROR) << __FUNCTION__ << " should never be called."; + return false; +} + class SnapshotMergeStatsStub : public ISnapshotMergeStats { bool Start() override { return false; } - void set_state(android::snapshot::UpdateState) override {} + void set_state(android::snapshot::UpdateState, bool) override {} void set_cow_file_size(uint64_t) override {} uint64_t cow_file_size() override { return 0; } std::unique_ptr Finish() override { return nullptr; }