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 
21 #include <android-base/chrono_utils.h>
22 #include <android-base/file.h>
23 #include <android-base/logging.h>
24 
25 #include <cutils/iosched_policy.h>
26 #include <private/android_filesystem_config.h>
27 #include <wakelock/wakelock.h>
28 
29 #include <thread>
30 
31 #include <sys/resource.h>
32 #include <sys/time.h>
33 #include <unistd.h>
34 
35 using android::base::ReadFileToString;
36 using android::base::WriteStringToFile;
37 
38 namespace android {
39 namespace vold {
40 
41 // Benchmark currently uses chdir(), which means we can only
42 // safely run one at a time.
43 static std::mutex kBenchmarkLock;
44 
45 static const char* kWakeLock = "Benchmark";
46 
47 // Reasonable cards are able to complete the create/run stages
48 // in under 20 seconds.
49 constexpr auto kTimeout = 20s;
50 
51 // RAII class for boosting device performance during benchmarks.
52 class PerformanceBoost {
53   private:
54     int orig_prio;
55     int orig_ioprio;
56     IoSchedClass orig_clazz;
57 
58   public:
PerformanceBoost()59     PerformanceBoost() {
60         errno = 0;
61         orig_prio = getpriority(PRIO_PROCESS, 0);
62         if (errno != 0) {
63             PLOG(WARNING) << "Failed to getpriority";
64             orig_prio = 0;
65         }
66         if (setpriority(PRIO_PROCESS, 0, -10) != 0) {
67             PLOG(WARNING) << "Failed to setpriority";
68         }
69         if (android_get_ioprio(0, &orig_clazz, &orig_ioprio)) {
70             PLOG(WARNING) << "Failed to android_get_ioprio";
71             orig_ioprio = 0;
72             orig_clazz = IoSchedClass_NONE;
73         }
74         if (android_set_ioprio(0, IoSchedClass_RT, 0)) {
75             PLOG(WARNING) << "Failed to android_set_ioprio";
76         }
77     }
78 
~PerformanceBoost()79     ~PerformanceBoost() {
80         if (android_set_ioprio(0, orig_clazz, orig_ioprio)) {
81             PLOG(WARNING) << "Failed to android_set_ioprio";
82         }
83         if (setpriority(PRIO_PROCESS, 0, orig_prio) != 0) {
84             PLOG(WARNING) << "Failed to setpriority";
85         }
86     }
87 };
88 
benchmarkInternal(const std::string & rootPath,const android::sp<android::os::IVoldTaskListener> & listener,android::os::PersistableBundle * extras)89 static status_t benchmarkInternal(const std::string& rootPath,
90                                   const android::sp<android::os::IVoldTaskListener>& listener,
91                                   android::os::PersistableBundle* extras) {
92     status_t res = 0;
93 
94     auto path = rootPath;
95     path += "/misc";
96     if (android::vold::PrepareDir(path, 01771, AID_SYSTEM, AID_MISC)) {
97         return -1;
98     }
99     path += "/vold";
100     if (android::vold::PrepareDir(path, 0700, AID_ROOT, AID_ROOT)) {
101         return -1;
102     }
103     path += "/bench";
104     if (android::vold::PrepareDir(path, 0700, AID_ROOT, AID_ROOT)) {
105         return -1;
106     }
107 
108     char orig_cwd[PATH_MAX];
109     if (getcwd(orig_cwd, PATH_MAX) == NULL) {
110         PLOG(ERROR) << "Failed getcwd";
111         return -1;
112     }
113     if (chdir(path.c_str()) != 0) {
114         PLOG(ERROR) << "Failed chdir";
115         return -1;
116     }
117 
118     sync();
119 
120     extras->putString(String16("path"), String16(path.c_str()));
121     extras->putString(String16("ident"), String16(BenchmarkIdent().c_str()));
122 
123     // Always create
124     {
125         android::base::Timer timer;
126         LOG(INFO) << "Creating " << path;
127         res |= BenchmarkCreate([&](int progress) -> bool {
128             if (listener) {
129                 listener->onStatus(progress, *extras);
130             }
131             return (timer.duration() < kTimeout);
132         });
133         sync();
134         if (res == OK) extras->putLong(String16("create"), timer.duration().count());
135     }
136 
137     // Only drop when we haven't aborted
138     if (res == OK) {
139         android::base::Timer timer;
140         LOG(DEBUG) << "Before drop_caches";
141         if (!WriteStringToFile("3", "/proc/sys/vm/drop_caches")) {
142             PLOG(ERROR) << "Failed to drop_caches";
143             res = -1;
144         }
145         LOG(DEBUG) << "After drop_caches";
146         sync();
147         if (res == OK) extras->putLong(String16("drop"), timer.duration().count());
148     }
149 
150     // Only run when we haven't aborted
151     if (res == OK) {
152         android::base::Timer timer;
153         LOG(INFO) << "Running " << path;
154         res |= BenchmarkRun([&](int progress) -> bool {
155             if (listener) {
156                 listener->onStatus(progress, *extras);
157             }
158             return (timer.duration() < kTimeout);
159         });
160         sync();
161         if (res == OK) extras->putLong(String16("run"), timer.duration().count());
162     }
163 
164     // Always destroy
165     {
166         android::base::Timer timer;
167         LOG(INFO) << "Destroying " << path;
168         res |= BenchmarkDestroy();
169         sync();
170         if (res == OK) extras->putLong(String16("destroy"), timer.duration().count());
171     }
172 
173     if (chdir(orig_cwd) != 0) {
174         PLOG(ERROR) << "Failed to chdir";
175         return -1;
176     }
177 
178     return res;
179 }
180 
Benchmark(const std::string & path,const android::sp<android::os::IVoldTaskListener> & listener)181 void Benchmark(const std::string& path,
182                const android::sp<android::os::IVoldTaskListener>& listener) {
183     std::lock_guard<std::mutex> lock(kBenchmarkLock);
184     android::wakelock::WakeLock wl{kWakeLock};
185 
186     PerformanceBoost boost;
187     android::os::PersistableBundle extras;
188 
189     status_t res = benchmarkInternal(path, listener, &extras);
190     if (listener) {
191         listener->onFinished(res, extras);
192     }
193 }
194 
195 }  // namespace vold
196 }  // namespace android
197