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> |
| 25 | |
| 26 | #include <sys/time.h> |
| 27 | #include <sys/resource.h> |
Jeff Sharkey | 721e580 | 2015-05-19 11:20:48 -0700 | [diff] [blame^] | 28 | #include <unistd.h> |
Jeff Sharkey | 5a6bfca | 2015-05-14 20:33:55 -0700 | [diff] [blame] | 29 | |
| 30 | using android::base::ReadFileToString; |
| 31 | using android::base::WriteStringToFile; |
| 32 | |
| 33 | namespace android { |
| 34 | namespace vold { |
| 35 | |
| 36 | static std::string simpleRead(const std::string& path) { |
| 37 | std::string tmp; |
| 38 | ReadFileToString(path, &tmp); |
| 39 | tmp.erase(tmp.find_last_not_of(" \n\r") + 1); |
| 40 | return tmp; |
| 41 | } |
| 42 | |
| 43 | nsecs_t Benchmark(const std::string& path, const std::string& sysPath) { |
| 44 | errno = 0; |
| 45 | int orig_prio = getpriority(PRIO_PROCESS, 0); |
| 46 | if (errno != 0) { |
| 47 | PLOG(ERROR) << "Failed to getpriority"; |
| 48 | return -1; |
| 49 | } |
| 50 | if (setpriority(PRIO_PROCESS, 0, -10) != 0) { |
| 51 | PLOG(ERROR) << "Failed to setpriority"; |
| 52 | return -1; |
| 53 | } |
| 54 | |
| 55 | IoSchedClass orig_clazz = IoSchedClass_NONE; |
| 56 | int orig_ioprio = 0; |
| 57 | if (android_get_ioprio(0, &orig_clazz, &orig_ioprio)) { |
| 58 | PLOG(ERROR) << "Failed to android_get_ioprio"; |
| 59 | return -1; |
| 60 | } |
| 61 | if (android_set_ioprio(0, IoSchedClass_RT, 0)) { |
| 62 | PLOG(ERROR) << "Failed to android_set_ioprio"; |
| 63 | return -1; |
| 64 | } |
| 65 | |
| 66 | char orig_cwd[PATH_MAX]; |
| 67 | if (getcwd(orig_cwd, PATH_MAX) == NULL) { |
| 68 | PLOG(ERROR) << "Failed getcwd"; |
| 69 | return -1; |
| 70 | } |
| 71 | if (chdir(path.c_str()) != 0) { |
| 72 | PLOG(ERROR) << "Failed chdir"; |
| 73 | return -1; |
| 74 | } |
| 75 | |
Jeff Sharkey | 721e580 | 2015-05-19 11:20:48 -0700 | [diff] [blame^] | 76 | sync(); |
| 77 | |
Jeff Sharkey | 5a6bfca | 2015-05-14 20:33:55 -0700 | [diff] [blame] | 78 | LOG(INFO) << "Benchmarking " << path; |
| 79 | nsecs_t start = systemTime(SYSTEM_TIME_BOOTTIME); |
| 80 | |
| 81 | BenchmarkCreate(); |
Jeff Sharkey | 721e580 | 2015-05-19 11:20:48 -0700 | [diff] [blame^] | 82 | sync(); |
Jeff Sharkey | 5a6bfca | 2015-05-14 20:33:55 -0700 | [diff] [blame] | 83 | nsecs_t create = systemTime(SYSTEM_TIME_BOOTTIME); |
| 84 | |
| 85 | if (!WriteStringToFile("3", "/proc/sys/vm/drop_caches")) { |
| 86 | PLOG(ERROR) << "Failed to drop_caches"; |
| 87 | } |
| 88 | nsecs_t drop = systemTime(SYSTEM_TIME_BOOTTIME); |
| 89 | |
| 90 | BenchmarkRun(); |
Jeff Sharkey | 721e580 | 2015-05-19 11:20:48 -0700 | [diff] [blame^] | 91 | sync(); |
Jeff Sharkey | 5a6bfca | 2015-05-14 20:33:55 -0700 | [diff] [blame] | 92 | nsecs_t run = systemTime(SYSTEM_TIME_BOOTTIME); |
| 93 | |
| 94 | BenchmarkDestroy(); |
Jeff Sharkey | 721e580 | 2015-05-19 11:20:48 -0700 | [diff] [blame^] | 95 | sync(); |
Jeff Sharkey | 5a6bfca | 2015-05-14 20:33:55 -0700 | [diff] [blame] | 96 | nsecs_t destroy = systemTime(SYSTEM_TIME_BOOTTIME); |
| 97 | |
| 98 | nsecs_t create_d = create - start; |
| 99 | nsecs_t drop_d = drop - create; |
| 100 | nsecs_t run_d = run - drop; |
| 101 | nsecs_t destroy_d = destroy - run; |
| 102 | |
| 103 | LOG(INFO) << "create took " << nanoseconds_to_milliseconds(create_d) << "ms"; |
| 104 | LOG(INFO) << "drop took " << nanoseconds_to_milliseconds(drop_d) << "ms"; |
| 105 | LOG(INFO) << "run took " << nanoseconds_to_milliseconds(run_d) << "ms"; |
| 106 | LOG(INFO) << "destroy took " << nanoseconds_to_milliseconds(destroy_d) << "ms"; |
| 107 | |
| 108 | std::string detail; |
| 109 | detail += "id=" + BenchmarkIdent() |
| 110 | + ",cr=" + std::to_string(create_d) |
| 111 | + ",dr=" + std::to_string(drop_d) |
| 112 | + ",ru=" + std::to_string(run_d) |
| 113 | + ",de=" + std::to_string(destroy_d) |
| 114 | + ",si=" + simpleRead(sysPath + "/size") |
| 115 | + ",ve=" + simpleRead(sysPath + "/device/vendor") |
| 116 | + ",mo=" + simpleRead(sysPath + "/device/model") |
Jeff Sharkey | 721e580 | 2015-05-19 11:20:48 -0700 | [diff] [blame^] | 117 | + ",csd=" + simpleRead(sysPath + "/device/csd"); |
Jeff Sharkey | 5a6bfca | 2015-05-14 20:33:55 -0700 | [diff] [blame] | 118 | |
| 119 | // Scrub CRC and serial number out of CID |
| 120 | std::string cid = simpleRead(sysPath + "/device/cid"); |
| 121 | if (cid.length() == 32) { |
| 122 | cid.erase(32, 1); |
| 123 | cid.erase(18, 8); |
| 124 | detail += ",cid=" + cid; |
| 125 | } |
| 126 | |
| 127 | VolumeManager::Instance()->getBroadcaster()->sendBroadcast( |
| 128 | ResponseCode::BenchmarkResult, detail.c_str(), false); |
| 129 | |
| 130 | if (chdir(orig_cwd) != 0) { |
| 131 | PLOG(ERROR) << "Failed to chdir"; |
| 132 | } |
| 133 | if (android_set_ioprio(0, orig_clazz, orig_ioprio)) { |
| 134 | PLOG(ERROR) << "Failed to android_set_ioprio"; |
| 135 | } |
| 136 | if (setpriority(PRIO_PROCESS, 0, orig_prio) != 0) { |
| 137 | PLOG(ERROR) << "Failed to setpriority"; |
| 138 | } |
| 139 | return run_d; |
| 140 | } |
| 141 | |
| 142 | } // namespace vold |
| 143 | } // namespace android |