2015-06-26 23:02:09 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2017-10-18 00:06:32 +02:00
|
|
|
#include "IdleMaint.h"
|
2018-07-29 15:56:57 +02:00
|
|
|
#include "FileDeviceUtils.h"
|
2015-06-26 23:02:09 +02:00
|
|
|
#include "Utils.h"
|
2020-10-16 01:54:38 +02:00
|
|
|
#include "VoldUtil.h"
|
2015-06-26 23:02:09 +02:00
|
|
|
#include "VolumeManager.h"
|
2017-10-18 00:41:45 +02:00
|
|
|
#include "model/PrivateVolume.h"
|
2015-06-26 23:02:09 +02:00
|
|
|
|
2017-10-18 00:41:45 +02:00
|
|
|
#include <thread>
|
2021-01-14 02:10:47 +01:00
|
|
|
#include <utility>
|
2017-10-18 00:41:45 +02:00
|
|
|
|
2021-01-14 02:27:42 +01:00
|
|
|
#include <aidl/android/hardware/health/storage/BnGarbageCollectCallback.h>
|
|
|
|
#include <aidl/android/hardware/health/storage/IStorage.h>
|
2017-10-18 00:41:45 +02:00
|
|
|
#include <android-base/chrono_utils.h>
|
|
|
|
#include <android-base/file.h>
|
2015-12-05 00:50:53 +01:00
|
|
|
#include <android-base/logging.h>
|
2018-08-10 22:50:46 +02:00
|
|
|
#include <android-base/stringprintf.h>
|
|
|
|
#include <android-base/strings.h>
|
2021-01-14 02:27:42 +01:00
|
|
|
#include <android/binder_manager.h>
|
2018-09-19 19:28:16 +02:00
|
|
|
#include <android/hardware/health/storage/1.0/IStorage.h>
|
2015-06-26 23:02:09 +02:00
|
|
|
#include <fs_mgr.h>
|
2018-08-10 22:50:46 +02:00
|
|
|
#include <private/android_filesystem_config.h>
|
2019-06-21 21:21:48 +02:00
|
|
|
#include <wakelock/wakelock.h>
|
2015-06-26 23:02:09 +02:00
|
|
|
|
|
|
|
#include <dirent.h>
|
2018-09-18 22:30:21 +02:00
|
|
|
#include <fcntl.h>
|
2015-06-26 23:02:09 +02:00
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
2017-10-18 00:41:45 +02:00
|
|
|
using android::base::Basename;
|
|
|
|
using android::base::ReadFileToString;
|
|
|
|
using android::base::Realpath;
|
2015-06-26 23:02:09 +02:00
|
|
|
using android::base::StringPrintf;
|
2017-10-18 00:41:45 +02:00
|
|
|
using android::base::Timer;
|
|
|
|
using android::base::WriteStringToFile;
|
2018-08-10 22:50:46 +02:00
|
|
|
using android::hardware::Return;
|
|
|
|
using android::hardware::Void;
|
2021-01-14 02:27:42 +01:00
|
|
|
using AStorage = aidl::android::hardware::health::storage::IStorage;
|
|
|
|
using ABnGarbageCollectCallback =
|
|
|
|
aidl::android::hardware::health::storage::BnGarbageCollectCallback;
|
|
|
|
using AResult = aidl::android::hardware::health::storage::Result;
|
2021-01-14 02:10:47 +01:00
|
|
|
using HStorage = android::hardware::health::storage::V1_0::IStorage;
|
|
|
|
using HGarbageCollectCallback = android::hardware::health::storage::V1_0::IGarbageCollectCallback;
|
|
|
|
using HResult = android::hardware::health::storage::V1_0::Result;
|
2021-01-14 02:27:42 +01:00
|
|
|
using std::string_literals::operator""s;
|
2015-06-26 23:02:09 +02:00
|
|
|
|
|
|
|
namespace android {
|
|
|
|
namespace vold {
|
|
|
|
|
2017-10-18 00:41:45 +02:00
|
|
|
enum class PathTypes {
|
|
|
|
kMountPoint = 1,
|
|
|
|
kBlkDevice,
|
|
|
|
};
|
|
|
|
|
|
|
|
enum class IdleMaintStats {
|
|
|
|
kStopped = 1,
|
|
|
|
kRunning,
|
|
|
|
kAbort,
|
|
|
|
};
|
|
|
|
|
2017-10-18 00:06:32 +02:00
|
|
|
static const char* kWakeLock = "IdleMaint";
|
2017-10-18 00:41:45 +02:00
|
|
|
static const int DIRTY_SEGMENTS_THRESHOLD = 100;
|
2018-02-13 06:57:04 +01:00
|
|
|
/*
|
|
|
|
* Timing policy:
|
|
|
|
* 1. F2FS_GC = 7 mins
|
|
|
|
* 2. Trim = 1 min
|
|
|
|
* 3. Dev GC = 2 mins
|
|
|
|
*/
|
|
|
|
static const int GC_TIMEOUT_SEC = 420;
|
|
|
|
static const int DEVGC_TIMEOUT_SEC = 120;
|
2022-01-04 20:37:39 +01:00
|
|
|
static const int KBYTES_IN_SEGMENT = 2048;
|
2022-03-18 20:24:41 +01:00
|
|
|
static const int ONE_MINUTE_IN_MS = 60000;
|
2022-01-04 20:37:39 +01:00
|
|
|
static const int GC_NORMAL_MODE = 0;
|
2022-03-16 05:42:55 +01:00
|
|
|
static const int GC_URGENT_MID_MODE = 3;
|
2022-01-04 20:37:39 +01:00
|
|
|
|
|
|
|
static int32_t previousSegmentWrite = 0;
|
2015-06-26 23:02:09 +02:00
|
|
|
|
2017-10-18 00:41:45 +02:00
|
|
|
static IdleMaintStats idle_maint_stat(IdleMaintStats::kStopped);
|
|
|
|
static std::condition_variable cv_abort, cv_stop;
|
|
|
|
static std::mutex cv_m;
|
|
|
|
|
2018-09-18 22:30:21 +02:00
|
|
|
static void addFromVolumeManager(std::list<std::string>* paths, PathTypes path_type) {
|
2015-06-26 23:02:09 +02:00
|
|
|
VolumeManager* vm = VolumeManager::Instance();
|
|
|
|
std::list<std::string> privateIds;
|
|
|
|
vm->listVolumes(VolumeBase::Type::kPrivate, privateIds);
|
2016-07-27 23:11:02 +02:00
|
|
|
for (const auto& id : privateIds) {
|
2017-10-18 00:41:45 +02:00
|
|
|
PrivateVolume* vol = static_cast<PrivateVolume*>(vm->findVolume(id).get());
|
2015-06-26 23:02:09 +02:00
|
|
|
if (vol != nullptr && vol->getState() == VolumeBase::State::kMounted) {
|
2017-10-18 00:41:45 +02:00
|
|
|
if (path_type == PathTypes::kMountPoint) {
|
|
|
|
paths->push_back(vol->getPath());
|
|
|
|
} else if (path_type == PathTypes::kBlkDevice) {
|
|
|
|
std::string gc_path;
|
|
|
|
const std::string& fs_type = vol->getFsType();
|
2018-07-29 15:56:57 +02:00
|
|
|
if (fs_type == "f2fs" && (Realpath(vol->getRawDmDevPath(), &gc_path) ||
|
|
|
|
Realpath(vol->getRawDevPath(), &gc_path))) {
|
2018-09-18 22:30:21 +02:00
|
|
|
paths->push_back(std::string("/sys/fs/") + fs_type + "/" + Basename(gc_path));
|
2017-10-18 00:41:45 +02:00
|
|
|
}
|
|
|
|
}
|
2015-06-26 23:02:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-04 20:37:39 +01:00
|
|
|
static void addFromFstab(std::list<std::string>* paths, PathTypes path_type, bool only_data_part) {
|
2019-01-29 23:34:01 +01:00
|
|
|
std::string previous_mount_point;
|
2020-10-16 01:54:38 +02:00
|
|
|
for (const auto& entry : fstab_default) {
|
IdleMaint: skip swap space, bind mounts, and virtual filesystems
When the idle maintenance task runs on Cuttlefish, there are several
warnings printed to logcat after the expected messages:
D vold : Starting trim of /data
I vold : Trimmed 0 bytes on /data in 36ms
D vold : Starting trim of /cache
I vold : Trimmed 58662912 bytes on /cache in 0ms
D vold : Starting trim of /metadata
I vold : Trimmed 7725056 bytes on /metadata in 12ms
D vold : Starting trim of none
W vold : Failed to open none: No such file or directory
D vold : Starting trim of /sdcard
W vold : Failed to open /sdcard: Not a directory
D vold : Starting trim of /mnt/vendor/shared
W vold : Trim failed on /mnt/vendor/shared: Inappropriate ioctl for device
This is because vold gathers the filesystems to trim from the fstab, but
it fails to exclude some entries that aren't appropriate to trim:
/dev/block/zram0 none swap defaults zramsize=75%
/tmp /sdcard none defaults,bind recoveryonly
shared /mnt/vendor/shared virtiofs nosuid,nodev,noatime nofail
These should be excluded because they are swap space, a bind mount, and
a virtual filesystem respectively.
Fix addFromFstab() to exclude the above cases.
Afterwards, the messages on Cuttlefish are:
D vold : Starting trim of /data
I vold : Trimmed 0 bytes on /data in 39ms
D vold : Starting trim of /cache
I vold : Trimmed 58662912 bytes on /cache in 0ms
D vold : Starting trim of /metadata
I vold : Trimmed 9822208 bytes on /metadata in 12ms
Change-Id: Idc575106fe6f81c737f684429d58dba4bd5478ad
2020-10-16 01:54:38 +02:00
|
|
|
// Skip raw partitions and swap space.
|
|
|
|
if (entry.fs_type == "emmc" || entry.fs_type == "mtd" || entry.fs_type == "swap") {
|
2015-06-26 23:02:09 +02:00
|
|
|
continue;
|
|
|
|
}
|
IdleMaint: skip swap space, bind mounts, and virtual filesystems
When the idle maintenance task runs on Cuttlefish, there are several
warnings printed to logcat after the expected messages:
D vold : Starting trim of /data
I vold : Trimmed 0 bytes on /data in 36ms
D vold : Starting trim of /cache
I vold : Trimmed 58662912 bytes on /cache in 0ms
D vold : Starting trim of /metadata
I vold : Trimmed 7725056 bytes on /metadata in 12ms
D vold : Starting trim of none
W vold : Failed to open none: No such file or directory
D vold : Starting trim of /sdcard
W vold : Failed to open /sdcard: Not a directory
D vold : Starting trim of /mnt/vendor/shared
W vold : Trim failed on /mnt/vendor/shared: Inappropriate ioctl for device
This is because vold gathers the filesystems to trim from the fstab, but
it fails to exclude some entries that aren't appropriate to trim:
/dev/block/zram0 none swap defaults zramsize=75%
/tmp /sdcard none defaults,bind recoveryonly
shared /mnt/vendor/shared virtiofs nosuid,nodev,noatime nofail
These should be excluded because they are swap space, a bind mount, and
a virtual filesystem respectively.
Fix addFromFstab() to exclude the above cases.
Afterwards, the messages on Cuttlefish are:
D vold : Starting trim of /data
I vold : Trimmed 0 bytes on /data in 39ms
D vold : Starting trim of /cache
I vold : Trimmed 58662912 bytes on /cache in 0ms
D vold : Starting trim of /metadata
I vold : Trimmed 9822208 bytes on /metadata in 12ms
Change-Id: Idc575106fe6f81c737f684429d58dba4bd5478ad
2020-10-16 01:54:38 +02:00
|
|
|
// Skip read-only filesystems and bind mounts.
|
|
|
|
if (entry.flags & (MS_RDONLY | MS_BIND)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Skip anything without an underlying block device, e.g. virtiofs.
|
|
|
|
if (entry.blk_device[0] != '/') {
|
2015-06-26 23:02:09 +02:00
|
|
|
continue;
|
|
|
|
}
|
2019-01-29 23:34:01 +01:00
|
|
|
if (entry.fs_mgr_flags.vold_managed) {
|
|
|
|
continue; // Should we trim fat32 filesystems?
|
2015-06-26 23:02:09 +02:00
|
|
|
}
|
2019-01-29 23:34:01 +01:00
|
|
|
if (entry.fs_mgr_flags.no_trim) {
|
2015-06-26 23:02:09 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-01-04 20:37:39 +01:00
|
|
|
if (only_data_part && entry.mount_point != "/data") {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-01-29 23:34:01 +01:00
|
|
|
// Skip the multi-type partitions, which are required to be following each other.
|
|
|
|
// See fs_mgr.c's mount_with_alternatives().
|
|
|
|
if (entry.mount_point == previous_mount_point) {
|
2015-06-26 23:02:09 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-10-18 00:41:45 +02:00
|
|
|
if (path_type == PathTypes::kMountPoint) {
|
2019-01-29 23:34:01 +01:00
|
|
|
paths->push_back(entry.mount_point);
|
2017-10-18 00:41:45 +02:00
|
|
|
} else if (path_type == PathTypes::kBlkDevice) {
|
2022-01-04 20:37:39 +01:00
|
|
|
std::string path;
|
2019-01-29 23:34:01 +01:00
|
|
|
if (entry.fs_type == "f2fs" &&
|
2022-01-04 20:37:39 +01:00
|
|
|
Realpath(android::vold::BlockDeviceForPath(entry.mount_point + "/"), &path)) {
|
|
|
|
paths->push_back("/sys/fs/" + entry.fs_type + "/" + Basename(path));
|
2017-10-18 00:41:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-29 23:34:01 +01:00
|
|
|
previous_mount_point = entry.mount_point;
|
2015-06-26 23:02:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-18 00:06:32 +02:00
|
|
|
void Trim(const android::sp<android::os::IVoldTaskListener>& listener) {
|
2021-02-22 21:10:45 +01:00
|
|
|
auto wl = android::wakelock::WakeLock::tryGet(kWakeLock);
|
|
|
|
if (!wl.has_value()) {
|
|
|
|
return;
|
|
|
|
}
|
2015-06-26 23:02:09 +02:00
|
|
|
|
2017-10-18 00:06:32 +02:00
|
|
|
// Collect both fstab and vold volumes
|
|
|
|
std::list<std::string> paths;
|
2022-01-04 20:37:39 +01:00
|
|
|
addFromFstab(&paths, PathTypes::kMountPoint, false);
|
2017-10-18 00:41:45 +02:00
|
|
|
addFromVolumeManager(&paths, PathTypes::kMountPoint);
|
2017-10-18 00:06:32 +02:00
|
|
|
|
|
|
|
for (const auto& path : paths) {
|
2015-06-26 23:02:09 +02:00
|
|
|
LOG(DEBUG) << "Starting trim of " << path;
|
|
|
|
|
2017-09-15 20:57:44 +02:00
|
|
|
android::os::PersistableBundle extras;
|
|
|
|
extras.putString(String16("path"), String16(path.c_str()));
|
|
|
|
|
2015-06-26 23:02:09 +02:00
|
|
|
int fd = open(path.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC | O_NOFOLLOW);
|
|
|
|
if (fd < 0) {
|
|
|
|
PLOG(WARNING) << "Failed to open " << path;
|
2017-10-18 00:06:32 +02:00
|
|
|
if (listener) {
|
|
|
|
listener->onStatus(-1, extras);
|
2017-09-15 20:57:44 +02:00
|
|
|
}
|
2015-06-26 23:02:09 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct fstrim_range range;
|
|
|
|
memset(&range, 0, sizeof(range));
|
|
|
|
range.len = ULLONG_MAX;
|
|
|
|
|
|
|
|
nsecs_t start = systemTime(SYSTEM_TIME_BOOTTIME);
|
2017-10-18 00:06:32 +02:00
|
|
|
if (ioctl(fd, FITRIM, &range)) {
|
2015-06-26 23:02:09 +02:00
|
|
|
PLOG(WARNING) << "Trim failed on " << path;
|
2017-10-18 00:06:32 +02:00
|
|
|
if (listener) {
|
|
|
|
listener->onStatus(-1, extras);
|
2017-09-15 20:57:44 +02:00
|
|
|
}
|
2015-06-26 23:02:09 +02:00
|
|
|
} else {
|
2017-09-15 20:57:44 +02:00
|
|
|
nsecs_t time = systemTime(SYSTEM_TIME_BOOTTIME) - start;
|
2018-09-18 22:30:21 +02:00
|
|
|
LOG(INFO) << "Trimmed " << range.len << " bytes on " << path << " in "
|
|
|
|
<< nanoseconds_to_milliseconds(time) << "ms";
|
2017-09-15 20:57:44 +02:00
|
|
|
extras.putLong(String16("bytes"), range.len);
|
|
|
|
extras.putLong(String16("time"), time);
|
2017-10-18 00:06:32 +02:00
|
|
|
if (listener) {
|
|
|
|
listener->onStatus(0, extras);
|
2017-09-15 20:57:44 +02:00
|
|
|
}
|
2015-06-26 23:02:09 +02:00
|
|
|
}
|
|
|
|
close(fd);
|
2017-09-15 20:57:44 +02:00
|
|
|
}
|
2015-06-26 23:02:09 +02:00
|
|
|
|
2017-10-18 00:06:32 +02:00
|
|
|
if (listener) {
|
2017-09-15 20:57:44 +02:00
|
|
|
android::os::PersistableBundle extras;
|
2017-10-18 00:06:32 +02:00
|
|
|
listener->onFinished(0, extras);
|
2015-06-26 23:02:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-10-18 00:41:45 +02:00
|
|
|
static bool waitForGc(const std::list<std::string>& paths) {
|
|
|
|
std::unique_lock<std::mutex> lk(cv_m, std::defer_lock);
|
|
|
|
bool stop = false, aborted = false;
|
|
|
|
Timer timer;
|
|
|
|
|
|
|
|
while (!stop && !aborted) {
|
|
|
|
stop = true;
|
|
|
|
for (const auto& path : paths) {
|
|
|
|
std::string dirty_segments;
|
|
|
|
if (!ReadFileToString(path + "/dirty_segments", &dirty_segments)) {
|
|
|
|
PLOG(WARNING) << "Reading dirty_segments failed in " << path;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (std::stoi(dirty_segments) > DIRTY_SEGMENTS_THRESHOLD) {
|
|
|
|
stop = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stop) break;
|
|
|
|
|
|
|
|
if (timer.duration() >= std::chrono::seconds(GC_TIMEOUT_SEC)) {
|
|
|
|
LOG(WARNING) << "GC timeout";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
lk.lock();
|
2018-09-18 22:30:21 +02:00
|
|
|
aborted =
|
|
|
|
cv_abort.wait_for(lk, 10s, [] { return idle_maint_stat == IdleMaintStats::kAbort; });
|
2017-10-18 00:41:45 +02:00
|
|
|
lk.unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
return aborted;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int startGc(const std::list<std::string>& paths) {
|
|
|
|
for (const auto& path : paths) {
|
|
|
|
LOG(DEBUG) << "Start GC on " << path;
|
|
|
|
if (!WriteStringToFile("1", path + "/gc_urgent")) {
|
|
|
|
PLOG(WARNING) << "Start GC failed on " << path;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return android::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int stopGc(const std::list<std::string>& paths) {
|
|
|
|
for (const auto& path : paths) {
|
|
|
|
LOG(DEBUG) << "Stop GC on " << path;
|
|
|
|
if (!WriteStringToFile("0", path + "/gc_urgent")) {
|
|
|
|
PLOG(WARNING) << "Stop GC failed on " << path;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return android::OK;
|
|
|
|
}
|
|
|
|
|
2022-01-04 20:37:39 +01:00
|
|
|
static std::string getDevSysfsPath() {
|
2020-10-16 01:54:38 +02:00
|
|
|
for (const auto& entry : fstab_default) {
|
2019-01-29 23:34:01 +01:00
|
|
|
if (!entry.sysfs_path.empty()) {
|
2022-01-04 20:37:39 +01:00
|
|
|
return entry.sysfs_path;
|
2018-02-13 06:57:04 +01:00
|
|
|
}
|
|
|
|
}
|
2022-01-04 20:37:39 +01:00
|
|
|
LOG(WARNING) << "Cannot find dev sysfs path";
|
|
|
|
return "";
|
|
|
|
}
|
2019-01-29 23:34:01 +01:00
|
|
|
|
2022-01-04 20:37:39 +01:00
|
|
|
static void runDevGcFstab(void) {
|
|
|
|
std::string path = getDevSysfsPath();
|
2019-01-29 23:34:01 +01:00
|
|
|
if (path.empty()) {
|
2018-02-13 06:57:04 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
path = path + "/manual_gc";
|
|
|
|
Timer timer;
|
|
|
|
|
|
|
|
LOG(DEBUG) << "Start Dev GC on " << path;
|
|
|
|
while (1) {
|
|
|
|
std::string require;
|
|
|
|
if (!ReadFileToString(path, &require)) {
|
|
|
|
PLOG(WARNING) << "Reading manual_gc failed in " << path;
|
|
|
|
break;
|
|
|
|
}
|
2018-08-10 22:50:46 +02:00
|
|
|
require = android::base::Trim(require);
|
2018-02-13 06:57:04 +01:00
|
|
|
if (require == "" || require == "off" || require == "disabled") {
|
|
|
|
LOG(DEBUG) << "No more to do Dev GC";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(DEBUG) << "Trigger Dev GC on " << path;
|
|
|
|
if (!WriteStringToFile("1", path)) {
|
|
|
|
PLOG(WARNING) << "Start Dev GC failed on " << path;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (timer.duration() >= std::chrono::seconds(DEVGC_TIMEOUT_SEC)) {
|
|
|
|
LOG(WARNING) << "Dev GC timeout";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
sleep(2);
|
|
|
|
}
|
|
|
|
LOG(DEBUG) << "Stop Dev GC on " << path;
|
|
|
|
if (!WriteStringToFile("0", path)) {
|
|
|
|
PLOG(WARNING) << "Stop Dev GC failed on " << path;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-14 02:27:42 +01:00
|
|
|
enum class IDL { HIDL, AIDL };
|
2021-01-14 02:10:47 +01:00
|
|
|
std::ostream& operator<<(std::ostream& os, IDL idl) {
|
2021-01-14 02:27:42 +01:00
|
|
|
return os << (idl == IDL::HIDL ? "HIDL" : "AIDL");
|
2021-01-14 02:10:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template <IDL idl, typename Result>
|
|
|
|
class GcCallbackImpl {
|
|
|
|
protected:
|
|
|
|
void onFinishInternal(Result result) {
|
2018-08-10 22:50:46 +02:00
|
|
|
std::unique_lock<std::mutex> lock(mMutex);
|
|
|
|
mFinished = true;
|
|
|
|
mResult = result;
|
|
|
|
lock.unlock();
|
|
|
|
mCv.notify_all();
|
|
|
|
}
|
2021-01-14 02:10:47 +01:00
|
|
|
|
|
|
|
public:
|
2018-08-10 22:50:46 +02:00
|
|
|
void wait(uint64_t seconds) {
|
|
|
|
std::unique_lock<std::mutex> lock(mMutex);
|
|
|
|
mCv.wait_for(lock, std::chrono::seconds(seconds), [this] { return mFinished; });
|
|
|
|
|
|
|
|
if (!mFinished) {
|
2021-01-14 02:10:47 +01:00
|
|
|
LOG(WARNING) << "Dev GC on " << idl << " HAL timeout";
|
2018-08-10 22:50:46 +02:00
|
|
|
} else if (mResult != Result::SUCCESS) {
|
2021-01-14 02:10:47 +01:00
|
|
|
LOG(WARNING) << "Dev GC on " << idl << " HAL failed with " << toString(mResult);
|
2018-08-10 22:50:46 +02:00
|
|
|
} else {
|
2021-01-14 02:10:47 +01:00
|
|
|
LOG(INFO) << "Dev GC on " << idl << " HAL successful";
|
2018-08-10 22:50:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::mutex mMutex;
|
|
|
|
std::condition_variable mCv;
|
|
|
|
bool mFinished{false};
|
|
|
|
Result mResult{Result::UNKNOWN_ERROR};
|
|
|
|
};
|
|
|
|
|
2021-01-14 02:27:42 +01:00
|
|
|
class AGcCallbackImpl : public ABnGarbageCollectCallback,
|
|
|
|
public GcCallbackImpl<IDL::AIDL, AResult> {
|
|
|
|
ndk::ScopedAStatus onFinish(AResult result) override {
|
|
|
|
onFinishInternal(result);
|
|
|
|
return ndk::ScopedAStatus::ok();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-01-14 02:10:47 +01:00
|
|
|
class HGcCallbackImpl : public HGarbageCollectCallback, public GcCallbackImpl<IDL::HIDL, HResult> {
|
|
|
|
Return<void> onFinish(HResult result) override {
|
|
|
|
onFinishInternal(result);
|
|
|
|
return Void();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <IDL idl, typename Service, typename GcCallbackImpl, typename GetDescription>
|
|
|
|
static void runDevGcOnHal(Service service, GcCallbackImpl cb, GetDescription get_description) {
|
|
|
|
LOG(DEBUG) << "Start Dev GC on " << idl << " HAL";
|
2018-08-10 22:50:46 +02:00
|
|
|
auto ret = service->garbageCollect(DEVGC_TIMEOUT_SEC, cb);
|
|
|
|
if (!ret.isOk()) {
|
2021-01-14 02:10:47 +01:00
|
|
|
LOG(WARNING) << "Cannot start Dev GC on " << idl
|
|
|
|
<< " HAL: " << std::invoke(get_description, ret);
|
2018-08-10 22:50:46 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
cb->wait(DEVGC_TIMEOUT_SEC);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void runDevGc(void) {
|
2021-01-14 02:10:47 +01:00
|
|
|
runDevGcFstab();
|
2018-08-10 22:50:46 +02:00
|
|
|
}
|
|
|
|
|
2022-01-04 20:37:39 +01:00
|
|
|
int RunIdleMaint(bool needGC, const android::sp<android::os::IVoldTaskListener>& listener) {
|
2017-10-18 00:41:45 +02:00
|
|
|
std::unique_lock<std::mutex> lk(cv_m);
|
2022-01-04 20:37:39 +01:00
|
|
|
bool gc_aborted = false;
|
|
|
|
|
2017-10-18 00:41:45 +02:00
|
|
|
if (idle_maint_stat != IdleMaintStats::kStopped) {
|
|
|
|
LOG(DEBUG) << "idle maintenance is already running";
|
|
|
|
if (listener) {
|
|
|
|
android::os::PersistableBundle extras;
|
|
|
|
listener->onFinished(0, extras);
|
|
|
|
}
|
|
|
|
return android::OK;
|
|
|
|
}
|
|
|
|
idle_maint_stat = IdleMaintStats::kRunning;
|
|
|
|
lk.unlock();
|
|
|
|
|
|
|
|
LOG(DEBUG) << "idle maintenance started";
|
|
|
|
|
2021-02-22 21:10:45 +01:00
|
|
|
auto wl = android::wakelock::WakeLock::tryGet(kWakeLock);
|
|
|
|
if (!wl.has_value()) {
|
|
|
|
return android::UNEXPECTED_NULL;
|
|
|
|
}
|
2017-10-18 00:41:45 +02:00
|
|
|
|
2022-01-04 20:37:39 +01:00
|
|
|
if (needGC) {
|
|
|
|
std::list<std::string> paths;
|
|
|
|
addFromFstab(&paths, PathTypes::kBlkDevice, false);
|
|
|
|
addFromVolumeManager(&paths, PathTypes::kBlkDevice);
|
2017-10-18 00:41:45 +02:00
|
|
|
|
2022-01-04 20:37:39 +01:00
|
|
|
startGc(paths);
|
2017-10-18 00:41:45 +02:00
|
|
|
|
2022-01-04 20:37:39 +01:00
|
|
|
gc_aborted = waitForGc(paths);
|
2017-10-18 00:41:45 +02:00
|
|
|
|
2022-01-04 20:37:39 +01:00
|
|
|
stopGc(paths);
|
|
|
|
}
|
2017-10-18 00:41:45 +02:00
|
|
|
|
2022-05-18 18:03:15 +02:00
|
|
|
if (!gc_aborted) {
|
|
|
|
Trim(nullptr);
|
|
|
|
runDevGc();
|
|
|
|
}
|
|
|
|
|
2017-10-18 00:41:45 +02:00
|
|
|
lk.lock();
|
|
|
|
idle_maint_stat = IdleMaintStats::kStopped;
|
|
|
|
lk.unlock();
|
|
|
|
|
|
|
|
cv_stop.notify_one();
|
|
|
|
|
|
|
|
if (listener) {
|
|
|
|
android::os::PersistableBundle extras;
|
|
|
|
listener->onFinished(0, extras);
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(DEBUG) << "idle maintenance completed";
|
|
|
|
|
|
|
|
return android::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int AbortIdleMaint(const android::sp<android::os::IVoldTaskListener>& listener) {
|
2021-02-22 21:10:45 +01:00
|
|
|
auto wl = android::wakelock::WakeLock::tryGet(kWakeLock);
|
|
|
|
if (!wl.has_value()) {
|
|
|
|
return android::UNEXPECTED_NULL;
|
|
|
|
}
|
2017-10-18 00:41:45 +02:00
|
|
|
|
|
|
|
std::unique_lock<std::mutex> lk(cv_m);
|
|
|
|
if (idle_maint_stat != IdleMaintStats::kStopped) {
|
|
|
|
idle_maint_stat = IdleMaintStats::kAbort;
|
|
|
|
lk.unlock();
|
|
|
|
cv_abort.notify_one();
|
|
|
|
lk.lock();
|
|
|
|
LOG(DEBUG) << "aborting idle maintenance";
|
2018-09-18 22:30:21 +02:00
|
|
|
cv_stop.wait(lk, [] { return idle_maint_stat == IdleMaintStats::kStopped; });
|
2017-10-18 00:41:45 +02:00
|
|
|
}
|
|
|
|
lk.unlock();
|
|
|
|
|
|
|
|
if (listener) {
|
|
|
|
android::os::PersistableBundle extras;
|
|
|
|
listener->onFinished(0, extras);
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(DEBUG) << "idle maintenance stopped";
|
|
|
|
|
|
|
|
return android::OK;
|
|
|
|
}
|
|
|
|
|
2022-01-04 20:37:39 +01:00
|
|
|
int getLifeTime(const std::string& path) {
|
|
|
|
std::string result;
|
|
|
|
|
|
|
|
if (!ReadFileToString(path, &result)) {
|
|
|
|
PLOG(WARNING) << "Reading lifetime estimation failed for " << path;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return std::stoi(result, 0, 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t GetStorageLifeTime() {
|
|
|
|
std::string path = getDevSysfsPath();
|
|
|
|
if (path.empty()) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string lifeTimeBasePath = path + "/health_descriptor/life_time_estimation_";
|
|
|
|
|
|
|
|
int32_t lifeTime = getLifeTime(lifeTimeBasePath + "c");
|
|
|
|
if (lifeTime != -1) {
|
|
|
|
return lifeTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t lifeTimeA = getLifeTime(lifeTimeBasePath + "a");
|
|
|
|
int32_t lifeTimeB = getLifeTime(lifeTimeBasePath + "b");
|
|
|
|
lifeTime = std::max(lifeTimeA, lifeTimeB);
|
|
|
|
if (lifeTime != -1) {
|
|
|
|
return lifeTime == 0 ? -1 : lifeTime * 10;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetGCUrgentPace(int32_t neededSegments, int32_t minSegmentThreshold, float dirtyReclaimRate,
|
2022-06-24 23:50:47 +02:00
|
|
|
float reclaimWeight, int32_t gcPeriod, int32_t minGCSleepTime,
|
|
|
|
int32_t targetDirtyRatio) {
|
2022-01-04 20:37:39 +01:00
|
|
|
std::list<std::string> paths;
|
2022-06-24 23:50:47 +02:00
|
|
|
bool needGC = false;
|
2022-03-19 05:30:47 +01:00
|
|
|
int32_t sleepTime;
|
2022-01-04 20:37:39 +01:00
|
|
|
|
|
|
|
addFromFstab(&paths, PathTypes::kBlkDevice, true);
|
|
|
|
if (paths.empty()) {
|
|
|
|
LOG(WARNING) << "There is no valid blk device path for data partition";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string f2fsSysfsPath = paths.front();
|
|
|
|
std::string freeSegmentsPath = f2fsSysfsPath + "/free_segments";
|
|
|
|
std::string dirtySegmentsPath = f2fsSysfsPath + "/dirty_segments";
|
|
|
|
std::string gcSleepTimePath = f2fsSysfsPath + "/gc_urgent_sleep_time";
|
|
|
|
std::string gcUrgentModePath = f2fsSysfsPath + "/gc_urgent";
|
2022-03-25 18:02:57 +01:00
|
|
|
std::string ovpSegmentsPath = f2fsSysfsPath + "/ovp_segments";
|
|
|
|
std::string reservedBlocksPath = f2fsSysfsPath + "/reserved_blocks";
|
|
|
|
std::string freeSegmentsStr, dirtySegmentsStr, ovpSegmentsStr, reservedBlocksStr;
|
2022-01-04 20:37:39 +01:00
|
|
|
|
|
|
|
if (!ReadFileToString(freeSegmentsPath, &freeSegmentsStr)) {
|
|
|
|
PLOG(WARNING) << "Reading failed in " << freeSegmentsPath;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ReadFileToString(dirtySegmentsPath, &dirtySegmentsStr)) {
|
|
|
|
PLOG(WARNING) << "Reading failed in " << dirtySegmentsPath;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-25 18:02:57 +01:00
|
|
|
if (!ReadFileToString(ovpSegmentsPath, &ovpSegmentsStr)) {
|
|
|
|
PLOG(WARNING) << "Reading failed in " << ovpSegmentsPath;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ReadFileToString(reservedBlocksPath, &reservedBlocksStr)) {
|
|
|
|
PLOG(WARNING) << "Reading failed in " << reservedBlocksPath;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-04 20:37:39 +01:00
|
|
|
int32_t freeSegments = std::stoi(freeSegmentsStr);
|
|
|
|
int32_t dirtySegments = std::stoi(dirtySegmentsStr);
|
2022-03-25 18:02:57 +01:00
|
|
|
int32_t reservedBlocks = std::stoi(ovpSegmentsStr) + std::stoi(reservedBlocksStr);
|
2022-01-04 20:37:39 +01:00
|
|
|
|
2022-03-25 18:02:57 +01:00
|
|
|
freeSegments = freeSegments > reservedBlocks ? freeSegments - reservedBlocks : 0;
|
2022-06-24 23:50:47 +02:00
|
|
|
int32_t totalSegments = freeSegments + dirtySegments;
|
|
|
|
int32_t finalTargetSegments = 0;
|
|
|
|
|
|
|
|
if (totalSegments < minSegmentThreshold) {
|
2022-01-04 20:37:39 +01:00
|
|
|
LOG(INFO) << "The sum of free segments: " << freeSegments
|
2022-06-24 23:50:47 +02:00
|
|
|
<< ", dirty segments: " << dirtySegments << " is under " << minSegmentThreshold;
|
2022-03-19 05:30:47 +01:00
|
|
|
} else {
|
2022-06-24 23:50:47 +02:00
|
|
|
int32_t dirtyRatio = dirtySegments * 100 / totalSegments;
|
|
|
|
int32_t neededForTargetRatio =
|
|
|
|
(dirtyRatio > targetDirtyRatio)
|
|
|
|
? totalSegments * (dirtyRatio - targetDirtyRatio) / 100
|
|
|
|
: 0;
|
|
|
|
neededSegments *= reclaimWeight;
|
|
|
|
neededSegments = (neededSegments > freeSegments) ? neededSegments - freeSegments : 0;
|
|
|
|
|
|
|
|
finalTargetSegments = std::max(neededSegments, neededForTargetRatio);
|
|
|
|
if (finalTargetSegments == 0) {
|
|
|
|
LOG(INFO) << "Enough free segments: " << freeSegments;
|
2022-03-19 05:30:47 +01:00
|
|
|
} else {
|
2022-06-24 23:50:47 +02:00
|
|
|
finalTargetSegments =
|
|
|
|
std::min(finalTargetSegments, (int32_t)(dirtySegments * dirtyReclaimRate));
|
|
|
|
if (finalTargetSegments == 0) {
|
|
|
|
LOG(INFO) << "Low dirty segments: " << dirtySegments;
|
|
|
|
} else if (neededSegments >= neededForTargetRatio) {
|
|
|
|
LOG(INFO) << "Trigger GC, because of needed segments exceeding free segments";
|
|
|
|
needGC = true;
|
|
|
|
} else {
|
|
|
|
LOG(INFO) << "Trigger GC for target dirty ratio diff of: "
|
|
|
|
<< dirtyRatio - targetDirtyRatio;
|
|
|
|
needGC = true;
|
2022-03-19 05:30:47 +01:00
|
|
|
}
|
|
|
|
}
|
2022-01-04 20:37:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!needGC) {
|
|
|
|
if (!WriteStringToFile(std::to_string(GC_NORMAL_MODE), gcUrgentModePath)) {
|
|
|
|
PLOG(WARNING) << "Writing failed in " << gcUrgentModePath;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-24 23:50:47 +02:00
|
|
|
sleepTime = gcPeriod * ONE_MINUTE_IN_MS / finalTargetSegments;
|
|
|
|
if (sleepTime < minGCSleepTime) {
|
|
|
|
sleepTime = minGCSleepTime;
|
|
|
|
}
|
|
|
|
|
2022-01-04 20:37:39 +01:00
|
|
|
if (!WriteStringToFile(std::to_string(sleepTime), gcSleepTimePath)) {
|
|
|
|
PLOG(WARNING) << "Writing failed in " << gcSleepTimePath;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-16 05:42:55 +01:00
|
|
|
if (!WriteStringToFile(std::to_string(GC_URGENT_MID_MODE), gcUrgentModePath)) {
|
2022-01-04 20:37:39 +01:00
|
|
|
PLOG(WARNING) << "Writing failed in " << gcUrgentModePath;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(INFO) << "Successfully set gc urgent mode: "
|
2022-06-24 23:50:47 +02:00
|
|
|
<< "free segments: " << freeSegments << ", reclaim target: " << finalTargetSegments
|
|
|
|
<< ", sleep time: " << sleepTime;
|
2022-01-04 20:37:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int32_t getLifeTimeWrite() {
|
|
|
|
std::list<std::string> paths;
|
|
|
|
addFromFstab(&paths, PathTypes::kBlkDevice, true);
|
|
|
|
if (paths.empty()) {
|
|
|
|
LOG(WARNING) << "There is no valid blk device path for data partition";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string writeKbytesPath = paths.front() + "/lifetime_write_kbytes";
|
|
|
|
std::string writeKbytesStr;
|
|
|
|
if (!ReadFileToString(writeKbytesPath, &writeKbytesStr)) {
|
|
|
|
PLOG(WARNING) << "Reading failed in " << writeKbytesPath;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
long long writeBytes = std::stoll(writeKbytesStr);
|
|
|
|
return writeBytes / KBYTES_IN_SEGMENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RefreshLatestWrite() {
|
|
|
|
int32_t segmentWrite = getLifeTimeWrite();
|
|
|
|
if (segmentWrite != -1) {
|
|
|
|
previousSegmentWrite = segmentWrite;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t GetWriteAmount() {
|
|
|
|
int32_t currentSegmentWrite = getLifeTimeWrite();
|
|
|
|
if (currentSegmentWrite == -1) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t writeAmount = currentSegmentWrite - previousSegmentWrite;
|
|
|
|
previousSegmentWrite = currentSegmentWrite;
|
|
|
|
return writeAmount;
|
|
|
|
}
|
|
|
|
|
2015-06-26 23:02:09 +02:00
|
|
|
} // namespace vold
|
|
|
|
} // namespace android
|