livoxGen1:StaginBuffer: simplify buff size/stride calcs
This commit is contained in:
@@ -3,12 +3,12 @@
|
|||||||
namespace smo {
|
namespace smo {
|
||||||
namespace stim_buff {
|
namespace stim_buff {
|
||||||
|
|
||||||
|
// Static defaults for io_uring
|
||||||
const StagingBuffer::InputEngineConstraints
|
const StagingBuffer::InputEngineConstraints
|
||||||
StagingBuffer::InputEngineConstraints::ioUringConstraints{
|
StagingBuffer::InputEngineConstraints::ioUringConstraints(
|
||||||
16, // nBytesPerPoint
|
4096, // slotStartAlignmentByteVal (page alignment for DMA)
|
||||||
28, // udpHeaderOverheadNBytes
|
1472 // slotPadToNBytes (MTU 1500 - UDP/IP header 28)
|
||||||
1500 // mtuNBytes
|
);
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace stim_buff
|
} // namespace stim_buff
|
||||||
} // namespace smo
|
} // namespace smo
|
||||||
|
|||||||
@@ -5,6 +5,12 @@
|
|||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
|
#include <vector>
|
||||||
|
#include <string>
|
||||||
|
#include <sstream>
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
#include "FrameAssemblyDesc.h"
|
||||||
|
|
||||||
namespace smo {
|
namespace smo {
|
||||||
namespace stim_buff {
|
namespace stim_buff {
|
||||||
@@ -25,15 +31,38 @@ public:
|
|||||||
class InputEngineConstraints
|
class InputEngineConstraints
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
InputEngineConstraints();
|
InputEngineConstraints(
|
||||||
~InputEngineConstraints();
|
size_t slotStartAlignmentByteVal_,
|
||||||
|
size_t slotPadToNBytes_)
|
||||||
|
: slotStartAlignmentByteVal(slotStartAlignmentByteVal_),
|
||||||
|
slotPadToNBytes(slotPadToNBytes_)
|
||||||
|
{}
|
||||||
|
|
||||||
|
~InputEngineConstraints() = default;
|
||||||
|
|
||||||
|
// Input-engine layout/constraints
|
||||||
|
size_t slotStartAlignmentByteVal; // power-of-2 alignment (e.g., 4096)
|
||||||
|
size_t slotPadToNBytes; // minimum size per datagram slot
|
||||||
|
|
||||||
|
// Static defaults for io_uring
|
||||||
|
static const InputEngineConstraints ioUringConstraints;
|
||||||
|
|
||||||
|
inline std::string stringify() const
|
||||||
|
{
|
||||||
|
std::ostringstream oss;
|
||||||
|
oss << "InputEngineConstraints{"
|
||||||
|
<< "slotStartAlignmentByteVal=" << slotStartAlignmentByteVal
|
||||||
|
<< ", slotPadToNBytes=" << slotPadToNBytes
|
||||||
|
<< "}";
|
||||||
|
return oss.str();
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class OutputEngineConstraints
|
class OutputEngineConstraints
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
OutputEngineConstraints();
|
OutputEngineConstraints() = default;
|
||||||
~OutputEngineConstraints();
|
~OutputEngineConstraints() = default;
|
||||||
};
|
};
|
||||||
|
|
||||||
public:
|
public:
|
||||||
@@ -45,8 +74,8 @@ public:
|
|||||||
explicit StagingBuffer(
|
explicit StagingBuffer(
|
||||||
const InputEngineConstraints& inputEngineConstraints,
|
const InputEngineConstraints& inputEngineConstraints,
|
||||||
const OutputEngineConstraints& outputEngineConstraints,
|
const OutputEngineConstraints& outputEngineConstraints,
|
||||||
size_t nPointsPerFrame);
|
size_t nDgramsPerFrame);
|
||||||
~StagingBuffer();
|
~StagingBuffer() = default;
|
||||||
|
|
||||||
// Non-copyable, movable
|
// Non-copyable, movable
|
||||||
StagingBuffer(const StagingBuffer&) = delete;
|
StagingBuffer(const StagingBuffer&) = delete;
|
||||||
@@ -55,32 +84,107 @@ public:
|
|||||||
StagingBuffer& operator=(StagingBuffer&&) = default;
|
StagingBuffer& operator=(StagingBuffer&&) = default;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// operator IoUringQueueDescriptor() const;
|
/** EXPLANATION:
|
||||||
|
* Returns an input-engine-agnostic descriptor describing per-frame packet
|
||||||
|
* slot layout. Different input engines should be able to convert this into
|
||||||
|
* engine-specific metadata. E.g: io_uring's SQE descriptor.
|
||||||
|
*/
|
||||||
|
operator std::shared_ptr<FrameAssemblyDesc>() const { return frameDesc; }
|
||||||
// operator OpenClSharedBufferDescriptor() const;
|
// operator OpenClSharedBufferDescriptor() const;
|
||||||
|
|
||||||
bool isAssembling() const { return isAssembling_; }
|
bool isAssembling() const { return assemblingFlag.load(); }
|
||||||
void startAssembly();
|
void startAssembly() { assemblingFlag.store(true); }
|
||||||
void stopAssembly();
|
void stopAssembly() { assemblingFlag.store(false); }
|
||||||
|
|
||||||
|
inline std::string stringify() const
|
||||||
|
{
|
||||||
|
std::ostringstream oss;
|
||||||
|
oss << "StagingBuffer{"
|
||||||
|
<< "nDgramsPerFrame=" << nDgramsPerFrame
|
||||||
|
<< ", bufferNBytes=" << bufferNBytes
|
||||||
|
<< ", slotStrideNBytes=" << slotStrideNBytes
|
||||||
|
<< ", constraints=" << inputConstraints.stringify()
|
||||||
|
<< "}";
|
||||||
|
return oss.str();
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
void computeSlotStrideAndBufferSize();
|
||||||
|
|
||||||
// Buffer data
|
// Buffer data
|
||||||
std::unique_ptr<uint8_t[]> buffer_;
|
std::unique_ptr<uint8_t[]> buffer;
|
||||||
size_t bufferSize_;
|
size_t bufferNBytes;
|
||||||
|
|
||||||
|
// Layout/invariants
|
||||||
|
size_t nDgramsPerFrame;
|
||||||
|
size_t slotStrideNBytes;
|
||||||
|
InputEngineConstraints inputConstraints;
|
||||||
|
|
||||||
|
// Descriptor (computed once; reused across frames)
|
||||||
|
mutable std::shared_ptr<FrameAssemblyDesc> frameDesc;
|
||||||
|
|
||||||
// Current state
|
// Current state
|
||||||
std::atomic<size_t> currentSize_;
|
std::atomic<size_t> currentNBytes;
|
||||||
std::atomic<bool> isAssembling_;
|
std::atomic<bool> assemblingFlag;
|
||||||
};
|
};
|
||||||
|
|
||||||
class IoUringConstraints
|
/** Inline implementations
|
||||||
: public StagingBuffer::InputEngineConstraints
|
******************************************************************************/
|
||||||
|
|
||||||
|
inline StagingBuffer::StagingBuffer(
|
||||||
|
const InputEngineConstraints& inputEngineConstraints_,
|
||||||
|
const OutputEngineConstraints& /*outputEngineConstraints*/,
|
||||||
|
size_t nDgramsPerFrame)
|
||||||
|
: buffer(nullptr), bufferNBytes(0),
|
||||||
|
nDgramsPerFrame(nDgramsPerFrame), slotStrideNBytes(0),
|
||||||
|
inputConstraints(inputEngineConstraints_),
|
||||||
|
assemblingFlag(false)
|
||||||
{
|
{
|
||||||
public:
|
if (nDgramsPerFrame == 0)
|
||||||
IoUringConstraints()
|
{
|
||||||
: StagingBuffer::InputEngineConstraints()
|
throw std::invalid_argument(std::string(__func__)
|
||||||
{}
|
+ ": StagingBuffer: nDgramsPerFrame must be > 0");
|
||||||
~IoUringConstraints() = default;
|
}
|
||||||
};
|
|
||||||
|
computeSlotStrideAndBufferSize();
|
||||||
|
|
||||||
|
buffer.reset(new uint8_t[bufferNBytes]);
|
||||||
|
currentNBytes.store(0);
|
||||||
|
|
||||||
|
// Build FrameAssemblyDesc once
|
||||||
|
std::vector<FrameAssemblyDesc::SlotDesc> slots;
|
||||||
|
slots.reserve(nDgramsPerFrame);
|
||||||
|
uint8_t *frameBase = buffer.get();
|
||||||
|
for (size_t i = 0; i < nDgramsPerFrame; ++i)
|
||||||
|
{
|
||||||
|
size_t off = i * slotStrideNBytes;
|
||||||
|
FrameAssemblyDesc::SlotDesc s{
|
||||||
|
off, frameBase + off, inputConstraints.slotPadToNBytes};
|
||||||
|
|
||||||
|
slots.push_back(s);
|
||||||
|
}
|
||||||
|
|
||||||
|
frameDesc = std::make_shared<FrameAssemblyDesc>(
|
||||||
|
nDgramsPerFrame, inputConstraints.slotPadToNBytes, bufferNBytes,
|
||||||
|
std::move(slots));
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void StagingBuffer::computeSlotStrideAndBufferSize()
|
||||||
|
{
|
||||||
|
// Slot stride is the maximum of alignment and padding
|
||||||
|
slotStrideNBytes = std::max(
|
||||||
|
inputConstraints.slotStartAlignmentByteVal,
|
||||||
|
inputConstraints.slotPadToNBytes);
|
||||||
|
|
||||||
|
// Buffer size is nDgramsPerFrame * slotStrideNBytes, aligned up to alignment
|
||||||
|
size_t rawSize = nDgramsPerFrame * slotStrideNBytes;
|
||||||
|
bufferNBytes = ((rawSize + inputConstraints.slotStartAlignmentByteVal - 1)
|
||||||
|
/ inputConstraints.slotStartAlignmentByteVal)
|
||||||
|
* inputConstraints.slotStartAlignmentByteVal;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Specific input/output engine constraints
|
||||||
|
******************************************************************************/
|
||||||
|
|
||||||
class OpenClConstraints
|
class OpenClConstraints
|
||||||
: public StagingBuffer::OutputEngineConstraints
|
: public StagingBuffer::OutputEngineConstraints
|
||||||
|
|||||||
Reference in New Issue
Block a user