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