blob: 63b4dd3c6e6927203a0eb2288069ed0445f35845 [file] [log] [blame]
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -07001/*
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 Sharkey01a0e7f2017-10-17 16:06:32 -060017#include "Benchmark.h"
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -070018#include "BenchmarkGen.h"
19#include "VolumeManager.h"
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -070020
Elliott Hughes7e128fb2015-12-04 15:50:53 -080021#include <android-base/file.h>
22#include <android-base/logging.h>
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -070023#include <cutils/iosched_policy.h>
Jeff Sharkey52f7a912017-09-15 12:57:44 -060024#include <hardware_legacy/power.h>
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070025#include <private/android_filesystem_config.h>
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -070026
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -060027#include <thread>
28
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -070029#include <sys/time.h>
30#include <sys/resource.h>
Jeff Sharkey721e5802015-05-19 11:20:48 -070031#include <unistd.h>
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -070032
Jeff Sharkey81f55c62015-07-07 14:37:03 -070033#define ENABLE_DROP_CACHES 1
34
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -070035using android::base::ReadFileToString;
36using android::base::WriteStringToFile;
37
38namespace android {
39namespace vold {
40
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -060041static const char* kWakeLock = "Benchmark";
Jeff Sharkey52f7a912017-09-15 12:57:44 -060042
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -060043static status_t benchmarkInternal(const std::string& rootPath,
44 android::os::PersistableBundle* extras) {
Jeff Sharkey52f7a912017-09-15 12:57:44 -060045 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 Sharkey5a6bfca2015-05-14 20:33:55 -070059 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 Sharkey721e5802015-05-19 11:20:48 -070091 sync();
92
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -070093 LOG(INFO) << "Benchmarking " << path;
94 nsecs_t start = systemTime(SYSTEM_TIME_BOOTTIME);
95
96 BenchmarkCreate();
Jeff Sharkey721e5802015-05-19 11:20:48 -070097 sync();
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -070098 nsecs_t create = systemTime(SYSTEM_TIME_BOOTTIME);
99
Jeff Sharkey81f55c62015-07-07 14:37:03 -0700100#if ENABLE_DROP_CACHES
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700101 LOG(VERBOSE) << "Before drop_caches";
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700102 if (!WriteStringToFile("3", "/proc/sys/vm/drop_caches")) {
103 PLOG(ERROR) << "Failed to drop_caches";
104 }
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700105 LOG(VERBOSE) << "After drop_caches";
Jeff Sharkey81f55c62015-07-07 14:37:03 -0700106#endif
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700107 nsecs_t drop = systemTime(SYSTEM_TIME_BOOTTIME);
108
109 BenchmarkRun();
Jeff Sharkey721e5802015-05-19 11:20:48 -0700110 sync();
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700111 nsecs_t run = systemTime(SYSTEM_TIME_BOOTTIME);
112
113 BenchmarkDestroy();
Jeff Sharkey721e5802015-05-19 11:20:48 -0700114 sync();
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700115 nsecs_t destroy = systemTime(SYSTEM_TIME_BOOTTIME);
116
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700117 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 Sharkey5a6bfca2015-05-14 20:33:55 -0700127 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 Sharkey01a0e7f2017-10-17 16:06:32 -0600137 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 Sharkey5a6bfca2015-05-14 20:33:55 -0700143
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600144 return 0;
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700145}
146
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600147void Benchmark(const std::string& path,
148 const android::sp<android::os::IVoldTaskListener>& listener) {
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600149 acquire_wake_lock(PARTIAL_WAKE_LOCK, kWakeLock);
150
151 android::os::PersistableBundle extras;
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600152 status_t res = benchmarkInternal(path, &extras);
153 if (listener) {
154 listener->onFinished(res, extras);
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700155 }
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600156
157 release_wake_lock(kWakeLock);
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700158}
159
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700160} // namespace vold
161} // namespace android