Line data Source code
1 : #include "fd_funk.h"
2 : #include "fd_funk_base.h"
3 : #include <stdio.h>
4 :
5 : ulong
6 147 : fd_funk_align( void ) {
7 147 : return FD_FUNK_ALIGN;
8 147 : }
9 :
10 : ulong
11 : fd_funk_footprint( ulong txn_max,
12 57 : ulong rec_max ) {
13 57 : if( FD_UNLIKELY( rec_max>UINT_MAX ) ) return 0UL;
14 :
15 57 : ulong l = FD_LAYOUT_INIT;
16 :
17 57 : l = FD_LAYOUT_APPEND( l, alignof(fd_funk_shmem_t), sizeof(fd_funk_shmem_t) );
18 :
19 57 : ulong txn_chain_cnt = fd_funk_txn_map_chain_cnt_est( txn_max );
20 57 : l = FD_LAYOUT_APPEND( l, fd_funk_txn_map_align(), fd_funk_txn_map_footprint( txn_chain_cnt ) );
21 57 : l = FD_LAYOUT_APPEND( l, fd_funk_txn_pool_align(), fd_funk_txn_pool_footprint() );
22 57 : l = FD_LAYOUT_APPEND( l, alignof(fd_funk_txn_t), sizeof(fd_funk_txn_t) * txn_max );
23 :
24 57 : ulong rec_chain_cnt = fd_funk_rec_map_chain_cnt_est( rec_max );
25 57 : l = FD_LAYOUT_APPEND( l, fd_funk_rec_map_align(), fd_funk_rec_map_footprint( rec_chain_cnt ) );
26 57 : l = FD_LAYOUT_APPEND( l, fd_funk_rec_pool_align(), fd_funk_rec_pool_footprint() );
27 57 : l = FD_LAYOUT_APPEND( l, alignof(fd_funk_rec_t), sizeof(fd_funk_rec_t) * rec_max );
28 :
29 57 : l = FD_LAYOUT_APPEND( l, fd_alloc_align(), fd_alloc_footprint() );
30 :
31 57 : return l;
32 57 : }
33 :
34 : /* TODO: Consider letter user just passing a join of alloc to use,
35 : inferring the backing wksp and cgroup_hint from that and then
36 : allocating exclusively from that? */
37 :
38 : void *
39 : fd_funk_new( void * shmem,
40 : ulong wksp_tag,
41 : ulong seed,
42 : ulong txn_max,
43 48 : ulong rec_max ) {
44 48 : fd_funk_shmem_t * funk = shmem;
45 48 : fd_wksp_t * wksp = fd_wksp_containing( funk );
46 :
47 48 : if( FD_UNLIKELY( !funk ) ) {
48 3 : FD_LOG_WARNING(( "NULL funk" ));
49 3 : return NULL;
50 3 : }
51 :
52 45 : if( FD_UNLIKELY( !fd_ulong_is_aligned( (ulong)funk, fd_funk_align() ) ) ) {
53 3 : FD_LOG_WARNING(( "misaligned funk" ));
54 3 : return NULL;
55 3 : }
56 :
57 42 : if( FD_UNLIKELY( !wksp_tag ) ) {
58 3 : FD_LOG_WARNING(( "bad wksp_tag" ));
59 3 : return NULL;
60 3 : }
61 :
62 39 : if( FD_UNLIKELY( !wksp ) ) {
63 3 : FD_LOG_WARNING(( "shmem must be part of a workspace" ));
64 3 : return NULL;
65 3 : }
66 :
67 36 : if( FD_UNLIKELY( txn_max>FD_FUNK_TXN_IDX_NULL ) ) { /* See note in fd_funk.h about this limit */
68 3 : FD_LOG_WARNING(( "txn_max too large for index compression" ));
69 3 : return NULL;
70 3 : }
71 :
72 33 : if( FD_UNLIKELY( rec_max>UINT_MAX ) ) {
73 0 : FD_LOG_WARNING(( "invalid rec_max" ));
74 0 : return NULL;
75 0 : }
76 :
77 33 : FD_SCRATCH_ALLOC_INIT( l, funk+1 );
78 :
79 33 : ulong txn_chain_cnt = fd_funk_txn_map_chain_cnt_est( txn_max );
80 33 : void * txn_map = FD_SCRATCH_ALLOC_APPEND( l, fd_funk_txn_map_align(), fd_funk_txn_map_footprint( txn_chain_cnt ) );
81 33 : void * txn_pool = FD_SCRATCH_ALLOC_APPEND( l, fd_funk_txn_pool_align(), fd_funk_txn_pool_footprint() );
82 33 : fd_funk_txn_t * txn_ele = (fd_funk_txn_t *)FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_funk_txn_t), sizeof(fd_funk_txn_t) * txn_max );
83 :
84 0 : ulong rec_chain_cnt = fd_funk_rec_map_chain_cnt_est( rec_max );
85 33 : void * rec_map = FD_SCRATCH_ALLOC_APPEND( l, fd_funk_rec_map_align(), fd_funk_rec_map_footprint( rec_chain_cnt ) );
86 33 : void * rec_pool = FD_SCRATCH_ALLOC_APPEND( l, fd_funk_rec_pool_align(), fd_funk_rec_pool_footprint() );
87 33 : fd_funk_rec_t * rec_ele = (fd_funk_rec_t *)FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_funk_rec_t), sizeof(fd_funk_rec_t) * rec_max );
88 :
89 33 : void * alloc = FD_SCRATCH_ALLOC_APPEND( l, fd_alloc_align(), fd_alloc_footprint() );
90 :
91 33 : FD_TEST( _l == (ulong)funk + fd_funk_footprint( txn_max, rec_max ) );
92 :
93 33 : fd_memset( funk, 0, sizeof(fd_funk_shmem_t) );
94 :
95 33 : funk->funk_gaddr = fd_wksp_gaddr_fast( wksp, funk );
96 33 : funk->wksp_tag = wksp_tag;
97 33 : funk->seed = seed;
98 33 : funk->cycle_tag = 3UL; /* various verify functions use tags 0-2 */
99 :
100 33 : funk->txn_map_gaddr = fd_wksp_gaddr_fast( wksp, fd_funk_txn_map_new( txn_map, txn_chain_cnt, seed ) );
101 33 : void * txn_pool2 = fd_funk_txn_pool_new( txn_pool );
102 33 : funk->txn_pool_gaddr = fd_wksp_gaddr_fast( wksp, txn_pool2 );
103 33 : fd_funk_txn_pool_t txn_join[1];
104 33 : fd_funk_txn_pool_join( txn_join, txn_pool2, txn_ele, txn_max );
105 33 : fd_funk_txn_pool_reset( txn_join, 0UL );
106 33 : funk->txn_ele_gaddr = fd_wksp_gaddr_fast( wksp, txn_ele );
107 33 : funk->txn_max = txn_max;
108 33 : funk->child_head_cidx = fd_funk_txn_cidx( FD_FUNK_TXN_IDX_NULL );
109 33 : funk->child_tail_cidx = fd_funk_txn_cidx( FD_FUNK_TXN_IDX_NULL );
110 :
111 33 : fd_funk_txn_xid_set_root( funk->root );
112 33 : fd_funk_txn_xid_set_root( funk->last_publish );
113 :
114 33 : funk->rec_map_gaddr = fd_wksp_gaddr_fast( wksp, fd_funk_rec_map_new( rec_map, rec_chain_cnt, seed ) );
115 33 : void * rec_pool2 = fd_funk_rec_pool_new( rec_pool );
116 33 : funk->rec_pool_gaddr = fd_wksp_gaddr_fast( wksp, rec_pool2 );
117 33 : fd_funk_rec_pool_t rec_join[1];
118 33 : fd_funk_rec_pool_join( rec_join, rec_pool2, rec_ele, rec_max );
119 33 : fd_funk_rec_pool_reset( rec_join, 0UL );
120 33 : funk->rec_ele_gaddr = fd_wksp_gaddr_fast( wksp, rec_ele );
121 33 : funk->rec_max = (uint)rec_max;
122 33 : funk->rec_head_idx = FD_FUNK_REC_IDX_NULL;
123 33 : funk->rec_tail_idx = FD_FUNK_REC_IDX_NULL;
124 :
125 33 : funk->alloc_gaddr = fd_wksp_gaddr_fast( wksp, fd_alloc_join( fd_alloc_new( alloc, wksp_tag ), 0UL ) );
126 :
127 33 : funk->lock = 0;
128 :
129 33 : FD_COMPILER_MFENCE();
130 33 : FD_VOLATILE( funk->magic ) = FD_FUNK_MAGIC;
131 33 : FD_COMPILER_MFENCE();
132 :
133 33 : return (void *)funk;
134 33 : }
135 :
136 : fd_funk_t *
137 : fd_funk_join( fd_funk_t * ljoin,
138 45 : void * shfunk ) {
139 45 : if( FD_UNLIKELY( !shfunk ) ) {
140 3 : FD_LOG_WARNING(( "NULL shfunk" ));
141 3 : return NULL;
142 3 : }
143 :
144 42 : if( FD_UNLIKELY( !fd_ulong_is_aligned( (ulong)shfunk, fd_funk_align() ) ) ) {
145 3 : FD_LOG_WARNING(( "misaligned shfunk" ));
146 3 : return NULL;
147 3 : }
148 :
149 39 : fd_wksp_t * wksp = fd_wksp_containing( shfunk );
150 39 : if( FD_UNLIKELY( !wksp ) ) {
151 3 : FD_LOG_WARNING(( "shfunk must be part of a workspace" ));
152 3 : return NULL;
153 3 : }
154 :
155 36 : fd_funk_shmem_t * shmem = shfunk;
156 36 : if( FD_UNLIKELY( shmem->magic!=FD_FUNK_MAGIC ) ) {
157 3 : FD_LOG_WARNING(( "bad magic" ));
158 3 : return NULL;
159 3 : }
160 :
161 33 : if( FD_UNLIKELY( !ljoin ) ) {
162 0 : FD_LOG_WARNING(( "NULL ljoin" ));
163 0 : return NULL;
164 0 : }
165 :
166 : #ifdef FD_FUNK_WKSP_PROTECT
167 : fd_wksp_mprotect( wksp, 1 );
168 : #endif
169 :
170 33 : fd_funk_t * funk = ljoin;
171 33 : memset( funk, 0, sizeof(fd_funk_t) );
172 :
173 33 : funk->shmem = shfunk;
174 33 : funk->wksp = wksp;
175 :
176 33 : if( FD_UNLIKELY( !fd_funk_txn_pool_join( funk->txn_pool, fd_wksp_laddr( wksp, shmem->txn_pool_gaddr ), fd_wksp_laddr( wksp, shmem->txn_ele_gaddr ), shmem->txn_max ) ) ) {
177 0 : FD_LOG_WARNING(( "failed to join txn_pool" ));
178 0 : return NULL;
179 0 : }
180 33 : if( FD_UNLIKELY( !fd_funk_txn_map_join( funk->txn_map, fd_wksp_laddr( wksp, shmem->txn_map_gaddr ), fd_wksp_laddr_fast( wksp, shmem->txn_ele_gaddr ), shmem->txn_max ) ) ) {
181 0 : FD_LOG_WARNING(( "failed to join txn_map" ));
182 0 : return NULL;
183 0 : }
184 33 : if( FD_UNLIKELY( !fd_funk_rec_map_join( funk->rec_map, fd_wksp_laddr( wksp, shmem->rec_map_gaddr ), fd_wksp_laddr( wksp, shmem->rec_ele_gaddr ), shmem->rec_max ) ) ) {
185 0 : FD_LOG_WARNING(( "failed to join rec_map" ));
186 0 : return NULL;
187 0 : }
188 33 : if( FD_UNLIKELY( !fd_funk_rec_pool_join( funk->rec_pool, fd_wksp_laddr( wksp, shmem->rec_pool_gaddr ), fd_wksp_laddr_fast( wksp, shmem->rec_ele_gaddr ), shmem->rec_max ) ) ) {
189 0 : FD_LOG_WARNING(( "failed to join rec_pool" ));
190 0 : return NULL;
191 0 : }
192 33 : funk->alloc = fd_wksp_laddr( wksp, shmem->alloc_gaddr );
193 33 : if( FD_UNLIKELY( !fd_alloc_join( funk->alloc, fd_tile_idx() ) ) ) {
194 0 : FD_LOG_WARNING(( "failed to join funk alloc" ));
195 0 : return NULL;
196 0 : }
197 :
198 33 : return funk;
199 33 : }
200 :
201 : void *
202 : fd_funk_leave( fd_funk_t * funk,
203 30 : void ** opt_shfunk ) {
204 :
205 30 : if( FD_UNLIKELY( !funk ) ) {
206 3 : FD_LOG_WARNING(( "NULL funk" ));
207 3 : if( opt_shfunk ) *opt_shfunk = NULL;
208 3 : return NULL;
209 3 : }
210 27 : void * shfunk = funk->shmem;
211 :
212 27 : memset( funk, 0, sizeof(fd_funk_t) );
213 :
214 27 : if( opt_shfunk ) *opt_shfunk = shfunk;
215 27 : return (void *)funk;
216 30 : }
217 :
218 : void *
219 39 : fd_funk_delete( void * shfunk ) {
220 :
221 39 : if( FD_UNLIKELY( !shfunk ) ) {
222 3 : FD_LOG_WARNING(( "NULL shfunk" ));
223 3 : return NULL;
224 3 : }
225 :
226 36 : if( FD_UNLIKELY( !fd_ulong_is_aligned( (ulong)shfunk, fd_funk_align() ) ) ) {
227 3 : FD_LOG_WARNING(( "misaligned shfunk" ));
228 3 : return NULL;
229 3 : }
230 :
231 33 : fd_wksp_t * wksp = fd_wksp_containing( shfunk );
232 33 : if( FD_UNLIKELY( !wksp ) ) {
233 3 : FD_LOG_WARNING(( "shfunk must be part of a workspace" ));
234 3 : return NULL;
235 3 : }
236 :
237 30 : fd_funk_shmem_t * shmem = shfunk;
238 30 : if( FD_UNLIKELY( shmem->magic!=FD_FUNK_MAGIC ) ) {
239 6 : FD_LOG_WARNING(( "bad magic" ));
240 6 : return NULL;
241 6 : }
242 :
243 : /* Free all fd_alloc allocations made, individually
244 : (FIXME consider walking the element pool instead of the map?) */
245 :
246 24 : fd_alloc_t * alloc = fd_alloc_join( fd_wksp_laddr_fast( wksp, shmem->alloc_gaddr ), fd_tile_idx() );
247 :
248 24 : void * shmap = fd_wksp_laddr_fast( wksp, shmem->rec_map_gaddr );
249 24 : void * shele = fd_wksp_laddr_fast( wksp, shmem->rec_ele_gaddr );
250 24 : fd_funk_rec_map_t rec_map[1];
251 24 : if( FD_UNLIKELY( !fd_funk_rec_map_join( rec_map, shmap, shele, 0UL ) ) ) {
252 0 : FD_LOG_ERR(( "failed to join rec_map (corrupt funk?)" ));
253 0 : return NULL;
254 0 : }
255 24 : ulong chain_cnt = fd_funk_rec_map_chain_cnt( rec_map );
256 2360334 : for( ulong chain_idx=0UL; chain_idx<chain_cnt; chain_idx++ ) {
257 2360310 : for(
258 2360310 : fd_funk_rec_map_iter_t iter = fd_funk_rec_map_iter( rec_map, chain_idx );
259 5506425 : !fd_funk_rec_map_iter_done( iter );
260 3146115 : iter = fd_funk_rec_map_iter_next( iter )
261 3146115 : ) {
262 3146115 : fd_funk_val_flush( fd_funk_rec_map_iter_ele( iter ), alloc, wksp );
263 3146115 : }
264 2360310 : }
265 :
266 24 : fd_funk_rec_map_leave( rec_map );
267 :
268 : /* Free the fd_alloc instance */
269 :
270 24 : fd_wksp_free_laddr( fd_alloc_delete( fd_alloc_leave( alloc ) ) );
271 :
272 24 : FD_COMPILER_MFENCE();
273 24 : FD_VOLATILE( shmem->magic ) = 0UL;
274 24 : FD_COMPILER_MFENCE();
275 :
276 24 : return shmem;
277 24 : }
278 :
279 : void
280 0 : fd_funk_delete_fast( void * shfunk ) {
281 :
282 0 : if( FD_UNLIKELY( !shfunk ) ) {
283 0 : FD_LOG_WARNING(( "NULL shfunk" ));
284 0 : }
285 :
286 0 : if( FD_UNLIKELY( !fd_ulong_is_aligned( (ulong)shfunk, fd_funk_align() ) ) ) {
287 0 : FD_LOG_WARNING(( "misaligned shfunk" ));
288 0 : }
289 :
290 0 : fd_funk_shmem_t * shmem = shfunk;
291 0 : if( FD_UNLIKELY( shmem->magic!=FD_FUNK_MAGIC ) ) {
292 0 : FD_LOG_WARNING(( "bad magic" ));
293 0 : }
294 :
295 0 : fd_wksp_t * wksp = fd_wksp_containing( shmem );
296 0 : if( FD_UNLIKELY( !wksp ) ) {
297 0 : FD_LOG_WARNING(( "shfunk must be part of a workspace" ));
298 0 : }
299 :
300 0 : ulong const tags[1] = { shmem->wksp_tag };
301 0 : fd_wksp_tag_free( wksp, tags, 1UL );
302 :
303 0 : }
304 :
305 : int
306 12 : fd_funk_verify( fd_funk_t * join ) {
307 12 : fd_funk_shmem_t * funk = join->shmem;
308 :
309 336 : # define TEST(c) do { \
310 336 : if( FD_UNLIKELY( !(c) ) ) { FD_LOG_WARNING(( "FAIL: %s", #c )); return FD_FUNK_ERR_INVAL; } \
311 336 : } while(0)
312 :
313 12 : TEST( funk );
314 :
315 : /* Test metadata */
316 :
317 12 : TEST( funk->magic==FD_FUNK_MAGIC );
318 :
319 12 : ulong funk_gaddr = funk->funk_gaddr;
320 12 : TEST( funk_gaddr );
321 12 : fd_wksp_t * wksp = fd_funk_wksp( join );
322 12 : TEST( wksp );
323 12 : TEST( fd_wksp_laddr_fast( wksp, funk_gaddr )==(void *)funk );
324 12 : TEST( fd_wksp_gaddr_fast( wksp, funk )==funk_gaddr );
325 :
326 12 : ulong wksp_tag = fd_funk_wksp_tag( join );
327 12 : TEST( !!wksp_tag );
328 :
329 12 : ulong seed = funk->seed; /* seed can be anything */
330 :
331 12 : TEST( funk->cycle_tag>2UL );
332 :
333 : /* Test transaction map */
334 :
335 12 : ulong txn_max = fd_funk_txn_pool_ele_max( join->txn_pool );
336 12 : TEST( txn_max<=FD_FUNK_TXN_IDX_NULL );
337 :
338 12 : ulong txn_map_gaddr = funk->txn_map_gaddr;
339 12 : TEST( txn_map_gaddr );
340 12 : fd_funk_txn_map_t * txn_map = fd_funk_txn_map( join );
341 12 : ulong txn_chain_cnt = fd_funk_txn_map_chain_cnt_est( txn_max );
342 12 : TEST( txn_chain_cnt==fd_funk_txn_map_chain_cnt( txn_map ) );
343 12 : TEST( seed==fd_funk_txn_map_seed( txn_map ) );
344 :
345 12 : ulong child_head_idx = fd_funk_txn_idx( funk->child_head_cidx );
346 12 : ulong child_tail_idx = fd_funk_txn_idx( funk->child_tail_cidx );
347 :
348 12 : int null_child_head = fd_funk_txn_idx_is_null( child_head_idx );
349 12 : int null_child_tail = fd_funk_txn_idx_is_null( child_tail_idx );
350 :
351 12 : if( !txn_max ) TEST( null_child_head & null_child_tail );
352 6 : else {
353 6 : if( null_child_head ) TEST( null_child_tail );
354 0 : else TEST( child_head_idx<txn_max );
355 :
356 6 : if( null_child_tail ) TEST( null_child_head );
357 0 : else TEST( child_tail_idx<txn_max );
358 6 : }
359 :
360 12 : if( !txn_max ) TEST( fd_funk_txn_idx_is_null( child_tail_idx ) );
361 :
362 12 : fd_funk_txn_xid_t const * root = fd_funk_root( join );
363 12 : TEST( root ); /* Practically guaranteed */
364 12 : TEST( fd_funk_txn_xid_eq_root( root ) );
365 :
366 12 : fd_funk_txn_xid_t * last_publish = funk->last_publish;
367 12 : TEST( last_publish ); /* Practically guaranteed */
368 : /* (*last_publish) only be root at creation and anything but root post
369 : creation. But we don't know which situation applies here so this
370 : could be anything. */
371 :
372 12 : TEST( !fd_funk_txn_verify( join ) );
373 :
374 : /* Test record map */
375 :
376 12 : ulong rec_max = fd_funk_rec_pool_ele_max( join->rec_pool );
377 12 : TEST( rec_max<=FD_FUNK_TXN_IDX_NULL );
378 :
379 12 : ulong rec_map_gaddr = funk->rec_map_gaddr;
380 12 : TEST( rec_map_gaddr );
381 12 : fd_funk_rec_map_t * rec_map = fd_funk_rec_map( join );
382 12 : ulong rec_chain_cnt = fd_funk_rec_map_chain_cnt_est( rec_max );
383 12 : TEST( rec_chain_cnt==fd_funk_rec_map_chain_cnt( rec_map ) );
384 12 : TEST( seed==fd_funk_rec_map_seed( rec_map ) );
385 :
386 12 : uint rec_head_idx = funk->rec_head_idx;
387 12 : uint rec_tail_idx = funk->rec_tail_idx;
388 :
389 12 : int null_rec_head = fd_funk_rec_idx_is_null( rec_head_idx );
390 12 : int null_rec_tail = fd_funk_rec_idx_is_null( rec_tail_idx );
391 :
392 12 : if( !rec_max ) TEST( null_rec_head & null_rec_tail );
393 6 : else {
394 6 : if( null_rec_head ) TEST( null_rec_tail );
395 0 : else TEST( rec_head_idx<rec_max );
396 :
397 6 : if( null_rec_tail ) TEST( null_rec_head );
398 0 : else TEST( rec_tail_idx<rec_max );
399 6 : }
400 :
401 12 : if( !rec_max ) TEST( fd_funk_rec_idx_is_null( rec_tail_idx ) );
402 :
403 12 : TEST( !fd_funk_rec_verify( join ) );
404 :
405 : /* Test values */
406 :
407 12 : ulong alloc_gaddr = funk->alloc_gaddr;
408 12 : TEST( alloc_gaddr );
409 12 : fd_alloc_t * alloc = fd_funk_alloc( join );
410 12 : TEST( alloc );
411 :
412 12 : TEST( !fd_funk_val_verify( join ) );
413 :
414 12 : # undef TEST
415 :
416 12 : return FD_FUNK_SUCCESS;
417 12 : }
|