Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ set(linyaps-box_LIBRARY_SOURCE
src/linyaps_box/utils/mkdir.h
src/linyaps_box/utils/mknod.cpp
src/linyaps_box/utils/mknod.h
src/linyaps_box/utils/mman.cpp
src/linyaps_box/utils/platform.cpp
src/linyaps_box/utils/platform.h
src/linyaps_box/utils/process.cpp
Expand Down
19 changes: 10 additions & 9 deletions src/linyaps_box/io/forwarder.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2025 UnionTech Software Technology Co., Ltd.
// SPDX-FileCopyrightText: 2025 - 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later

Expand Down Expand Up @@ -50,14 +50,14 @@ auto Forwarder::pull() -> Status

last_pull_again = false;
while (!rb->full()) {
auto vecs = rb->get_write_vecs();
const auto iov_cnt = (vecs[1].iov_len > 0) ? 2U : 1U;
auto *ptr = rb->get_write_ptr();
const utils::span<std::byte> span(ptr, rb->free_space());

std::size_t bytes_read{ 0 };
auto status = src_.fd->read_vecs({ vecs.data(), iov_cnt }, bytes_read);
auto status = src_.fd->read_span(span, bytes_read);

if (bytes_read > 0) {
rb->advance_tail(bytes_read);
rb->advance_head(bytes_read);
}

if (status == utils::file_descriptor::IOStatus::TryAgain) {
Expand Down Expand Up @@ -91,14 +91,15 @@ auto Forwarder::push() -> Status

last_push_again = false;
while (!rb->empty()) {
auto vecs = rb->get_read_vecs();
const auto iov_cnt = (vecs[1].iov_len > 0) ? 2U : 1U;
auto *ptr = rb->get_read_ptr();

const utils::span<const std::byte> span(ptr, rb->size());
std::size_t bytes_write{ 0 };
auto status = dst_.fd->write_vecs({ vecs.data(), iov_cnt }, bytes_write);

auto status = dst_.fd->write_span(span, bytes_write);

if (bytes_write > 0) {
rb->advance_head(bytes_write);
rb->advance_tail(bytes_write);
}

if (status == utils::file_descriptor::IOStatus::TryAgain) {
Expand Down
4 changes: 2 additions & 2 deletions src/linyaps_box/io/forwarder.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2025 UnionTech Software Technology Co., Ltd.
// SPDX-FileCopyrightText: 2025 - 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later

Expand Down Expand Up @@ -53,7 +53,7 @@ class Forwarder
std::reference_wrapper<Epoll> poller;
FdContext src_;
FdContext dst_;
utils::ring_buffer_ptr rb;
utils::ring_buffer::ptr rb;
};

} // namespace linyaps_box::io
4 changes: 2 additions & 2 deletions src/linyaps_box/utils/close_range.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2025 UnionTech Software Technology Co., Ltd.
// SPDX-FileCopyrightText: 2025 - 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later

Expand Down Expand Up @@ -37,7 +37,7 @@ void close_range_fallback(uint first, uint last, int flags)
throw std::system_error(errno, std::system_category(), "opendir /proc/self/fd");
}

auto close_dir = make_defer([dir]() noexcept {
auto close_dir = linyaps_box::utils::make_defer([dir]() noexcept {
if (closedir(dir) < 0) {
LINYAPS_BOX_WARNING() << "closedir /proc/self/fd failed: " << strerror(errno)
<< ", but this may not be a problem";
Expand Down
6 changes: 5 additions & 1 deletion src/linyaps_box/utils/defer.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2025 UnionTech Software Technology Co., Ltd.
// SPDX-FileCopyrightText: 2025 - 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later

Expand All @@ -9,6 +9,8 @@
#include <type_traits>
#include <utility>

namespace linyaps_box::utils {

// Type constraints for defer, the cleanup function should not throw exceptions
template<typename Fn>
constexpr bool compatible_defer = std::is_nothrow_invocable_r_v<void, Fn>;
Expand Down Expand Up @@ -99,3 +101,5 @@ auto make_errdefer(Fn &&fn) noexcept
{
return defer<std::decay_t<Fn>, defer_policy::on_error>(std::forward<Fn>(fn));
}

} // namespace linyaps_box::utils
48 changes: 48 additions & 0 deletions src/linyaps_box/utils/mman.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// SPDX-FileCopyrightText: 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later

#include "mman.h"

#include <sys/mman.h>

Check warning on line 7 in src/linyaps_box/utils/mman.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <sys/mman.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.

namespace linyaps_box::utils {
auto memfd_create(const char *name, unsigned int flags) -> file_descriptor
{
auto fd = ::memfd_create(name, flags);
if (fd == -1) {
throw std::system_error(errno, std::system_category(), "memfd_create failed");
}

return file_descriptor{ fd };
}

auto mmap(void *addr,
std::size_t length,
int prot,
int flags,
std::optional<std::reference_wrapper<const file_descriptor>> fd,
off_t offset) -> std::byte *
{
auto *result = static_cast<std::byte *>(::mmap(addr,
length,
prot,
flags,
fd.has_value() ? fd.value().get().get() : -1,
offset));
if (result == MAP_FAILED) {
throw std::system_error(errno, std::system_category(), "mmap failed");
}

return result;
}

auto munmap(std::byte *addr, std::size_t length) -> void
{
auto ret = ::munmap(addr, length);
if (ret == -1) {
throw std::system_error(errno, std::system_category(), "munmap failed");
}
}

} // namespace linyaps_box::utils
20 changes: 20 additions & 0 deletions src/linyaps_box/utils/mman.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// SPDX-FileCopyrightText: 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later

#pragma once

#include "linyaps_box/utils/file_describer.h"

Check warning on line 7 in src/linyaps_box/utils/mman.h

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "linyaps_box/utils/file_describer.h" not found.

#include <optional>

Check warning on line 9 in src/linyaps_box/utils/mman.h

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <optional> not found. Please note: Cppcheck does not need standard library headers to get proper results.

namespace linyaps_box::utils {
auto memfd_create(const char *name, unsigned int flags) -> file_descriptor;
auto mmap(void *addr,
std::size_t length,
int prot,
int flags,
std::optional<std::reference_wrapper<const file_descriptor>> fd,
off_t offset) -> std::byte *;
auto munmap(std::byte *addr, std::size_t length) -> void;
} // namespace linyaps_box::utils
24 changes: 23 additions & 1 deletion src/linyaps_box/utils/platform.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2025 UnionTech Software Technology Co., Ltd.
// SPDX-FileCopyrightText: 2025 - 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later

Expand Down Expand Up @@ -95,4 +95,26 @@ auto get_path_max(const std::filesystem::path &fs_dir) noexcept -> std::size_t
return static_cast<std::size_t>(max);
}

auto get_page_size() noexcept -> std::size_t
{
static const auto page_size = []() noexcept -> std::size_t {
errno = 0;
const auto sz = ::sysconf(_SC_PAGESIZE);

if (sz == -1) {
if (errno != 0) {
auto saved_errno = errno;
LINYAPS_BOX_WARNING() << "Failed to get page size: " << ::std::strerror(saved_errno)
<< ", defaulting to 4096";
}

return 4096;
}

return static_cast<std::size_t>(sz);
}();

return page_size;
}

} // namespace linyaps_box::utils
3 changes: 2 additions & 1 deletion src/linyaps_box/utils/platform.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2025 UnionTech Software Technology Co., Ltd.
// SPDX-FileCopyrightText: 2025 - 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later

Expand All @@ -10,4 +10,5 @@ namespace linyaps_box::utils {
auto str_to_signal(std::string_view str) -> int;
auto str_to_rlimit(std::string_view str) -> int;
auto get_path_max(const std::filesystem::path &fs_dir) noexcept -> std::size_t;
auto get_page_size() noexcept -> std::size_t;
} // namespace linyaps_box::utils
97 changes: 49 additions & 48 deletions src/linyaps_box/utils/ringbuffer.cpp
Original file line number Diff line number Diff line change
@@ -1,74 +1,75 @@
// SPDX-FileCopyrightText: 2025 UnionTech Software Technology Co., Ltd.
// SPDX-FileCopyrightText: 2025 - 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later

#include "linyaps_box/utils/ringbuffer.h"

Check warning on line 5 in src/linyaps_box/utils/ringbuffer.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "linyaps_box/utils/ringbuffer.h" not found.

#include "linyaps_box/utils/defer.h"

Check warning on line 7 in src/linyaps_box/utils/ringbuffer.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "linyaps_box/utils/defer.h" not found.
#include "linyaps_box/utils/log.h"

Check warning on line 8 in src/linyaps_box/utils/ringbuffer.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "linyaps_box/utils/log.h" not found.
#include "linyaps_box/utils/mman.h"

Check warning on line 9 in src/linyaps_box/utils/ringbuffer.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "linyaps_box/utils/mman.h" not found.
#include "linyaps_box/utils/platform.h"

Check warning on line 10 in src/linyaps_box/utils/ringbuffer.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "linyaps_box/utils/platform.h" not found.

#include <cassert>

Check warning on line 12 in src/linyaps_box/utils/ringbuffer.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <cassert> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <cstring>

Check warning on line 13 in src/linyaps_box/utils/ringbuffer.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <cstring> not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include <sys/mman.h>
#include <unistd.h>

namespace linyaps_box::utils {

ring_buffer_deleter::ring_buffer_deleter(std::size_t total_size) noexcept
: total_size{ total_size }
auto ring_buffer::deleter::operator()(ring_buffer *rb) const noexcept -> void
{
}
if (rb == nullptr) {
return;
}

auto ring_buffer_deleter::operator()(ring_buffer *rb) const -> void
{
rb->~ring_buffer();
::operator delete(rb,
total_size,
std::align_val_t(compat::hardware_constructive_interference_size));
}

auto ring_buffer::create(std::size_t requested_capacity)
-> std::unique_ptr<ring_buffer, ring_buffer_deleter>
{
requested_capacity = std::max<std::size_t>(requested_capacity, 2);

std::size_t capacity = 1;
while (capacity < requested_capacity) {
capacity <<= 1U;
try {
munmap(reinterpret_cast<std::byte *>(rb), total_size); // NOLINT
} catch (const std::system_error &e) {
LINYAPS_BOX_ERR() << "Failed to munmap ring buffer: " << e.what();
assert(false);
}

const auto total_size = sizeof(ring_buffer) + capacity;
auto *mem = ::operator new(total_size,
std::align_val_t(compat::hardware_constructive_interference_size));
auto *rb = new (mem) ring_buffer(capacity);
return { rb, ring_buffer_deleter(total_size) };
}

auto ring_buffer::get_read_vecs() const noexcept -> iov_view
auto ring_buffer::create(std::size_t requested_capacity) -> ptr
{
if (empty()) {
return {};
}
const auto page_size = utils::get_page_size();
auto meta_size = (sizeof(ring_buffer) + page_size - 1) & ~(page_size - 1);

const auto *base = data_ptr();
if (tail_ > head_) {
return { { { const_cast<std::byte *>(base + head_), tail_ - head_ }, // NOLINT
{ nullptr, 0 } } };
auto cap = page_size;
while (cap < requested_capacity) {
cap <<= 1U;
}

return { { { const_cast<std::byte *>(base + head_), capacity_ - head_ }, // NOLINT
{ const_cast<std::byte *>(base), tail_ } } }; // NOLINT
}
auto total_vma = meta_size + (2 * cap);

auto ring_buffer::get_write_vecs() const noexcept -> iov_view
{
if (full()) {
return {};
auto fd = memfd_create("linyaps_box_io_buffer", MFD_CLOEXEC);
if (::ftruncate(fd.get(), meta_size + cap) == -1) {
throw std::system_error(errno, std::system_category(), "ftruncate failed");
}

const auto *base = data_ptr();
const auto space = capacity_ - size() - 1;
auto *addr = mmap(nullptr, total_vma, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, std::nullopt, 0);
auto mem_guard = utils::make_errdefer([addr, total_vma]() noexcept {
munmap(addr, total_vma);
});

if (tail_ >= head_) {
const auto first_part = std::min(space, capacity_ - tail_);
const auto second_part = space - first_part;
return { { { const_cast<std::byte *>(base + tail_), first_part }, // NOLINT
{ const_cast<std::byte *>(base), second_part } } }; // NOLINT
}
mmap(addr,
meta_size + cap,
PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_FIXED | MAP_POPULATE,
fd,
0);

auto *mirror_addr = static_cast<std::byte *>(addr) + meta_size + cap;
mmap(mirror_addr, cap, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, fd, meta_size);

auto *data_base = static_cast<std::byte *>(addr) + meta_size;
auto *rb = new (addr) ring_buffer(cap, data_base);

return { { { const_cast<std::byte *>(base + tail_), space }, { nullptr, 0 } } }; // NOLINT
auto deleter = ring_buffer::deleter{ .total_size = total_vma };
return { rb, std::move(deleter) };
}

} // namespace linyaps_box::utils
Loading
Loading