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