|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +#include "linux_aio_queue.h" |
| 19 | +#include "posix_backend.h" |
| 20 | +#include <errno.h> |
| 21 | +#include "common/nixl_log.h" |
| 22 | +#include <string.h> |
| 23 | +#include <time.h> |
| 24 | +#include <stdexcept> |
| 25 | + |
| 26 | +linuxAioQueue::linuxAioQueue(int num_entries, nixl_xfer_op_t operation) |
| 27 | + : io_ctx(io_context_t()), |
| 28 | + ios(num_entries), |
| 29 | + num_entries(num_entries), |
| 30 | + num_ios_to_submit(0), |
| 31 | + completed(num_entries), |
| 32 | + num_completed(0), |
| 33 | + operation(operation) { |
| 34 | + if (num_entries <= 0) { |
| 35 | + throw std::runtime_error("Invalid number of entries for AIO queue"); |
| 36 | + } |
| 37 | + |
| 38 | + if (operation != NIXL_READ && operation != NIXL_WRITE) { |
| 39 | + throw std::runtime_error("Invalid operation for AIO queue"); |
| 40 | + } |
| 41 | + |
| 42 | + int res = io_queue_init(num_entries, &io_ctx); |
| 43 | + if (res) { |
| 44 | + throw std::runtime_error("io_queue_init (" + std::to_string(num_entries) + |
| 45 | + ") failed with " + std::to_string(res)); |
| 46 | + } |
| 47 | + |
| 48 | + ios_to_submit.assign(num_entries, nullptr); |
| 49 | +} |
| 50 | + |
| 51 | +linuxAioQueue::~linuxAioQueue() { |
| 52 | + io_queue_release(io_ctx); |
| 53 | +} |
| 54 | + |
| 55 | +nixl_status_t |
| 56 | +linuxAioQueue::submit(const nixl_meta_dlist_t &, const nixl_meta_dlist_t &) { |
| 57 | + if (!num_ios_to_submit) { |
| 58 | + return NIXL_IN_PROG; |
| 59 | + } |
| 60 | + |
| 61 | + int ret = io_submit(io_ctx, num_ios_to_submit, ios_to_submit.data()); |
| 62 | + if (ret != num_ios_to_submit) { |
| 63 | + if (ret < 0) { |
| 64 | + NIXL_ERROR << absl::StrFormat("linux_aio submit failed: %s", nixl_strerror(-ret)); |
| 65 | + } else { |
| 66 | + NIXL_ERROR << absl::StrFormat( |
| 67 | + "linux_aio submit failed. Partial submission: %d/%d", num_ios_to_submit, ret); |
| 68 | + } |
| 69 | + return NIXL_ERR_BACKEND; |
| 70 | + } |
| 71 | + |
| 72 | + num_completed = 0; |
| 73 | + num_ios_to_submit = 0; |
| 74 | + return NIXL_IN_PROG; |
| 75 | +} |
| 76 | + |
| 77 | +nixl_status_t |
| 78 | +linuxAioQueue::checkCompleted() { |
| 79 | + if (num_completed == num_entries) { |
| 80 | + return NIXL_SUCCESS; |
| 81 | + } |
| 82 | + |
| 83 | + struct io_event events[32]; |
| 84 | + int rc; |
| 85 | + struct timespec timeout = {0, 0}; |
| 86 | + |
| 87 | + rc = io_getevents(io_ctx, 0, 32, events, &timeout); |
| 88 | + if (rc < 0) { |
| 89 | + NIXL_ERROR << "io_getevents error: " << rc; |
| 90 | + return NIXL_ERR_BACKEND; |
| 91 | + } |
| 92 | + |
| 93 | + for (int i = 0; i < rc; i++) { |
| 94 | + struct iocb *io = events[i].obj; |
| 95 | + size_t idx = (size_t)io->data; |
| 96 | + |
| 97 | + ios_to_submit[idx] = nullptr; // Mark as completed |
| 98 | + |
| 99 | + if (events[i].res < 0) { |
| 100 | + NIXL_ERROR << "AIO operation failed: " << events[i].res; |
| 101 | + return NIXL_ERR_BACKEND; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + num_completed += rc; |
| 106 | + |
| 107 | + return (num_completed == num_entries) ? NIXL_SUCCESS : NIXL_IN_PROG; |
| 108 | +} |
| 109 | + |
| 110 | +nixl_status_t |
| 111 | +linuxAioQueue::prepIO(int fd, void *buf, size_t len, off_t offset) { |
| 112 | + if (num_ios_to_submit == num_entries) { |
| 113 | + NIXL_ERROR << "No available IOs"; |
| 114 | + return NIXL_ERR_BACKEND; |
| 115 | + } |
| 116 | + |
| 117 | + // Check if file descriptor is valid |
| 118 | + if (fd < 0) { |
| 119 | + NIXL_ERROR << "Invalid file descriptor provided to prepareIO"; |
| 120 | + return NIXL_ERR_BACKEND; |
| 121 | + } |
| 122 | + |
| 123 | + // Check buffer and length |
| 124 | + if (!buf || len == 0) { |
| 125 | + NIXL_ERROR << "Invalid buffer or length provided to prepareIO"; |
| 126 | + return NIXL_ERR_BACKEND; |
| 127 | + } |
| 128 | + |
| 129 | + int idx = num_ios_to_submit; |
| 130 | + auto io = &ios[idx]; |
| 131 | + |
| 132 | + if (operation == NIXL_READ) { |
| 133 | + io_prep_pread(io, fd, buf, len, offset); |
| 134 | + } else { |
| 135 | + io_prep_pwrite(io, fd, buf, len, offset); |
| 136 | + } |
| 137 | + |
| 138 | + ios_to_submit[idx] = io; |
| 139 | + io->data = (void *)(uintptr_t)idx; |
| 140 | + num_ios_to_submit++; |
| 141 | + |
| 142 | + return NIXL_SUCCESS; |
| 143 | +} |
0 commit comments