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