blob: d10d792987860902618a4729846d6aafda4dbc05 [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"
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
27#include <sys/time.h>
28#include <sys/resource.h>
Jeff Sharkey721e5802015-05-19 11:20:48 -070029#include <unistd.h>
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -070030
Jeff Sharkey81f55c62015-07-07 14:37:03 -070031#define ENABLE_DROP_CACHES 1
32
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -070033using android::base::ReadFileToString;
34using android::base::WriteStringToFile;
35
36namespace android {
37namespace vold {
38
Jeff Sharkey52f7a912017-09-15 12:57:44 -060039static const char* kWakeLock = "BenchmarkTask";
40
41BenchmarkTask::BenchmarkTask(const std::string& path,
42 const android::sp<android::os::IVoldTaskListener>& listener) :
43 mPath(path), mListener(listener) {
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -070044}
45
Jeff Sharkey52f7a912017-09-15 12:57:44 -060046BenchmarkTask::~BenchmarkTask() {
47}
48
49void BenchmarkTask::start() {
50 mThread = std::thread(&BenchmarkTask::run, this);
51}
52
53static status_t runInternal(const std::string& rootPath, android::os::PersistableBundle& extras) {
54 auto path = rootPath;
55 path += "/misc";
56 if (android::vold::PrepareDir(path, 01771, AID_SYSTEM, AID_MISC)) {
57 return -1;
58 }
59 path += "/vold";
60 if (android::vold::PrepareDir(path, 0700, AID_ROOT, AID_ROOT)) {
61 return -1;
62 }
63 path += "/bench";
64 if (android::vold::PrepareDir(path, 0700, AID_ROOT, AID_ROOT)) {
65 return -1;
66 }
67
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -070068 errno = 0;
69 int orig_prio = getpriority(PRIO_PROCESS, 0);
70 if (errno != 0) {
71 PLOG(ERROR) << "Failed to getpriority";
72 return -1;
73 }
74 if (setpriority(PRIO_PROCESS, 0, -10) != 0) {
75 PLOG(ERROR) << "Failed to setpriority";
76 return -1;
77 }
78
79 IoSchedClass orig_clazz = IoSchedClass_NONE;
80 int orig_ioprio = 0;
81 if (android_get_ioprio(0, &orig_clazz, &orig_ioprio)) {
82 PLOG(ERROR) << "Failed to android_get_ioprio";
83 return -1;
84 }
85 if (android_set_ioprio(0, IoSchedClass_RT, 0)) {
86 PLOG(ERROR) << "Failed to android_set_ioprio";
87 return -1;
88 }
89
90 char orig_cwd[PATH_MAX];
91 if (getcwd(orig_cwd, PATH_MAX) == NULL) {
92 PLOG(ERROR) << "Failed getcwd";
93 return -1;
94 }
95 if (chdir(path.c_str()) != 0) {
96 PLOG(ERROR) << "Failed chdir";
97 return -1;
98 }
99
Jeff Sharkey721e5802015-05-19 11:20:48 -0700100 sync();
101
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700102 LOG(INFO) << "Benchmarking " << path;
103 nsecs_t start = systemTime(SYSTEM_TIME_BOOTTIME);
104
105 BenchmarkCreate();
Jeff Sharkey721e5802015-05-19 11:20:48 -0700106 sync();
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700107 nsecs_t create = systemTime(SYSTEM_TIME_BOOTTIME);
108
Jeff Sharkey81f55c62015-07-07 14:37:03 -0700109#if ENABLE_DROP_CACHES
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700110 LOG(VERBOSE) << "Before drop_caches";
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700111 if (!WriteStringToFile("3", "/proc/sys/vm/drop_caches")) {
112 PLOG(ERROR) << "Failed to drop_caches";
113 }
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700114 LOG(VERBOSE) << "After drop_caches";
Jeff Sharkey81f55c62015-07-07 14:37:03 -0700115#endif
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700116 nsecs_t drop = systemTime(SYSTEM_TIME_BOOTTIME);
117
118 BenchmarkRun();
Jeff Sharkey721e5802015-05-19 11:20:48 -0700119 sync();
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700120 nsecs_t run = systemTime(SYSTEM_TIME_BOOTTIME);
121
122 BenchmarkDestroy();
Jeff Sharkey721e5802015-05-19 11:20:48 -0700123 sync();
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700124 nsecs_t destroy = systemTime(SYSTEM_TIME_BOOTTIME);
125
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700126 if (chdir(orig_cwd) != 0) {
127 PLOG(ERROR) << "Failed to chdir";
128 }
129 if (android_set_ioprio(0, orig_clazz, orig_ioprio)) {
130 PLOG(ERROR) << "Failed to android_set_ioprio";
131 }
132 if (setpriority(PRIO_PROCESS, 0, orig_prio) != 0) {
133 PLOG(ERROR) << "Failed to setpriority";
134 }
135
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700136 nsecs_t create_d = create - start;
137 nsecs_t drop_d = drop - create;
138 nsecs_t run_d = run - drop;
139 nsecs_t destroy_d = destroy - run;
140
141 LOG(INFO) << "create took " << nanoseconds_to_milliseconds(create_d) << "ms";
142 LOG(INFO) << "drop took " << nanoseconds_to_milliseconds(drop_d) << "ms";
143 LOG(INFO) << "run took " << nanoseconds_to_milliseconds(run_d) << "ms";
144 LOG(INFO) << "destroy took " << nanoseconds_to_milliseconds(destroy_d) << "ms";
145
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600146 extras.putString(String16("path"), String16(path.c_str()));
147 extras.putString(String16("ident"), String16(BenchmarkIdent().c_str()));
148 extras.putLong(String16("create"), create_d);
149 extras.putLong(String16("drop"), drop_d);
150 extras.putLong(String16("run"), run_d);
151 extras.putLong(String16("destroy"), destroy_d);
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700152
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600153 return 0;
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700154}
155
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600156void BenchmarkTask::run() {
157 acquire_wake_lock(PARTIAL_WAKE_LOCK, kWakeLock);
158
159 android::os::PersistableBundle extras;
160 status_t res = runInternal(mPath, extras);
161 if (mListener) {
162 mListener->onFinished(res, extras);
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700163 }
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600164
165 release_wake_lock(kWakeLock);
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700166}
167
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700168} // namespace vold
169} // namespace android