MXL : Media eXchange Layer
MXL
Loading...
Searching...
No Matches
mxl::lib::PosixContinuousFlowWriter Class Referencefinal

#include <PosixContinuousFlowWriter.hpp>

Inheritance diagram for mxl::lib::PosixContinuousFlowWriter:
Collaboration diagram for mxl::lib::PosixContinuousFlowWriter:

Public Member Functions

virtual mxlStatus cancel () override
virtual mxlStatus commit () override
virtual mxlFlowConfigInfo getFlowConfigInfo () const override
virtual FlowData const & getFlowData () const override
virtual FlowData & getFlowData () override
virtual mxlFlowInfo getFlowInfo () const override
virtual mxlFlowRuntimeInfo getFlowRuntimeInfo () const override
virtual std::size_t getMaxWriteLength () const override
virtual bool isExclusive () const override
virtual bool makeExclusive () override
virtual mxlStatus openSamples (std::uint64_t index, std::size_t count, mxlMutableWrappedMultiBufferSlice &payloadBufferSlices) override
 PosixContinuousFlowWriter (FlowManager const &manager, uuids::uuid const &flowId, std::unique_ptr< ContinuousFlowData > &&data)
Public Member Functions inherited from mxl::lib::FlowWriter
uuids::uuid const & getId () const
virtual ~FlowWriter ()

Private Member Functions

bool signalCompletedBatch () noexcept

Private Attributes

std::size_t _bufferLength
std::size_t _channelCount
std::size_t _currentCount
std::uint64_t _currentIndex
std::uint32_t _earlySyncThreshold
std::unique_ptr< ContinuousFlowData > _flowData
std::uint64_t _lastCommittedIndex
std::uint64_t _lastSyncSampleBatch
std::uint32_t _syncBatchSize

Additional Inherited Members

Protected Member Functions inherited from mxl::lib::ContinuousFlowWriter
 FlowWriter (uuids::uuid &&flowId, std::filesystem::path const &domain)
 FlowWriter (uuids::uuid const &flowId, std::filesystem::path const &domain)
Protected Member Functions inherited from mxl::lib::FlowWriter
bool checkPermissions () const
 FlowWriter (uuids::uuid &&flowId, std::filesystem::path const &domain)
 FlowWriter (uuids::uuid const &flowId, std::filesystem::path const &domain)

Detailed Description

Implementation of a FlowWriter based on POSIX shared memory mapping.

Definition at line 22 of file PosixContinuousFlowWriter.hpp.

Constructor & Destructor Documentation

◆ PosixContinuousFlowWriter()

mxl::lib::PosixContinuousFlowWriter::PosixContinuousFlowWriter ( FlowManager const & manager,
uuids::uuid const & flowId,
std::unique_ptr< ContinuousFlowData > && data )

Creates a PosixContinuousFlowWriter

Parameters
[in]managerA referene to the flow manager used to obtain additional information about the flows context.

Definition at line 15 of file PosixContinuousFlowWriter.cpp.

17 : ContinuousFlowWriter{flowId, manager.getDomain()}
18 , _flowData{std::move(data)}
19 , _channelCount{_flowData->channelCount()}
20 , _bufferLength{_flowData->channelBufferLength()}
22 , _currentCount{0U}
24 , _syncBatchSize{1U}
27 {
28 if (!checkPermissions())
29 {
30 throw std::filesystem::filesystem_error{
31 "Flow is not accessible due to insufficient permissions.", manager.getDomain(), std::make_error_code(std::errc::permission_denied)};
32 }
33 if (_flowData)
34 {
35 auto const& commonFlowConfigInfo = _flowData->flowInfo()->config.common;
36
37 auto const commitBatchSize = std::max(commonFlowConfigInfo.maxCommitBatchSizeHint, 1U);
38 _syncBatchSize = std::max(commonFlowConfigInfo.maxSyncBatchSizeHint, 1U);
39 _earlySyncThreshold = (_syncBatchSize >= commitBatchSize) ? (_syncBatchSize - commitBatchSize) : 0U;
40 }
41 }
bool checkPermissions() const
std::unique_ptr< ContinuousFlowData > _flowData
#define MXL_UNDEFINED_INDEX
Definition rational.h:17
auto const flowId

Member Function Documentation

◆ cancel()

mxlStatus mxl::lib::PosixContinuousFlowWriter::cancel ( )
overridevirtual
See also
ContinuousFlowWriter::cancel

Implements mxl::lib::ContinuousFlowWriter.

Definition at line 188 of file PosixContinuousFlowWriter.cpp.

189 {
191 _currentCount = 0U;
192 return MXL_STATUS_OK;
193 }
@ MXL_STATUS_OK
Definition mxl.h:23

◆ commit()

mxlStatus mxl::lib::PosixContinuousFlowWriter::commit ( )
overridevirtual
See also
ContinuousFlowWriter::commit

Implements mxl::lib::ContinuousFlowWriter.

Definition at line 130 of file PosixContinuousFlowWriter.cpp.

131 {
132 if (_flowData)
133 {
135 {
136 return MXL_ERR_INVALID_ARG;
137 }
138
139 // Invalidate any skipped samples in the writable ring. This prevents readers from
140 // seeing stale payload data when the producer introduces a write gap.
141 auto const writeStartIndex = _currentIndex - _currentCount;
143 {
144 auto const skippedCount = writeStartIndex - _lastCommittedIndex;
145 auto const clearableLength = _bufferLength - _currentCount;
146 auto const clearedCount = std::min<std::uint64_t>(skippedCount, clearableLength);
147 auto const gapEndIndex = writeStartIndex;
148
149 auto const clearStartOffset = (gapEndIndex + _bufferLength - clearedCount) % _bufferLength;
150 auto const clearEndOffset = gapEndIndex % _bufferLength;
151 auto const firstClearLength = (clearStartOffset < clearEndOffset) ? clearedCount : _bufferLength - clearStartOffset;
152 auto const secondClearLength = clearedCount - firstClearLength;
153
154 auto const sampleWordSize = _flowData->sampleWordSize();
155 auto const channelStride = sampleWordSize * _bufferLength;
156 auto* const baseBufferPtr = static_cast<std::uint8_t*>(_flowData->channelData());
157
158 for (auto channel = std::size_t{0}; channel < _channelCount; ++channel)
159 {
160 auto* const channelBasePtr = baseBufferPtr + (channel * channelStride);
161 std::memset(channelBasePtr + (sampleWordSize * clearStartOffset), 0, sampleWordSize * firstClearLength);
162 std::memset(channelBasePtr, 0, sampleWordSize * secondClearLength);
163 }
164 }
165
166 auto const flow = _flowData->flow();
167 flow->info.runtime.headIndex = _currentIndex;
170 _currentCount = 0U;
171
173 {
174 // Let readers know that the head has moved
175 flow->state.syncCounter++;
176 wakeAll(&flow->state.syncCounter);
177 }
178
179 return MXL_STATUS_OK;
180 }
181 else
182 {
184 return MXL_ERR_UNKNOWN;
185 }
186 }
@ MXL_ERR_UNKNOWN
Definition mxl.h:27
@ MXL_ERR_INVALID_ARG
Definition mxl.h:35
void wakeAll(T const *in_addr)
Definition Sync.cpp:131

◆ getFlowConfigInfo()

mxlFlowConfigInfo mxl::lib::PosixContinuousFlowWriter::getFlowConfigInfo ( ) const
nodiscardoverridevirtual
See also
FlowWriter::getFlowConfigInfo

Implements mxl::lib::FlowWriter.

Definition at line 66 of file PosixContinuousFlowWriter.cpp.

67 {
68 return getFlowData().flowInfo()->config;
69 }
constexpr mxlFlowInfo * flowInfo() noexcept
Definition FlowData.hpp:92
mxlFlowConfigInfo config
Definition flowinfo.h:227

◆ getFlowData() [1/2]

FlowData const & mxl::lib::PosixContinuousFlowWriter::getFlowData ( ) const
nodiscardoverridevirtual
See also
FlowWriter::getFlowData

Implements mxl::lib::FlowWriter.

Definition at line 52 of file PosixContinuousFlowWriter.cpp.

53 {
54 if (_flowData)
55 {
56 return *_flowData;
57 }
58 throw std::runtime_error("No open flow.");
59 }

◆ getFlowData() [2/2]

FlowData & mxl::lib::PosixContinuousFlowWriter::getFlowData ( )
nodiscardoverridevirtual
See also
FlowWriter::getFlowData

Implements mxl::lib::FlowWriter.

Definition at line 43 of file PosixContinuousFlowWriter.cpp.

44 {
45 if (_flowData)
46 {
47 return *_flowData;
48 }
49 throw std::runtime_error("No open flow.");
50 }

◆ getFlowInfo()

mxlFlowInfo mxl::lib::PosixContinuousFlowWriter::getFlowInfo ( ) const
nodiscardoverridevirtual
See also
FlowWriter::getFlowInfo

Implements mxl::lib::FlowWriter.

Definition at line 61 of file PosixContinuousFlowWriter.cpp.

62 {
63 return *getFlowData().flowInfo();
64 }

◆ getFlowRuntimeInfo()

mxlFlowRuntimeInfo mxl::lib::PosixContinuousFlowWriter::getFlowRuntimeInfo ( ) const
nodiscardoverridevirtual
See also
FlowWriter::getFlowRuntimeInfo

Implements mxl::lib::FlowWriter.

Definition at line 71 of file PosixContinuousFlowWriter.cpp.

72 {
73 return getFlowData().flowInfo()->runtime;
74 }
mxlFlowRuntimeInfo runtime
Definition flowinfo.h:229

◆ getMaxWriteLength()

std::size_t mxl::lib::PosixContinuousFlowWriter::getMaxWriteLength ( ) const
nodiscardoverridevirtual
See also
ContinuousFlowWriter::getMaxWriteLength

Implements mxl::lib::ContinuousFlowWriter.

Definition at line 76 of file PosixContinuousFlowWriter.cpp.

77 {
78 return _bufferLength / 2U;
79 }

◆ isExclusive()

bool mxl::lib::PosixContinuousFlowWriter::isExclusive ( ) const
nodiscardoverridevirtual

Check if this is the only writer for this flow.

Implements mxl::lib::FlowWriter.

Definition at line 223 of file PosixContinuousFlowWriter.cpp.

224 {
225 if (!_flowData)
226 {
227 throw std::runtime_error("Flow writer not initialized");
228 }
229
230 return _flowData->isExclusive();
231 }

◆ makeExclusive()

bool mxl::lib::PosixContinuousFlowWriter::makeExclusive ( )
overridevirtual

Try to turn this into a exclusive writer.

Returns
true if the operation was successful and this is the only writer, false if the operation was not successful because there are other active writers for this flow.

Implements mxl::lib::FlowWriter.

Definition at line 233 of file PosixContinuousFlowWriter.cpp.

234 {
235 if (!_flowData)
236 {
237 throw std::runtime_error("Flow writer not initialized");
238 }
239
240 return _flowData->makeExclusive();
241 }

◆ openSamples()

mxlStatus mxl::lib::PosixContinuousFlowWriter::openSamples ( std::uint64_t index,
std::size_t count,
mxlMutableWrappedMultiBufferSlice & payloadBufferSlices )
overridevirtual
See also
ContinuousFlowWriter::openSamples

Implements mxl::lib::ContinuousFlowWriter.

Definition at line 81 of file PosixContinuousFlowWriter.cpp.

82 {
83 if (_flowData)
84 {
85 if (count <= (_bufferLength / 2))
86 {
87 if (index < count)
88 {
90 }
91
92 auto const writeStartIndex = index - count;
94 {
95 if ((index <= _lastCommittedIndex) || (writeStartIndex < _lastCommittedIndex))
96 {
98 }
99 }
100
101 auto const startOffset = (index + _bufferLength - count) % _bufferLength;
102 auto const endOffset = (index % _bufferLength);
103
104 auto const firstLength = (startOffset < endOffset) ? count : _bufferLength - startOffset;
105 auto const secondLength = count - firstLength;
106
107 auto const baseBufferPtr = static_cast<std::uint8_t*>(_flowData->channelData());
108 auto const sampleWordSize = _flowData->sampleWordSize();
109
110 payloadBufferSlices.base.fragments[0].pointer = baseBufferPtr + sampleWordSize * startOffset;
111 payloadBufferSlices.base.fragments[0].size = sampleWordSize * firstLength;
112
113 payloadBufferSlices.base.fragments[1].pointer = baseBufferPtr;
114 payloadBufferSlices.base.fragments[1].size = sampleWordSize * secondLength;
115
116 payloadBufferSlices.stride = sampleWordSize * _bufferLength;
117 payloadBufferSlices.count = _channelCount;
118
121
122 return MXL_STATUS_OK;
123 }
124
125 return MXL_ERR_INVALID_ARG;
126 }
127 return MXL_ERR_UNKNOWN;
128 }
std::size_t count(uuids::uuid id) const noexcept
Returns the number of writers registered for flow id 'id'.
mxlMutableBufferSlice fragments[2]
Definition flow.h:87
mxlMutableWrappedBufferSlice base
Definition flow.h:117
uint64_t index

◆ signalCompletedBatch()

bool mxl::lib::PosixContinuousFlowWriter::signalCompletedBatch ( )
privatenoexcept

Definition at line 195 of file PosixContinuousFlowWriter.cpp.

196 {
197 auto const flow = _flowData->flow();
198 auto const currentSyncSampleBatch = flow->info.runtime.headIndex / _syncBatchSize;
199 if (currentSyncSampleBatch < _lastSyncSampleBatch)
200 {
201 return false;
202 }
203 if (currentSyncSampleBatch == _lastSyncSampleBatch)
204 {
205 // Signal now before overshooting the maximum the next time around
206 if ((flow->info.runtime.headIndex % _syncBatchSize) > _earlySyncThreshold)
207 {
208 _lastSyncSampleBatch = currentSyncSampleBatch + 1U;
209 }
210 else
211 {
212 return false;
213 }
214 }
215 else
216 {
217 _lastSyncSampleBatch = currentSyncSampleBatch;
218 }
219
220 return true;
221 }

Member Data Documentation

◆ _bufferLength

std::size_t mxl::lib::PosixContinuousFlowWriter::_bufferLength
private

Cached copy of the length of the per channel buffers from mxlFlowInfo.

Definition at line 81 of file PosixContinuousFlowWriter.hpp.

◆ _channelCount

std::size_t mxl::lib::PosixContinuousFlowWriter::_channelCount
private

Cached copy of the numer of channels from mxlFlowInfo.

Definition at line 79 of file PosixContinuousFlowWriter.hpp.

◆ _currentCount

std::size_t mxl::lib::PosixContinuousFlowWriter::_currentCount
private

The sample count for the currently opened range. 0 if no range is currently opened.

Definition at line 85 of file PosixContinuousFlowWriter.hpp.

◆ _currentIndex

std::uint64_t mxl::lib::PosixContinuousFlowWriter::_currentIndex
private

The currently opened sample range head index. MXL_UNDEFINED_INDEX if no range is currently opened.

Definition at line 83 of file PosixContinuousFlowWriter.hpp.

◆ _earlySyncThreshold

std::uint32_t mxl::lib::PosixContinuousFlowWriter::_earlySyncThreshold
private

The threshold at which to signal even before _syncBatchSize is reached, in order to not exceed the maximum.

Definition at line 92 of file PosixContinuousFlowWriter.hpp.

◆ _flowData

std::unique_ptr<ContinuousFlowData> mxl::lib::PosixContinuousFlowWriter::_flowData
private

The FlowData for the currently opened flow. null if no flow is opened.

Definition at line 77 of file PosixContinuousFlowWriter.hpp.

◆ _lastCommittedIndex

std::uint64_t mxl::lib::PosixContinuousFlowWriter::_lastCommittedIndex
private

The last committed sample range head index. MXL_UNDEFINED_INDEX if no range has been committed yet.

Definition at line 87 of file PosixContinuousFlowWriter.hpp.

◆ _lastSyncSampleBatch

std::uint64_t mxl::lib::PosixContinuousFlowWriter::_lastSyncSampleBatch
private

The last sample batch (as a factor of _syncBatchSize) that has been signaled.

Definition at line 95 of file PosixContinuousFlowWriter.hpp.

◆ _syncBatchSize

std::uint32_t mxl::lib::PosixContinuousFlowWriter::_syncBatchSize
private

Cached preprocessed copy of mxlCommonFlowInfo::maxSyncBatchSizeHint.

Definition at line 90 of file PosixContinuousFlowWriter.hpp.


The documentation for this class was generated from the following files: