Jeff Sharkey | 5a6bfca | 2015-05-14 20:33:55 -0700 | [diff] [blame] | 1 | /* |
| 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 Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 17 | #include "Benchmark.h" |
Jeff Sharkey | 5a6bfca | 2015-05-14 20:33:55 -0700 | [diff] [blame] | 18 | #include "BenchmarkGen.h" |
| 19 | #include "VolumeManager.h" |
Jeff Sharkey | 5a6bfca | 2015-05-14 20:33:55 -0700 | [diff] [blame] | 20 | |
Jeff Sharkey | cbcb292 | 2017-11-06 13:53:59 -0700 | [diff] [blame] | 21 | #include <android-base/chrono_utils.h> |
Elliott Hughes | 7e128fb | 2015-12-04 15:50:53 -0800 | [diff] [blame] | 22 | #include <android-base/file.h> |
| 23 | #include <android-base/logging.h> |
Jeff Sharkey | cbcb292 | 2017-11-06 13:53:59 -0700 | [diff] [blame] | 24 | |
Jeff Sharkey | 5a6bfca | 2015-05-14 20:33:55 -0700 | [diff] [blame] | 25 | #include <cutils/iosched_policy.h> |
Jeff Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame] | 26 | #include <hardware_legacy/power.h> |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 27 | #include <private/android_filesystem_config.h> |
Jeff Sharkey | 5a6bfca | 2015-05-14 20:33:55 -0700 | [diff] [blame] | 28 | |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 29 | #include <thread> |
| 30 | |
Jeff Sharkey | 5a6bfca | 2015-05-14 20:33:55 -0700 | [diff] [blame] | 31 | #include <sys/time.h> |
| 32 | #include <sys/resource.h> |
Jeff Sharkey | 721e580 | 2015-05-19 11:20:48 -0700 | [diff] [blame] | 33 | #include <unistd.h> |
Jeff Sharkey | 5a6bfca | 2015-05-14 20:33:55 -0700 | [diff] [blame] | 34 | |
| 35 | using android::base::ReadFileToString; |
| 36 | using android::base::WriteStringToFile; |
| 37 | |
| 38 | namespace android { |
| 39 | namespace vold { |
| 40 | |
Jeff Sharkey | cbcb292 | 2017-11-06 13:53:59 -0700 | [diff] [blame] | 41 | // Benchmark currently uses chdir(), which means we can only |
| 42 | // safely run one at a time. |
| 43 | static std::mutex kBenchmarkLock; |
| 44 | |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 45 | static const char* kWakeLock = "Benchmark"; |
Jeff Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame] | 46 | |
Jeff Sharkey | cbcb292 | 2017-11-06 13:53:59 -0700 | [diff] [blame] | 47 | // Reasonable cards are able to complete the create/run stages |
| 48 | // in under 20 seconds. |
| 49 | constexpr auto kTimeout = 20s; |
| 50 | |
| 51 | // RAII class for boosting device performance during benchmarks. |
| 52 | class PerformanceBoost { |
| 53 | private: |
| 54 | int orig_prio; |
| 55 | int orig_ioprio; |
| 56 | IoSchedClass orig_clazz; |
| 57 | |
| 58 | public: |
| 59 | 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 Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 89 | static status_t benchmarkInternal(const std::string& rootPath, |
Jeff Sharkey | cbcb292 | 2017-11-06 13:53:59 -0700 | [diff] [blame] | 90 | const android::sp<android::os::IVoldTaskListener>& listener, |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 91 | android::os::PersistableBundle* extras) { |
Jeff Sharkey | cbcb292 | 2017-11-06 13:53:59 -0700 | [diff] [blame] | 92 | status_t res = 0; |
| 93 | |
Jeff Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame] | 94 | 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 Sharkey | 5a6bfca | 2015-05-14 20:33:55 -0700 | [diff] [blame] | 108 | 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 Sharkey | 721e580 | 2015-05-19 11:20:48 -0700 | [diff] [blame] | 118 | sync(); |
| 119 | |
Jeff Sharkey | cbcb292 | 2017-11-06 13:53:59 -0700 | [diff] [blame] | 120 | extras->putString(String16("path"), String16(path.c_str())); |
| 121 | extras->putString(String16("ident"), String16(BenchmarkIdent().c_str())); |
Jeff Sharkey | 5a6bfca | 2015-05-14 20:33:55 -0700 | [diff] [blame] | 122 | |
Jeff Sharkey | cbcb292 | 2017-11-06 13:53:59 -0700 | [diff] [blame] | 123 | // 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 Sharkey | 5a6bfca | 2015-05-14 20:33:55 -0700 | [diff] [blame] | 135 | } |
Jeff Sharkey | 5a6bfca | 2015-05-14 20:33:55 -0700 | [diff] [blame] | 136 | |
Jeff Sharkey | cbcb292 | 2017-11-06 13:53:59 -0700 | [diff] [blame] | 137 | // Only drop when we haven't aborted |
| 138 | if (res == OK) { |
| 139 | android::base::Timer timer; |
| 140 | LOG(VERBOSE) << "Before drop_caches"; |
| 141 | if (!WriteStringToFile("3", "/proc/sys/vm/drop_caches")) { |
| 142 | PLOG(ERROR) << "Failed to drop_caches"; |
| 143 | res = -1; |
| 144 | } |
| 145 | LOG(VERBOSE) << "After drop_caches"; |
| 146 | sync(); |
| 147 | if (res == OK) extras->putLong(String16("drop"), timer.duration().count()); |
| 148 | } |
Jeff Sharkey | 5a6bfca | 2015-05-14 20:33:55 -0700 | [diff] [blame] | 149 | |
Jeff Sharkey | cbcb292 | 2017-11-06 13:53:59 -0700 | [diff] [blame] | 150 | // 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 Sharkey | 5a6bfca | 2015-05-14 20:33:55 -0700 | [diff] [blame] | 172 | |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 173 | if (chdir(orig_cwd) != 0) { |
| 174 | PLOG(ERROR) << "Failed to chdir"; |
Jeff Sharkey | cbcb292 | 2017-11-06 13:53:59 -0700 | [diff] [blame] | 175 | return -1; |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Jeff Sharkey | cbcb292 | 2017-11-06 13:53:59 -0700 | [diff] [blame] | 178 | return res; |
Jeff Sharkey | 5a6bfca | 2015-05-14 20:33:55 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 181 | void Benchmark(const std::string& path, |
| 182 | const android::sp<android::os::IVoldTaskListener>& listener) { |
Jeff Sharkey | cbcb292 | 2017-11-06 13:53:59 -0700 | [diff] [blame] | 183 | std::lock_guard<std::mutex> lock(kBenchmarkLock); |
Jeff Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame] | 184 | acquire_wake_lock(PARTIAL_WAKE_LOCK, kWakeLock); |
| 185 | |
Jeff Sharkey | cbcb292 | 2017-11-06 13:53:59 -0700 | [diff] [blame] | 186 | PerformanceBoost boost; |
Jeff Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame] | 187 | android::os::PersistableBundle extras; |
Jeff Sharkey | cbcb292 | 2017-11-06 13:53:59 -0700 | [diff] [blame] | 188 | |
| 189 | status_t res = benchmarkInternal(path, listener, &extras); |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 190 | if (listener) { |
| 191 | listener->onFinished(res, extras); |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 192 | } |
Jeff Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame] | 193 | |
| 194 | release_wake_lock(kWakeLock); |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Jeff Sharkey | 5a6bfca | 2015-05-14 20:33:55 -0700 | [diff] [blame] | 197 | } // namespace vold |
| 198 | } // namespace android |