1 // Copyright (C) 2020 The Android Open Source Project 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #pragma once 16 17 #include <chrono> 18 #include <memory> 19 20 #include <android/snapshot/snapshot.pb.h> 21 #include <libsnapshot/snapshot.h> 22 23 namespace android { 24 namespace snapshot { 25 26 class ISnapshotMergeStats { 27 public: 28 virtual ~ISnapshotMergeStats() = default; 29 // Called when merge starts or resumes. 30 virtual bool Start() = 0; 31 virtual void set_state(android::snapshot::UpdateState state, bool using_compression) = 0; 32 virtual void set_cow_file_size(uint64_t cow_file_size) = 0; 33 virtual void set_total_cow_size_bytes(uint64_t bytes) = 0; 34 virtual void set_estimated_cow_size_bytes(uint64_t bytes) = 0; 35 virtual void set_boot_complete_time_ms(uint32_t ms) = 0; 36 virtual void set_boot_complete_to_merge_start_time_ms(uint32_t ms) = 0; 37 virtual void set_merge_failure_code(MergeFailureCode code) = 0; 38 virtual void set_source_build_fingerprint(const std::string& fingerprint) = 0; 39 virtual uint64_t cow_file_size() = 0; 40 virtual uint64_t total_cow_size_bytes() = 0; 41 virtual uint64_t estimated_cow_size_bytes() = 0; 42 virtual uint32_t boot_complete_time_ms() = 0; 43 virtual uint32_t boot_complete_to_merge_start_time_ms() = 0; 44 virtual MergeFailureCode merge_failure_code() = 0; 45 virtual std::string source_build_fingerprint() = 0; 46 47 // Called when merge ends. Properly clean up permanent storage. 48 class Result { 49 public: ~Result()50 virtual ~Result() {} 51 virtual const SnapshotMergeReport& report() const = 0; 52 // Time between successful Start() / Resume() to Finish(). 53 virtual std::chrono::steady_clock::duration merge_time() const = 0; 54 }; 55 // Return nullptr if any failure. 56 virtual std::unique_ptr<Result> Finish() = 0; 57 58 // Write out the current state. This should be called when data might be lost that 59 // cannot be recovered (eg the COW sizes). 60 virtual bool WriteState() = 0; 61 }; 62 63 class SnapshotMergeStats : public ISnapshotMergeStats { 64 public: 65 // Not thread safe. 66 static SnapshotMergeStats* GetInstance(SnapshotManager& manager); 67 68 // ISnapshotMergeStats overrides 69 bool Start() override; 70 void set_state(android::snapshot::UpdateState state, bool using_compression) override; 71 void set_cow_file_size(uint64_t cow_file_size) override; 72 uint64_t cow_file_size() override; 73 void set_total_cow_size_bytes(uint64_t bytes) override; 74 void set_estimated_cow_size_bytes(uint64_t bytes) override; 75 uint64_t total_cow_size_bytes() override; 76 uint64_t estimated_cow_size_bytes() override; 77 void set_boot_complete_time_ms(uint32_t ms) override; 78 uint32_t boot_complete_time_ms() override; 79 void set_boot_complete_to_merge_start_time_ms(uint32_t ms) override; 80 uint32_t boot_complete_to_merge_start_time_ms() override; 81 void set_merge_failure_code(MergeFailureCode code) override; 82 MergeFailureCode merge_failure_code() override; 83 void set_source_build_fingerprint(const std::string& fingerprint) override; 84 std::string source_build_fingerprint() override; 85 std::unique_ptr<Result> Finish() override; 86 bool WriteState() override; 87 88 private: 89 bool ReadState(); 90 bool DeleteState(); 91 SnapshotMergeStats(const std::string& path); 92 93 std::string path_; 94 SnapshotMergeReport report_; 95 // Time of the last successful Start() / Resume() call. 96 std::chrono::time_point<std::chrono::steady_clock> start_time_; 97 bool running_{false}; 98 }; 99 100 } // namespace snapshot 101 } // namespace android 102