blob: 7a3af65684525bf8ceeed82a339a35ab0c3f1f6f [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
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>
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
31using android::base::ReadFileToString;
32using android::base::WriteStringToFile;
33
34namespace android {
35namespace vold {
36
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070037static void notifyResult(const std::string& path, int64_t create_d,
38 int64_t drop_d, int64_t run_d, int64_t destroy_d) {
39 std::string res(path +
40 + " " + BenchmarkIdent()
41 + " " + std::to_string(create_d)
42 + " " + std::to_string(drop_d)
43 + " " + std::to_string(run_d)
44 + " " + std::to_string(destroy_d));
45 VolumeManager::Instance()->getBroadcaster()->sendBroadcast(
46 ResponseCode::BenchmarkResult, res.c_str(), false);
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -070047}
48
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070049static nsecs_t benchmark(const std::string& path) {
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -070050 errno = 0;
51 int orig_prio = getpriority(PRIO_PROCESS, 0);
52 if (errno != 0) {
53 PLOG(ERROR) << "Failed to getpriority";
54 return -1;
55 }
56 if (setpriority(PRIO_PROCESS, 0, -10) != 0) {
57 PLOG(ERROR) << "Failed to setpriority";
58 return -1;
59 }
60
61 IoSchedClass orig_clazz = IoSchedClass_NONE;
62 int orig_ioprio = 0;
63 if (android_get_ioprio(0, &orig_clazz, &orig_ioprio)) {
64 PLOG(ERROR) << "Failed to android_get_ioprio";
65 return -1;
66 }
67 if (android_set_ioprio(0, IoSchedClass_RT, 0)) {
68 PLOG(ERROR) << "Failed to android_set_ioprio";
69 return -1;
70 }
71
72 char orig_cwd[PATH_MAX];
73 if (getcwd(orig_cwd, PATH_MAX) == NULL) {
74 PLOG(ERROR) << "Failed getcwd";
75 return -1;
76 }
77 if (chdir(path.c_str()) != 0) {
78 PLOG(ERROR) << "Failed chdir";
79 return -1;
80 }
81
Jeff Sharkey721e5802015-05-19 11:20:48 -070082 sync();
83
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -070084 LOG(INFO) << "Benchmarking " << path;
85 nsecs_t start = systemTime(SYSTEM_TIME_BOOTTIME);
86
87 BenchmarkCreate();
Jeff Sharkey721e5802015-05-19 11:20:48 -070088 sync();
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -070089 nsecs_t create = systemTime(SYSTEM_TIME_BOOTTIME);
90
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070091 LOG(VERBOSE) << "Before drop_caches";
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -070092 if (!WriteStringToFile("3", "/proc/sys/vm/drop_caches")) {
93 PLOG(ERROR) << "Failed to drop_caches";
94 }
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070095 LOG(VERBOSE) << "After drop_caches";
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -070096 nsecs_t drop = systemTime(SYSTEM_TIME_BOOTTIME);
97
98 BenchmarkRun();
Jeff Sharkey721e5802015-05-19 11:20:48 -070099 sync();
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700100 nsecs_t run = systemTime(SYSTEM_TIME_BOOTTIME);
101
102 BenchmarkDestroy();
Jeff Sharkey721e5802015-05-19 11:20:48 -0700103 sync();
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700104 nsecs_t destroy = systemTime(SYSTEM_TIME_BOOTTIME);
105
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700106 if (chdir(orig_cwd) != 0) {
107 PLOG(ERROR) << "Failed to chdir";
108 }
109 if (android_set_ioprio(0, orig_clazz, orig_ioprio)) {
110 PLOG(ERROR) << "Failed to android_set_ioprio";
111 }
112 if (setpriority(PRIO_PROCESS, 0, orig_prio) != 0) {
113 PLOG(ERROR) << "Failed to setpriority";
114 }
115
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700116 nsecs_t create_d = create - start;
117 nsecs_t drop_d = drop - create;
118 nsecs_t run_d = run - drop;
119 nsecs_t destroy_d = destroy - run;
120
121 LOG(INFO) << "create took " << nanoseconds_to_milliseconds(create_d) << "ms";
122 LOG(INFO) << "drop took " << nanoseconds_to_milliseconds(drop_d) << "ms";
123 LOG(INFO) << "run took " << nanoseconds_to_milliseconds(run_d) << "ms";
124 LOG(INFO) << "destroy took " << nanoseconds_to_milliseconds(destroy_d) << "ms";
125
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700126 notifyResult(path, create_d, drop_d, run_d, destroy_d);
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700127
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700128 return run_d;
129}
130
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700131nsecs_t BenchmarkPrivate(const std::string& path) {
132 std::string benchPath(path);
133 benchPath += "/misc";
134 if (android::vold::PrepareDir(benchPath, 01771, AID_SYSTEM, AID_MISC)) {
135 return -1;
136 }
137 benchPath += "/vold";
138 if (android::vold::PrepareDir(benchPath, 0700, AID_ROOT, AID_ROOT)) {
139 return -1;
140 }
141 benchPath += "/bench";
142 if (android::vold::PrepareDir(benchPath, 0700, AID_ROOT, AID_ROOT)) {
143 return -1;
144 }
145 return benchmark(benchPath);
146}
147
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700148} // namespace vold
149} // namespace android