Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 17 | #include "IdleMaint.h" |
Jaegeuk Kim | 31e962f | 2018-07-29 06:56:57 -0700 | [diff] [blame] | 18 | #include "FileDeviceUtils.h" |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 19 | #include "Utils.h" |
Eric Biggers | 019d516 | 2020-10-15 16:54:38 -0700 | [diff] [blame] | 20 | #include "VoldUtil.h" |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 21 | #include "VolumeManager.h" |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 22 | #include "model/PrivateVolume.h" |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 23 | |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 24 | #include <thread> |
Yifan Hong | 8f0d454 | 2021-01-13 17:10:47 -0800 | [diff] [blame] | 25 | #include <utility> |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 26 | |
Yifan Hong | e1e4945 | 2021-01-13 17:27:42 -0800 | [diff] [blame] | 27 | #include <aidl/android/hardware/health/storage/BnGarbageCollectCallback.h> |
| 28 | #include <aidl/android/hardware/health/storage/IStorage.h> |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 29 | #include <android-base/chrono_utils.h> |
| 30 | #include <android-base/file.h> |
Elliott Hughes | 7e128fb | 2015-12-04 15:50:53 -0800 | [diff] [blame] | 31 | #include <android-base/logging.h> |
Yifan Hong | 024a124 | 2018-08-10 13:50:46 -0700 | [diff] [blame] | 32 | #include <android-base/stringprintf.h> |
| 33 | #include <android-base/strings.h> |
Yifan Hong | e1e4945 | 2021-01-13 17:27:42 -0800 | [diff] [blame] | 34 | #include <android/binder_manager.h> |
Yifan Hong | 7a37c93 | 2018-09-19 10:28:16 -0700 | [diff] [blame] | 35 | #include <android/hardware/health/storage/1.0/IStorage.h> |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 36 | #include <fs_mgr.h> |
Yifan Hong | 024a124 | 2018-08-10 13:50:46 -0700 | [diff] [blame] | 37 | #include <private/android_filesystem_config.h> |
Tri Vo | 15bbe22 | 2019-06-21 12:21:48 -0700 | [diff] [blame] | 38 | #include <wakelock/wakelock.h> |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 39 | |
| 40 | #include <dirent.h> |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 41 | #include <fcntl.h> |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 42 | #include <sys/mount.h> |
| 43 | #include <sys/stat.h> |
| 44 | #include <sys/types.h> |
| 45 | #include <sys/wait.h> |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 46 | |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 47 | using android::base::Basename; |
| 48 | using android::base::ReadFileToString; |
| 49 | using android::base::Realpath; |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 50 | using android::base::StringPrintf; |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 51 | using android::base::Timer; |
| 52 | using android::base::WriteStringToFile; |
Yifan Hong | 024a124 | 2018-08-10 13:50:46 -0700 | [diff] [blame] | 53 | using android::hardware::Return; |
| 54 | using android::hardware::Void; |
Yifan Hong | e1e4945 | 2021-01-13 17:27:42 -0800 | [diff] [blame] | 55 | using AStorage = aidl::android::hardware::health::storage::IStorage; |
| 56 | using ABnGarbageCollectCallback = |
| 57 | aidl::android::hardware::health::storage::BnGarbageCollectCallback; |
| 58 | using AResult = aidl::android::hardware::health::storage::Result; |
Yifan Hong | 8f0d454 | 2021-01-13 17:10:47 -0800 | [diff] [blame] | 59 | using HStorage = android::hardware::health::storage::V1_0::IStorage; |
| 60 | using HGarbageCollectCallback = android::hardware::health::storage::V1_0::IGarbageCollectCallback; |
| 61 | using HResult = android::hardware::health::storage::V1_0::Result; |
Yifan Hong | e1e4945 | 2021-01-13 17:27:42 -0800 | [diff] [blame] | 62 | using std::string_literals::operator""s; |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 63 | |
| 64 | namespace android { |
| 65 | namespace vold { |
| 66 | |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 67 | enum class PathTypes { |
| 68 | kMountPoint = 1, |
| 69 | kBlkDevice, |
| 70 | }; |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 71 | |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 72 | enum class IdleMaintStats { |
| 73 | kStopped = 1, |
| 74 | kRunning, |
| 75 | kAbort, |
| 76 | }; |
| 77 | |
| 78 | static const char* kWakeLock = "IdleMaint"; |
| 79 | static const int DIRTY_SEGMENTS_THRESHOLD = 100; |
Jaegeuk Kim | eefc5ee | 2018-02-12 21:57:04 -0800 | [diff] [blame] | 80 | /* |
| 81 | * Timing policy: |
| 82 | * 1. F2FS_GC = 7 mins |
| 83 | * 2. Trim = 1 min |
| 84 | * 3. Dev GC = 2 mins |
| 85 | */ |
| 86 | static const int GC_TIMEOUT_SEC = 420; |
| 87 | static const int DEVGC_TIMEOUT_SEC = 120; |
Daeho Jeong | 999fceb | 2022-01-04 11:37:39 -0800 | [diff] [blame] | 88 | static const int KBYTES_IN_SEGMENT = 2048; |
| 89 | static const int MIN_GC_URGENT_SLEEP_TIME = 500; |
Daeho Jeong | 7c788fc | 2022-03-18 12:24:41 -0700 | [diff] [blame] | 90 | static const int ONE_MINUTE_IN_MS = 60000; |
Daeho Jeong | 999fceb | 2022-01-04 11:37:39 -0800 | [diff] [blame] | 91 | static const int GC_NORMAL_MODE = 0; |
Daeho Jeong | 5e87942 | 2022-03-15 21:42:55 -0700 | [diff] [blame] | 92 | static const int GC_URGENT_MID_MODE = 3; |
Daeho Jeong | 999fceb | 2022-01-04 11:37:39 -0800 | [diff] [blame] | 93 | |
| 94 | static int32_t previousSegmentWrite = 0; |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 95 | |
| 96 | static IdleMaintStats idle_maint_stat(IdleMaintStats::kStopped); |
| 97 | static std::condition_variable cv_abort, cv_stop; |
| 98 | static std::mutex cv_m; |
| 99 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 100 | static void addFromVolumeManager(std::list<std::string>* paths, PathTypes path_type) { |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 101 | VolumeManager* vm = VolumeManager::Instance(); |
| 102 | std::list<std::string> privateIds; |
| 103 | vm->listVolumes(VolumeBase::Type::kPrivate, privateIds); |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 104 | for (const auto& id : privateIds) { |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 105 | PrivateVolume* vol = static_cast<PrivateVolume*>(vm->findVolume(id).get()); |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 106 | if (vol != nullptr && vol->getState() == VolumeBase::State::kMounted) { |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 107 | if (path_type == PathTypes::kMountPoint) { |
| 108 | paths->push_back(vol->getPath()); |
| 109 | } else if (path_type == PathTypes::kBlkDevice) { |
| 110 | std::string gc_path; |
| 111 | const std::string& fs_type = vol->getFsType(); |
Jaegeuk Kim | 31e962f | 2018-07-29 06:56:57 -0700 | [diff] [blame] | 112 | if (fs_type == "f2fs" && (Realpath(vol->getRawDmDevPath(), &gc_path) || |
| 113 | Realpath(vol->getRawDevPath(), &gc_path))) { |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 114 | paths->push_back(std::string("/sys/fs/") + fs_type + "/" + Basename(gc_path)); |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 115 | } |
| 116 | } |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
Daeho Jeong | 999fceb | 2022-01-04 11:37:39 -0800 | [diff] [blame] | 121 | static void addFromFstab(std::list<std::string>* paths, PathTypes path_type, bool only_data_part) { |
Tom Cherry | 4c5bde2 | 2019-01-29 14:34:01 -0800 | [diff] [blame] | 122 | std::string previous_mount_point; |
Eric Biggers | 019d516 | 2020-10-15 16:54:38 -0700 | [diff] [blame] | 123 | for (const auto& entry : fstab_default) { |
Eric Biggers | 9a3dc8c | 2020-10-15 16:54:38 -0700 | [diff] [blame] | 124 | // Skip raw partitions and swap space. |
| 125 | if (entry.fs_type == "emmc" || entry.fs_type == "mtd" || entry.fs_type == "swap") { |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 126 | continue; |
| 127 | } |
Eric Biggers | 9a3dc8c | 2020-10-15 16:54:38 -0700 | [diff] [blame] | 128 | // Skip read-only filesystems and bind mounts. |
| 129 | if (entry.flags & (MS_RDONLY | MS_BIND)) { |
| 130 | continue; |
| 131 | } |
| 132 | // Skip anything without an underlying block device, e.g. virtiofs. |
| 133 | if (entry.blk_device[0] != '/') { |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 134 | continue; |
| 135 | } |
Tom Cherry | 4c5bde2 | 2019-01-29 14:34:01 -0800 | [diff] [blame] | 136 | if (entry.fs_mgr_flags.vold_managed) { |
| 137 | continue; // Should we trim fat32 filesystems? |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 138 | } |
Tom Cherry | 4c5bde2 | 2019-01-29 14:34:01 -0800 | [diff] [blame] | 139 | if (entry.fs_mgr_flags.no_trim) { |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 140 | continue; |
| 141 | } |
| 142 | |
Daeho Jeong | 999fceb | 2022-01-04 11:37:39 -0800 | [diff] [blame] | 143 | if (only_data_part && entry.mount_point != "/data") { |
| 144 | continue; |
| 145 | } |
| 146 | |
Tom Cherry | 4c5bde2 | 2019-01-29 14:34:01 -0800 | [diff] [blame] | 147 | // Skip the multi-type partitions, which are required to be following each other. |
| 148 | // See fs_mgr.c's mount_with_alternatives(). |
| 149 | if (entry.mount_point == previous_mount_point) { |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 150 | continue; |
| 151 | } |
| 152 | |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 153 | if (path_type == PathTypes::kMountPoint) { |
Tom Cherry | 4c5bde2 | 2019-01-29 14:34:01 -0800 | [diff] [blame] | 154 | paths->push_back(entry.mount_point); |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 155 | } else if (path_type == PathTypes::kBlkDevice) { |
Daeho Jeong | 999fceb | 2022-01-04 11:37:39 -0800 | [diff] [blame] | 156 | std::string path; |
Tom Cherry | 4c5bde2 | 2019-01-29 14:34:01 -0800 | [diff] [blame] | 157 | if (entry.fs_type == "f2fs" && |
Daeho Jeong | 999fceb | 2022-01-04 11:37:39 -0800 | [diff] [blame] | 158 | Realpath(android::vold::BlockDeviceForPath(entry.mount_point + "/"), &path)) { |
| 159 | paths->push_back("/sys/fs/" + entry.fs_type + "/" + Basename(path)); |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | |
Tom Cherry | 4c5bde2 | 2019-01-29 14:34:01 -0800 | [diff] [blame] | 163 | previous_mount_point = entry.mount_point; |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 164 | } |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 167 | void Trim(const android::sp<android::os::IVoldTaskListener>& listener) { |
Kalesh Singh | 98062dc | 2021-02-22 15:10:45 -0500 | [diff] [blame] | 168 | auto wl = android::wakelock::WakeLock::tryGet(kWakeLock); |
| 169 | if (!wl.has_value()) { |
| 170 | return; |
| 171 | } |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 172 | |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 173 | // Collect both fstab and vold volumes |
| 174 | std::list<std::string> paths; |
Daeho Jeong | 999fceb | 2022-01-04 11:37:39 -0800 | [diff] [blame] | 175 | addFromFstab(&paths, PathTypes::kMountPoint, false); |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 176 | addFromVolumeManager(&paths, PathTypes::kMountPoint); |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 177 | |
| 178 | for (const auto& path : paths) { |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 179 | LOG(DEBUG) << "Starting trim of " << path; |
| 180 | |
Jeff Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame] | 181 | android::os::PersistableBundle extras; |
| 182 | extras.putString(String16("path"), String16(path.c_str())); |
| 183 | |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 184 | int fd = open(path.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC | O_NOFOLLOW); |
| 185 | if (fd < 0) { |
| 186 | PLOG(WARNING) << "Failed to open " << path; |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 187 | if (listener) { |
| 188 | listener->onStatus(-1, extras); |
Jeff Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame] | 189 | } |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 190 | continue; |
| 191 | } |
| 192 | |
| 193 | struct fstrim_range range; |
| 194 | memset(&range, 0, sizeof(range)); |
| 195 | range.len = ULLONG_MAX; |
| 196 | |
| 197 | nsecs_t start = systemTime(SYSTEM_TIME_BOOTTIME); |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 198 | if (ioctl(fd, FITRIM, &range)) { |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 199 | PLOG(WARNING) << "Trim failed on " << path; |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 200 | if (listener) { |
| 201 | listener->onStatus(-1, extras); |
Jeff Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame] | 202 | } |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 203 | } else { |
Jeff Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame] | 204 | nsecs_t time = systemTime(SYSTEM_TIME_BOOTTIME) - start; |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 205 | LOG(INFO) << "Trimmed " << range.len << " bytes on " << path << " in " |
| 206 | << nanoseconds_to_milliseconds(time) << "ms"; |
Jeff Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame] | 207 | extras.putLong(String16("bytes"), range.len); |
| 208 | extras.putLong(String16("time"), time); |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 209 | if (listener) { |
| 210 | listener->onStatus(0, extras); |
Jeff Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame] | 211 | } |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 212 | } |
| 213 | close(fd); |
Jeff Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame] | 214 | } |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 215 | |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 216 | if (listener) { |
Jeff Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame] | 217 | android::os::PersistableBundle extras; |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 218 | listener->onFinished(0, extras); |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 223 | static bool waitForGc(const std::list<std::string>& paths) { |
| 224 | std::unique_lock<std::mutex> lk(cv_m, std::defer_lock); |
| 225 | bool stop = false, aborted = false; |
| 226 | Timer timer; |
| 227 | |
| 228 | while (!stop && !aborted) { |
| 229 | stop = true; |
| 230 | for (const auto& path : paths) { |
| 231 | std::string dirty_segments; |
| 232 | if (!ReadFileToString(path + "/dirty_segments", &dirty_segments)) { |
| 233 | PLOG(WARNING) << "Reading dirty_segments failed in " << path; |
| 234 | continue; |
| 235 | } |
| 236 | if (std::stoi(dirty_segments) > DIRTY_SEGMENTS_THRESHOLD) { |
| 237 | stop = false; |
| 238 | break; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | if (stop) break; |
| 243 | |
| 244 | if (timer.duration() >= std::chrono::seconds(GC_TIMEOUT_SEC)) { |
| 245 | LOG(WARNING) << "GC timeout"; |
| 246 | break; |
| 247 | } |
| 248 | |
| 249 | lk.lock(); |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 250 | aborted = |
| 251 | cv_abort.wait_for(lk, 10s, [] { return idle_maint_stat == IdleMaintStats::kAbort; }); |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 252 | lk.unlock(); |
| 253 | } |
| 254 | |
| 255 | return aborted; |
| 256 | } |
| 257 | |
| 258 | static int startGc(const std::list<std::string>& paths) { |
| 259 | for (const auto& path : paths) { |
| 260 | LOG(DEBUG) << "Start GC on " << path; |
| 261 | if (!WriteStringToFile("1", path + "/gc_urgent")) { |
| 262 | PLOG(WARNING) << "Start GC failed on " << path; |
| 263 | } |
| 264 | } |
| 265 | return android::OK; |
| 266 | } |
| 267 | |
| 268 | static int stopGc(const std::list<std::string>& paths) { |
| 269 | for (const auto& path : paths) { |
| 270 | LOG(DEBUG) << "Stop GC on " << path; |
| 271 | if (!WriteStringToFile("0", path + "/gc_urgent")) { |
| 272 | PLOG(WARNING) << "Stop GC failed on " << path; |
| 273 | } |
| 274 | } |
| 275 | return android::OK; |
| 276 | } |
| 277 | |
Daeho Jeong | 999fceb | 2022-01-04 11:37:39 -0800 | [diff] [blame] | 278 | static std::string getDevSysfsPath() { |
Eric Biggers | 019d516 | 2020-10-15 16:54:38 -0700 | [diff] [blame] | 279 | for (const auto& entry : fstab_default) { |
Tom Cherry | 4c5bde2 | 2019-01-29 14:34:01 -0800 | [diff] [blame] | 280 | if (!entry.sysfs_path.empty()) { |
Daeho Jeong | 999fceb | 2022-01-04 11:37:39 -0800 | [diff] [blame] | 281 | return entry.sysfs_path; |
Jaegeuk Kim | eefc5ee | 2018-02-12 21:57:04 -0800 | [diff] [blame] | 282 | } |
| 283 | } |
Daeho Jeong | 999fceb | 2022-01-04 11:37:39 -0800 | [diff] [blame] | 284 | LOG(WARNING) << "Cannot find dev sysfs path"; |
| 285 | return ""; |
| 286 | } |
Tom Cherry | 4c5bde2 | 2019-01-29 14:34:01 -0800 | [diff] [blame] | 287 | |
Daeho Jeong | 999fceb | 2022-01-04 11:37:39 -0800 | [diff] [blame] | 288 | static void runDevGcFstab(void) { |
| 289 | std::string path = getDevSysfsPath(); |
Tom Cherry | 4c5bde2 | 2019-01-29 14:34:01 -0800 | [diff] [blame] | 290 | if (path.empty()) { |
Jaegeuk Kim | eefc5ee | 2018-02-12 21:57:04 -0800 | [diff] [blame] | 291 | return; |
| 292 | } |
| 293 | |
Jaegeuk Kim | eefc5ee | 2018-02-12 21:57:04 -0800 | [diff] [blame] | 294 | path = path + "/manual_gc"; |
| 295 | Timer timer; |
| 296 | |
| 297 | LOG(DEBUG) << "Start Dev GC on " << path; |
| 298 | while (1) { |
| 299 | std::string require; |
| 300 | if (!ReadFileToString(path, &require)) { |
| 301 | PLOG(WARNING) << "Reading manual_gc failed in " << path; |
| 302 | break; |
| 303 | } |
Yifan Hong | 024a124 | 2018-08-10 13:50:46 -0700 | [diff] [blame] | 304 | require = android::base::Trim(require); |
Jaegeuk Kim | eefc5ee | 2018-02-12 21:57:04 -0800 | [diff] [blame] | 305 | if (require == "" || require == "off" || require == "disabled") { |
| 306 | LOG(DEBUG) << "No more to do Dev GC"; |
| 307 | break; |
| 308 | } |
| 309 | |
| 310 | LOG(DEBUG) << "Trigger Dev GC on " << path; |
| 311 | if (!WriteStringToFile("1", path)) { |
| 312 | PLOG(WARNING) << "Start Dev GC failed on " << path; |
| 313 | break; |
| 314 | } |
| 315 | |
| 316 | if (timer.duration() >= std::chrono::seconds(DEVGC_TIMEOUT_SEC)) { |
| 317 | LOG(WARNING) << "Dev GC timeout"; |
| 318 | break; |
| 319 | } |
| 320 | sleep(2); |
| 321 | } |
| 322 | LOG(DEBUG) << "Stop Dev GC on " << path; |
| 323 | if (!WriteStringToFile("0", path)) { |
| 324 | PLOG(WARNING) << "Stop Dev GC failed on " << path; |
| 325 | } |
| 326 | return; |
| 327 | } |
| 328 | |
Yifan Hong | e1e4945 | 2021-01-13 17:27:42 -0800 | [diff] [blame] | 329 | enum class IDL { HIDL, AIDL }; |
Yifan Hong | 8f0d454 | 2021-01-13 17:10:47 -0800 | [diff] [blame] | 330 | std::ostream& operator<<(std::ostream& os, IDL idl) { |
Yifan Hong | e1e4945 | 2021-01-13 17:27:42 -0800 | [diff] [blame] | 331 | return os << (idl == IDL::HIDL ? "HIDL" : "AIDL"); |
Yifan Hong | 8f0d454 | 2021-01-13 17:10:47 -0800 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | template <IDL idl, typename Result> |
| 335 | class GcCallbackImpl { |
| 336 | protected: |
| 337 | void onFinishInternal(Result result) { |
Yifan Hong | 024a124 | 2018-08-10 13:50:46 -0700 | [diff] [blame] | 338 | std::unique_lock<std::mutex> lock(mMutex); |
| 339 | mFinished = true; |
| 340 | mResult = result; |
| 341 | lock.unlock(); |
| 342 | mCv.notify_all(); |
Yifan Hong | 024a124 | 2018-08-10 13:50:46 -0700 | [diff] [blame] | 343 | } |
Yifan Hong | 8f0d454 | 2021-01-13 17:10:47 -0800 | [diff] [blame] | 344 | |
| 345 | public: |
Yifan Hong | 024a124 | 2018-08-10 13:50:46 -0700 | [diff] [blame] | 346 | void wait(uint64_t seconds) { |
| 347 | std::unique_lock<std::mutex> lock(mMutex); |
| 348 | mCv.wait_for(lock, std::chrono::seconds(seconds), [this] { return mFinished; }); |
| 349 | |
| 350 | if (!mFinished) { |
Yifan Hong | 8f0d454 | 2021-01-13 17:10:47 -0800 | [diff] [blame] | 351 | LOG(WARNING) << "Dev GC on " << idl << " HAL timeout"; |
Yifan Hong | 024a124 | 2018-08-10 13:50:46 -0700 | [diff] [blame] | 352 | } else if (mResult != Result::SUCCESS) { |
Yifan Hong | 8f0d454 | 2021-01-13 17:10:47 -0800 | [diff] [blame] | 353 | LOG(WARNING) << "Dev GC on " << idl << " HAL failed with " << toString(mResult); |
Yifan Hong | 024a124 | 2018-08-10 13:50:46 -0700 | [diff] [blame] | 354 | } else { |
Yifan Hong | 8f0d454 | 2021-01-13 17:10:47 -0800 | [diff] [blame] | 355 | LOG(INFO) << "Dev GC on " << idl << " HAL successful"; |
Yifan Hong | 024a124 | 2018-08-10 13:50:46 -0700 | [diff] [blame] | 356 | } |
| 357 | } |
| 358 | |
| 359 | private: |
| 360 | std::mutex mMutex; |
| 361 | std::condition_variable mCv; |
| 362 | bool mFinished{false}; |
| 363 | Result mResult{Result::UNKNOWN_ERROR}; |
| 364 | }; |
| 365 | |
Yifan Hong | e1e4945 | 2021-01-13 17:27:42 -0800 | [diff] [blame] | 366 | class AGcCallbackImpl : public ABnGarbageCollectCallback, |
| 367 | public GcCallbackImpl<IDL::AIDL, AResult> { |
| 368 | ndk::ScopedAStatus onFinish(AResult result) override { |
| 369 | onFinishInternal(result); |
| 370 | return ndk::ScopedAStatus::ok(); |
| 371 | } |
| 372 | }; |
| 373 | |
Yifan Hong | 8f0d454 | 2021-01-13 17:10:47 -0800 | [diff] [blame] | 374 | class HGcCallbackImpl : public HGarbageCollectCallback, public GcCallbackImpl<IDL::HIDL, HResult> { |
| 375 | Return<void> onFinish(HResult result) override { |
| 376 | onFinishInternal(result); |
| 377 | return Void(); |
| 378 | } |
| 379 | }; |
| 380 | |
| 381 | template <IDL idl, typename Service, typename GcCallbackImpl, typename GetDescription> |
| 382 | static void runDevGcOnHal(Service service, GcCallbackImpl cb, GetDescription get_description) { |
| 383 | LOG(DEBUG) << "Start Dev GC on " << idl << " HAL"; |
Yifan Hong | 024a124 | 2018-08-10 13:50:46 -0700 | [diff] [blame] | 384 | auto ret = service->garbageCollect(DEVGC_TIMEOUT_SEC, cb); |
| 385 | if (!ret.isOk()) { |
Yifan Hong | 8f0d454 | 2021-01-13 17:10:47 -0800 | [diff] [blame] | 386 | LOG(WARNING) << "Cannot start Dev GC on " << idl |
| 387 | << " HAL: " << std::invoke(get_description, ret); |
Yifan Hong | 024a124 | 2018-08-10 13:50:46 -0700 | [diff] [blame] | 388 | return; |
| 389 | } |
| 390 | cb->wait(DEVGC_TIMEOUT_SEC); |
| 391 | } |
| 392 | |
| 393 | static void runDevGc(void) { |
Yifan Hong | e1e4945 | 2021-01-13 17:27:42 -0800 | [diff] [blame] | 394 | auto aidl_service_name = AStorage::descriptor + "/default"s; |
| 395 | if (AServiceManager_isDeclared(aidl_service_name.c_str())) { |
| 396 | ndk::SpAIBinder binder(AServiceManager_waitForService(aidl_service_name.c_str())); |
| 397 | if (binder.get() != nullptr) { |
| 398 | std::shared_ptr<AStorage> aidl_service = AStorage::fromBinder(binder); |
| 399 | if (aidl_service != nullptr) { |
| 400 | runDevGcOnHal<IDL::AIDL>(aidl_service, ndk::SharedRefBase::make<AGcCallbackImpl>(), |
| 401 | &ndk::ScopedAStatus::getDescription); |
| 402 | return; |
| 403 | } |
| 404 | } |
| 405 | LOG(WARNING) << "Device declares " << aidl_service_name |
| 406 | << " but it is not running, skip dev GC on AIDL HAL"; |
| 407 | return; |
| 408 | } |
Yifan Hong | 8f0d454 | 2021-01-13 17:10:47 -0800 | [diff] [blame] | 409 | auto hidl_service = HStorage::getService(); |
| 410 | if (hidl_service != nullptr) { |
| 411 | runDevGcOnHal<IDL::HIDL>(hidl_service, sp<HGcCallbackImpl>(new HGcCallbackImpl()), |
| 412 | &Return<void>::description); |
| 413 | return; |
Yifan Hong | 024a124 | 2018-08-10 13:50:46 -0700 | [diff] [blame] | 414 | } |
Yifan Hong | 8f0d454 | 2021-01-13 17:10:47 -0800 | [diff] [blame] | 415 | // fallback to legacy code path |
| 416 | runDevGcFstab(); |
Yifan Hong | 024a124 | 2018-08-10 13:50:46 -0700 | [diff] [blame] | 417 | } |
| 418 | |
Daeho Jeong | 999fceb | 2022-01-04 11:37:39 -0800 | [diff] [blame] | 419 | int RunIdleMaint(bool needGC, const android::sp<android::os::IVoldTaskListener>& listener) { |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 420 | std::unique_lock<std::mutex> lk(cv_m); |
Daeho Jeong | 999fceb | 2022-01-04 11:37:39 -0800 | [diff] [blame] | 421 | bool gc_aborted = false; |
| 422 | |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 423 | if (idle_maint_stat != IdleMaintStats::kStopped) { |
| 424 | LOG(DEBUG) << "idle maintenance is already running"; |
| 425 | if (listener) { |
| 426 | android::os::PersistableBundle extras; |
| 427 | listener->onFinished(0, extras); |
| 428 | } |
| 429 | return android::OK; |
| 430 | } |
| 431 | idle_maint_stat = IdleMaintStats::kRunning; |
| 432 | lk.unlock(); |
| 433 | |
| 434 | LOG(DEBUG) << "idle maintenance started"; |
| 435 | |
Kalesh Singh | 98062dc | 2021-02-22 15:10:45 -0500 | [diff] [blame] | 436 | auto wl = android::wakelock::WakeLock::tryGet(kWakeLock); |
| 437 | if (!wl.has_value()) { |
| 438 | return android::UNEXPECTED_NULL; |
| 439 | } |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 440 | |
Daeho Jeong | 999fceb | 2022-01-04 11:37:39 -0800 | [diff] [blame] | 441 | if (needGC) { |
| 442 | std::list<std::string> paths; |
| 443 | addFromFstab(&paths, PathTypes::kBlkDevice, false); |
| 444 | addFromVolumeManager(&paths, PathTypes::kBlkDevice); |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 445 | |
Daeho Jeong | 999fceb | 2022-01-04 11:37:39 -0800 | [diff] [blame] | 446 | startGc(paths); |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 447 | |
Daeho Jeong | 999fceb | 2022-01-04 11:37:39 -0800 | [diff] [blame] | 448 | gc_aborted = waitForGc(paths); |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 449 | |
Daeho Jeong | 999fceb | 2022-01-04 11:37:39 -0800 | [diff] [blame] | 450 | stopGc(paths); |
| 451 | } |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 452 | |
| 453 | lk.lock(); |
| 454 | idle_maint_stat = IdleMaintStats::kStopped; |
| 455 | lk.unlock(); |
| 456 | |
| 457 | cv_stop.notify_one(); |
| 458 | |
| 459 | if (!gc_aborted) { |
| 460 | Trim(nullptr); |
Jaegeuk Kim | eefc5ee | 2018-02-12 21:57:04 -0800 | [diff] [blame] | 461 | runDevGc(); |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | if (listener) { |
| 465 | android::os::PersistableBundle extras; |
| 466 | listener->onFinished(0, extras); |
| 467 | } |
| 468 | |
| 469 | LOG(DEBUG) << "idle maintenance completed"; |
| 470 | |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 471 | return android::OK; |
| 472 | } |
| 473 | |
| 474 | int AbortIdleMaint(const android::sp<android::os::IVoldTaskListener>& listener) { |
Kalesh Singh | 98062dc | 2021-02-22 15:10:45 -0500 | [diff] [blame] | 475 | auto wl = android::wakelock::WakeLock::tryGet(kWakeLock); |
| 476 | if (!wl.has_value()) { |
| 477 | return android::UNEXPECTED_NULL; |
| 478 | } |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 479 | |
| 480 | std::unique_lock<std::mutex> lk(cv_m); |
| 481 | if (idle_maint_stat != IdleMaintStats::kStopped) { |
| 482 | idle_maint_stat = IdleMaintStats::kAbort; |
| 483 | lk.unlock(); |
| 484 | cv_abort.notify_one(); |
| 485 | lk.lock(); |
| 486 | LOG(DEBUG) << "aborting idle maintenance"; |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 487 | cv_stop.wait(lk, [] { return idle_maint_stat == IdleMaintStats::kStopped; }); |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 488 | } |
| 489 | lk.unlock(); |
| 490 | |
| 491 | if (listener) { |
| 492 | android::os::PersistableBundle extras; |
| 493 | listener->onFinished(0, extras); |
| 494 | } |
| 495 | |
Jin Qian | a370c14 | 2017-10-17 15:41:45 -0700 | [diff] [blame] | 496 | LOG(DEBUG) << "idle maintenance stopped"; |
| 497 | |
| 498 | return android::OK; |
| 499 | } |
| 500 | |
Daeho Jeong | 999fceb | 2022-01-04 11:37:39 -0800 | [diff] [blame] | 501 | int getLifeTime(const std::string& path) { |
| 502 | std::string result; |
| 503 | |
| 504 | if (!ReadFileToString(path, &result)) { |
| 505 | PLOG(WARNING) << "Reading lifetime estimation failed for " << path; |
| 506 | return -1; |
| 507 | } |
| 508 | return std::stoi(result, 0, 16); |
| 509 | } |
| 510 | |
| 511 | int32_t GetStorageLifeTime() { |
| 512 | std::string path = getDevSysfsPath(); |
| 513 | if (path.empty()) { |
| 514 | return -1; |
| 515 | } |
| 516 | |
| 517 | std::string lifeTimeBasePath = path + "/health_descriptor/life_time_estimation_"; |
| 518 | |
| 519 | int32_t lifeTime = getLifeTime(lifeTimeBasePath + "c"); |
| 520 | if (lifeTime != -1) { |
| 521 | return lifeTime; |
| 522 | } |
| 523 | |
| 524 | int32_t lifeTimeA = getLifeTime(lifeTimeBasePath + "a"); |
| 525 | int32_t lifeTimeB = getLifeTime(lifeTimeBasePath + "b"); |
| 526 | lifeTime = std::max(lifeTimeA, lifeTimeB); |
| 527 | if (lifeTime != -1) { |
| 528 | return lifeTime == 0 ? -1 : lifeTime * 10; |
| 529 | } |
| 530 | return -1; |
| 531 | } |
| 532 | |
| 533 | void SetGCUrgentPace(int32_t neededSegments, int32_t minSegmentThreshold, float dirtyReclaimRate, |
Daeho Jeong | 7c788fc | 2022-03-18 12:24:41 -0700 | [diff] [blame] | 534 | float reclaimWeight, int32_t gcPeriod) { |
Daeho Jeong | 999fceb | 2022-01-04 11:37:39 -0800 | [diff] [blame] | 535 | std::list<std::string> paths; |
| 536 | bool needGC = true; |
Daeho Jeong | 0b5f397 | 2022-03-18 21:30:47 -0700 | [diff] [blame] | 537 | int32_t sleepTime; |
Daeho Jeong | 999fceb | 2022-01-04 11:37:39 -0800 | [diff] [blame] | 538 | |
| 539 | addFromFstab(&paths, PathTypes::kBlkDevice, true); |
| 540 | if (paths.empty()) { |
| 541 | LOG(WARNING) << "There is no valid blk device path for data partition"; |
| 542 | return; |
| 543 | } |
| 544 | |
| 545 | std::string f2fsSysfsPath = paths.front(); |
| 546 | std::string freeSegmentsPath = f2fsSysfsPath + "/free_segments"; |
| 547 | std::string dirtySegmentsPath = f2fsSysfsPath + "/dirty_segments"; |
| 548 | std::string gcSleepTimePath = f2fsSysfsPath + "/gc_urgent_sleep_time"; |
| 549 | std::string gcUrgentModePath = f2fsSysfsPath + "/gc_urgent"; |
Daeho Jeong | 3ccdeb3 | 2022-03-25 10:02:57 -0700 | [diff] [blame] | 550 | std::string ovpSegmentsPath = f2fsSysfsPath + "/ovp_segments"; |
| 551 | std::string reservedBlocksPath = f2fsSysfsPath + "/reserved_blocks"; |
| 552 | std::string freeSegmentsStr, dirtySegmentsStr, ovpSegmentsStr, reservedBlocksStr; |
Daeho Jeong | 999fceb | 2022-01-04 11:37:39 -0800 | [diff] [blame] | 553 | |
| 554 | if (!ReadFileToString(freeSegmentsPath, &freeSegmentsStr)) { |
| 555 | PLOG(WARNING) << "Reading failed in " << freeSegmentsPath; |
| 556 | return; |
| 557 | } |
| 558 | |
| 559 | if (!ReadFileToString(dirtySegmentsPath, &dirtySegmentsStr)) { |
| 560 | PLOG(WARNING) << "Reading failed in " << dirtySegmentsPath; |
| 561 | return; |
| 562 | } |
| 563 | |
Daeho Jeong | 3ccdeb3 | 2022-03-25 10:02:57 -0700 | [diff] [blame] | 564 | if (!ReadFileToString(ovpSegmentsPath, &ovpSegmentsStr)) { |
| 565 | PLOG(WARNING) << "Reading failed in " << ovpSegmentsPath; |
| 566 | return; |
| 567 | } |
| 568 | |
| 569 | if (!ReadFileToString(reservedBlocksPath, &reservedBlocksStr)) { |
| 570 | PLOG(WARNING) << "Reading failed in " << reservedBlocksPath; |
| 571 | return; |
| 572 | } |
| 573 | |
Daeho Jeong | 999fceb | 2022-01-04 11:37:39 -0800 | [diff] [blame] | 574 | int32_t freeSegments = std::stoi(freeSegmentsStr); |
| 575 | int32_t dirtySegments = std::stoi(dirtySegmentsStr); |
Daeho Jeong | 3ccdeb3 | 2022-03-25 10:02:57 -0700 | [diff] [blame] | 576 | int32_t reservedBlocks = std::stoi(ovpSegmentsStr) + std::stoi(reservedBlocksStr); |
Daeho Jeong | 999fceb | 2022-01-04 11:37:39 -0800 | [diff] [blame] | 577 | |
Daeho Jeong | 3ccdeb3 | 2022-03-25 10:02:57 -0700 | [diff] [blame] | 578 | freeSegments = freeSegments > reservedBlocks ? freeSegments - reservedBlocks : 0; |
Daeho Jeong | 999fceb | 2022-01-04 11:37:39 -0800 | [diff] [blame] | 579 | neededSegments *= reclaimWeight; |
| 580 | if (freeSegments >= neededSegments) { |
| 581 | LOG(INFO) << "Enough free segments: " << freeSegments |
| 582 | << ", needed segments: " << neededSegments; |
| 583 | needGC = false; |
| 584 | } else if (freeSegments + dirtySegments < minSegmentThreshold) { |
| 585 | LOG(INFO) << "The sum of free segments: " << freeSegments |
| 586 | << ", dirty segments: " << dirtySegments << " is under " << minSegmentThreshold; |
| 587 | needGC = false; |
Daeho Jeong | 0b5f397 | 2022-03-18 21:30:47 -0700 | [diff] [blame] | 588 | } else { |
| 589 | neededSegments -= freeSegments; |
| 590 | neededSegments = std::min(neededSegments, (int32_t)(dirtySegments * dirtyReclaimRate)); |
| 591 | if (neededSegments == 0) { |
| 592 | LOG(INFO) << "Low dirty segments: " << dirtySegments; |
| 593 | needGC = false; |
| 594 | } else { |
| 595 | sleepTime = gcPeriod * ONE_MINUTE_IN_MS / neededSegments; |
| 596 | if (sleepTime < MIN_GC_URGENT_SLEEP_TIME) { |
| 597 | sleepTime = MIN_GC_URGENT_SLEEP_TIME; |
| 598 | } |
| 599 | } |
Daeho Jeong | 999fceb | 2022-01-04 11:37:39 -0800 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | if (!needGC) { |
| 603 | if (!WriteStringToFile(std::to_string(GC_NORMAL_MODE), gcUrgentModePath)) { |
| 604 | PLOG(WARNING) << "Writing failed in " << gcUrgentModePath; |
| 605 | } |
| 606 | return; |
| 607 | } |
| 608 | |
Daeho Jeong | 999fceb | 2022-01-04 11:37:39 -0800 | [diff] [blame] | 609 | if (!WriteStringToFile(std::to_string(sleepTime), gcSleepTimePath)) { |
| 610 | PLOG(WARNING) << "Writing failed in " << gcSleepTimePath; |
| 611 | return; |
| 612 | } |
| 613 | |
Daeho Jeong | 5e87942 | 2022-03-15 21:42:55 -0700 | [diff] [blame] | 614 | if (!WriteStringToFile(std::to_string(GC_URGENT_MID_MODE), gcUrgentModePath)) { |
Daeho Jeong | 999fceb | 2022-01-04 11:37:39 -0800 | [diff] [blame] | 615 | PLOG(WARNING) << "Writing failed in " << gcUrgentModePath; |
| 616 | return; |
| 617 | } |
| 618 | |
| 619 | LOG(INFO) << "Successfully set gc urgent mode: " |
| 620 | << "free segments: " << freeSegments << ", reclaim target: " << neededSegments |
| 621 | << ", sleep time: " << sleepTime; |
| 622 | } |
| 623 | |
| 624 | static int32_t getLifeTimeWrite() { |
| 625 | std::list<std::string> paths; |
| 626 | addFromFstab(&paths, PathTypes::kBlkDevice, true); |
| 627 | if (paths.empty()) { |
| 628 | LOG(WARNING) << "There is no valid blk device path for data partition"; |
| 629 | return -1; |
| 630 | } |
| 631 | |
| 632 | std::string writeKbytesPath = paths.front() + "/lifetime_write_kbytes"; |
| 633 | std::string writeKbytesStr; |
| 634 | if (!ReadFileToString(writeKbytesPath, &writeKbytesStr)) { |
| 635 | PLOG(WARNING) << "Reading failed in " << writeKbytesPath; |
| 636 | return -1; |
| 637 | } |
| 638 | |
| 639 | long long writeBytes = std::stoll(writeKbytesStr); |
| 640 | return writeBytes / KBYTES_IN_SEGMENT; |
| 641 | } |
| 642 | |
| 643 | void RefreshLatestWrite() { |
| 644 | int32_t segmentWrite = getLifeTimeWrite(); |
| 645 | if (segmentWrite != -1) { |
| 646 | previousSegmentWrite = segmentWrite; |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | int32_t GetWriteAmount() { |
| 651 | int32_t currentSegmentWrite = getLifeTimeWrite(); |
| 652 | if (currentSegmentWrite == -1) { |
| 653 | return -1; |
| 654 | } |
| 655 | |
| 656 | int32_t writeAmount = currentSegmentWrite - previousSegmentWrite; |
| 657 | previousSegmentWrite = currentSegmentWrite; |
| 658 | return writeAmount; |
| 659 | } |
| 660 | |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 661 | } // namespace vold |
| 662 | } // namespace android |