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 "Benchmark.h"
18 #include "BenchmarkGen.h"
19 #include "VolumeManager.h"
21 #include <android-base/chrono_utils.h>
22 #include <android-base/file.h>
23 #include <android-base/logging.h>
25 #include <cutils/iosched_policy.h>
26 #include <private/android_filesystem_config.h>
27 #include <wakelock/wakelock.h>
31 #include <sys/resource.h>
35 using android::base::ReadFileToString;
36 using android::base::WriteStringToFile;
41 // Benchmark currently uses chdir(), which means we can only
42 // safely run one at a time.
43 static std::mutex kBenchmarkLock;
45 static const char* kWakeLock = "Benchmark";
47 // Reasonable cards are able to complete the create/run stages
48 // in under 20 seconds.
49 constexpr auto kTimeout = 20s;
51 // RAII class for boosting device performance during benchmarks.
52 class PerformanceBoost {
56 IoSchedClass orig_clazz;
61 orig_prio = getpriority(PRIO_PROCESS, 0);
63 PLOG(WARNING) << "Failed to getpriority";
66 if (setpriority(PRIO_PROCESS, 0, -10) != 0) {
67 PLOG(WARNING) << "Failed to setpriority";
69 if (android_get_ioprio(0, &orig_clazz, &orig_ioprio)) {
70 PLOG(WARNING) << "Failed to android_get_ioprio";
72 orig_clazz = IoSchedClass_NONE;
74 if (android_set_ioprio(0, IoSchedClass_RT, 0)) {
75 PLOG(WARNING) << "Failed to android_set_ioprio";
80 if (android_set_ioprio(0, orig_clazz, orig_ioprio)) {
81 PLOG(WARNING) << "Failed to android_set_ioprio";
83 if (setpriority(PRIO_PROCESS, 0, orig_prio) != 0) {
84 PLOG(WARNING) << "Failed to setpriority";
89 static status_t benchmarkInternal(const std::string& rootPath,
90 const android::sp<android::os::IVoldTaskListener>& listener,
91 android::os::PersistableBundle* extras) {
96 if (android::vold::PrepareDir(path, 01771, AID_SYSTEM, AID_MISC)) {
100 if (android::vold::PrepareDir(path, 0700, AID_ROOT, AID_ROOT)) {
104 if (android::vold::PrepareDir(path, 0700, AID_ROOT, AID_ROOT)) {
108 char orig_cwd[PATH_MAX];
109 if (getcwd(orig_cwd, PATH_MAX) == NULL) {
110 PLOG(ERROR) << "Failed getcwd";
113 if (chdir(path.c_str()) != 0) {
114 PLOG(ERROR) << "Failed chdir";
120 extras->putString(String16("path"), String16(path.c_str()));
121 extras->putString(String16("ident"), String16(BenchmarkIdent().c_str()));
125 android::base::Timer timer;
126 LOG(INFO) << "Creating " << path;
127 res |= BenchmarkCreate([&](int progress) -> bool {
129 listener->onStatus(progress, *extras);
131 return (timer.duration() < kTimeout);
134 if (res == OK) extras->putLong(String16("create"), timer.duration().count());
137 // Only drop when we haven't aborted
139 android::base::Timer timer;
140 LOG(DEBUG) << "Before drop_caches";
141 if (!WriteStringToFile("3", "/proc/sys/vm/drop_caches")) {
142 PLOG(ERROR) << "Failed to drop_caches";
145 LOG(DEBUG) << "After drop_caches";
147 if (res == OK) extras->putLong(String16("drop"), timer.duration().count());
150 // Only run when we haven't aborted
152 android::base::Timer timer;
153 LOG(INFO) << "Running " << path;
154 res |= BenchmarkRun([&](int progress) -> bool {
156 listener->onStatus(progress, *extras);
158 return (timer.duration() < kTimeout);
161 if (res == OK) extras->putLong(String16("run"), timer.duration().count());
166 android::base::Timer timer;
167 LOG(INFO) << "Destroying " << path;
168 res |= BenchmarkDestroy();
170 if (res == OK) extras->putLong(String16("destroy"), timer.duration().count());
173 if (chdir(orig_cwd) != 0) {
174 PLOG(ERROR) << "Failed to chdir";
181 void Benchmark(const std::string& path,
182 const android::sp<android::os::IVoldTaskListener>& listener) {
183 std::lock_guard<std::mutex> lock(kBenchmarkLock);
184 auto wl = android::wakelock::WakeLock::tryGet(kWakeLock);
185 if (!wl.has_value()) {
189 PerformanceBoost boost;
190 android::os::PersistableBundle extras;
192 status_t res = benchmarkInternal(path, listener, &extras);
194 listener->onFinished(res, extras);
199 } // namespace android