|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 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 <folly/io/async/IoUringBufferProvider.h> |
| 18 | + |
| 19 | +#if FOLLY_HAS_LIBURING |
| 20 | + |
| 21 | +namespace folly { |
| 22 | + |
| 23 | +namespace { |
| 24 | + |
| 25 | +template <typename Ring> |
| 26 | +typename Ring::UniquePtr createRing( |
| 27 | + io_uring* ioRingPtr, const IoUringBufferProvider::Options& options) { |
| 28 | + return Ring::create( |
| 29 | + ioRingPtr, |
| 30 | + { |
| 31 | + .gid = options.gid, |
| 32 | + .bufferCount = options.bufferCount, |
| 33 | + .bufferSize = options.bufferSize, |
| 34 | + .useIncrementalBuffers = options.useIncrementalBuffers, |
| 35 | + }); |
| 36 | +} |
| 37 | + |
| 38 | +} // namespace |
| 39 | + |
| 40 | +std::unique_ptr<IoUringBufferProvider> IoUringBufferProvider::create( |
| 41 | + io_uring* ioRingPtr, bool useDynamicRing, Options options) { |
| 42 | + try { |
| 43 | + if (useDynamicRing) { |
| 44 | + return std::unique_ptr<IoUringBufferProvider>(new IoUringBufferProvider( |
| 45 | + Ring{createRing<IoUringDynamicProvidedBufferRing>( |
| 46 | + ioRingPtr, options)})); |
| 47 | + } |
| 48 | + return std::unique_ptr<IoUringBufferProvider>(new IoUringBufferProvider( |
| 49 | + Ring{createRing<IoUringProvidedBufferRing>(ioRingPtr, options)})); |
| 50 | + } catch (const IoUringProvidedBufferRing::LibUringCallError& ex) { |
| 51 | + throw LibUringCallError(ex.what()); |
| 52 | + } catch (const IoUringDynamicProvidedBufferRing::LibUringCallError& ex) { |
| 53 | + throw LibUringCallError(ex.what()); |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +void IoUringBufferProvider::enobuf() noexcept { |
| 58 | + std::visit([](auto& ring) { ring->enobuf(); }, ring_); |
| 59 | +} |
| 60 | + |
| 61 | +uint32_t IoUringBufferProvider::getAndResetEnobufCount() noexcept { |
| 62 | + return std::visit( |
| 63 | + [](auto& ring) { return ring->getAndResetEnobufCount(); }, ring_); |
| 64 | +} |
| 65 | + |
| 66 | +std::unique_ptr<IOBuf> IoUringBufferProvider::getIoBuf( |
| 67 | + uint16_t startBufId, size_t totalLength, bool hasMore) noexcept { |
| 68 | + return std::visit( |
| 69 | + [&](auto& ring) { |
| 70 | + return ring->getIoBuf(startBufId, totalLength, hasMore); |
| 71 | + }, |
| 72 | + ring_); |
| 73 | +} |
| 74 | + |
| 75 | +std::unique_ptr<IOBuf> IoUringBufferProvider::getIoBuf( |
| 76 | + const struct io_uring_cqe* cqe) noexcept { |
| 77 | + return std::visit([&](auto& ring) { return ring->getIoBuf(cqe); }, ring_); |
| 78 | +} |
| 79 | + |
| 80 | +uint32_t IoUringBufferProvider::count() const noexcept { |
| 81 | + return std::visit([](const auto& ring) { return ring->count(); }, ring_); |
| 82 | +} |
| 83 | + |
| 84 | +bool IoUringBufferProvider::available() const noexcept { |
| 85 | + return std::visit([](const auto& ring) { return ring->available(); }, ring_); |
| 86 | +} |
| 87 | + |
| 88 | +size_t IoUringBufferProvider::sizePerBuffer() const noexcept { |
| 89 | + return std::visit( |
| 90 | + [](const auto& ring) { return ring->sizePerBuffer(); }, ring_); |
| 91 | +} |
| 92 | + |
| 93 | +uint16_t IoUringBufferProvider::gid() const noexcept { |
| 94 | + return std::visit([](const auto& ring) { return ring->gid(); }, ring_); |
| 95 | +} |
| 96 | + |
| 97 | +int IoUringBufferProvider::getUtilPct() const noexcept { |
| 98 | + return std::visit([](const auto& ring) { return ring->getUtilPct(); }, ring_); |
| 99 | +} |
| 100 | + |
| 101 | +uint16_t IoUringBufferProvider::areaCount() const noexcept { |
| 102 | + auto* ring = std::get_if<IoUringDynamicProvidedBufferRing::UniquePtr>(&ring_); |
| 103 | + if (!ring) { |
| 104 | + return 1; |
| 105 | + } |
| 106 | + return (*ring)->areaCount(); |
| 107 | +} |
| 108 | + |
| 109 | +} // namespace folly |
| 110 | + |
| 111 | +#endif |
0 commit comments