Line data Source code
1 : #include "../../disco/tiles.h"
2 :
3 : #include <linux/futex.h>
4 :
5 : #include "../../discof/fd_startup.h"
6 :
7 : #include <time.h>
8 : #include "generated/fd_accdb_tile_seccomp.h"
9 :
10 : #include "../../disco/metrics/fd_metrics.h"
11 : #include "../../disco/events/generated/fd_event_gen.h"
12 : #include "../../tango/fseq/fd_fseq.h"
13 : #include "../../util/pod/fd_pod.h"
14 :
15 : #include "fd_accdb.h"
16 : #include "../progcache/fd_progcache.h"
17 : #include "../progcache/fd_progcache_clock.h"
18 : #include "../progcache/fd_progcache_user.h"
19 :
20 : #include <fcntl.h>
21 :
22 : /* Maximum number of read-only accdb consumer fseqs the accdb tile can
23 : bind external_epoch_slots[] to. Bumped as new RO consumers are
24 : added. Today: resolv tiles, rpc tile (optional), snapmk/zp tiles
25 : (optional). */
26 : #define FD_ACCDB_TILE_MAX_EXTERNAL_EPOCHS (128UL)
27 :
28 : struct fd_accdb_tile_ctx {
29 : fd_accdb_t * accdb;
30 :
31 : int progcache_enabled;
32 : fd_progcache_join_t progcache[1];
33 : fd_progcache_metrics_t progcache_metrics[1];
34 :
35 : fd_startup_gate_t startup_gate[1];
36 :
37 : int busy;
38 :
39 : ulong seed;
40 : };
41 :
42 : typedef struct fd_accdb_tile_ctx fd_accdb_tile_ctx_t;
43 :
44 : FD_FN_CONST static inline ulong
45 0 : scratch_align( void ) {
46 0 : return alignof( fd_accdb_tile_ctx_t );
47 0 : }
48 :
49 : FD_FN_PURE static inline ulong
50 0 : scratch_footprint( fd_topo_tile_t const * tile ) {
51 0 : ulong l = FD_LAYOUT_INIT;
52 0 : l = FD_LAYOUT_APPEND( l, alignof( fd_accdb_tile_ctx_t ), sizeof( fd_accdb_tile_ctx_t ) );
53 0 : l = FD_LAYOUT_APPEND( l, fd_accdb_align(), fd_accdb_footprint( tile->accdb.max_live_slots, 1 ) );
54 0 : return FD_LAYOUT_FINI( l, scratch_align() );
55 0 : }
56 :
57 : static inline void
58 0 : metrics_write( fd_accdb_tile_ctx_t * ctx ) {
59 0 : fd_accdb_flush_metrics( ctx->accdb );
60 :
61 0 : fd_accdb_shmem_metrics_t const * metrics = fd_accdb_shmetrics( ctx->accdb );
62 :
63 0 : FD_MGAUGE_SET( ACCDB, ACCOUNT_USED, metrics->accounts_total );
64 0 : FD_MGAUGE_SET( ACCDB, ACCOUNT_CAPACITY, metrics->accounts_capacity );
65 0 : FD_MCNT_SET( ACCDB, ACCOUNT_RELOCATED, metrics->accounts_relocated );
66 0 : FD_MGAUGE_SET( ACCDB, DISK_ALLOCATED_BYTES, metrics->disk_allocated_bytes );
67 0 : FD_MGAUGE_SET( ACCDB, DISK_CONSUMED_BYTES, metrics->disk_current_bytes );
68 0 : FD_MGAUGE_SET( ACCDB, DISK_USED_BYTES, metrics->disk_used_bytes );
69 0 : FD_MGAUGE_SET( ACCDB, IS_COMPACTING, (ulong)metrics->in_compaction );
70 0 : FD_MCNT_SET( ACCDB, COMPACTION_REQUESTED, metrics->compactions_requested );
71 0 : FD_MCNT_SET( ACCDB, COMPACTION_COMPLETED, metrics->compactions_completed );
72 0 : FD_MCNT_SET( ACCDB, ACCOUNT_RELOCATED_BYTES, metrics->accounts_relocated_bytes );
73 :
74 0 : fd_accdb_metrics_t const * rt = fd_accdb_metrics( ctx->accdb );
75 0 : FD_MCNT_ENUM_COPY( ACCDB, ACCOUNT_PREEVICTED, rt->accounts_preevicted_per_class );
76 0 : FD_MCNT_SET( ACCDB, DISK_READ_BYTES, rt->bytes_read );
77 0 : FD_MCNT_SET( ACCDB, DISK_WRITTEN_BYTES, rt->bytes_written );
78 0 : FD_MCNT_SET( ACCDB, WRITEBACK_WRITTEN, rt->write_ops );
79 0 : FD_MCNT_SET( ACCDB, COMPACTION_WRITTEN, rt->copy_ops );
80 0 : FD_MCNT_SET( ACCDB, ACCOUNT_VERSION_REMOVED, rt->accounts_deleted );
81 :
82 0 : ulong cache_used [ FD_ACCDB_CACHE_CLASS_CNT ];
83 0 : ulong cache_max [ FD_ACCDB_CACHE_CLASS_CNT ];
84 0 : ulong cache_reserved[ FD_ACCDB_CACHE_CLASS_CNT ];
85 0 : fd_accdb_cache_class_occupancy( ctx->accdb, cache_used, cache_max, cache_reserved );
86 0 : FD_MGAUGE_ENUM_COPY( ACCDB, CACHE_CLASS_USED, cache_used );
87 0 : FD_MGAUGE_ENUM_COPY( ACCDB, CACHE_CLASS_CAPACITY, cache_max );
88 0 : ulong cache_tracked[ FD_ACCDB_CACHE_CLASS_CNT ];
89 0 : for( ulong c=0UL; c<FD_ACCDB_CACHE_CLASS_CNT; c++ ) {
90 0 : cache_tracked [ c ] = (ulong)( cache_reserved[ c ]!=ULONG_MAX );
91 0 : cache_reserved[ c ] = fd_ulong_if( cache_reserved[ c ]!=ULONG_MAX, cache_reserved[ c ], 0UL );
92 0 : }
93 0 : FD_MGAUGE_ENUM_COPY( ACCDB, CACHE_CLASS_RESERVED, cache_reserved );
94 0 : FD_MGAUGE_ENUM_COPY( ACCDB, IS_CACHE_CLASS_RESERVATION_TRACKED, cache_tracked );
95 :
96 0 : ulong cache_free_target [ FD_ACCDB_CACHE_CLASS_CNT ];
97 0 : ulong cache_free_low_water[ FD_ACCDB_CACHE_CLASS_CNT ];
98 0 : fd_accdb_cache_class_thresholds( ctx->accdb, cache_free_target, cache_free_low_water );
99 0 : FD_MGAUGE_ENUM_COPY( ACCDB, CACHE_CLASS_FREE_TARGET, cache_free_target );
100 0 : FD_MGAUGE_ENUM_COPY( ACCDB, CACHE_CLASS_FREE_LOW_WATER, cache_free_low_water );
101 :
102 0 : FD_MCNT_ENUM_COPY( ACCDB, PROGCACHE_PREEVICTED, ctx->progcache_metrics->evict_per_class );
103 0 : FD_MCNT_SET ( ACCDB, PROGCACHE_PREEVICTED_BYTES, ctx->progcache_metrics->evict_tot_sz );
104 0 : }
105 :
106 : static inline void
107 : before_credit( fd_accdb_tile_ctx_t * ctx,
108 : fd_stem_context_t * stem FD_FN_UNUSED,
109 0 : int * charge_busy ) {
110 : /* Commands are serviced even before replay starts, so a poster can
111 : never be delayed by the boot gate; the gate only idles the spin
112 : while there is no work. */
113 0 : fd_accdb_background( ctx->accdb, charge_busy );
114 0 : if( FD_LIKELY( ctx->progcache_enabled ) ) *charge_busy |= !!fd_progcache_housekeeping( ctx->progcache, ctx->progcache_metrics );
115 0 : ctx->busy = *charge_busy;
116 0 : if( FD_LIKELY( *charge_busy ) ) fd_startup_gate_busy( ctx->startup_gate );
117 0 : else fd_startup_gate_idle( ctx->startup_gate );
118 0 : }
119 :
120 : static inline int
121 0 : prevent_park( fd_accdb_tile_ctx_t * ctx ) {
122 0 : return ctx->busy;
123 0 : }
124 :
125 : static void
126 : privileged_init( fd_topo_t const * topo,
127 0 : fd_topo_tile_t const * tile ) {
128 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
129 :
130 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
131 0 : fd_accdb_tile_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_accdb_tile_ctx_t ), sizeof( fd_accdb_tile_ctx_t ) );
132 0 : FD_TEST( fd_rng_secure( &ctx->seed, 8U ) );
133 0 : }
134 :
135 : static void
136 : unprivileged_init( fd_topo_t const * topo,
137 0 : fd_topo_tile_t const * tile ) {
138 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
139 :
140 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
141 0 : fd_accdb_tile_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_accdb_tile_ctx_t ), sizeof( fd_accdb_tile_ctx_t ) );
142 0 : void * _accdb = FD_SCRATCH_ALLOC_APPEND( l, fd_accdb_align(), fd_accdb_footprint( tile->accdb.max_live_slots, 1 ) );
143 :
144 0 : void * _accdb_shmem = fd_topo_obj_laddr( topo, tile->accdb.accdb_obj_id );
145 0 : fd_accdb_shmem_t * accdb_shmem = fd_accdb_shmem_join( _accdb_shmem );
146 0 : FD_TEST( accdb_shmem );
147 :
148 : /* Gather per-RO-consumer epoch fseq pointers. Each enabled RO
149 : consumer has a private fseq it owns RW; we map it RO and pass
150 : the pointer to fd_accdb_new so the compaction tile's
151 : deferred-free reclamation will wait on it. */
152 0 : static ulong const * external_epoch_slots[ FD_ACCDB_TILE_MAX_EXTERNAL_EPOCHS ];
153 0 : ulong external_epoch_cnt = 0UL;
154 0 : if( FD_LIKELY( tile->accdb.rpc_epoch_obj_id!=ULONG_MAX ) ) {
155 0 : ulong * fseq = fd_fseq_join( fd_topo_obj_laddr( topo, tile->accdb.rpc_epoch_obj_id ) );
156 0 : FD_TEST( fseq );
157 0 : FD_TEST( external_epoch_cnt<FD_ACCDB_TILE_MAX_EXTERNAL_EPOCHS );
158 0 : external_epoch_slots[ external_epoch_cnt++ ] = fseq;
159 0 : }
160 0 : for( ulong i=0UL; i<tile->accdb.resolv_epoch_obj_cnt; i++ ) {
161 0 : ulong * fseq = fd_fseq_join( fd_topo_obj_laddr( topo, tile->accdb.resolv_epoch_obj_ids[ i ] ) );
162 0 : FD_TEST( fseq );
163 0 : FD_TEST( external_epoch_cnt<FD_ACCDB_TILE_MAX_EXTERNAL_EPOCHS );
164 0 : external_epoch_slots[ external_epoch_cnt++ ] = fseq;
165 0 : }
166 0 : if( FD_UNLIKELY( tile->accdb.snapmk_epoch_obj_id!=ULONG_MAX ) ) {
167 0 : ulong * fseq = fd_fseq_join( fd_topo_obj_laddr( topo, tile->accdb.snapmk_epoch_obj_id ) );
168 0 : FD_TEST( fseq );
169 0 : FD_TEST( external_epoch_cnt<FD_ACCDB_TILE_MAX_EXTERNAL_EPOCHS );
170 0 : external_epoch_slots[ external_epoch_cnt++ ] = fseq;
171 0 : }
172 0 : for( ulong i=0UL; i<tile->accdb.snapzp_epoch_obj_cnt; i++ ) {
173 0 : ulong obj_id = tile->accdb.snapzp_epoch_obj_ids[ i ];
174 0 : if( FD_UNLIKELY( obj_id==ULONG_MAX ) ) continue;
175 0 : ulong * fseq = fd_fseq_join( fd_topo_obj_laddr( topo, tile->accdb.snapzp_epoch_obj_ids[ i ] ) );
176 0 : FD_TEST( fseq );
177 0 : FD_TEST( external_epoch_cnt<FD_ACCDB_TILE_MAX_EXTERNAL_EPOCHS );
178 0 : external_epoch_slots[ external_epoch_cnt++ ] = fseq;
179 0 : }
180 :
181 0 : ctx->accdb = fd_accdb_join( fd_accdb_new( _accdb, accdb_shmem, FD_ACCDB_FD_RW, external_epoch_cnt, external_epoch_slots, NULL, 0UL, 1 ) );
182 0 : FD_TEST( ctx->accdb );
183 :
184 0 : ulong progcache_obj_id = fd_pod_query_ulong( topo->props, "progcache", ULONG_MAX );
185 0 : ctx->progcache_enabled = ( progcache_obj_id!=ULONG_MAX );
186 0 : if( FD_LIKELY( ctx->progcache_enabled ) ) FD_TEST( fd_progcache_shmem_join( ctx->progcache, fd_topo_obj_laddr( topo, progcache_obj_id ) ) );
187 0 : memset( ctx->progcache_metrics, 0, sizeof(ctx->progcache_metrics) );
188 :
189 0 : fd_startup_gate_init( ctx->startup_gate, topo, tile->in_cnt );
190 :
191 0 : ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, 1UL );
192 0 : if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
193 0 : FD_LOG_ERR(( "scratch overflow %lu %lu %lu", scratch_top - (ulong)scratch - scratch_footprint( tile ), scratch_top, (ulong)scratch + scratch_footprint( tile ) ));
194 0 : }
195 :
196 : static ulong
197 : populate_allowed_seccomp( fd_topo_t const * topo,
198 : fd_topo_tile_t const * tile,
199 : ulong out_cnt,
200 0 : struct sock_filter * out ) {
201 0 : (void)topo; (void)tile;
202 0 : populate_sock_filter_policy_fd_accdb_tile( out_cnt, out, (uint)fd_log_private_logfile_fd(), (uint)FD_ACCDB_FD_RW );
203 0 : return sock_filter_policy_fd_accdb_tile_instr_cnt;
204 0 : }
205 :
206 : static ulong
207 : populate_allowed_fds( fd_topo_t const * topo,
208 : fd_topo_tile_t const * tile,
209 : ulong out_fds_cnt,
210 0 : int * out_fds ) {
211 0 : (void)topo; (void)tile;
212 :
213 0 : if( FD_UNLIKELY( out_fds_cnt<3UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
214 :
215 0 : ulong out_cnt = 0UL;
216 0 : out_fds[ out_cnt++ ] = 2; /* stderr */
217 0 : if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) )
218 0 : out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
219 0 : out_fds[ out_cnt++ ] = FD_ACCDB_FD_RW; /* accounts db fd */
220 0 : return out_cnt;
221 0 : }
222 :
223 0 : #define STEM_BURST (1UL)
224 0 : #define STEM_LAZY (128L*3000L)
225 :
226 0 : #define STEM_CALLBACK_CONTEXT_TYPE fd_accdb_tile_ctx_t
227 0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_accdb_tile_ctx_t)
228 :
229 0 : #define STEM_CALLBACK_METRICS_WRITE metrics_write
230 : #define STEM_CALLBACK_PREVENT_PARK prevent_park
231 0 : #define STEM_CALLBACK_BEFORE_CREDIT before_credit
232 :
233 : #include "../../disco/stem/fd_stem.c"
234 :
235 : static ulong
236 0 : max_event_sz( fd_topo_tile_t const * tile FD_PARAM_UNUSED ) {
237 0 : return sizeof(fd_event_accdb_compaction_completed_t) > sizeof(fd_event_accdb_partition_added_t) ?
238 0 : sizeof(fd_event_accdb_compaction_completed_t) : sizeof(fd_event_accdb_partition_added_t);
239 0 : }
240 :
241 : fd_topo_run_tile_t fd_tile_accdb = {
242 : .name = "accdb",
243 : .populate_allowed_seccomp = populate_allowed_seccomp,
244 : .populate_allowed_fds = populate_allowed_fds,
245 : .scratch_align = scratch_align,
246 : .scratch_footprint = scratch_footprint,
247 : .privileged_init = privileged_init,
248 : .unprivileged_init = unprivileged_init,
249 : .max_event_sz = max_event_sz,
250 : .run = stem_run,
251 : };
|