| Inseob Kim | 5d5476b | 2022-06-27 13:22:09 +0900 | [diff] [blame] | 1 | /* |
| 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 Wang | 246108f | 2022-07-25 14:44:13 +0000 | [diff] [blame] | 16 | |
| 17 | #include <aidl/android/system/virtualmachineservice/IVirtualMachineService.h> |
| 18 | #include <aidl/com/android/microdroid/testservice/BnBenchmarkService.h> |
| Alice Wang | 98e6e8a | 2022-08-08 15:31:58 +0000 | [diff] [blame] | 19 | #include <android-base/logging.h> |
| 20 | #include <android-base/parseint.h> |
| Alice Wang | 246108f | 2022-07-25 14:44:13 +0000 | [diff] [blame] | 21 | #include <android-base/result.h> |
| Alice Wang | 98e6e8a | 2022-08-08 15:31:58 +0000 | [diff] [blame] | 22 | #include <android-base/strings.h> |
| Alice Wang | 246108f | 2022-07-25 14:44:13 +0000 | [diff] [blame] | 23 | #include <android-base/unique_fd.h> |
| 24 | #include <fcntl.h> |
| 25 | #include <linux/vm_sockets.h> |
| 26 | #include <stdio.h> |
| Alice Wang | 98e6e8a | 2022-08-08 15:31:58 +0000 | [diff] [blame] | 27 | #include <time.h> |
| Inseob Kim | 5d5476b | 2022-06-27 13:22:09 +0900 | [diff] [blame] | 28 | #include <unistd.h> |
| Alan Stokes | 52d3c72 | 2022-10-04 17:27:13 +0100 | [diff] [blame^] | 29 | #include <vm_main.h> |
| Alice Wang | 4d370e6 | 2022-10-11 08:33:34 +0000 | [diff] [blame] | 30 | #include <vm_payload.h> |
| Inseob Kim | 5d5476b | 2022-06-27 13:22:09 +0900 | [diff] [blame] | 31 | |
| Alice Wang | 246108f | 2022-07-25 14:44:13 +0000 | [diff] [blame] | 32 | #include <binder_rpc_unstable.hpp> |
| David Brazdil | a2129e0 | 2022-08-19 14:01:44 +0100 | [diff] [blame] | 33 | #include <fstream> |
| Alice Wang | 246108f | 2022-07-25 14:44:13 +0000 | [diff] [blame] | 34 | #include <random> |
| 35 | #include <string> |
| 36 | |
| Alice Wang | 98e6e8a | 2022-08-08 15:31:58 +0000 | [diff] [blame] | 37 | #include "io_vsock.h" |
| Alice Wang | 246108f | 2022-07-25 14:44:13 +0000 | [diff] [blame] | 38 | |
| 39 | using aidl::android::system::virtualmachineservice::IVirtualMachineService; |
| 40 | using android::base::ErrnoError; |
| 41 | using android::base::Error; |
| 42 | using android::base::Result; |
| 43 | using android::base::unique_fd; |
| 44 | |
| 45 | namespace { |
| 46 | constexpr uint64_t kBlockSizeBytes = 4096; |
| Alice Wang | f2685f6 | 2022-09-06 07:26:53 +0000 | [diff] [blame] | 47 | constexpr uint64_t kNumBytesPerMB = 1024 * 1024; |
| Alice Wang | 246108f | 2022-07-25 14:44:13 +0000 | [diff] [blame] | 48 | |
| David Brazdil | a2129e0 | 2022-08-19 14:01:44 +0100 | [diff] [blame] | 49 | template <typename T> |
| 50 | static ndk::ScopedAStatus resultStatus(const T& result) { |
| 51 | if (!result.ok()) { |
| 52 | std::stringstream error; |
| 53 | error << result.error(); |
| 54 | return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT, |
| 55 | error.str().c_str()); |
| 56 | } |
| 57 | return ndk::ScopedAStatus::ok(); |
| 58 | } |
| 59 | |
| Alice Wang | 246108f | 2022-07-25 14:44:13 +0000 | [diff] [blame] | 60 | class IOBenchmarkService : public aidl::com::android::microdroid::testservice::BnBenchmarkService { |
| 61 | public: |
| Alice Wang | f2685f6 | 2022-09-06 07:26:53 +0000 | [diff] [blame] | 62 | ndk::ScopedAStatus measureReadRate(const std::string& filename, int64_t fileSizeBytes, |
| 63 | bool isRand, double* out) override { |
| 64 | auto res = measure_read_rate(filename, fileSizeBytes, isRand); |
| David Brazdil | a2129e0 | 2022-08-19 14:01:44 +0100 | [diff] [blame] | 65 | if (res.ok()) { |
| Alice Wang | 246108f | 2022-07-25 14:44:13 +0000 | [diff] [blame] | 66 | *out = res.value(); |
| Alice Wang | 246108f | 2022-07-25 14:44:13 +0000 | [diff] [blame] | 67 | } |
| David Brazdil | a2129e0 | 2022-08-19 14:01:44 +0100 | [diff] [blame] | 68 | return resultStatus(res); |
| 69 | } |
| 70 | |
| 71 | ndk::ScopedAStatus getMemInfoEntry(const std::string& name, int64_t* out) override { |
| 72 | auto value = read_meminfo_entry(name); |
| 73 | if (!value.ok()) { |
| 74 | return resultStatus(value); |
| 75 | } |
| 76 | |
| 77 | *out = (int64_t)value.value(); |
| Alice Wang | 246108f | 2022-07-25 14:44:13 +0000 | [diff] [blame] | 78 | return ndk::ScopedAStatus::ok(); |
| Inseob Kim | 5d5476b | 2022-06-27 13:22:09 +0900 | [diff] [blame] | 79 | } |
| Alice Wang | 246108f | 2022-07-25 14:44:13 +0000 | [diff] [blame] | 80 | |
| Alice Wang | 98e6e8a | 2022-08-08 15:31:58 +0000 | [diff] [blame] | 81 | ndk::ScopedAStatus initVsockServer(int32_t port, int32_t* out) override { |
| 82 | auto res = io_vsock::init_vsock_server(port); |
| 83 | if (res.ok()) { |
| 84 | *out = res.value(); |
| 85 | } |
| 86 | return resultStatus(res); |
| 87 | } |
| 88 | |
| 89 | ndk::ScopedAStatus runVsockServerAndReceiveData(int32_t server_fd, |
| 90 | int32_t num_bytes_to_receive) override { |
| 91 | auto res = io_vsock::run_vsock_server_and_receive_data(server_fd, num_bytes_to_receive); |
| 92 | return resultStatus(res); |
| 93 | } |
| 94 | |
| Alice Wang | 246108f | 2022-07-25 14:44:13 +0000 | [diff] [blame] | 95 | private: |
| Alice Wang | f2685f6 | 2022-09-06 07:26:53 +0000 | [diff] [blame] | 96 | /** Measures the read rate for reading the given file. */ |
| 97 | Result<double> measure_read_rate(const std::string& filename, int64_t fileSizeBytes, |
| 98 | bool is_rand) { |
| Alice Wang | 246108f | 2022-07-25 14:44:13 +0000 | [diff] [blame] | 99 | const int64_t block_count = fileSizeBytes / kBlockSizeBytes; |
| 100 | std::vector<uint64_t> offsets; |
| 101 | if (is_rand) { |
| 102 | std::mt19937 rd{std::random_device{}()}; |
| 103 | offsets.reserve(block_count); |
| 104 | for (auto i = 0; i < block_count; ++i) offsets.push_back(i * kBlockSizeBytes); |
| 105 | std::shuffle(offsets.begin(), offsets.end(), rd); |
| 106 | } |
| 107 | char buf[kBlockSizeBytes]; |
| 108 | |
| 109 | clock_t start = clock(); |
| Alice Wang | fc24b37 | 2022-07-28 16:45:42 +0000 | [diff] [blame] | 110 | unique_fd fd(open(filename.c_str(), O_RDONLY | O_CLOEXEC)); |
| Alice Wang | 246108f | 2022-07-25 14:44:13 +0000 | [diff] [blame] | 111 | if (fd.get() == -1) { |
| 112 | return ErrnoError() << "Read: opening " << filename << " failed"; |
| 113 | } |
| 114 | for (auto i = 0; i < block_count; ++i) { |
| 115 | if (is_rand) { |
| 116 | if (lseek(fd.get(), offsets[i], SEEK_SET) == -1) { |
| 117 | return ErrnoError() << "failed to lseek"; |
| 118 | } |
| 119 | } |
| 120 | auto bytes = read(fd.get(), buf, kBlockSizeBytes); |
| 121 | if (bytes == 0) { |
| 122 | return Error() << "unexpected end of file"; |
| 123 | } else if (bytes == -1) { |
| 124 | return ErrnoError() << "failed to read"; |
| 125 | } |
| 126 | } |
| Alice Wang | f2685f6 | 2022-09-06 07:26:53 +0000 | [diff] [blame] | 127 | double elapsed_seconds = ((double)clock() - start) / CLOCKS_PER_SEC; |
| 128 | double read_rate = (double)fileSizeBytes / kNumBytesPerMB / elapsed_seconds; |
| 129 | return {read_rate}; |
| Alice Wang | 246108f | 2022-07-25 14:44:13 +0000 | [diff] [blame] | 130 | } |
| David Brazdil | a2129e0 | 2022-08-19 14:01:44 +0100 | [diff] [blame] | 131 | |
| 132 | Result<size_t> read_meminfo_entry(const std::string& stat) { |
| 133 | std::ifstream fs("/proc/meminfo"); |
| 134 | if (!fs.is_open()) { |
| 135 | return Error() << "could not open /proc/meminfo"; |
| 136 | } |
| 137 | |
| 138 | std::string line; |
| 139 | while (std::getline(fs, line)) { |
| 140 | auto elems = android::base::Split(line, ":"); |
| 141 | if (elems[0] != stat) continue; |
| 142 | |
| 143 | std::string str = android::base::Trim(elems[1]); |
| 144 | if (android::base::EndsWith(str, " kB")) { |
| 145 | str = str.substr(0, str.length() - 3); |
| 146 | } |
| 147 | |
| 148 | size_t value; |
| 149 | if (!android::base::ParseUint(str, &value)) { |
| 150 | return ErrnoError() << "failed to parse \"" << str << "\" as size_t"; |
| 151 | } |
| 152 | return {value}; |
| 153 | } |
| 154 | |
| 155 | return Error() << "entry \"" << stat << "\" not found"; |
| 156 | } |
| Alice Wang | 246108f | 2022-07-25 14:44:13 +0000 | [diff] [blame] | 157 | }; |
| 158 | |
| 159 | Result<void> run_io_benchmark_tests() { |
| 160 | auto test_service = ndk::SharedRefBase::make<IOBenchmarkService>(); |
| 161 | auto callback = []([[maybe_unused]] void* param) { |
| Andrew Scull | 655e98e | 2022-10-10 22:24:58 +0000 | [diff] [blame] | 162 | if (!AVmPayload_notifyPayloadReady()) { |
| Alice Wang | 4d370e6 | 2022-10-11 08:33:34 +0000 | [diff] [blame] | 163 | LOG(ERROR) << "failed to notify payload ready to virtualizationservice"; |
| Alice Wang | 246108f | 2022-07-25 14:44:13 +0000 | [diff] [blame] | 164 | abort(); |
| 165 | } |
| 166 | }; |
| 167 | |
| 168 | if (!RunRpcServerCallback(test_service->asBinder().get(), test_service->SERVICE_PORT, callback, |
| 169 | nullptr)) { |
| 170 | return Error() << "RPC Server failed to run"; |
| 171 | } |
| 172 | return {}; |
| 173 | } |
| 174 | } // Anonymous namespace |
| 175 | |
| Alan Stokes | 52d3c72 | 2022-10-04 17:27:13 +0100 | [diff] [blame^] | 176 | extern "C" int AVmPayload_main() { |
| Alan Stokes | 38d00f8 | 2022-10-03 17:43:45 +0100 | [diff] [blame] | 177 | if (auto res = run_io_benchmark_tests(); !res.ok()) { |
| 178 | LOG(ERROR) << "IO benchmark test failed: " << res.error() << "\n"; |
| 179 | return EXIT_FAILURE; |
| Alice Wang | 246108f | 2022-07-25 14:44:13 +0000 | [diff] [blame] | 180 | } |
| Alice Wang | 98e6e8a | 2022-08-08 15:31:58 +0000 | [diff] [blame] | 181 | return EXIT_SUCCESS; |
| Inseob Kim | 5d5476b | 2022-06-27 13:22:09 +0900 | [diff] [blame] | 182 | } |