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