Motion Master 6.0.0-alpha.50
Next-generation motion control software
Loading...
Searching...
No Matches
process_data_ring.h
Go to the documentation of this file.
1#pragma once
2
3#include <atomic>
4#include <cstdint>
5#include <span>
6#include <vector>
7
8namespace mm::node {
9
35 public:
38 struct Record {
39 uint64_t seq = 0;
40 // Wall-clock epoch nanoseconds at exchange (system clock). u64 holds it until ~year 2554.
41 // INTERNAL precision only: epoch-ns today (~1.7e18) far exceeds the 2^53 exact-integer limit
42 // of a JavaScript double. The live monitoring row reduces it to epoch MICROSECONDS (ns / 1000)
43 // before JSON — epoch-µs stays exact in JS until ~year 2255 and, unlike milliseconds, gives
44 // every cycle a distinct timestamp even at sub-millisecond cycle periods (periodUs < 1000).
45 // The full ns precision is retained here for the binary dump.
46 uint64_t timestampNs = 0;
47 int32_t wkc = 0;
48 std::vector<uint8_t> inputs;
49 std::vector<uint8_t> outputs;
50 };
51
52 ProcessDataRing() = default;
54
57
66 void allocate(uint32_t inputCap, uint32_t outputCap, size_t capacityCycles);
67
69 void clear();
70
72 bool allocated() const { return capacity_ != 0; }
73
78 void write(uint64_t timestampNs, int wkc, std::span<const uint8_t> inputs,
79 std::span<const uint8_t> outputs);
80
83 uint64_t head() const { return head_.load(std::memory_order_acquire); }
84
86 size_t capacity() const { return capacity_; }
87
90 uint64_t oldestValidSeq() const;
91
97 bool readRecord(uint64_t seq, Record& out) const;
98
99 private:
100 // Per-record payload layout in buffer_ (accessed by memcpy, so no alignment constraints):
101 // [0] timestampNs (u64) [8] wkc (i32) [12] inputBytes (u32) [16] outputBytes (u32)
102 // [20] input region (inputCap_ bytes) [20+inputCap_] output region (outputCap_ bytes)
103 static constexpr size_t kHeaderBytes = 20;
104
105 std::vector<uint8_t> buffer_; // capacity_ * stride_ bytes of payload records
106 std::vector<std::atomic<uint64_t>>
107 seqWords_; // one publication word per slot; kInvalidSeq = empty
108 size_t stride_ = 0;
109 size_t capacity_ = 0;
110 uint32_t inputCap_ = 0;
111 uint32_t outputCap_ = 0;
112 std::atomic<uint64_t> head_{0}; // next sequence number to write (monotonic, never wraps)
113 bool locked_ = false; // whether the storage was successfully mlock'd
114};
115
116} // namespace mm::node
A lock-free circular recorder of the raw process image, one record per RT cycle.
Definition process_data_ring.h:34
void allocate(uint32_t inputCap, uint32_t outputCap, size_t capacityCycles)
(Re)allocates the ring for an image of inputCap / outputCap bytes per direction and capacity cycles,...
Definition process_data_ring.cc:24
void clear()
Releases the storage and resets to the unallocated state. Idempotent.
Definition process_data_ring.cc:53
~ProcessDataRing()
Definition process_data_ring.cc:22
bool allocated() const
Whether allocate has been called with a non-zero capacity.
Definition process_data_ring.h:72
size_t capacity() const
Number of cycles the ring can hold.
Definition process_data_ring.h:86
ProcessDataRing(const ProcessDataRing &)=delete
uint64_t head() const
The producer's next sequence number; head()-1 is the newest recorded cycle, and head()==0 means nothi...
Definition process_data_ring.h:83
bool readRecord(uint64_t seq, Record &out) const
Copies the record for seq into out. Lock-free; retries are the caller's job.
Definition process_data_ring.cc:113
void write(uint64_t timestampNs, int wkc, std::span< const uint8_t > inputs, std::span< const uint8_t > outputs)
Records one cycle. RT, wait-free, single-writer. No-op until allocate.
Definition process_data_ring.cc:79
ProcessDataRing & operator=(const ProcessDataRing &)=delete
uint64_t oldestValidSeq() const
The oldest sequence number still in the ring: max(0, head - capacity). A cursor below this has been l...
Definition process_data_ring.cc:108
Definition http_server.h:16
One recorded cycle, copied out by a reader. inputs / outputs are the raw IOmap bytes for that cycle (...
Definition process_data_ring.h:38
std::vector< uint8_t > outputs
Definition process_data_ring.h:49
int32_t wkc
Working counter for the cycle.
Definition process_data_ring.h:47
uint64_t timestampNs
Definition process_data_ring.h:46
uint64_t seq
Absolute cycle sequence number.
Definition process_data_ring.h:39
std::vector< uint8_t > inputs
Definition process_data_ring.h:48