blob: 4e3c14da4a6452479a79dcdf067d8e2fa607e310 [file] [log] [blame]
Inseob Kim5d5476b2022-06-27 13:22:09 +09001/*
2 * Copyright (C) 2022 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 */
Alice Wang246108f2022-07-25 14:44:13 +000016
17#include <aidl/android/system/virtualmachineservice/IVirtualMachineService.h>
18#include <aidl/com/android/microdroid/testservice/BnBenchmarkService.h>
Alice Wang98e6e8a2022-08-08 15:31:58 +000019#include <android-base/logging.h>
20#include <android-base/parseint.h>
Alice Wang246108f2022-07-25 14:44:13 +000021#include <android-base/result.h>
Alice Wang98e6e8a2022-08-08 15:31:58 +000022#include <android-base/strings.h>
Alice Wang246108f2022-07-25 14:44:13 +000023#include <android-base/unique_fd.h>
24#include <fcntl.h>
25#include <linux/vm_sockets.h>
26#include <stdio.h>
Alice Wang98e6e8a2022-08-08 15:31:58 +000027#include <time.h>
Inseob Kim5d5476b2022-06-27 13:22:09 +090028#include <unistd.h>
Alice Wang4d370e62022-10-11 08:33:34 +000029#include <vm_payload.h>
Inseob Kim5d5476b2022-06-27 13:22:09 +090030
Alice Wang246108f2022-07-25 14:44:13 +000031#include <binder_rpc_unstable.hpp>
David Brazdila2129e02022-08-19 14:01:44 +010032#include <fstream>
Alice Wang246108f2022-07-25 14:44:13 +000033#include <random>
34#include <string>
35
Alice Wang98e6e8a2022-08-08 15:31:58 +000036#include "io_vsock.h"
Alice Wang246108f2022-07-25 14:44:13 +000037
38using aidl::android::system::virtualmachineservice::IVirtualMachineService;
39using android::base::ErrnoError;
40using android::base::Error;
41using android::base::Result;
42using android::base::unique_fd;
43
44namespace {
45constexpr uint64_t kBlockSizeBytes = 4096;
Alice Wangf2685f62022-09-06 07:26:53 +000046constexpr uint64_t kNumBytesPerMB = 1024 * 1024;
Alice Wang246108f2022-07-25 14:44:13 +000047
David Brazdila2129e02022-08-19 14:01:44 +010048template <typename T>
49static ndk::ScopedAStatus resultStatus(const T& result) {
50 if (!result.ok()) {
51 std::stringstream error;
52 error << result.error();
53 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
54 error.str().c_str());
55 }
56 return ndk::ScopedAStatus::ok();
57}
58
Alice Wang246108f2022-07-25 14:44:13 +000059class IOBenchmarkService : public aidl::com::android::microdroid::testservice::BnBenchmarkService {
60public:
Alice Wangf2685f62022-09-06 07:26:53 +000061 ndk::ScopedAStatus measureReadRate(const std::string& filename, int64_t fileSizeBytes,
62 bool isRand, double* out) override {
63 auto res = measure_read_rate(filename, fileSizeBytes, isRand);
David Brazdila2129e02022-08-19 14:01:44 +010064 if (res.ok()) {
Alice Wang246108f2022-07-25 14:44:13 +000065 *out = res.value();
Alice Wang246108f2022-07-25 14:44:13 +000066 }
David Brazdila2129e02022-08-19 14:01:44 +010067 return resultStatus(res);
68 }
69
70 ndk::ScopedAStatus getMemInfoEntry(const std::string& name, int64_t* out) override {
71 auto value = read_meminfo_entry(name);
72 if (!value.ok()) {
73 return resultStatus(value);
74 }
75
76 *out = (int64_t)value.value();
Alice Wang246108f2022-07-25 14:44:13 +000077 return ndk::ScopedAStatus::ok();
Inseob Kim5d5476b2022-06-27 13:22:09 +090078 }
Alice Wang246108f2022-07-25 14:44:13 +000079
Alice Wang98e6e8a2022-08-08 15:31:58 +000080 ndk::ScopedAStatus initVsockServer(int32_t port, int32_t* out) override {
81 auto res = io_vsock::init_vsock_server(port);
82 if (res.ok()) {
83 *out = res.value();
84 }
85 return resultStatus(res);
86 }
87
88 ndk::ScopedAStatus runVsockServerAndReceiveData(int32_t server_fd,
89 int32_t num_bytes_to_receive) override {
90 auto res = io_vsock::run_vsock_server_and_receive_data(server_fd, num_bytes_to_receive);
91 return resultStatus(res);
92 }
93
Alice Wang246108f2022-07-25 14:44:13 +000094private:
Alice Wangf2685f62022-09-06 07:26:53 +000095 /** Measures the read rate for reading the given file. */
96 Result<double> measure_read_rate(const std::string& filename, int64_t fileSizeBytes,
97 bool is_rand) {
Alice Wang246108f2022-07-25 14:44:13 +000098 const int64_t block_count = fileSizeBytes / kBlockSizeBytes;
99 std::vector<uint64_t> offsets;
100 if (is_rand) {
101 std::mt19937 rd{std::random_device{}()};
102 offsets.reserve(block_count);
103 for (auto i = 0; i < block_count; ++i) offsets.push_back(i * kBlockSizeBytes);
104 std::shuffle(offsets.begin(), offsets.end(), rd);
105 }
106 char buf[kBlockSizeBytes];
107
108 clock_t start = clock();
Alice Wangfc24b372022-07-28 16:45:42 +0000109 unique_fd fd(open(filename.c_str(), O_RDONLY | O_CLOEXEC));
Alice Wang246108f2022-07-25 14:44:13 +0000110 if (fd.get() == -1) {
111 return ErrnoError() << "Read: opening " << filename << " failed";
112 }
113 for (auto i = 0; i < block_count; ++i) {
114 if (is_rand) {
115 if (lseek(fd.get(), offsets[i], SEEK_SET) == -1) {
116 return ErrnoError() << "failed to lseek";
117 }
118 }
119 auto bytes = read(fd.get(), buf, kBlockSizeBytes);
120 if (bytes == 0) {
121 return Error() << "unexpected end of file";
122 } else if (bytes == -1) {
123 return ErrnoError() << "failed to read";
124 }
125 }
Alice Wangf2685f62022-09-06 07:26:53 +0000126 double elapsed_seconds = ((double)clock() - start) / CLOCKS_PER_SEC;
127 double read_rate = (double)fileSizeBytes / kNumBytesPerMB / elapsed_seconds;
128 return {read_rate};
Alice Wang246108f2022-07-25 14:44:13 +0000129 }
David Brazdila2129e02022-08-19 14:01:44 +0100130
131 Result<size_t> read_meminfo_entry(const std::string& stat) {
132 std::ifstream fs("/proc/meminfo");
133 if (!fs.is_open()) {
134 return Error() << "could not open /proc/meminfo";
135 }
136
137 std::string line;
138 while (std::getline(fs, line)) {
139 auto elems = android::base::Split(line, ":");
140 if (elems[0] != stat) continue;
141
142 std::string str = android::base::Trim(elems[1]);
143 if (android::base::EndsWith(str, " kB")) {
144 str = str.substr(0, str.length() - 3);
145 }
146
147 size_t value;
148 if (!android::base::ParseUint(str, &value)) {
149 return ErrnoError() << "failed to parse \"" << str << "\" as size_t";
150 }
151 return {value};
152 }
153
154 return Error() << "entry \"" << stat << "\" not found";
155 }
Alice Wang246108f2022-07-25 14:44:13 +0000156};
157
158Result<void> run_io_benchmark_tests() {
159 auto test_service = ndk::SharedRefBase::make<IOBenchmarkService>();
160 auto callback = []([[maybe_unused]] void* param) {
Andrew Scull655e98e2022-10-10 22:24:58 +0000161 if (!AVmPayload_notifyPayloadReady()) {
Alice Wang4d370e62022-10-11 08:33:34 +0000162 LOG(ERROR) << "failed to notify payload ready to virtualizationservice";
Alice Wang246108f2022-07-25 14:44:13 +0000163 abort();
164 }
165 };
166
167 if (!RunRpcServerCallback(test_service->asBinder().get(), test_service->SERVICE_PORT, callback,
168 nullptr)) {
169 return Error() << "RPC Server failed to run";
170 }
171 return {};
172}
173} // Anonymous namespace
174
Alan Stokes38d00f82022-10-03 17:43:45 +0100175extern "C" int android_native_main(int /* argc */, char* /* argv */[]) {
176 if (auto res = run_io_benchmark_tests(); !res.ok()) {
177 LOG(ERROR) << "IO benchmark test failed: " << res.error() << "\n";
178 return EXIT_FAILURE;
Alice Wang246108f2022-07-25 14:44:13 +0000179 }
Alice Wang98e6e8a2022-08-08 15:31:58 +0000180 return EXIT_SUCCESS;
Inseob Kim5d5476b2022-06-27 13:22:09 +0900181}