triton_cpp v1.2.1
Header-only C++ wrapper for NVIDIA Triton Inference Server clients
Loading...
Searching...
No Matches
shm.hpp
Go to the documentation of this file.
1// Copyright Institute for Automotive Engineering (ika), RWTH Aachen University
2// SPDX-License-Identifier: Apache-2.0
3
4#pragma once
5
6#include <fcntl.h>
7#include <sys/mman.h>
8#include <unistd.h>
9
10#include <cstdint>
11#include <stdexcept>
12#include <string>
13#include <utility>
14
15namespace triton_cpp {
16
17namespace detail {
18
21 public:
22 PosixSharedMemory(std::string key, std::int64_t size) : key_{std::move(key)}, size_{size} {
23 fd_ = ::shm_open(key_.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
24 if (fd_ == -1) {
25 throw std::runtime_error("CreateSharedMemoryRegion: unable to get shared memory descriptor for shared-memory key '" + key_ +
26 "'");
27 }
28
29 if (::ftruncate(fd_, size_) == -1) {
30 closeDescriptor();
31 unlinkRegion();
32 throw std::runtime_error("CreateSharedMemoryRegion: unable to initialize shared-memory key '" + key_ +
33 "' to requested size: " + std::to_string(static_cast<std::size_t>(size_)) + " bytes");
34 }
35
36 address_ = ::mmap(nullptr, static_cast<std::size_t>(size_), PROT_READ | PROT_WRITE, MAP_SHARED, fd_, 0);
37 if (address_ == MAP_FAILED) {
38 address_ = nullptr;
39 const int failed_fd = fd_;
40 closeDescriptor();
41 unlinkRegion();
42 throw std::runtime_error("MapSharedMemory: unable to process address space or shared-memory descriptor: " +
43 std::to_string(failed_fd));
44 }
45
46 const int mapped_fd = fd_;
47 if (::close(fd_) == -1) {
48 fd_ = -1;
49 unmapRegion();
50 unlinkRegion();
51 throw std::runtime_error("CloseSharedMemory: unable to close shared-memory descriptor: " + std::to_string(mapped_fd));
52 }
53 fd_ = -1;
54 }
55
58
60 : key_{std::move(other.key_)},
61 address_{std::exchange(other.address_, nullptr)},
62 fd_{std::exchange(other.fd_, -1)},
63 size_{other.size_},
64 owns_name_{std::exchange(other.owns_name_, false)} {}
65
67
68 ~PosixSharedMemory() noexcept {
69 unmapRegion();
70 closeDescriptor();
71 unlinkRegion();
72 }
73
74 std::uint8_t* address() const noexcept { return static_cast<std::uint8_t*>(address_); }
75 const std::string& key() const noexcept { return key_; }
76
77 private:
78 void unmapRegion() noexcept {
79 if (address_ != nullptr) {
80 ::munmap(address_, static_cast<std::size_t>(size_));
81 address_ = nullptr;
82 }
83 }
84
85 void closeDescriptor() noexcept {
86 if (fd_ != -1) {
87 ::close(fd_);
88 fd_ = -1;
89 }
90 }
91
92 void unlinkRegion() noexcept {
93 if (owns_name_) {
94 ::shm_unlink(key_.c_str());
95 owns_name_ = false;
96 }
97 }
98
99 std::string key_;
100 void* address_{nullptr};
101 int fd_{-1};
102 std::int64_t size_;
103 bool owns_name_{true};
104};
105
106} // namespace detail
107
115 public:
122 SharedMemoryRegion(const std::string& key, std::int64_t size) : memory_{key, size} {}
123
128 ~SharedMemoryRegion() = default;
129
131 uint8_t* getAddress() const { return memory_.address(); }
133 std::string getKey() const { return memory_.key(); }
134
135 private:
137};
138
139} // namespace triton_cpp
Own a POSIX shared-memory mapping used by the Triton client.
Definition shm.hpp:114
SharedMemoryRegion(const SharedMemoryRegion &)=delete
SharedMemoryRegion(SharedMemoryRegion &&) noexcept=default
SharedMemoryRegion & operator=(const SharedMemoryRegion &)=delete
uint8_t * getAddress() const
Definition shm.hpp:131
std::string getKey() const
Definition shm.hpp:133
SharedMemoryRegion(const std::string &key, std::int64_t size)
Create and map a system shared-memory region.
Definition shm.hpp:122
std::uint8_t * address() const noexcept
Definition shm.hpp:74
PosixSharedMemory(const PosixSharedMemory &)=delete
PosixSharedMemory & operator=(PosixSharedMemory &&)=delete
PosixSharedMemory(std::string key, std::int64_t size)
Definition shm.hpp:22
const std::string & key() const noexcept
Definition shm.hpp:75
PosixSharedMemory & operator=(const PosixSharedMemory &)=delete
PosixSharedMemory(PosixSharedMemory &&other) noexcept
Definition shm.hpp:59