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 | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame^] | 17 | #include "BenchmarkTask.h" |
Jeff Sharkey | 5a6bfca | 2015-05-14 20:33:55 -0700 | [diff] [blame] | 18 | #include "BenchmarkGen.h" |
| 19 | #include "VolumeManager.h" |
| 20 | #include "ResponseCode.h" |
| 21 | |
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 | 5a6bfca | 2015-05-14 20:33:55 -0700 | [diff] [blame] | 24 | #include <cutils/iosched_policy.h> |
Jeff Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame^] | 25 | #include <hardware_legacy/power.h> |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 26 | #include <private/android_filesystem_config.h> |
Jeff Sharkey | 5a6bfca | 2015-05-14 20:33:55 -0700 | [diff] [blame] | 27 | |
| 28 | #include <sys/time.h> |
| 29 | #include <sys/resource.h> |
Jeff Sharkey | 721e580 | 2015-05-19 11:20:48 -0700 | [diff] [blame] | 30 | #include <unistd.h> |
Jeff Sharkey | 5a6bfca | 2015-05-14 20:33:55 -0700 | [diff] [blame] | 31 | |
Jeff Sharkey | 81f55c6 | 2015-07-07 14:37:03 -0700 | [diff] [blame] | 32 | #define ENABLE_DROP_CACHES 1 |
| 33 | |
Jeff Sharkey | 5a6bfca | 2015-05-14 20:33:55 -0700 | [diff] [blame] | 34 | using android::base::ReadFileToString; |
| 35 | using android::base::WriteStringToFile; |
| 36 | |
| 37 | namespace android { |
| 38 | namespace vold { |
| 39 | |
Jeff Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame^] | 40 | static const char* kWakeLock = "BenchmarkTask"; |
| 41 | |
| 42 | BenchmarkTask::BenchmarkTask(const std::string& path, |
| 43 | const android::sp<android::os::IVoldTaskListener>& listener) : |
| 44 | mPath(path), mListener(listener) { |
Jeff Sharkey | 5a6bfca | 2015-05-14 20:33:55 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Jeff Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame^] | 47 | BenchmarkTask::~BenchmarkTask() { |
| 48 | } |
| 49 | |
| 50 | void BenchmarkTask::start() { |
| 51 | mThread = std::thread(&BenchmarkTask::run, this); |
| 52 | } |
| 53 | |
| 54 | static status_t runInternal(const std::string& rootPath, android::os::PersistableBundle& extras) { |
| 55 | auto path = rootPath; |
| 56 | path += "/misc"; |
| 57 | if (android::vold::PrepareDir(path, 01771, AID_SYSTEM, AID_MISC)) { |
| 58 | return -1; |
| 59 | } |
| 60 | path += "/vold"; |
| 61 | if (android::vold::PrepareDir(path, 0700, AID_ROOT, AID_ROOT)) { |
| 62 | return -1; |
| 63 | } |
| 64 | path += "/bench"; |
| 65 | if (android::vold::PrepareDir(path, 0700, AID_ROOT, AID_ROOT)) { |
| 66 | return -1; |
| 67 | } |
| 68 | |
Jeff Sharkey | 5a6bfca | 2015-05-14 20:33:55 -0700 | [diff] [blame] | 69 | errno = 0; |
| 70 | int orig_prio = getpriority(PRIO_PROCESS, 0); |
| 71 | if (errno != 0) { |
| 72 | PLOG(ERROR) << "Failed to getpriority"; |
| 73 | return -1; |
| 74 | } |
| 75 | if (setpriority(PRIO_PROCESS, 0, -10) != 0) { |
| 76 | PLOG(ERROR) << "Failed to setpriority"; |
| 77 | return -1; |
| 78 | } |
| 79 | |
| 80 | IoSchedClass orig_clazz = IoSchedClass_NONE; |
| 81 | int orig_ioprio = 0; |
| 82 | if (android_get_ioprio(0, &orig_clazz, &orig_ioprio)) { |
| 83 | PLOG(ERROR) << "Failed to android_get_ioprio"; |
| 84 | return -1; |
| 85 | } |
| 86 | if (android_set_ioprio(0, IoSchedClass_RT, 0)) { |
| 87 | PLOG(ERROR) << "Failed to android_set_ioprio"; |
| 88 | return -1; |
| 89 | } |
| 90 | |
| 91 | char orig_cwd[PATH_MAX]; |
| 92 | if (getcwd(orig_cwd, PATH_MAX) == NULL) { |
| 93 | PLOG(ERROR) << "Failed getcwd"; |
| 94 | return -1; |
| 95 | } |
| 96 | if (chdir(path.c_str()) != 0) { |
| 97 | PLOG(ERROR) << "Failed chdir"; |
| 98 | return -1; |
| 99 | } |
| 100 | |
Jeff Sharkey | 721e580 | 2015-05-19 11:20:48 -0700 | [diff] [blame] | 101 | sync(); |
| 102 | |
Jeff Sharkey | 5a6bfca | 2015-05-14 20:33:55 -0700 | [diff] [blame] | 103 | LOG(INFO) << "Benchmarking " << path; |
| 104 | nsecs_t start = systemTime(SYSTEM_TIME_BOOTTIME); |
| 105 | |
| 106 | BenchmarkCreate(); |
Jeff Sharkey | 721e580 | 2015-05-19 11:20:48 -0700 | [diff] [blame] | 107 | sync(); |
Jeff Sharkey | 5a6bfca | 2015-05-14 20:33:55 -0700 | [diff] [blame] | 108 | nsecs_t create = systemTime(SYSTEM_TIME_BOOTTIME); |
| 109 | |
Jeff Sharkey | 81f55c6 | 2015-07-07 14:37:03 -0700 | [diff] [blame] | 110 | #if ENABLE_DROP_CACHES |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 111 | LOG(VERBOSE) << "Before drop_caches"; |
Jeff Sharkey | 5a6bfca | 2015-05-14 20:33:55 -0700 | [diff] [blame] | 112 | if (!WriteStringToFile("3", "/proc/sys/vm/drop_caches")) { |
| 113 | PLOG(ERROR) << "Failed to drop_caches"; |
| 114 | } |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 115 | LOG(VERBOSE) << "After drop_caches"; |
Jeff Sharkey | 81f55c6 | 2015-07-07 14:37:03 -0700 | [diff] [blame] | 116 | #endif |
Jeff Sharkey | 5a6bfca | 2015-05-14 20:33:55 -0700 | [diff] [blame] | 117 | nsecs_t drop = systemTime(SYSTEM_TIME_BOOTTIME); |
| 118 | |
| 119 | BenchmarkRun(); |
Jeff Sharkey | 721e580 | 2015-05-19 11:20:48 -0700 | [diff] [blame] | 120 | sync(); |
Jeff Sharkey | 5a6bfca | 2015-05-14 20:33:55 -0700 | [diff] [blame] | 121 | nsecs_t run = systemTime(SYSTEM_TIME_BOOTTIME); |
| 122 | |
| 123 | BenchmarkDestroy(); |
Jeff Sharkey | 721e580 | 2015-05-19 11:20:48 -0700 | [diff] [blame] | 124 | sync(); |
Jeff Sharkey | 5a6bfca | 2015-05-14 20:33:55 -0700 | [diff] [blame] | 125 | nsecs_t destroy = systemTime(SYSTEM_TIME_BOOTTIME); |
| 126 | |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 127 | if (chdir(orig_cwd) != 0) { |
| 128 | PLOG(ERROR) << "Failed to chdir"; |
| 129 | } |
| 130 | if (android_set_ioprio(0, orig_clazz, orig_ioprio)) { |
| 131 | PLOG(ERROR) << "Failed to android_set_ioprio"; |
| 132 | } |
| 133 | if (setpriority(PRIO_PROCESS, 0, orig_prio) != 0) { |
| 134 | PLOG(ERROR) << "Failed to setpriority"; |
| 135 | } |
| 136 | |
Jeff Sharkey | 5a6bfca | 2015-05-14 20:33:55 -0700 | [diff] [blame] | 137 | nsecs_t create_d = create - start; |
| 138 | nsecs_t drop_d = drop - create; |
| 139 | nsecs_t run_d = run - drop; |
| 140 | nsecs_t destroy_d = destroy - run; |
| 141 | |
| 142 | LOG(INFO) << "create took " << nanoseconds_to_milliseconds(create_d) << "ms"; |
| 143 | LOG(INFO) << "drop took " << nanoseconds_to_milliseconds(drop_d) << "ms"; |
| 144 | LOG(INFO) << "run took " << nanoseconds_to_milliseconds(run_d) << "ms"; |
| 145 | LOG(INFO) << "destroy took " << nanoseconds_to_milliseconds(destroy_d) << "ms"; |
| 146 | |
Jeff Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame^] | 147 | extras.putString(String16("path"), String16(path.c_str())); |
| 148 | extras.putString(String16("ident"), String16(BenchmarkIdent().c_str())); |
| 149 | extras.putLong(String16("create"), create_d); |
| 150 | extras.putLong(String16("drop"), drop_d); |
| 151 | extras.putLong(String16("run"), run_d); |
| 152 | extras.putLong(String16("destroy"), destroy_d); |
Jeff Sharkey | 5a6bfca | 2015-05-14 20:33:55 -0700 | [diff] [blame] | 153 | |
Jeff Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame^] | 154 | return 0; |
Jeff Sharkey | 5a6bfca | 2015-05-14 20:33:55 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Jeff Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame^] | 157 | void BenchmarkTask::run() { |
| 158 | acquire_wake_lock(PARTIAL_WAKE_LOCK, kWakeLock); |
| 159 | |
| 160 | android::os::PersistableBundle extras; |
| 161 | status_t res = runInternal(mPath, extras); |
| 162 | if (mListener) { |
| 163 | mListener->onFinished(res, extras); |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 164 | } |
Jeff Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame^] | 165 | |
| 166 | release_wake_lock(kWakeLock); |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Jeff Sharkey | 5a6bfca | 2015-05-14 20:33:55 -0700 | [diff] [blame] | 169 | } // namespace vold |
| 170 | } // namespace android |