2015-05-15 05:33:55 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2017-10-18 00:06:32 +02:00
|
|
|
#include "Benchmark.h"
|
2015-05-15 05:33:55 +02:00
|
|
|
#include "BenchmarkGen.h"
|
|
|
|
#include "VolumeManager.h"
|
|
|
|
|
2017-11-06 21:53:59 +01:00
|
|
|
#include <android-base/chrono_utils.h>
|
2015-12-05 00:50:53 +01:00
|
|
|
#include <android-base/file.h>
|
|
|
|
#include <android-base/logging.h>
|
2017-11-06 21:53:59 +01:00
|
|
|
|
2015-05-15 05:33:55 +02:00
|
|
|
#include <cutils/iosched_policy.h>
|
2015-06-26 23:02:09 +02:00
|
|
|
#include <private/android_filesystem_config.h>
|
2019-06-21 21:21:48 +02:00
|
|
|
#include <wakelock/wakelock.h>
|
2015-05-15 05:33:55 +02:00
|
|
|
|
2017-10-18 00:06:32 +02:00
|
|
|
#include <thread>
|
|
|
|
|
2015-05-15 05:33:55 +02:00
|
|
|
#include <sys/resource.h>
|
2018-09-18 22:30:21 +02:00
|
|
|
#include <sys/time.h>
|
2015-05-19 20:20:48 +02:00
|
|
|
#include <unistd.h>
|
2015-05-15 05:33:55 +02:00
|
|
|
|
|
|
|
using android::base::ReadFileToString;
|
|
|
|
using android::base::WriteStringToFile;
|
|
|
|
|
|
|
|
namespace android {
|
|
|
|
namespace vold {
|
|
|
|
|
2017-11-06 21:53:59 +01:00
|
|
|
// Benchmark currently uses chdir(), which means we can only
|
|
|
|
// safely run one at a time.
|
|
|
|
static std::mutex kBenchmarkLock;
|
|
|
|
|
2017-10-18 00:06:32 +02:00
|
|
|
static const char* kWakeLock = "Benchmark";
|
2015-05-15 05:33:55 +02:00
|
|
|
|
2017-11-06 21:53:59 +01:00
|
|
|
// Reasonable cards are able to complete the create/run stages
|
|
|
|
// in under 20 seconds.
|
|
|
|
constexpr auto kTimeout = 20s;
|
|
|
|
|
|
|
|
// RAII class for boosting device performance during benchmarks.
|
|
|
|
class PerformanceBoost {
|
2018-09-18 22:30:21 +02:00
|
|
|
private:
|
2017-11-06 21:53:59 +01:00
|
|
|
int orig_prio;
|
|
|
|
int orig_ioprio;
|
|
|
|
IoSchedClass orig_clazz;
|
|
|
|
|
2018-09-18 22:30:21 +02:00
|
|
|
public:
|
2017-11-06 21:53:59 +01:00
|
|
|
PerformanceBoost() {
|
|
|
|
errno = 0;
|
|
|
|
orig_prio = getpriority(PRIO_PROCESS, 0);
|
|
|
|
if (errno != 0) {
|
|
|
|
PLOG(WARNING) << "Failed to getpriority";
|
|
|
|
orig_prio = 0;
|
|
|
|
}
|
|
|
|
if (setpriority(PRIO_PROCESS, 0, -10) != 0) {
|
|
|
|
PLOG(WARNING) << "Failed to setpriority";
|
|
|
|
}
|
|
|
|
if (android_get_ioprio(0, &orig_clazz, &orig_ioprio)) {
|
|
|
|
PLOG(WARNING) << "Failed to android_get_ioprio";
|
|
|
|
orig_ioprio = 0;
|
|
|
|
orig_clazz = IoSchedClass_NONE;
|
|
|
|
}
|
|
|
|
if (android_set_ioprio(0, IoSchedClass_RT, 0)) {
|
|
|
|
PLOG(WARNING) << "Failed to android_set_ioprio";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~PerformanceBoost() {
|
|
|
|
if (android_set_ioprio(0, orig_clazz, orig_ioprio)) {
|
|
|
|
PLOG(WARNING) << "Failed to android_set_ioprio";
|
|
|
|
}
|
|
|
|
if (setpriority(PRIO_PROCESS, 0, orig_prio) != 0) {
|
|
|
|
PLOG(WARNING) << "Failed to setpriority";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-10-18 00:06:32 +02:00
|
|
|
static status_t benchmarkInternal(const std::string& rootPath,
|
2018-09-18 22:30:21 +02:00
|
|
|
const android::sp<android::os::IVoldTaskListener>& listener,
|
|
|
|
android::os::PersistableBundle* extras) {
|
2017-11-06 21:53:59 +01:00
|
|
|
status_t res = 0;
|
|
|
|
|
2017-09-15 20:57:44 +02:00
|
|
|
auto path = rootPath;
|
|
|
|
path += "/misc";
|
|
|
|
if (android::vold::PrepareDir(path, 01771, AID_SYSTEM, AID_MISC)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
path += "/vold";
|
|
|
|
if (android::vold::PrepareDir(path, 0700, AID_ROOT, AID_ROOT)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
path += "/bench";
|
|
|
|
if (android::vold::PrepareDir(path, 0700, AID_ROOT, AID_ROOT)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-05-15 05:33:55 +02:00
|
|
|
char orig_cwd[PATH_MAX];
|
|
|
|
if (getcwd(orig_cwd, PATH_MAX) == NULL) {
|
|
|
|
PLOG(ERROR) << "Failed getcwd";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (chdir(path.c_str()) != 0) {
|
|
|
|
PLOG(ERROR) << "Failed chdir";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-05-19 20:20:48 +02:00
|
|
|
sync();
|
|
|
|
|
2017-11-06 21:53:59 +01:00
|
|
|
extras->putString(String16("path"), String16(path.c_str()));
|
|
|
|
extras->putString(String16("ident"), String16(BenchmarkIdent().c_str()));
|
2015-05-15 05:33:55 +02:00
|
|
|
|
2017-11-06 21:53:59 +01:00
|
|
|
// Always create
|
|
|
|
{
|
|
|
|
android::base::Timer timer;
|
|
|
|
LOG(INFO) << "Creating " << path;
|
|
|
|
res |= BenchmarkCreate([&](int progress) -> bool {
|
|
|
|
if (listener) {
|
|
|
|
listener->onStatus(progress, *extras);
|
|
|
|
}
|
|
|
|
return (timer.duration() < kTimeout);
|
|
|
|
});
|
|
|
|
sync();
|
|
|
|
if (res == OK) extras->putLong(String16("create"), timer.duration().count());
|
|
|
|
}
|
2015-05-15 05:33:55 +02:00
|
|
|
|
2017-11-06 21:53:59 +01:00
|
|
|
// Only drop when we haven't aborted
|
|
|
|
if (res == OK) {
|
|
|
|
android::base::Timer timer;
|
2018-09-21 19:54:54 +02:00
|
|
|
LOG(DEBUG) << "Before drop_caches";
|
2017-11-06 21:53:59 +01:00
|
|
|
if (!WriteStringToFile("3", "/proc/sys/vm/drop_caches")) {
|
|
|
|
PLOG(ERROR) << "Failed to drop_caches";
|
|
|
|
res = -1;
|
|
|
|
}
|
2018-09-21 19:54:54 +02:00
|
|
|
LOG(DEBUG) << "After drop_caches";
|
2017-11-06 21:53:59 +01:00
|
|
|
sync();
|
|
|
|
if (res == OK) extras->putLong(String16("drop"), timer.duration().count());
|
2015-05-15 05:33:55 +02:00
|
|
|
}
|
|
|
|
|
2017-11-06 21:53:59 +01:00
|
|
|
// Only run when we haven't aborted
|
|
|
|
if (res == OK) {
|
|
|
|
android::base::Timer timer;
|
|
|
|
LOG(INFO) << "Running " << path;
|
|
|
|
res |= BenchmarkRun([&](int progress) -> bool {
|
|
|
|
if (listener) {
|
|
|
|
listener->onStatus(progress, *extras);
|
|
|
|
}
|
|
|
|
return (timer.duration() < kTimeout);
|
|
|
|
});
|
|
|
|
sync();
|
|
|
|
if (res == OK) extras->putLong(String16("run"), timer.duration().count());
|
|
|
|
}
|
2015-05-15 05:33:55 +02:00
|
|
|
|
2017-11-06 21:53:59 +01:00
|
|
|
// Always destroy
|
|
|
|
{
|
|
|
|
android::base::Timer timer;
|
|
|
|
LOG(INFO) << "Destroying " << path;
|
|
|
|
res |= BenchmarkDestroy();
|
|
|
|
sync();
|
|
|
|
if (res == OK) extras->putLong(String16("destroy"), timer.duration().count());
|
|
|
|
}
|
2015-05-15 05:33:55 +02:00
|
|
|
|
2015-06-26 23:02:09 +02:00
|
|
|
if (chdir(orig_cwd) != 0) {
|
|
|
|
PLOG(ERROR) << "Failed to chdir";
|
2017-11-06 21:53:59 +01:00
|
|
|
return -1;
|
2015-06-26 23:02:09 +02:00
|
|
|
}
|
2015-05-15 05:33:55 +02:00
|
|
|
|
2017-11-06 21:53:59 +01:00
|
|
|
return res;
|
2015-06-26 23:02:09 +02:00
|
|
|
}
|
2015-05-15 05:33:55 +02:00
|
|
|
|
2017-10-18 00:06:32 +02:00
|
|
|
void Benchmark(const std::string& path,
|
2018-09-18 22:30:21 +02:00
|
|
|
const android::sp<android::os::IVoldTaskListener>& listener) {
|
2017-11-06 21:53:59 +01:00
|
|
|
std::lock_guard<std::mutex> lock(kBenchmarkLock);
|
2021-02-22 21:10:45 +01:00
|
|
|
auto wl = android::wakelock::WakeLock::tryGet(kWakeLock);
|
|
|
|
if (!wl.has_value()) {
|
|
|
|
return;
|
|
|
|
}
|
2017-09-15 20:57:44 +02:00
|
|
|
|
2017-11-06 21:53:59 +01:00
|
|
|
PerformanceBoost boost;
|
2017-09-15 20:57:44 +02:00
|
|
|
android::os::PersistableBundle extras;
|
2017-11-06 21:53:59 +01:00
|
|
|
|
|
|
|
status_t res = benchmarkInternal(path, listener, &extras);
|
2017-10-18 00:06:32 +02:00
|
|
|
if (listener) {
|
|
|
|
listener->onFinished(res, extras);
|
2015-05-15 05:33:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace vold
|
|
|
|
} // namespace android
|