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 #include <libsnapshot/snapshot_stats.h>
16 
17 #include <sstream>
18 
19 #include <android-base/file.h>
20 #include <android-base/logging.h>
21 #include "utility.h"
22 
23 namespace android {
24 namespace snapshot {
25 
GetInstance(SnapshotManager & parent)26 SnapshotMergeStats* SnapshotMergeStats::GetInstance(SnapshotManager& parent) {
27     static SnapshotMergeStats g_instance(parent.GetMergeStateFilePath());
28     CHECK(g_instance.path_ == parent.GetMergeStateFilePath());
29     return &g_instance;
30 }
31 
SnapshotMergeStats(const std::string & path)32 SnapshotMergeStats::SnapshotMergeStats(const std::string& path) : path_(path), running_(false) {}
33 
ReadState()34 bool SnapshotMergeStats::ReadState() {
35     std::string contents;
36     if (!android::base::ReadFileToString(path_, &contents)) {
37         PLOG(INFO) << "Read merge statistics file failed";
38         return false;
39     }
40     if (!report_.ParseFromString(contents)) {
41         LOG(ERROR) << "Unable to parse merge statistics file as SnapshotMergeReport";
42         return false;
43     }
44     return true;
45 }
46 
WriteState()47 bool SnapshotMergeStats::WriteState() {
48     std::string contents;
49     if (!report_.SerializeToString(&contents)) {
50         LOG(ERROR) << "Unable to serialize SnapshotMergeStats.";
51         return false;
52     }
53     if (!WriteStringToFileAtomic(contents, path_)) {
54         PLOG(ERROR) << "Could not write to merge statistics file";
55         return false;
56     }
57     return true;
58 }
59 
DeleteState()60 bool SnapshotMergeStats::DeleteState() {
61     std::string error;
62     if (!android::base::RemoveFileIfExists(path_, &error)) {
63         LOG(ERROR) << "Failed to remove merge statistics file " << path_ << ": " << error;
64         return false;
65     }
66     return true;
67 }
68 
Start()69 bool SnapshotMergeStats::Start() {
70     if (running_) {
71         LOG(ERROR) << "SnapshotMergeStats running_ == " << running_;
72         return false;
73     }
74     running_ = true;
75 
76     start_time_ = std::chrono::steady_clock::now();
77     if (ReadState()) {
78         report_.set_resume_count(report_.resume_count() + 1);
79     } else {
80         report_.set_resume_count(0);
81         report_.set_state(UpdateState::None);
82     }
83 
84     return WriteState();
85 }
86 
set_state(android::snapshot::UpdateState state)87 void SnapshotMergeStats::set_state(android::snapshot::UpdateState state) {
88     report_.set_state(state);
89 }
90 
set_cow_file_size(uint64_t cow_file_size)91 void SnapshotMergeStats::set_cow_file_size(uint64_t cow_file_size) {
92     report_.set_cow_file_size(cow_file_size);
93     WriteState();
94 }
95 
cow_file_size()96 uint64_t SnapshotMergeStats::cow_file_size() {
97     return report_.cow_file_size();
98 }
99 
100 class SnapshotMergeStatsResultImpl : public SnapshotMergeStats::Result {
101   public:
SnapshotMergeStatsResultImpl(const SnapshotMergeReport & report,std::chrono::steady_clock::duration merge_time)102     SnapshotMergeStatsResultImpl(const SnapshotMergeReport& report,
103                                  std::chrono::steady_clock::duration merge_time)
104         : report_(report), merge_time_(merge_time) {}
report() const105     const SnapshotMergeReport& report() const override { return report_; }
merge_time() const106     std::chrono::steady_clock::duration merge_time() const override { return merge_time_; }
107 
108   private:
109     SnapshotMergeReport report_;
110     std::chrono::steady_clock::duration merge_time_;
111 };
112 
Finish()113 std::unique_ptr<SnapshotMergeStats::Result> SnapshotMergeStats::Finish() {
114     if (!running_) {
115         LOG(ERROR) << "SnapshotMergeStats running_ == " << running_;
116         return nullptr;
117     }
118     running_ = false;
119 
120     auto result = std::make_unique<SnapshotMergeStatsResultImpl>(
121             report_, std::chrono::steady_clock::now() - start_time_);
122 
123     // We still want to report result if state is not deleted. Just leave
124     // it there and move on. A side effect is that it may be reported over and
125     // over again in the future, but there is nothing we can do.
126     (void)DeleteState();
127 
128     return result;
129 }
130 
131 }  // namespace snapshot
132 }  // namespace android
133