Line data Source code
1 : #include "fd_funk.h"
2 : #include "fd_funk_base.h"
3 : #include <stdio.h>
4 :
5 : ulong
6 153 : fd_funk_align( void ) {
7 153 : return FD_FUNK_ALIGN;
8 153 : }
9 :
10 : ulong
11 : fd_funk_footprint( ulong txn_max,
12 63 : ulong rec_max ) {
13 63 : if( FD_UNLIKELY( rec_max>UINT_MAX ) ) return 0UL;
14 :
15 63 : ulong l = FD_LAYOUT_INIT;
16 :
17 63 : l = FD_LAYOUT_APPEND( l, alignof(fd_funk_shmem_t), sizeof(fd_funk_shmem_t) );
18 :
19 63 : ulong txn_chain_cnt = fd_funk_txn_map_chain_cnt_est( txn_max );
20 63 : l = FD_LAYOUT_APPEND( l, fd_funk_txn_map_align(), fd_funk_txn_map_footprint( txn_chain_cnt ) );
21 63 : l = FD_LAYOUT_APPEND( l, fd_funk_txn_pool_align(), fd_funk_txn_pool_footprint() );
22 63 : l = FD_LAYOUT_APPEND( l, alignof(fd_funk_txn_t), sizeof(fd_funk_txn_t) * txn_max );
23 :
24 63 : ulong rec_chain_cnt = fd_funk_rec_map_chain_cnt_est( rec_max );
25 63 : l = FD_LAYOUT_APPEND( l, fd_funk_rec_map_align(), fd_funk_rec_map_footprint( rec_chain_cnt ) );
26 63 : l = FD_LAYOUT_APPEND( l, fd_funk_rec_pool_align(), fd_funk_rec_pool_footprint() );
27 63 : l = FD_LAYOUT_APPEND( l, alignof(fd_funk_rec_t), sizeof(fd_funk_rec_t) * rec_max );
28 :
29 63 : l = FD_LAYOUT_APPEND( l, fd_alloc_align(), fd_alloc_footprint() );
30 :
31 63 : return l;
32 63 : }
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 :
123 33 : funk->alloc_gaddr = fd_wksp_gaddr_fast( wksp, fd_alloc_join( fd_alloc_new( alloc, wksp_tag ), 0UL ) );
124 :
125 33 : funk->lock = 0;
126 :
127 33 : FD_COMPILER_MFENCE();
128 33 : FD_VOLATILE( funk->magic ) = FD_FUNK_MAGIC;
129 33 : FD_COMPILER_MFENCE();
130 :
131 33 : return (void *)funk;
132 33 : }
133 :
134 : fd_funk_t *
135 : fd_funk_join( fd_funk_t * ljoin,
136 45 : void * shfunk ) {
137 45 : if( FD_UNLIKELY( !shfunk ) ) {
138 3 : FD_LOG_WARNING(( "NULL shfunk" ));
139 3 : return NULL;
140 3 : }
141 :
142 42 : if( FD_UNLIKELY( !fd_ulong_is_aligned( (ulong)shfunk, fd_funk_align() ) ) ) {
143 3 : FD_LOG_WARNING(( "misaligned shfunk" ));
144 3 : return NULL;
145 3 : }
146 :
147 39 : fd_wksp_t * wksp = fd_wksp_containing( shfunk );
148 39 : if( FD_UNLIKELY( !wksp ) ) {
149 3 : FD_LOG_WARNING(( "shfunk must be part of a workspace" ));
150 3 : return NULL;
151 3 : }
152 :
153 36 : fd_funk_shmem_t * shmem = shfunk;
154 36 : if( FD_UNLIKELY( shmem->magic!=FD_FUNK_MAGIC ) ) {
155 3 : if (shmem->magic == FD_FUNK_MAGIC+1) {
156 0 : FD_LOG_WARNING(( "attempted to join a funk that crashed in a critical section" ));
157 3 : } else {
158 3 : FD_LOG_WARNING(( "bad magic" ));
159 3 : }
160 3 : return NULL;
161 3 : }
162 :
163 33 : if( FD_UNLIKELY( !ljoin ) ) {
164 0 : FD_LOG_WARNING(( "NULL ljoin" ));
165 0 : return NULL;
166 0 : }
167 :
168 : #ifdef FD_FUNK_WKSP_PROTECT
169 : fd_wksp_mprotect( wksp, 1 );
170 : #endif
171 :
172 33 : fd_funk_t * funk = ljoin;
173 33 : memset( funk, 0, sizeof(fd_funk_t) );
174 :
175 33 : funk->shmem = shfunk;
176 33 : funk->wksp = wksp;
177 :
178 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 ) ) ) {
179 0 : FD_LOG_WARNING(( "failed to join txn_pool" ));
180 0 : return NULL;
181 0 : }
182 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 ) ) ) {
183 0 : FD_LOG_WARNING(( "failed to join txn_map" ));
184 0 : return NULL;
185 0 : }
186 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 ) ) ) {
187 0 : FD_LOG_WARNING(( "failed to join rec_map" ));
188 0 : return NULL;
189 0 : }
190 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 ) ) ) {
191 0 : FD_LOG_WARNING(( "failed to join rec_pool" ));
192 0 : return NULL;
193 0 : }
194 33 : funk->alloc = fd_wksp_laddr( wksp, shmem->alloc_gaddr );
195 33 : if( FD_UNLIKELY( !fd_alloc_join( funk->alloc, fd_tile_idx() ) ) ) {
196 0 : FD_LOG_WARNING(( "failed to join funk alloc" ));
197 0 : return NULL;
198 0 : }
199 :
200 33 : return funk;
201 33 : }
202 :
203 : int
204 0 : fd_funk_purify( void * shfunk ) {
205 : /* Join should work even if there was a crash */
206 0 : fd_funk_t ljoin[1];
207 0 : fd_funk_t * funk = fd_funk_join( ljoin, shfunk );
208 0 : if( funk == NULL ) return FD_FUNK_ERR_PURIFY;
209 :
210 : /* Reset the txn map. We discard any pending transactions. */
211 0 : fd_funk_txn_map_reset( funk->txn_map );
212 0 : fd_funk_txn_pool_reset( funk->txn_pool, 0 );
213 0 : funk->shmem->child_head_cidx = funk->shmem->child_tail_cidx = fd_funk_txn_cidx( FD_FUNK_TXN_IDX_NULL );
214 :
215 0 : return fd_funk_rec_purify( funk );
216 0 : }
217 :
218 : void *
219 : fd_funk_leave( fd_funk_t * funk,
220 33 : void ** opt_shfunk ) {
221 :
222 33 : if( FD_UNLIKELY( !funk ) ) {
223 3 : FD_LOG_WARNING(( "NULL funk" ));
224 3 : if( opt_shfunk ) *opt_shfunk = NULL;
225 3 : return NULL;
226 3 : }
227 30 : void * shfunk = funk->shmem;
228 :
229 30 : memset( funk, 0, sizeof(fd_funk_t) );
230 :
231 30 : if( opt_shfunk ) *opt_shfunk = shfunk;
232 30 : return (void *)funk;
233 33 : }
234 :
235 : void *
236 33 : fd_funk_delete( void * shfunk ) {
237 :
238 33 : if( FD_UNLIKELY( !shfunk ) ) {
239 3 : FD_LOG_WARNING(( "NULL shfunk" ));
240 3 : return NULL;
241 3 : }
242 :
243 30 : if( FD_UNLIKELY( !fd_ulong_is_aligned( (ulong)shfunk, fd_funk_align() ) ) ) {
244 3 : FD_LOG_WARNING(( "misaligned shfunk" ));
245 3 : return NULL;
246 3 : }
247 :
248 27 : fd_wksp_t * wksp = fd_wksp_containing( shfunk );
249 27 : if( FD_UNLIKELY( !wksp ) ) {
250 3 : FD_LOG_WARNING(( "shfunk must be part of a workspace" ));
251 3 : return NULL;
252 3 : }
253 :
254 24 : fd_funk_shmem_t * shmem = shfunk;
255 24 : if( FD_UNLIKELY( shmem->magic!=FD_FUNK_MAGIC ) ) {
256 6 : FD_LOG_WARNING(( "bad magic" ));
257 6 : return NULL;
258 6 : }
259 :
260 : /* Free all fd_alloc allocations made, individually
261 : (FIXME consider walking the element pool instead of the map?) */
262 :
263 18 : fd_alloc_t * alloc = fd_alloc_join( fd_wksp_laddr_fast( wksp, shmem->alloc_gaddr ), fd_tile_idx() );
264 :
265 18 : void * shmap = fd_wksp_laddr_fast( wksp, shmem->rec_map_gaddr );
266 18 : void * shele = fd_wksp_laddr_fast( wksp, shmem->rec_ele_gaddr );
267 18 : fd_funk_rec_map_t rec_map[1];
268 18 : if( FD_UNLIKELY( !fd_funk_rec_map_join( rec_map, shmap, shele, 0UL ) ) ) {
269 0 : FD_LOG_ERR(( "failed to join rec_map (corrupt funk?)" ));
270 0 : return NULL;
271 0 : }
272 18 : ulong chain_cnt = fd_funk_rec_map_chain_cnt( rec_map );
273 786696 : for( ulong chain_idx=0UL; chain_idx<chain_cnt; chain_idx++ ) {
274 786678 : for(
275 786678 : fd_funk_rec_map_iter_t iter = fd_funk_rec_map_iter( rec_map, chain_idx );
276 787059 : !fd_funk_rec_map_iter_done( iter );
277 786678 : iter = fd_funk_rec_map_iter_next( iter )
278 786678 : ) {
279 381 : fd_funk_val_flush( fd_funk_rec_map_iter_ele( iter ), alloc, wksp );
280 381 : }
281 786678 : }
282 :
283 18 : fd_funk_rec_map_leave( rec_map );
284 :
285 : /* Free the fd_alloc instance */
286 :
287 18 : fd_wksp_free_laddr( fd_alloc_delete( fd_alloc_leave( alloc ) ) );
288 :
289 18 : FD_COMPILER_MFENCE();
290 18 : FD_VOLATILE( shmem->magic ) = 0UL;
291 18 : FD_COMPILER_MFENCE();
292 :
293 18 : return shmem;
294 18 : }
295 :
296 : void
297 9 : fd_funk_delete_fast( void * shfunk ) {
298 :
299 9 : if( FD_UNLIKELY( !shfunk ) ) {
300 0 : FD_LOG_WARNING(( "NULL shfunk" ));
301 0 : }
302 :
303 9 : if( FD_UNLIKELY( !fd_ulong_is_aligned( (ulong)shfunk, fd_funk_align() ) ) ) {
304 0 : FD_LOG_WARNING(( "misaligned shfunk" ));
305 0 : }
306 :
307 9 : fd_funk_shmem_t * shmem = shfunk;
308 9 : if( FD_UNLIKELY( shmem->magic!=FD_FUNK_MAGIC ) ) {
309 0 : FD_LOG_WARNING(( "bad magic" ));
310 0 : }
311 :
312 9 : fd_wksp_t * wksp = fd_wksp_containing( shmem );
313 9 : if( FD_UNLIKELY( !wksp ) ) {
314 0 : FD_LOG_WARNING(( "shfunk must be part of a workspace" ));
315 0 : }
316 :
317 9 : ulong const tags[1] = { shmem->wksp_tag };
318 9 : fd_wksp_tag_free( wksp, tags, 1UL );
319 :
320 9 : }
321 :
322 : int
323 12 : fd_funk_verify( fd_funk_t * join ) {
324 12 : fd_funk_shmem_t * funk = join->shmem;
325 :
326 312 : # define TEST(c) do { \
327 312 : if( FD_UNLIKELY( !(c) ) ) { FD_LOG_WARNING(( "FAIL: %s", #c )); return FD_FUNK_ERR_INVAL; } \
328 312 : } while(0)
329 :
330 12 : TEST( funk );
331 :
332 : /* Test metadata */
333 :
334 12 : TEST( funk->magic==FD_FUNK_MAGIC );
335 :
336 12 : ulong funk_gaddr = funk->funk_gaddr;
337 12 : TEST( funk_gaddr );
338 12 : fd_wksp_t * wksp = fd_funk_wksp( join );
339 12 : TEST( wksp );
340 12 : TEST( fd_wksp_laddr_fast( wksp, funk_gaddr )==(void *)funk );
341 12 : TEST( fd_wksp_gaddr_fast( wksp, funk )==funk_gaddr );
342 :
343 12 : ulong wksp_tag = fd_funk_wksp_tag( join );
344 12 : TEST( !!wksp_tag );
345 :
346 12 : ulong seed = funk->seed; /* seed can be anything */
347 :
348 12 : TEST( funk->cycle_tag>2UL );
349 :
350 : /* Test transaction map */
351 :
352 12 : ulong txn_max = fd_funk_txn_pool_ele_max( join->txn_pool );
353 12 : TEST( txn_max<=FD_FUNK_TXN_IDX_NULL );
354 :
355 12 : ulong txn_map_gaddr = funk->txn_map_gaddr;
356 12 : TEST( txn_map_gaddr );
357 12 : fd_funk_txn_map_t * txn_map = fd_funk_txn_map( join );
358 12 : ulong txn_chain_cnt = fd_funk_txn_map_chain_cnt_est( txn_max );
359 12 : TEST( txn_chain_cnt==fd_funk_txn_map_chain_cnt( txn_map ) );
360 12 : TEST( seed==fd_funk_txn_map_seed( txn_map ) );
361 :
362 12 : ulong child_head_idx = fd_funk_txn_idx( funk->child_head_cidx );
363 12 : ulong child_tail_idx = fd_funk_txn_idx( funk->child_tail_cidx );
364 :
365 12 : int null_child_head = fd_funk_txn_idx_is_null( child_head_idx );
366 12 : int null_child_tail = fd_funk_txn_idx_is_null( child_tail_idx );
367 :
368 12 : if( !txn_max ) TEST( null_child_head & null_child_tail );
369 6 : else {
370 6 : if( null_child_head ) TEST( null_child_tail );
371 0 : else TEST( child_head_idx<txn_max );
372 :
373 6 : if( null_child_tail ) TEST( null_child_head );
374 0 : else TEST( child_tail_idx<txn_max );
375 6 : }
376 :
377 12 : if( !txn_max ) TEST( fd_funk_txn_idx_is_null( child_tail_idx ) );
378 :
379 12 : fd_funk_txn_xid_t const * root = fd_funk_root( join );
380 12 : TEST( root ); /* Practically guaranteed */
381 12 : TEST( fd_funk_txn_xid_eq_root( root ) );
382 :
383 12 : fd_funk_txn_xid_t * last_publish = funk->last_publish;
384 12 : TEST( last_publish ); /* Practically guaranteed */
385 : /* (*last_publish) only be root at creation and anything but root post
386 : creation. But we don't know which situation applies here so this
387 : could be anything. */
388 :
389 12 : TEST( !fd_funk_txn_verify( join ) );
390 :
391 : /* Test record map */
392 :
393 12 : ulong rec_max = fd_funk_rec_pool_ele_max( join->rec_pool );
394 12 : TEST( rec_max<=FD_FUNK_TXN_IDX_NULL );
395 :
396 12 : ulong rec_map_gaddr = funk->rec_map_gaddr;
397 12 : TEST( rec_map_gaddr );
398 12 : fd_funk_rec_map_t * rec_map = fd_funk_rec_map( join );
399 12 : ulong rec_chain_cnt = fd_funk_rec_map_chain_cnt_est( rec_max );
400 12 : TEST( rec_chain_cnt==fd_funk_rec_map_chain_cnt( rec_map ) );
401 12 : TEST( seed==fd_funk_rec_map_seed( rec_map ) );
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 : }
|