Line data Source code
1 : #ifndef HEADER_fd_src_disco_gui_fd_gui_shred_h
2 : #define HEADER_fd_src_disco_gui_fd_gui_shred_h
3 :
4 : #include "fd_gui_hist.h"
5 : #include "../shred/fd_fec_set.h"
6 :
7 : struct fd_gui;
8 : typedef struct fd_gui fd_gui_t;
9 :
10 : #define FD_GUI_SHRED_BATCH_SZ (4096UL)
11 10278129 : #define FD_GUI_SHRED_EVENT_TS_MAX (0xFFFFFFUL)
12 :
13 : /* FEC staging holds at most FD_GUI_FEC_RECORD_MAX records, and only
14 : admits slots below a high watermark FD_GUI_FEC_SLOT_WMARK_CNT slots
15 : past the closed slot watermark. */
16 159 : #define FD_GUI_FEC_RECORD_MAX (1UL<<20)
17 3154593 : #define FD_GUI_FEC_SLOT_WMARK_CNT (1024UL)
18 :
19 : /* The FEC archive is an ordinary time-series ring ordered by admission
20 : time, while queries select by event time. Events skewed further than
21 : LAG_MAX are excluded from query responses and counted in
22 : clamped_fec_event_cnt. */
23 6298101 : #define FD_GUI_FEC_EVENT_LAG_MAX_NS (10L*1000L*1000L*1000L)
24 3139668 : #define FD_GUI_FEC_BATCH_SPAN_NS (1000L*1000L*1000L)
25 :
26 6090 : #define FD_GUI_SLOT_SHRED_REPAIR_REQUEST (0UL)
27 808188 : #define FD_GUI_SLOT_SHRED_SHRED_RECEIVED_TURBINE (1UL)
28 370569 : #define FD_GUI_SLOT_SHRED_SHRED_RECEIVED_REPAIR (2UL)
29 16236 : #define FD_GUI_SLOT_SHRED_SHRED_REPLAY_EXEC_DONE (3UL)
30 3471942 : #define FD_GUI_SLOT_SHRED_SHRED_SLOT_COMPLETE (4UL)
31 53685 : #define FD_GUI_SLOT_SHRED_SHRED_PUBLISHED (6UL)
32 :
33 : struct __attribute__((packed)) fd_gui_shred_event {
34 : long insert_time_ns;
35 : long event_time_ns;
36 : uint slot;
37 : ushort idx;
38 : uchar event;
39 : };
40 : typedef struct fd_gui_shred_event fd_gui_shred_event_t;
41 :
42 : struct fd_gui_shred_scratch {
43 : long event_time_ns;
44 : uint slot;
45 : ushort idx;
46 : uchar event;
47 : };
48 : typedef struct fd_gui_shred_scratch fd_gui_shred_scratch_t;
49 :
50 903 : #define FD_GUI_SHRED_SCRATCH_MAX (262144UL)
51 :
52 : /* Each block belongs to one insertion-time second. The index timestamp
53 : is that second's start; individual insertion times remain in data.
54 : Entries are a one-byte tag followed by either a literal event or a
55 : delta from the preceding event (same slot). Deltas store the event
56 : type and delta signs in the tag, then four insertion-time bytes, three
57 : event-time bytes, and one index byte. Slot changes and larger deltas
58 : use literals. */
59 : struct fd_gui_shred_batch {
60 : long insert_time_ns;
61 : uint event_cnt;
62 : uint data_sz;
63 : uchar data[ FD_GUI_SHRED_BATCH_SZ-16UL ];
64 : };
65 : typedef struct fd_gui_shred_batch fd_gui_shred_batch_t;
66 :
67 : /* Encode the open insertion second directly. Full batches and closed
68 : seconds move to the history ring. History retention only applies to
69 : sealed batches in the store; the active batch remains queryable. */
70 : struct fd_gui_shred_builder {
71 : fd_gui_shred_batch_t batch;
72 : fd_gui_shred_event_t prev;
73 : };
74 : typedef struct fd_gui_shred_builder fd_gui_shred_builder_t;
75 :
76 : struct fd_gui_fec_event_key {
77 : uint slot;
78 : ushort idx;
79 : uchar event;
80 : uchar reserved;
81 : ulong marker_sequence;
82 : };
83 : typedef struct fd_gui_fec_event_key fd_gui_fec_event_key_t;
84 :
85 : struct fd_gui_fec_event {
86 : fd_gui_fec_event_key_t key;
87 : long insert_time_ns;
88 : long event_time_ns;
89 : };
90 : typedef struct fd_gui_fec_event fd_gui_fec_event_t;
91 :
92 : struct fd_gui_fec_event_batch {
93 : long insert_time_ns;
94 : long min_ns;
95 : long max_ns;
96 : fd_gui_shred_batch_t batch;
97 : };
98 : typedef struct fd_gui_fec_event_batch fd_gui_fec_event_batch_t;
99 :
100 : FD_STATIC_ASSERT( sizeof(fd_gui_shred_batch_t)==FD_GUI_SHRED_BATCH_SZ, shred_batch_size );
101 : FD_STATIC_ASSERT( sizeof(fd_gui_fec_event_key_t)==16UL, fec_event_key_size );
102 : FD_STATIC_ASSERT( sizeof(fd_gui_fec_event_t)<=32UL, fec_event_size );
103 : FD_STATIC_ASSERT( FD_GUI_STORE_REGION_SZ/32UL>=FD_GUI_FEC_RECORD_MAX, fec_region_capacity );
104 : /* The batch codec stores admission deltas in 32 bits. */
105 : FD_STATIC_ASSERT( FD_GUI_FEC_BATCH_SPAN_NS<=(long)UINT_MAX, fec_batch_span );
106 :
107 : struct fd_gui_shred_event_iter {
108 : fd_gui_shred_event_t event;
109 : fd_gui_t * _gui;
110 : long _after_ns;
111 : long _before_ns;
112 : fd_gui_hist_iter_t _hist_iter;
113 : fd_gui_shred_batch_t const * _batch;
114 : ulong _batch_idx;
115 : ulong _data_off;
116 : int _event_time;
117 : int _fec_archive;
118 : int _builder_pending;
119 : int _hist_active;
120 : long _fec_insert_hi;
121 : fd_gui_store_kv_scan_t _fec_iter;
122 : };
123 : typedef struct fd_gui_shred_event_iter fd_gui_shred_event_iter_t;
124 :
125 : FD_PROTOTYPES_BEGIN
126 :
127 : /* Iterate by original insertion time, inclusive at both ends. Event
128 : timestamps are preserved verbatim and need not be ordered. */
129 : fd_gui_shred_event_iter_t *
130 : fd_gui_shred_event_iter_begin( fd_gui_t * gui,
131 : fd_gui_shred_event_iter_t * iter,
132 : long after_ns,
133 : long before_ns );
134 :
135 : /* fd_gui_shred_event_hist_iter_begin selects event time in
136 : [after_ns,before_ns). Event times are only approximately ordered
137 : with insertion, so the scan is widened by one second on each side. */
138 : fd_gui_shred_event_iter_t *
139 : fd_gui_shred_event_hist_iter_begin( fd_gui_t * gui,
140 : fd_gui_shred_event_iter_t * iter,
141 : long after_ns,
142 : long before_ns );
143 :
144 : int
145 : fd_gui_shred_event_iter_next( fd_gui_shred_event_iter_t * iter );
146 :
147 : void
148 : fd_gui_shred_event_iter_end( fd_gui_shred_event_iter_t * iter );
149 :
150 : /* Append immediately. now is the GUI insertion time and must be
151 : nondecreasing across all channels; timestamp is the source event time. */
152 : void
153 : fd_gui_shred_event_append( fd_gui_t * gui,
154 : ulong slot,
155 : ulong idx,
156 : uchar event,
157 : long timestamp,
158 : long now );
159 :
160 : /* Seal the active batch once its insertion second has closed.
161 : Returns 1 when a batch was flushed. */
162 : int
163 : fd_gui_shred_flush( fd_gui_t * gui,
164 : long now );
165 :
166 : /* fd_gui_shred_event_slot_complete records a slot complete marker at
167 : timestamp in both the shred and FEC event histories. now is the
168 : insertion time. */
169 :
170 : void
171 : fd_gui_shred_event_slot_complete( fd_gui_t * gui,
172 : ulong slot,
173 : long timestamp,
174 : long now );
175 :
176 : /* fd_gui_fec_event_append records a FEC event, keeping the earliest
177 : timestamp for each (slot, idx, event) identity. idx is the FEC set
178 : index, or USHORT_MAX for slot complete markers. now is the GUI
179 : insertion time. */
180 :
181 : void
182 : fd_gui_fec_event_append( fd_gui_t * gui,
183 : ulong slot,
184 : ulong idx,
185 : uchar event,
186 : long timestamp,
187 : long now );
188 :
189 : /* fd_gui_fec_archive moves at most budget staged FEC events, in
190 : admission order, to the archive. A staged event is only removed once
191 : its archive append succeeds. Returns the number of events archived. */
192 :
193 : ulong
194 : fd_gui_fec_archive( fd_gui_t * gui,
195 : ulong budget );
196 :
197 : /* fd_gui_fec_event_iter_begin selects archived and staged FEC events
198 : by event time in [after_ns,before_ns). */
199 :
200 : void
201 : fd_gui_fec_event_iter_begin( fd_gui_t * gui,
202 : fd_gui_shred_event_iter_t * iter,
203 : long after_ns,
204 : long before_ns );
205 :
206 : /* fd_gui_event_closed_slot_wmark returns the closed slot watermark.
207 : Slots below it are permanently closed. */
208 :
209 : ulong
210 : fd_gui_event_closed_slot_wmark( fd_gui_t const * gui );
211 :
212 : /* fd_gui_event_closed_slot_wmark_advance permanently closes slots
213 : below wmark (one slot behind the rooted slot). */
214 :
215 : void
216 : fd_gui_event_closed_slot_wmark_advance( fd_gui_t * gui,
217 : ulong wmark );
218 :
219 : int
220 : fd_gui_shred_window_is_empty( fd_gui_t * gui,
221 : long after_ns,
222 : long before_ns );
223 :
224 : /* fd_gui_event_bounds returns whether dbi has lookup bounds, writing
225 : half-open [lo,hi) bounds that include the active shred batch. */
226 :
227 : int
228 : fd_gui_event_bounds( fd_gui_t * gui,
229 : int dbi,
230 : long * lo,
231 : long * hi );
232 :
233 : FD_PROTOTYPES_END
234 :
235 : #endif /* HEADER_fd_src_disco_gui_fd_gui_shred_h */
|