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