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