Line data Source code
1 : #include "../metrics/fd_metrics.h"
2 : #include "../stem/fd_stem.h"
3 : #include "../topo/fd_topo.h"
4 :
5 : #include <fcntl.h>
6 : #include <errno.h>
7 : #include <sys/types.h> /* SEEK_SET */
8 : #include <time.h>
9 : #include <unistd.h>
10 :
11 : #include "generated/fd_cswtch_tile_seccomp.h"
12 :
13 0 : #define REPORT_INTERVAL_MILLIS (100L)
14 :
15 : typedef struct {
16 : long next_report_nanos;
17 :
18 : ulong tile_cnt;
19 : long first_seen_died[ FD_TILE_MAX ];
20 : int status_fds[ FD_TILE_MAX ];
21 : volatile ulong * metrics[ FD_TILE_MAX ];
22 : } fd_cswtch_ctx_t;
23 :
24 : FD_FN_CONST static inline ulong
25 3 : scratch_align( void ) {
26 3 : return 128UL;
27 3 : }
28 :
29 : FD_FN_PURE static inline ulong
30 3 : scratch_footprint( fd_topo_tile_t const * tile ) {
31 3 : (void)tile;
32 3 : ulong l = FD_LAYOUT_INIT;
33 3 : l = FD_LAYOUT_APPEND( l, alignof( fd_cswtch_ctx_t ), sizeof( fd_cswtch_ctx_t ) );
34 3 : return FD_LAYOUT_FINI( l, scratch_align() );
35 3 : }
36 :
37 : static void
38 : before_credit( fd_cswtch_ctx_t * ctx,
39 : fd_stem_context_t * mux,
40 0 : int * charge_busy ) {
41 0 : (void)mux;
42 :
43 0 : long now = fd_log_wallclock();
44 0 : if( now<ctx->next_report_nanos ) {
45 0 : long diff = ctx->next_report_nanos - now;
46 0 : diff = fd_long_min( diff, 2e6 /* 2ms */ );
47 0 : struct timespec const ts = {
48 0 : .tv_sec = diff / (long)1e9,
49 0 : .tv_nsec = diff % (long)1e9
50 0 : };
51 0 : clock_nanosleep( CLOCK_REALTIME, 0, &ts, NULL );
52 0 : return;
53 0 : }
54 0 : ctx->next_report_nanos += REPORT_INTERVAL_MILLIS*1000L*1000L;
55 :
56 0 : *charge_busy = 1;
57 :
58 0 : for( ulong i=0UL; i<ctx->tile_cnt; i++ ) {
59 0 : if( FD_UNLIKELY( -1==lseek( ctx->status_fds[ i ], 0, SEEK_SET ) ) ) FD_LOG_ERR(( "lseek failed (%i-%s)", errno, strerror( errno ) ));
60 :
61 0 : char contents[ 4096 ] = {0};
62 0 : ulong contents_len = 0UL;
63 :
64 0 : int process_died = 0;
65 0 : while( 1 ) {
66 0 : if( FD_UNLIKELY( contents_len>=sizeof( contents ) ) ) FD_LOG_ERR(( "contents overflow" ));
67 0 : long n = read( ctx->status_fds[ i ], contents + contents_len, sizeof( contents ) - contents_len );
68 0 : if( FD_UNLIKELY( -1==n ) ) {
69 0 : if( FD_UNLIKELY( errno==ESRCH ) ) {
70 0 : process_died = 1;
71 0 : break;
72 0 : }
73 0 : FD_LOG_ERR(( "read failed (%i-%s)", errno, strerror( errno ) ));
74 0 : }
75 0 : if( FD_LIKELY( 0==n ) ) break;
76 0 : contents_len += (ulong)n;
77 0 : }
78 :
79 : /* Supervisor is going to bring the whole process tree down if any
80 : of the target PIDs died, so we can ignore this and wait. */
81 0 : if( FD_UNLIKELY( process_died ) ) {
82 0 : if( FD_UNLIKELY( !ctx->first_seen_died[ i ] ) ) {
83 0 : ctx->first_seen_died[ i ] = now;
84 0 : } else if( FD_LIKELY( ctx->first_seen_died[ i ]==LONG_MAX ) ) {
85 : /* We already reported this, so we can ignore it. */
86 0 : } else if( FD_UNLIKELY( now-ctx->first_seen_died[ i ] < 10L*1000L*1000L*1000L ) ) {
87 : /* Wait 10 seconds for supervisor to kill us before reporting WARNING */
88 0 : } else {
89 0 : FD_LOG_WARNING(( "cannot get context switch metrics for dead tile idx %lu", i ));
90 0 : ctx->first_seen_died[ i ] = LONG_MAX;
91 0 : }
92 0 : continue;
93 0 : }
94 :
95 0 : int found_voluntary = 0;
96 0 : int found_involuntary = 0;
97 :
98 0 : char * line = contents;
99 0 : while( 1 ) {
100 0 : char * next_line = strchr( line, '\n' );
101 0 : if( FD_UNLIKELY( NULL==next_line ) ) break;
102 0 : *next_line = '\0';
103 :
104 0 : char * colon = strchr( line, ':' );
105 0 : if( FD_UNLIKELY( NULL==colon ) ) FD_LOG_ERR(( "no colon in line '%s'", line ));
106 :
107 0 : *colon = '\0';
108 0 : char * key = line;
109 0 : char * value = colon + 1;
110 :
111 0 : while( ' '==*value || '\t'==*value ) value++;
112 :
113 0 : if( FD_LIKELY( !strncmp( key, "voluntary_ctxt_switches", 23UL ) ) ) {
114 0 : char * endptr;
115 0 : ulong voluntary_ctxt_switches = strtoul( value, &endptr, 10 );
116 0 : if( FD_UNLIKELY( *endptr!='\0' || voluntary_ctxt_switches==ULONG_MAX ) ) FD_LOG_ERR(( "strtoul failed" ));
117 0 : ctx->metrics[ i ][ FD_METRICS_COUNTER_TILE_CONTEXT_SWITCH_VOLUNTARY_COUNT_OFF ] = voluntary_ctxt_switches;
118 0 : found_voluntary = 1;
119 0 : } else if( FD_LIKELY( !strncmp( key, "nonvoluntary_ctxt_switches", 26UL ) ) ) {
120 0 : char * endptr;
121 0 : ulong involuntary_ctxt_switches = strtoul( value, &endptr, 10 );
122 0 : if( FD_UNLIKELY( *endptr!='\0' || involuntary_ctxt_switches==ULONG_MAX ) ) FD_LOG_ERR(( "strtoul failed" ));
123 0 : ctx->metrics[ i ][ FD_METRICS_COUNTER_TILE_CONTEXT_SWITCH_INVOLUNTARY_COUNT_OFF ] = involuntary_ctxt_switches;
124 0 : found_involuntary = 1;
125 0 : }
126 :
127 0 : line = next_line + 1;
128 0 : }
129 :
130 0 : if( FD_UNLIKELY( !found_voluntary ) ) FD_LOG_ERR(( "voluntary_ctxt_switches not found" ));
131 0 : if( FD_UNLIKELY( !found_involuntary ) ) FD_LOG_ERR(( "nonvoluntary_ctxt_switches not found" ));
132 0 : }
133 0 : }
134 :
135 : static void
136 : privileged_init( fd_topo_t * topo,
137 0 : fd_topo_tile_t * 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_cswtch_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_cswtch_ctx_t ), sizeof( fd_cswtch_ctx_t ) );
142 :
143 0 : FD_TEST( topo->tile_cnt<FD_TILE_MAX );
144 :
145 0 : ctx->tile_cnt = topo->tile_cnt;
146 0 : for( ulong i=0UL; i<topo->tile_cnt; i++ ) {
147 0 : ulong * metrics = fd_metrics_join( fd_topo_obj_laddr( topo, topo->tiles[ i ].metrics_obj_id ) );
148 :
149 0 : for(;;) {
150 0 : ulong pid, tid;
151 0 : if( FD_UNLIKELY( tile->id==i ) ) {
152 0 : pid = fd_sandbox_getpid();
153 0 : tid = fd_sandbox_gettid();
154 0 : } else {
155 0 : pid = fd_metrics_tile( metrics )[ FD_METRICS_GAUGE_TILE_PID_OFF ];
156 0 : tid = fd_metrics_tile( metrics )[ FD_METRICS_GAUGE_TILE_TID_OFF ];
157 0 : if( FD_UNLIKELY( !pid || !tid ) ) {
158 0 : FD_SPIN_PAUSE();
159 0 : continue;
160 0 : }
161 0 : }
162 :
163 0 : char path[ 64 ];
164 0 : FD_TEST( fd_cstr_printf_check( path, sizeof( path ), NULL, "/proc/%lu/task/%lu/status", pid, tid ) );
165 0 : ctx->status_fds[ i ] = open( path, O_RDONLY );
166 0 : ctx->metrics[ i ] = fd_metrics_tile( metrics );
167 0 : if( FD_UNLIKELY( -1==ctx->status_fds[ i ] ) ) FD_LOG_ERR(( "open failed (%i-%s)", errno, strerror( errno ) ));
168 0 : break;
169 0 : }
170 0 : }
171 0 : }
172 :
173 : static void
174 : unprivileged_init( fd_topo_t * topo,
175 0 : fd_topo_tile_t * tile ) {
176 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
177 :
178 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
179 0 : fd_cswtch_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_cswtch_ctx_t ), sizeof( fd_cswtch_ctx_t ) );
180 :
181 0 : memset( ctx->first_seen_died, 0, sizeof( ctx->first_seen_died ) );
182 0 : ctx->next_report_nanos = fd_log_wallclock();
183 :
184 0 : ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, 1UL );
185 0 : if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
186 0 : FD_LOG_ERR(( "scratch overflow %lu %lu %lu", scratch_top - (ulong)scratch - scratch_footprint( tile ), scratch_top, (ulong)scratch + scratch_footprint( tile ) ));
187 0 : }
188 :
189 : static ulong
190 : populate_allowed_seccomp( fd_topo_t const * topo,
191 : fd_topo_tile_t const * tile,
192 : ulong out_cnt,
193 0 : struct sock_filter * out ) {
194 0 : (void)topo;
195 0 : (void)tile;
196 :
197 0 : populate_sock_filter_policy_fd_cswtch_tile( out_cnt, out, (uint)fd_log_private_logfile_fd() );
198 0 : return sock_filter_policy_fd_cswtch_tile_instr_cnt;
199 0 : }
200 :
201 : static ulong
202 : populate_allowed_fds( fd_topo_t const * topo,
203 : fd_topo_tile_t const * tile,
204 : ulong out_fds_cnt,
205 0 : int * out_fds ) {
206 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
207 :
208 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
209 0 : fd_cswtch_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_cswtch_ctx_t ), sizeof( fd_cswtch_ctx_t ) );
210 :
211 0 : if( FD_UNLIKELY( out_fds_cnt<2UL+ctx->tile_cnt ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
212 :
213 0 : ulong out_cnt = 0UL;
214 0 : out_fds[ out_cnt++ ] = 2; /* stderr */
215 0 : if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) )
216 0 : out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
217 0 : for( ulong i=0UL; i<ctx->tile_cnt; i++ )
218 0 : out_fds[ out_cnt++ ] = ctx->status_fds[ i ]; /* /proc/<pid>/task/<tid>/status descriptor */
219 0 : return out_cnt;
220 0 : }
221 :
222 0 : #define STEM_BURST (1UL)
223 0 : #define STEM_LAZY ((long)10e6) /* 10ms */
224 :
225 0 : #define STEM_CALLBACK_CONTEXT_TYPE fd_cswtch_ctx_t
226 0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_cswtch_ctx_t)
227 :
228 0 : #define STEM_CALLBACK_BEFORE_CREDIT before_credit
229 :
230 : #include "../../disco/stem/fd_stem.c"
231 :
232 : fd_topo_run_tile_t fd_tile_cswtch = {
233 : .name = "cswtch",
234 : .populate_allowed_seccomp = populate_allowed_seccomp,
235 : .populate_allowed_fds = populate_allowed_fds,
236 : .scratch_align = scratch_align,
237 : .scratch_footprint = scratch_footprint,
238 : .privileged_init = privileged_init,
239 : .unprivileged_init = unprivileged_init,
240 : .run = stem_run,
241 : };
|