blob: e3295888bcb29cb7909dd34acb07d61318bee457 [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>
25
26#include <sys/time.h>
27#include <sys/resource.h>
Jeff Sharkey721e5802015-05-19 11:20:48 -070028#include <unistd.h>
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -070029
30using android::base::ReadFileToString;
31using android::base::WriteStringToFile;
32
33namespace android {
34namespace vold {
35
36static std::string simpleRead(const std::string& path) {
37 std::string tmp;
38 ReadFileToString(path, &tmp);
39 tmp.erase(tmp.find_last_not_of(" \n\r") + 1);
40 return tmp;
41}
42
43nsecs_t Benchmark(const std::string& path, const std::string& sysPath) {
44 errno = 0;
45 int orig_prio = getpriority(PRIO_PROCESS, 0);
46 if (errno != 0) {
47 PLOG(ERROR) << "Failed to getpriority";
48 return -1;
49 }
50 if (setpriority(PRIO_PROCESS, 0, -10) != 0) {
51 PLOG(ERROR) << "Failed to setpriority";
52 return -1;
53 }
54
55 IoSchedClass orig_clazz = IoSchedClass_NONE;
56 int orig_ioprio = 0;
57 if (android_get_ioprio(0, &orig_clazz, &orig_ioprio)) {
58 PLOG(ERROR) << "Failed to android_get_ioprio";
59 return -1;
60 }
61 if (android_set_ioprio(0, IoSchedClass_RT, 0)) {
62 PLOG(ERROR) << "Failed to android_set_ioprio";
63 return -1;
64 }
65
66 char orig_cwd[PATH_MAX];
67 if (getcwd(orig_cwd, PATH_MAX) == NULL) {
68 PLOG(ERROR) << "Failed getcwd";
69 return -1;
70 }
71 if (chdir(path.c_str()) != 0) {
72 PLOG(ERROR) << "Failed chdir";
73 return -1;
74 }
75
Jeff Sharkey721e5802015-05-19 11:20:48 -070076 sync();
77
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -070078 LOG(INFO) << "Benchmarking " << path;
79 nsecs_t start = systemTime(SYSTEM_TIME_BOOTTIME);
80
81 BenchmarkCreate();
Jeff Sharkey721e5802015-05-19 11:20:48 -070082 sync();
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -070083 nsecs_t create = systemTime(SYSTEM_TIME_BOOTTIME);
84
85 if (!WriteStringToFile("3", "/proc/sys/vm/drop_caches")) {
86 PLOG(ERROR) << "Failed to drop_caches";
87 }
88 nsecs_t drop = systemTime(SYSTEM_TIME_BOOTTIME);
89
90 BenchmarkRun();
Jeff Sharkey721e5802015-05-19 11:20:48 -070091 sync();
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -070092 nsecs_t run = systemTime(SYSTEM_TIME_BOOTTIME);
93
94 BenchmarkDestroy();
Jeff Sharkey721e5802015-05-19 11:20:48 -070095 sync();
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -070096 nsecs_t destroy = systemTime(SYSTEM_TIME_BOOTTIME);
97
98 nsecs_t create_d = create - start;
99 nsecs_t drop_d = drop - create;
100 nsecs_t run_d = run - drop;
101 nsecs_t destroy_d = destroy - run;
102
103 LOG(INFO) << "create took " << nanoseconds_to_milliseconds(create_d) << "ms";
104 LOG(INFO) << "drop took " << nanoseconds_to_milliseconds(drop_d) << "ms";
105 LOG(INFO) << "run took " << nanoseconds_to_milliseconds(run_d) << "ms";
106 LOG(INFO) << "destroy took " << nanoseconds_to_milliseconds(destroy_d) << "ms";
107
108 std::string detail;
109 detail += "id=" + BenchmarkIdent()
110 + ",cr=" + std::to_string(create_d)
111 + ",dr=" + std::to_string(drop_d)
112 + ",ru=" + std::to_string(run_d)
113 + ",de=" + std::to_string(destroy_d)
114 + ",si=" + simpleRead(sysPath + "/size")
115 + ",ve=" + simpleRead(sysPath + "/device/vendor")
116 + ",mo=" + simpleRead(sysPath + "/device/model")
Jeff Sharkey721e5802015-05-19 11:20:48 -0700117 + ",csd=" + simpleRead(sysPath + "/device/csd");
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700118
119 // Scrub CRC and serial number out of CID
120 std::string cid = simpleRead(sysPath + "/device/cid");
121 if (cid.length() == 32) {
122 cid.erase(32, 1);
123 cid.erase(18, 8);
124 detail += ",cid=" + cid;
125 }
126
127 VolumeManager::Instance()->getBroadcaster()->sendBroadcast(
128 ResponseCode::BenchmarkResult, detail.c_str(), false);
129
130 if (chdir(orig_cwd) != 0) {
131 PLOG(ERROR) << "Failed to chdir";
132 }
133 if (android_set_ioprio(0, orig_clazz, orig_ioprio)) {
134 PLOG(ERROR) << "Failed to android_set_ioprio";
135 }
136 if (setpriority(PRIO_PROCESS, 0, orig_prio) != 0) {
137 PLOG(ERROR) << "Failed to setpriority";
138 }
139 return run_d;
140}
141
142} // namespace vold
143} // namespace android