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