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.
18 #include "Benchmark.h"
20 #include "VolumeManager.h"
21 #include "ResponseCode.h"
23 #include <android-base/stringprintf.h>
24 #include <android-base/logging.h>
25 #include <cutils/properties.h>
27 #include <private/android_filesystem_config.h>
28 #include <hardware_legacy/power.h>
31 #include <sys/mount.h>
33 #include <sys/types.h>
37 /* From a would-be kernel header */
38 #define FIDTRIM _IOWR('f', 128, struct fstrim_range) /* Deep discard trim */
40 #define BENCHMARK_ENABLED 1
42 using android::base::StringPrintf;
47 static const char* kWakeLock = "TrimTask";
49 TrimTask::TrimTask(int flags) : mFlags(flags) {
50 // Collect both fstab and vold volumes
53 VolumeManager* vm = VolumeManager::Instance();
54 std::list<std::string> privateIds;
55 vm->listVolumes(VolumeBase::Type::kPrivate, privateIds);
56 for (const auto& id : privateIds) {
57 auto vol = vm->findVolume(id);
58 if (vol != nullptr && vol->getState() == VolumeBase::State::kMounted) {
59 mPaths.push_back(vol->getPath());
64 TrimTask::~TrimTask() {
67 void TrimTask::addFromFstab() {
68 std::unique_ptr<fstab, decltype(&fs_mgr_free_fstab)> fstab(fs_mgr_read_fstab_default(),
70 struct fstab_rec *prev_rec = NULL;
72 for (int i = 0; i < fstab->num_entries; i++) {
73 /* Skip raw partitions */
74 if (!strcmp(fstab->recs[i].fs_type, "emmc") ||
75 !strcmp(fstab->recs[i].fs_type, "mtd")) {
78 /* Skip read-only filesystems */
79 if (fstab->recs[i].flags & MS_RDONLY) {
82 if (fs_mgr_is_voldmanaged(&fstab->recs[i])) {
83 continue; /* Should we trim fat32 filesystems? */
85 if (fs_mgr_is_notrim(&fstab->recs[i])) {
89 /* Skip the multi-type partitions, which are required to be following each other.
90 * See fs_mgr.c's mount_with_alternatives().
92 if (prev_rec && !strcmp(prev_rec->mount_point, fstab->recs[i].mount_point)) {
96 mPaths.push_back(fstab->recs[i].mount_point);
97 prev_rec = &fstab->recs[i];
101 void TrimTask::start() {
102 mThread = std::thread(&TrimTask::run, this);
105 static void notifyResult(const std::string& path, int64_t bytes, int64_t delta) {
107 + " " + std::to_string(bytes)
108 + " " + std::to_string(delta));
109 VolumeManager::Instance()->getBroadcaster()->sendBroadcast(
110 ResponseCode::TrimResult, res.c_str(), false);
113 void TrimTask::run() {
114 acquire_wake_lock(PARTIAL_WAKE_LOCK, kWakeLock);
116 for (const auto& path : mPaths) {
117 LOG(DEBUG) << "Starting trim of " << path;
119 int fd = open(path.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC | O_NOFOLLOW);
121 PLOG(WARNING) << "Failed to open " << path;
125 struct fstrim_range range;
126 memset(&range, 0, sizeof(range));
127 range.len = ULLONG_MAX;
129 nsecs_t start = systemTime(SYSTEM_TIME_BOOTTIME);
130 if (ioctl(fd, (mFlags & Flags::kDeepTrim) ? FIDTRIM : FITRIM, &range)) {
131 PLOG(WARNING) << "Trim failed on " << path;
132 notifyResult(path, -1, -1);
134 nsecs_t delta = systemTime(SYSTEM_TIME_BOOTTIME) - start;
135 LOG(INFO) << "Trimmed " << range.len << " bytes on " << path
136 << " in " << nanoseconds_to_milliseconds(delta) << "ms";
137 notifyResult(path, range.len, delta);
141 if (mFlags & Flags::kBenchmarkAfter) {
142 #if BENCHMARK_ENABLED
143 BenchmarkPrivate(path);
145 LOG(DEBUG) << "Benchmark disabled";
150 release_wake_lock(kWakeLock);
154 } // namespace android