blob: 5ec249a16ab87ffc707d99eb9e50ee56b60a8e9b [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 Sharkey52f7a912017-09-15 12:57:44 -060017#include "BenchmarkTask.h"
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -070018#include "BenchmarkGen.h"
19#include "VolumeManager.h"
20#include "ResponseCode.h"
21
Elliott Hughes7e128fb2015-12-04 15:50:53 -080022#include <android-base/file.h>
23#include <android-base/logging.h>
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -070024#include <cutils/iosched_policy.h>
Jeff Sharkey52f7a912017-09-15 12:57:44 -060025#include <hardware_legacy/power.h>
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070026#include <private/android_filesystem_config.h>
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -070027
28#include <sys/time.h>
29#include <sys/resource.h>
Jeff Sharkey721e5802015-05-19 11:20:48 -070030#include <unistd.h>
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -070031
Jeff Sharkey81f55c62015-07-07 14:37:03 -070032#define ENABLE_DROP_CACHES 1
33
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -070034using android::base::ReadFileToString;
35using android::base::WriteStringToFile;
36
37namespace android {
38namespace vold {
39
Jeff Sharkey52f7a912017-09-15 12:57:44 -060040static const char* kWakeLock = "BenchmarkTask";
41
42BenchmarkTask::BenchmarkTask(const std::string& path,
43 const android::sp<android::os::IVoldTaskListener>& listener) :
44 mPath(path), mListener(listener) {
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -070045}
46
Jeff Sharkey52f7a912017-09-15 12:57:44 -060047BenchmarkTask::~BenchmarkTask() {
48}
49
50void BenchmarkTask::start() {
51 mThread = std::thread(&BenchmarkTask::run, this);
52}
53
54static status_t runInternal(const std::string& rootPath, android::os::PersistableBundle& extras) {
55 auto path = rootPath;
56 path += "/misc";
57 if (android::vold::PrepareDir(path, 01771, AID_SYSTEM, AID_MISC)) {
58 return -1;
59 }
60 path += "/vold";
61 if (android::vold::PrepareDir(path, 0700, AID_ROOT, AID_ROOT)) {
62 return -1;
63 }
64 path += "/bench";
65 if (android::vold::PrepareDir(path, 0700, AID_ROOT, AID_ROOT)) {
66 return -1;
67 }
68
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -070069 errno = 0;
70 int orig_prio = getpriority(PRIO_PROCESS, 0);
71 if (errno != 0) {
72 PLOG(ERROR) << "Failed to getpriority";
73 return -1;
74 }
75 if (setpriority(PRIO_PROCESS, 0, -10) != 0) {
76 PLOG(ERROR) << "Failed to setpriority";
77 return -1;
78 }
79
80 IoSchedClass orig_clazz = IoSchedClass_NONE;
81 int orig_ioprio = 0;
82 if (android_get_ioprio(0, &orig_clazz, &orig_ioprio)) {
83 PLOG(ERROR) << "Failed to android_get_ioprio";
84 return -1;
85 }
86 if (android_set_ioprio(0, IoSchedClass_RT, 0)) {
87 PLOG(ERROR) << "Failed to android_set_ioprio";
88 return -1;
89 }
90
91 char orig_cwd[PATH_MAX];
92 if (getcwd(orig_cwd, PATH_MAX) == NULL) {
93 PLOG(ERROR) << "Failed getcwd";
94 return -1;
95 }
96 if (chdir(path.c_str()) != 0) {
97 PLOG(ERROR) << "Failed chdir";
98 return -1;
99 }
100
Jeff Sharkey721e5802015-05-19 11:20:48 -0700101 sync();
102
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700103 LOG(INFO) << "Benchmarking " << path;
104 nsecs_t start = systemTime(SYSTEM_TIME_BOOTTIME);
105
106 BenchmarkCreate();
Jeff Sharkey721e5802015-05-19 11:20:48 -0700107 sync();
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700108 nsecs_t create = systemTime(SYSTEM_TIME_BOOTTIME);
109
Jeff Sharkey81f55c62015-07-07 14:37:03 -0700110#if ENABLE_DROP_CACHES
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700111 LOG(VERBOSE) << "Before drop_caches";
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700112 if (!WriteStringToFile("3", "/proc/sys/vm/drop_caches")) {
113 PLOG(ERROR) << "Failed to drop_caches";
114 }
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700115 LOG(VERBOSE) << "After drop_caches";
Jeff Sharkey81f55c62015-07-07 14:37:03 -0700116#endif
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700117 nsecs_t drop = systemTime(SYSTEM_TIME_BOOTTIME);
118
119 BenchmarkRun();
Jeff Sharkey721e5802015-05-19 11:20:48 -0700120 sync();
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700121 nsecs_t run = systemTime(SYSTEM_TIME_BOOTTIME);
122
123 BenchmarkDestroy();
Jeff Sharkey721e5802015-05-19 11:20:48 -0700124 sync();
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700125 nsecs_t destroy = systemTime(SYSTEM_TIME_BOOTTIME);
126
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700127 if (chdir(orig_cwd) != 0) {
128 PLOG(ERROR) << "Failed to chdir";
129 }
130 if (android_set_ioprio(0, orig_clazz, orig_ioprio)) {
131 PLOG(ERROR) << "Failed to android_set_ioprio";
132 }
133 if (setpriority(PRIO_PROCESS, 0, orig_prio) != 0) {
134 PLOG(ERROR) << "Failed to setpriority";
135 }
136
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700137 nsecs_t create_d = create - start;
138 nsecs_t drop_d = drop - create;
139 nsecs_t run_d = run - drop;
140 nsecs_t destroy_d = destroy - run;
141
142 LOG(INFO) << "create took " << nanoseconds_to_milliseconds(create_d) << "ms";
143 LOG(INFO) << "drop took " << nanoseconds_to_milliseconds(drop_d) << "ms";
144 LOG(INFO) << "run took " << nanoseconds_to_milliseconds(run_d) << "ms";
145 LOG(INFO) << "destroy took " << nanoseconds_to_milliseconds(destroy_d) << "ms";
146
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600147 extras.putString(String16("path"), String16(path.c_str()));
148 extras.putString(String16("ident"), String16(BenchmarkIdent().c_str()));
149 extras.putLong(String16("create"), create_d);
150 extras.putLong(String16("drop"), drop_d);
151 extras.putLong(String16("run"), run_d);
152 extras.putLong(String16("destroy"), destroy_d);
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700153
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600154 return 0;
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700155}
156
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600157void BenchmarkTask::run() {
158 acquire_wake_lock(PARTIAL_WAKE_LOCK, kWakeLock);
159
160 android::os::PersistableBundle extras;
161 status_t res = runInternal(mPath, extras);
162 if (mListener) {
163 mListener->onFinished(res, extras);
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700164 }
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600165
166 release_wake_lock(kWakeLock);
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700167}
168
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700169} // namespace vold
170} // namespace android