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 : /* Requests are serviced even before replay starts. A request posted
111 : during the startup gate's idle sleep waits until the next pass. */
112 0 : fd_accdb_background( ctx->accdb, charge_busy );
113 0 : if( FD_LIKELY( ctx->progcache_enabled ) ) *charge_busy |= !!fd_progcache_housekeeping( ctx->progcache, ctx->progcache_metrics );
114 0 : ctx->busy = *charge_busy;
115 0 : if( FD_LIKELY( *charge_busy ) ) fd_startup_gate_busy( ctx->startup_gate );
116 0 : else fd_startup_gate_idle( ctx->startup_gate );
117 0 : }
118 :
119 : static inline int
120 0 : prevent_park( fd_accdb_tile_ctx_t * ctx ) {
121 0 : return ctx->busy;
122 0 : }
123 :
124 : static void
125 : privileged_init( fd_topo_t const * topo,
126 0 : fd_topo_tile_t const * tile ) {
127 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
128 :
129 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
130 0 : fd_accdb_tile_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_accdb_tile_ctx_t ), sizeof( fd_accdb_tile_ctx_t ) );
131 0 : FD_TEST( fd_rng_secure( &ctx->seed, 8U ) );
132 0 : }
133 :
134 : static void
135 : unprivileged_init( fd_topo_t const * topo,
136 0 : fd_topo_tile_t const * tile ) {
137 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
138 :
139 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
140 0 : fd_accdb_tile_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_accdb_tile_ctx_t ), sizeof( fd_accdb_tile_ctx_t ) );
141 0 : void * _accdb = FD_SCRATCH_ALLOC_APPEND( l, fd_accdb_align(), fd_accdb_footprint( tile->accdb.max_live_slots, 1 ) );
142 :
143 0 : void * _accdb_shmem = fd_topo_obj_laddr( topo, tile->accdb.accdb_obj_id );
144 0 : fd_accdb_shmem_t * accdb_shmem = fd_accdb_shmem_join( _accdb_shmem );
145 0 : FD_TEST( accdb_shmem );
146 :
147 : /* Gather per-RO-consumer epoch fseq pointers. Each enabled RO
148 : consumer has a private fseq it owns RW; we map it RO and pass
149 : the pointer to fd_accdb_new so the compaction tile's
150 : deferred-free reclamation will wait on it. */
151 0 : static ulong const * external_epoch_slots[ FD_ACCDB_TILE_MAX_EXTERNAL_EPOCHS ];
152 0 : ulong external_epoch_cnt = 0UL;
153 0 : if( FD_LIKELY( tile->accdb.rpc_epoch_obj_id!=ULONG_MAX ) ) {
154 0 : ulong * fseq = fd_fseq_join( fd_topo_obj_laddr( topo, tile->accdb.rpc_epoch_obj_id ) );
155 0 : FD_TEST( fseq );
156 0 : FD_TEST( external_epoch_cnt<FD_ACCDB_TILE_MAX_EXTERNAL_EPOCHS );
157 0 : external_epoch_slots[ external_epoch_cnt++ ] = fseq;
158 0 : }
159 0 : for( ulong i=0UL; i<tile->accdb.resolv_epoch_obj_cnt; i++ ) {
160 0 : ulong * fseq = fd_fseq_join( fd_topo_obj_laddr( topo, tile->accdb.resolv_epoch_obj_ids[ i ] ) );
161 0 : FD_TEST( fseq );
162 0 : FD_TEST( external_epoch_cnt<FD_ACCDB_TILE_MAX_EXTERNAL_EPOCHS );
163 0 : external_epoch_slots[ external_epoch_cnt++ ] = fseq;
164 0 : }
165 0 : if( FD_UNLIKELY( tile->accdb.snapmk_epoch_obj_id!=ULONG_MAX ) ) {
166 0 : ulong * fseq = fd_fseq_join( fd_topo_obj_laddr( topo, tile->accdb.snapmk_epoch_obj_id ) );
167 0 : FD_TEST( fseq );
168 0 : FD_TEST( external_epoch_cnt<FD_ACCDB_TILE_MAX_EXTERNAL_EPOCHS );
169 0 : external_epoch_slots[ external_epoch_cnt++ ] = fseq;
170 0 : }
171 0 : for( ulong i=0UL; i<tile->accdb.snapzp_epoch_obj_cnt; i++ ) {
172 0 : ulong obj_id = tile->accdb.snapzp_epoch_obj_ids[ i ];
173 0 : if( FD_UNLIKELY( obj_id==ULONG_MAX ) ) continue;
174 0 : ulong * fseq = fd_fseq_join( fd_topo_obj_laddr( topo, tile->accdb.snapzp_epoch_obj_ids[ i ] ) );
175 0 : FD_TEST( fseq );
176 0 : FD_TEST( external_epoch_cnt<FD_ACCDB_TILE_MAX_EXTERNAL_EPOCHS );
177 0 : external_epoch_slots[ external_epoch_cnt++ ] = fseq;
178 0 : }
179 :
180 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 ) );
181 0 : FD_TEST( ctx->accdb );
182 :
183 0 : ulong progcache_obj_id = fd_pod_query_ulong( topo->props, "progcache", ULONG_MAX );
184 0 : ctx->progcache_enabled = ( progcache_obj_id!=ULONG_MAX );
185 0 : if( FD_LIKELY( ctx->progcache_enabled ) ) FD_TEST( fd_progcache_shmem_join( ctx->progcache, fd_topo_obj_laddr( topo, progcache_obj_id ) ) );
186 0 : memset( ctx->progcache_metrics, 0, sizeof(ctx->progcache_metrics) );
187 :
188 0 : fd_startup_gate_init( ctx->startup_gate, topo, tile->in_cnt );
189 :
190 0 : ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, 1UL );
191 0 : if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
192 0 : FD_LOG_ERR(( "scratch overflow %lu %lu %lu", scratch_top - (ulong)scratch - scratch_footprint( tile ), scratch_top, (ulong)scratch + scratch_footprint( tile ) ));
193 0 : }
194 :
195 : static ulong
196 : populate_allowed_seccomp( fd_topo_t const * topo,
197 : fd_topo_tile_t const * tile,
198 : ulong out_cnt,
199 0 : struct sock_filter * out ) {
200 0 : (void)topo; (void)tile;
201 0 : populate_sock_filter_policy_fd_accdb_tile( out_cnt, out, (uint)fd_log_private_logfile_fd(), (uint)FD_ACCDB_FD_RW );
202 0 : return sock_filter_policy_fd_accdb_tile_instr_cnt;
203 0 : }
204 :
205 : static ulong
206 : populate_allowed_fds( fd_topo_t const * topo,
207 : fd_topo_tile_t const * tile,
208 : ulong out_fds_cnt,
209 0 : int * out_fds ) {
210 0 : (void)topo; (void)tile;
211 :
212 0 : if( FD_UNLIKELY( out_fds_cnt<3UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
213 :
214 0 : ulong out_cnt = 0UL;
215 0 : out_fds[ out_cnt++ ] = 2; /* stderr */
216 0 : if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) )
217 0 : out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
218 0 : out_fds[ out_cnt++ ] = FD_ACCDB_FD_RW; /* accounts db fd */
219 0 : return out_cnt;
220 0 : }
221 :
222 0 : #define STEM_BURST (1UL)
223 0 : #define STEM_LAZY (128L*3000L)
224 :
225 0 : #define STEM_CALLBACK_CONTEXT_TYPE fd_accdb_tile_ctx_t
226 0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_accdb_tile_ctx_t)
227 :
228 0 : #define STEM_CALLBACK_METRICS_WRITE metrics_write
229 : #define STEM_CALLBACK_PREVENT_PARK prevent_park
230 0 : #define STEM_CALLBACK_BEFORE_CREDIT before_credit
231 :
232 : #include "../../disco/stem/fd_stem.c"
233 :
234 : static ulong
235 0 : max_event_sz( fd_topo_tile_t const * tile FD_PARAM_UNUSED ) {
236 0 : return sizeof(fd_event_accdb_compaction_completed_t) > sizeof(fd_event_accdb_partition_added_t) ?
237 0 : sizeof(fd_event_accdb_compaction_completed_t) : sizeof(fd_event_accdb_partition_added_t);
238 0 : }
239 :
240 : fd_topo_run_tile_t fd_tile_accdb = {
241 : .name = "accdb",
242 : .populate_allowed_seccomp = populate_allowed_seccomp,
243 : .populate_allowed_fds = populate_allowed_fds,
244 : .scratch_align = scratch_align,
245 : .scratch_footprint = scratch_footprint,
246 : .privileged_init = privileged_init,
247 : .unprivileged_init = unprivileged_init,
248 : .max_event_sz = max_event_sz,
249 : .run = stem_run,
250 : };
|