Motion Master 6.0.0-alpha.86
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
43 public:
46 struct Record {
47 uint64_t seq = 0;
48 // Wall-clock epoch nanoseconds at exchange (system clock). u64 holds it until ~year 2554.
49 // INTERNAL precision only: epoch-ns today (~1.7e18) far exceeds the 2^53 exact-integer limit
50 // of a JavaScript double. The live monitoring row reduces it to epoch MICROSECONDS (ns / 1000)
51 // before JSON — epoch-µs stays exact in JS until ~year 2255 and, unlike milliseconds, gives
52 // every cycle a distinct timestamp even at sub-millisecond cycle periods (periodUs < 1000).
53 // The full ns precision is retained here for the binary dump.
54 uint64_t timestampNs = 0;
55 int32_t wkc = 0;
56 std::vector<uint8_t> inputs;
57 std::vector<uint8_t> outputs;
58 };
59
60 ProcessDataRing() = default;
62
65
77 void allocate(uint32_t inputCap, uint32_t outputCap, size_t capacityCycles);
78
82 void clear();
83
85 bool allocated() const { return capacity_ != 0; }
86
91 void write(uint64_t timestampNs, int wkc, std::span<const uint8_t> inputs,
92 std::span<const uint8_t> outputs);
93
96 uint64_t head() const { return head_.load(std::memory_order_acquire); }
97
99 size_t capacity() const { return capacity_; }
100
103 uint64_t oldestValidSeq() const;
104
110 bool readRecord(uint64_t seq, Record& out) const;
111
112 private:
113 // Per-record payload layout in buffer_ (accessed by memcpy, so no alignment constraints):
114 // [0] timestampNs (u64) [8] wkc (i32) [12] inputBytes (u32) [16] outputBytes (u32)
115 // [20] input region (inputCap_ bytes) [20+inputCap_] output region (outputCap_ bytes)
116 static constexpr size_t kHeaderBytes = 20;
117
118 std::vector<uint8_t> buffer_; // capacity_ * stride_ bytes of payload records
119 std::vector<std::atomic<uint64_t>>
120 seqWords_; // one publication word per slot; kInvalidSeq = empty
121 size_t stride_ = 0;
122 size_t capacity_ = 0;
123 uint32_t inputCap_ = 0;
124 uint32_t outputCap_ = 0;
125 std::atomic<uint64_t> head_{0}; // next sequence number to write (monotonic, never wraps)
126 bool locked_ = false; // whether the storage was successfully mlock'd
127};
128
129} // namespace mm::node
A lock-free circular recorder of the raw process image, one record per RT cycle.
Definition process_data_ring.h:42
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 ran with a non-zero capacity.
Definition process_data_ring.h:85
size_t capacity() const
Number of cycles the ring can hold.
Definition process_data_ring.h:99
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:96
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 was lapped...
Definition process_data_ring.cc:108
Definition bus_health_source.h:7
int wkc
Definition soem_fieldbus_driver.cc:954
One recorded cycle, copied out by a reader. inputs / outputs are the raw IOmap bytes for that cycle (...
Definition process_data_ring.h:46
std::vector< uint8_t > outputs
Definition process_data_ring.h:57
int32_t wkc
Working counter for the cycle.
Definition process_data_ring.h:55
uint64_t timestampNs
Definition process_data_ring.h:54
uint64_t seq
Absolute cycle sequence number.
Definition process_data_ring.h:47
std::vector< uint8_t > inputs
Definition process_data_ring.h:56