2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "IdleMaint.h"
19 #include "VolumeManager.h"
20 #include "model/PrivateVolume.h"
24 #include <android-base/chrono_utils.h>
25 #include <android-base/file.h>
26 #include <android-base/stringprintf.h>
27 #include <android-base/logging.h>
29 #include <private/android_filesystem_config.h>
30 #include <hardware_legacy/power.h>
33 #include <sys/mount.h>
35 #include <sys/types.h>
39 using android::base::Basename;
40 using android::base::ReadFileToString;
41 using android::base::Realpath;
42 using android::base::StringPrintf;
43 using android::base::Timer;
44 using android::base::WriteStringToFile;
49 enum class PathTypes {
54 enum class IdleMaintStats {
60 static const char* kWakeLock = "IdleMaint";
61 static const int DIRTY_SEGMENTS_THRESHOLD = 100;
68 static const int GC_TIMEOUT_SEC = 420;
69 static const int DEVGC_TIMEOUT_SEC = 120;
71 static IdleMaintStats idle_maint_stat(IdleMaintStats::kStopped);
72 static std::condition_variable cv_abort, cv_stop;
73 static std::mutex cv_m;
75 static void addFromVolumeManager(std::list<std::string>* paths,
76 PathTypes path_type) {
77 VolumeManager* vm = VolumeManager::Instance();
78 std::list<std::string> privateIds;
79 vm->listVolumes(VolumeBase::Type::kPrivate, privateIds);
80 for (const auto& id : privateIds) {
81 PrivateVolume* vol = static_cast<PrivateVolume*>(vm->findVolume(id).get());
82 if (vol != nullptr && vol->getState() == VolumeBase::State::kMounted) {
83 if (path_type == PathTypes::kMountPoint) {
84 paths->push_back(vol->getPath());
85 } else if (path_type == PathTypes::kBlkDevice) {
87 const std::string& fs_type = vol->getFsType();
88 if (fs_type == "f2fs" &&
89 Realpath(vol->getRawDevPath(), &gc_path)) {
90 paths->push_back(std::string("/sys/fs/") + fs_type +
91 "/" + Basename(gc_path));
99 static void addFromFstab(std::list<std::string>* paths, PathTypes path_type) {
100 std::unique_ptr<fstab, decltype(&fs_mgr_free_fstab)> fstab(fs_mgr_read_fstab_default(),
102 struct fstab_rec *prev_rec = NULL;
104 for (int i = 0; i < fstab->num_entries; i++) {
105 auto fs_type = std::string(fstab->recs[i].fs_type);
106 /* Skip raw partitions */
107 if (fs_type == "emmc" || fs_type == "mtd") {
110 /* Skip read-only filesystems */
111 if (fstab->recs[i].flags & MS_RDONLY) {
114 if (fs_mgr_is_voldmanaged(&fstab->recs[i])) {
115 continue; /* Should we trim fat32 filesystems? */
117 if (fs_mgr_is_notrim(&fstab->recs[i])) {
121 /* Skip the multi-type partitions, which are required to be following each other.
122 * See fs_mgr.c's mount_with_alternatives().
124 if (prev_rec && !strcmp(prev_rec->mount_point, fstab->recs[i].mount_point)) {
128 if (path_type == PathTypes::kMountPoint) {
129 paths->push_back(fstab->recs[i].mount_point);
130 } else if (path_type == PathTypes::kBlkDevice) {
132 if (std::string(fstab->recs[i].fs_type) == "f2fs" &&
133 Realpath(fstab->recs[i].blk_device, &gc_path)) {
134 paths->push_back(std::string("/sys/fs/") + fstab->recs[i].fs_type +
135 "/" + Basename(gc_path));
139 prev_rec = &fstab->recs[i];
143 void Trim(const android::sp<android::os::IVoldTaskListener>& listener) {
144 acquire_wake_lock(PARTIAL_WAKE_LOCK, kWakeLock);
146 // Collect both fstab and vold volumes
147 std::list<std::string> paths;
148 addFromFstab(&paths, PathTypes::kMountPoint);
149 addFromVolumeManager(&paths, PathTypes::kMountPoint);
151 for (const auto& path : paths) {
152 LOG(DEBUG) << "Starting trim of " << path;
154 android::os::PersistableBundle extras;
155 extras.putString(String16("path"), String16(path.c_str()));
157 int fd = open(path.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC | O_NOFOLLOW);
159 PLOG(WARNING) << "Failed to open " << path;
161 listener->onStatus(-1, extras);
166 struct fstrim_range range;
167 memset(&range, 0, sizeof(range));
168 range.len = ULLONG_MAX;
170 nsecs_t start = systemTime(SYSTEM_TIME_BOOTTIME);
171 if (ioctl(fd, FITRIM, &range)) {
172 PLOG(WARNING) << "Trim failed on " << path;
174 listener->onStatus(-1, extras);
177 nsecs_t time = systemTime(SYSTEM_TIME_BOOTTIME) - start;
178 LOG(INFO) << "Trimmed " << range.len << " bytes on " << path
179 << " in " << nanoseconds_to_milliseconds(time) << "ms";
180 extras.putLong(String16("bytes"), range.len);
181 extras.putLong(String16("time"), time);
183 listener->onStatus(0, extras);
190 android::os::PersistableBundle extras;
191 listener->onFinished(0, extras);
194 release_wake_lock(kWakeLock);
197 static bool waitForGc(const std::list<std::string>& paths) {
198 std::unique_lock<std::mutex> lk(cv_m, std::defer_lock);
199 bool stop = false, aborted = false;
202 while (!stop && !aborted) {
204 for (const auto& path : paths) {
205 std::string dirty_segments;
206 if (!ReadFileToString(path + "/dirty_segments", &dirty_segments)) {
207 PLOG(WARNING) << "Reading dirty_segments failed in " << path;
210 if (std::stoi(dirty_segments) > DIRTY_SEGMENTS_THRESHOLD) {
218 if (timer.duration() >= std::chrono::seconds(GC_TIMEOUT_SEC)) {
219 LOG(WARNING) << "GC timeout";
224 aborted = cv_abort.wait_for(lk, 10s, []{
225 return idle_maint_stat == IdleMaintStats::kAbort;});
232 static int startGc(const std::list<std::string>& paths) {
233 for (const auto& path : paths) {
234 LOG(DEBUG) << "Start GC on " << path;
235 if (!WriteStringToFile("1", path + "/discard_granularity")) {
236 PLOG(WARNING) << "Set discard gralunarity failed on" << path;
238 if (!WriteStringToFile("1", path + "/gc_urgent")) {
239 PLOG(WARNING) << "Start GC failed on " << path;
245 static int stopGc(const std::list<std::string>& paths) {
246 for (const auto& path : paths) {
247 LOG(DEBUG) << "Stop GC on " << path;
248 if (!WriteStringToFile("0", path + "/gc_urgent")) {
249 PLOG(WARNING) << "Stop GC failed on " << path;
251 if (!WriteStringToFile("16", path + "/discard_granularity")) {
252 PLOG(WARNING) << "Set discard gralunarity failed on" << path;
258 static void runDevGc(void) {
259 std::unique_ptr<fstab, decltype(&fs_mgr_free_fstab)> fstab(fs_mgr_read_fstab_default(),
261 struct fstab_rec *rec = NULL;
263 for (int i = 0; i < fstab->num_entries; i++) {
264 if (fs_mgr_has_sysfs_path(&fstab->recs[i])) {
265 rec = &fstab->recs[i];
274 path.append(rec->sysfs_path);
275 path = path + "/manual_gc";
278 LOG(DEBUG) << "Start Dev GC on " << path;
281 if (!ReadFileToString(path, &require)) {
282 PLOG(WARNING) << "Reading manual_gc failed in " << path;
286 if (require == "" || require == "off" || require == "disabled") {
287 LOG(DEBUG) << "No more to do Dev GC";
291 LOG(DEBUG) << "Trigger Dev GC on " << path;
292 if (!WriteStringToFile("1", path)) {
293 PLOG(WARNING) << "Start Dev GC failed on " << path;
297 if (timer.duration() >= std::chrono::seconds(DEVGC_TIMEOUT_SEC)) {
298 LOG(WARNING) << "Dev GC timeout";
303 LOG(DEBUG) << "Stop Dev GC on " << path;
304 if (!WriteStringToFile("0", path)) {
305 PLOG(WARNING) << "Stop Dev GC failed on " << path;
310 int RunIdleMaint(const android::sp<android::os::IVoldTaskListener>& listener) {
311 std::unique_lock<std::mutex> lk(cv_m);
312 if (idle_maint_stat != IdleMaintStats::kStopped) {
313 LOG(DEBUG) << "idle maintenance is already running";
315 android::os::PersistableBundle extras;
316 listener->onFinished(0, extras);
320 idle_maint_stat = IdleMaintStats::kRunning;
323 LOG(DEBUG) << "idle maintenance started";
325 acquire_wake_lock(PARTIAL_WAKE_LOCK, kWakeLock);
327 std::list<std::string> paths;
328 addFromFstab(&paths, PathTypes::kBlkDevice);
329 addFromVolumeManager(&paths, PathTypes::kBlkDevice);
333 bool gc_aborted = waitForGc(paths);
338 idle_maint_stat = IdleMaintStats::kStopped;
341 cv_stop.notify_one();
349 android::os::PersistableBundle extras;
350 listener->onFinished(0, extras);
353 LOG(DEBUG) << "idle maintenance completed";
355 release_wake_lock(kWakeLock);
360 int AbortIdleMaint(const android::sp<android::os::IVoldTaskListener>& listener) {
361 acquire_wake_lock(PARTIAL_WAKE_LOCK, kWakeLock);
363 std::unique_lock<std::mutex> lk(cv_m);
364 if (idle_maint_stat != IdleMaintStats::kStopped) {
365 idle_maint_stat = IdleMaintStats::kAbort;
367 cv_abort.notify_one();
369 LOG(DEBUG) << "aborting idle maintenance";
371 return idle_maint_stat == IdleMaintStats::kStopped;});
376 android::os::PersistableBundle extras;
377 listener->onFinished(0, extras);
380 release_wake_lock(kWakeLock);
382 LOG(DEBUG) << "idle maintenance stopped";
388 } // namespace android