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 | |
| 17 | #include "Benchmark.h" |
| 18 | #include "BenchmarkGen.h" |
| 19 | #include "VolumeManager.h" |
| 20 | #include "ResponseCode.h" |
| 21 | |
| 22 | #include <base/file.h> |
| 23 | #include <base/logging.h> |
| 24 | #include <cutils/iosched_policy.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 | |
| 31 | using android::base::ReadFileToString; |
| 32 | using android::base::WriteStringToFile; |
| 33 | |
| 34 | namespace android { |
| 35 | namespace vold { |
| 36 | |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame^] | 37 | static void notifyResult(const std::string& path, int64_t create_d, |
| 38 | int64_t drop_d, int64_t run_d, int64_t destroy_d) { |
| 39 | std::string res(path + |
| 40 | + " " + BenchmarkIdent() |
| 41 | + " " + std::to_string(create_d) |
| 42 | + " " + std::to_string(drop_d) |
| 43 | + " " + std::to_string(run_d) |
| 44 | + " " + std::to_string(destroy_d)); |
| 45 | VolumeManager::Instance()->getBroadcaster()->sendBroadcast( |
| 46 | ResponseCode::BenchmarkResult, res.c_str(), false); |
Jeff Sharkey | 5a6bfca | 2015-05-14 20:33:55 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame^] | 49 | static nsecs_t benchmark(const std::string& path) { |
Jeff Sharkey | 5a6bfca | 2015-05-14 20:33:55 -0700 | [diff] [blame] | 50 | errno = 0; |
| 51 | int orig_prio = getpriority(PRIO_PROCESS, 0); |
| 52 | if (errno != 0) { |
| 53 | PLOG(ERROR) << "Failed to getpriority"; |
| 54 | return -1; |
| 55 | } |
| 56 | if (setpriority(PRIO_PROCESS, 0, -10) != 0) { |
| 57 | PLOG(ERROR) << "Failed to setpriority"; |
| 58 | return -1; |
| 59 | } |
| 60 | |
| 61 | IoSchedClass orig_clazz = IoSchedClass_NONE; |
| 62 | int orig_ioprio = 0; |
| 63 | if (android_get_ioprio(0, &orig_clazz, &orig_ioprio)) { |
| 64 | PLOG(ERROR) << "Failed to android_get_ioprio"; |
| 65 | return -1; |
| 66 | } |
| 67 | if (android_set_ioprio(0, IoSchedClass_RT, 0)) { |
| 68 | PLOG(ERROR) << "Failed to android_set_ioprio"; |
| 69 | return -1; |
| 70 | } |
| 71 | |
| 72 | char orig_cwd[PATH_MAX]; |
| 73 | if (getcwd(orig_cwd, PATH_MAX) == NULL) { |
| 74 | PLOG(ERROR) << "Failed getcwd"; |
| 75 | return -1; |
| 76 | } |
| 77 | if (chdir(path.c_str()) != 0) { |
| 78 | PLOG(ERROR) << "Failed chdir"; |
| 79 | return -1; |
| 80 | } |
| 81 | |
Jeff Sharkey | 721e580 | 2015-05-19 11:20:48 -0700 | [diff] [blame] | 82 | sync(); |
| 83 | |
Jeff Sharkey | 5a6bfca | 2015-05-14 20:33:55 -0700 | [diff] [blame] | 84 | LOG(INFO) << "Benchmarking " << path; |
| 85 | nsecs_t start = systemTime(SYSTEM_TIME_BOOTTIME); |
| 86 | |
| 87 | BenchmarkCreate(); |
Jeff Sharkey | 721e580 | 2015-05-19 11:20:48 -0700 | [diff] [blame] | 88 | sync(); |
Jeff Sharkey | 5a6bfca | 2015-05-14 20:33:55 -0700 | [diff] [blame] | 89 | nsecs_t create = systemTime(SYSTEM_TIME_BOOTTIME); |
| 90 | |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame^] | 91 | LOG(VERBOSE) << "Before drop_caches"; |
Jeff Sharkey | 5a6bfca | 2015-05-14 20:33:55 -0700 | [diff] [blame] | 92 | if (!WriteStringToFile("3", "/proc/sys/vm/drop_caches")) { |
| 93 | PLOG(ERROR) << "Failed to drop_caches"; |
| 94 | } |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame^] | 95 | LOG(VERBOSE) << "After drop_caches"; |
Jeff Sharkey | 5a6bfca | 2015-05-14 20:33:55 -0700 | [diff] [blame] | 96 | nsecs_t drop = systemTime(SYSTEM_TIME_BOOTTIME); |
| 97 | |
| 98 | BenchmarkRun(); |
Jeff Sharkey | 721e580 | 2015-05-19 11:20:48 -0700 | [diff] [blame] | 99 | sync(); |
Jeff Sharkey | 5a6bfca | 2015-05-14 20:33:55 -0700 | [diff] [blame] | 100 | nsecs_t run = systemTime(SYSTEM_TIME_BOOTTIME); |
| 101 | |
| 102 | BenchmarkDestroy(); |
Jeff Sharkey | 721e580 | 2015-05-19 11:20:48 -0700 | [diff] [blame] | 103 | sync(); |
Jeff Sharkey | 5a6bfca | 2015-05-14 20:33:55 -0700 | [diff] [blame] | 104 | nsecs_t destroy = systemTime(SYSTEM_TIME_BOOTTIME); |
| 105 | |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame^] | 106 | if (chdir(orig_cwd) != 0) { |
| 107 | PLOG(ERROR) << "Failed to chdir"; |
| 108 | } |
| 109 | if (android_set_ioprio(0, orig_clazz, orig_ioprio)) { |
| 110 | PLOG(ERROR) << "Failed to android_set_ioprio"; |
| 111 | } |
| 112 | if (setpriority(PRIO_PROCESS, 0, orig_prio) != 0) { |
| 113 | PLOG(ERROR) << "Failed to setpriority"; |
| 114 | } |
| 115 | |
Jeff Sharkey | 5a6bfca | 2015-05-14 20:33:55 -0700 | [diff] [blame] | 116 | nsecs_t create_d = create - start; |
| 117 | nsecs_t drop_d = drop - create; |
| 118 | nsecs_t run_d = run - drop; |
| 119 | nsecs_t destroy_d = destroy - run; |
| 120 | |
| 121 | LOG(INFO) << "create took " << nanoseconds_to_milliseconds(create_d) << "ms"; |
| 122 | LOG(INFO) << "drop took " << nanoseconds_to_milliseconds(drop_d) << "ms"; |
| 123 | LOG(INFO) << "run took " << nanoseconds_to_milliseconds(run_d) << "ms"; |
| 124 | LOG(INFO) << "destroy took " << nanoseconds_to_milliseconds(destroy_d) << "ms"; |
| 125 | |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame^] | 126 | notifyResult(path, create_d, drop_d, run_d, destroy_d); |
Jeff Sharkey | 5a6bfca | 2015-05-14 20:33:55 -0700 | [diff] [blame] | 127 | |
Jeff Sharkey | 5a6bfca | 2015-05-14 20:33:55 -0700 | [diff] [blame] | 128 | return run_d; |
| 129 | } |
| 130 | |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame^] | 131 | nsecs_t BenchmarkPrivate(const std::string& path) { |
| 132 | std::string benchPath(path); |
| 133 | benchPath += "/misc"; |
| 134 | if (android::vold::PrepareDir(benchPath, 01771, AID_SYSTEM, AID_MISC)) { |
| 135 | return -1; |
| 136 | } |
| 137 | benchPath += "/vold"; |
| 138 | if (android::vold::PrepareDir(benchPath, 0700, AID_ROOT, AID_ROOT)) { |
| 139 | return -1; |
| 140 | } |
| 141 | benchPath += "/bench"; |
| 142 | if (android::vold::PrepareDir(benchPath, 0700, AID_ROOT, AID_ROOT)) { |
| 143 | return -1; |
| 144 | } |
| 145 | return benchmark(benchPath); |
| 146 | } |
| 147 | |
Jeff Sharkey | 5a6bfca | 2015-05-14 20:33:55 -0700 | [diff] [blame] | 148 | } // namespace vold |
| 149 | } // namespace android |