blob: 0770da72040a869ba506fb86615f5e55ab806cf3 [file] [log] [blame]
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -07001/*
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 Sharkey01a0e7f2017-10-17 16:06:32 -060017#include "Benchmark.h"
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -070018#include "BenchmarkGen.h"
19#include "VolumeManager.h"
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -070020
Jeff Sharkeycbcb2922017-11-06 13:53:59 -070021#include <android-base/chrono_utils.h>
Elliott Hughes7e128fb2015-12-04 15:50:53 -080022#include <android-base/file.h>
23#include <android-base/logging.h>
Jeff Sharkeycbcb2922017-11-06 13:53:59 -070024
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -070025#include <cutils/iosched_policy.h>
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070026#include <private/android_filesystem_config.h>
Tri Vo15bbe222019-06-21 12:21:48 -070027#include <wakelock/wakelock.h>
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -070028
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -060029#include <thread>
30
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -070031#include <sys/resource.h>
Paul Crowley14c8c072018-09-18 13:30:21 -070032#include <sys/time.h>
Jeff Sharkey721e5802015-05-19 11:20:48 -070033#include <unistd.h>
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -070034
35using android::base::ReadFileToString;
36using android::base::WriteStringToFile;
37
38namespace android {
39namespace vold {
40
Jeff Sharkeycbcb2922017-11-06 13:53:59 -070041// Benchmark currently uses chdir(), which means we can only
42// safely run one at a time.
43static std::mutex kBenchmarkLock;
44
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -060045static const char* kWakeLock = "Benchmark";
Jeff Sharkey52f7a912017-09-15 12:57:44 -060046
Jeff Sharkeycbcb2922017-11-06 13:53:59 -070047// Reasonable cards are able to complete the create/run stages
48// in under 20 seconds.
49constexpr auto kTimeout = 20s;
50
51// RAII class for boosting device performance during benchmarks.
52class PerformanceBoost {
Paul Crowley14c8c072018-09-18 13:30:21 -070053 private:
Jeff Sharkeycbcb2922017-11-06 13:53:59 -070054 int orig_prio;
55 int orig_ioprio;
56 IoSchedClass orig_clazz;
57
Paul Crowley14c8c072018-09-18 13:30:21 -070058 public:
Jeff Sharkeycbcb2922017-11-06 13:53:59 -070059 PerformanceBoost() {
60 errno = 0;
61 orig_prio = getpriority(PRIO_PROCESS, 0);
62 if (errno != 0) {
63 PLOG(WARNING) << "Failed to getpriority";
64 orig_prio = 0;
65 }
66 if (setpriority(PRIO_PROCESS, 0, -10) != 0) {
67 PLOG(WARNING) << "Failed to setpriority";
68 }
69 if (android_get_ioprio(0, &orig_clazz, &orig_ioprio)) {
70 PLOG(WARNING) << "Failed to android_get_ioprio";
71 orig_ioprio = 0;
72 orig_clazz = IoSchedClass_NONE;
73 }
74 if (android_set_ioprio(0, IoSchedClass_RT, 0)) {
75 PLOG(WARNING) << "Failed to android_set_ioprio";
76 }
77 }
78
79 ~PerformanceBoost() {
80 if (android_set_ioprio(0, orig_clazz, orig_ioprio)) {
81 PLOG(WARNING) << "Failed to android_set_ioprio";
82 }
83 if (setpriority(PRIO_PROCESS, 0, orig_prio) != 0) {
84 PLOG(WARNING) << "Failed to setpriority";
85 }
86 }
87};
88
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -060089static status_t benchmarkInternal(const std::string& rootPath,
Paul Crowley14c8c072018-09-18 13:30:21 -070090 const android::sp<android::os::IVoldTaskListener>& listener,
91 android::os::PersistableBundle* extras) {
Jeff Sharkeycbcb2922017-11-06 13:53:59 -070092 status_t res = 0;
93
Jeff Sharkey52f7a912017-09-15 12:57:44 -060094 auto path = rootPath;
95 path += "/misc";
96 if (android::vold::PrepareDir(path, 01771, AID_SYSTEM, AID_MISC)) {
97 return -1;
98 }
99 path += "/vold";
100 if (android::vold::PrepareDir(path, 0700, AID_ROOT, AID_ROOT)) {
101 return -1;
102 }
103 path += "/bench";
104 if (android::vold::PrepareDir(path, 0700, AID_ROOT, AID_ROOT)) {
105 return -1;
106 }
107
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700108 char orig_cwd[PATH_MAX];
109 if (getcwd(orig_cwd, PATH_MAX) == NULL) {
110 PLOG(ERROR) << "Failed getcwd";
111 return -1;
112 }
113 if (chdir(path.c_str()) != 0) {
114 PLOG(ERROR) << "Failed chdir";
115 return -1;
116 }
117
Jeff Sharkey721e5802015-05-19 11:20:48 -0700118 sync();
119
Jeff Sharkeycbcb2922017-11-06 13:53:59 -0700120 extras->putString(String16("path"), String16(path.c_str()));
121 extras->putString(String16("ident"), String16(BenchmarkIdent().c_str()));
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700122
Jeff Sharkeycbcb2922017-11-06 13:53:59 -0700123 // Always create
124 {
125 android::base::Timer timer;
126 LOG(INFO) << "Creating " << path;
127 res |= BenchmarkCreate([&](int progress) -> bool {
128 if (listener) {
129 listener->onStatus(progress, *extras);
130 }
131 return (timer.duration() < kTimeout);
132 });
133 sync();
134 if (res == OK) extras->putLong(String16("create"), timer.duration().count());
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700135 }
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700136
Jeff Sharkeycbcb2922017-11-06 13:53:59 -0700137 // Only drop when we haven't aborted
138 if (res == OK) {
139 android::base::Timer timer;
Sudheer Shanka4b6ca4e2018-09-21 10:54:54 -0700140 LOG(DEBUG) << "Before drop_caches";
Jeff Sharkeycbcb2922017-11-06 13:53:59 -0700141 if (!WriteStringToFile("3", "/proc/sys/vm/drop_caches")) {
142 PLOG(ERROR) << "Failed to drop_caches";
143 res = -1;
144 }
Sudheer Shanka4b6ca4e2018-09-21 10:54:54 -0700145 LOG(DEBUG) << "After drop_caches";
Jeff Sharkeycbcb2922017-11-06 13:53:59 -0700146 sync();
147 if (res == OK) extras->putLong(String16("drop"), timer.duration().count());
148 }
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700149
Jeff Sharkeycbcb2922017-11-06 13:53:59 -0700150 // Only run when we haven't aborted
151 if (res == OK) {
152 android::base::Timer timer;
153 LOG(INFO) << "Running " << path;
154 res |= BenchmarkRun([&](int progress) -> bool {
155 if (listener) {
156 listener->onStatus(progress, *extras);
157 }
158 return (timer.duration() < kTimeout);
159 });
160 sync();
161 if (res == OK) extras->putLong(String16("run"), timer.duration().count());
162 }
163
164 // Always destroy
165 {
166 android::base::Timer timer;
167 LOG(INFO) << "Destroying " << path;
168 res |= BenchmarkDestroy();
169 sync();
170 if (res == OK) extras->putLong(String16("destroy"), timer.duration().count());
171 }
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700172
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700173 if (chdir(orig_cwd) != 0) {
174 PLOG(ERROR) << "Failed to chdir";
Jeff Sharkeycbcb2922017-11-06 13:53:59 -0700175 return -1;
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700176 }
177
Jeff Sharkeycbcb2922017-11-06 13:53:59 -0700178 return res;
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700179}
180
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600181void Benchmark(const std::string& path,
Paul Crowley14c8c072018-09-18 13:30:21 -0700182 const android::sp<android::os::IVoldTaskListener>& listener) {
Jeff Sharkeycbcb2922017-11-06 13:53:59 -0700183 std::lock_guard<std::mutex> lock(kBenchmarkLock);
Tri Vo15bbe222019-06-21 12:21:48 -0700184 android::wakelock::WakeLock wl{kWakeLock};
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600185
Jeff Sharkeycbcb2922017-11-06 13:53:59 -0700186 PerformanceBoost boost;
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600187 android::os::PersistableBundle extras;
Jeff Sharkeycbcb2922017-11-06 13:53:59 -0700188
189 status_t res = benchmarkInternal(path, listener, &extras);
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600190 if (listener) {
191 listener->onFinished(res, extras);
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700192 }
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700193}
194
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700195} // namespace vold
196} // namespace android