Line data Source code
1 : #include "fd_metrics_sample.h" 2 : 3 : int 4 : fd_metrics_sample_encode( fd_pb_encoder_t * encoder, 5 : fd_metrics_sample_schema_t const * s, 6 : ulong const volatile * area, 7 0 : double ns_per_tick ) { 8 0 : int ok = 1; 9 0 : for( ulong i=0UL; i<s->field_cnt; i++ ) { 10 0 : fd_metrics_sample_field_t const * f = s->field+i; 11 0 : switch( f->kind ) { 12 0 : case FD_METRICS_SAMPLE_OPEN: 13 0 : if( FD_UNLIKELY( !fd_pb_submsg_open( encoder, f->num ) ) ) return -1; 14 0 : continue; 15 0 : case FD_METRICS_SAMPLE_CLOSE: 16 0 : ok &= !!fd_pb_submsg_close( encoder ); 17 0 : continue; 18 0 : default: break; 19 0 : } 20 : 21 0 : ulong v = area[ f->off ]; 22 : /* A zero field is omitted (decodes as 0), except a state: raw 0 is 23 : a real state, while an absent enum decodes as unspecified */ 24 0 : if( !v && f->kind!=FD_METRICS_SAMPLE_STATE ) continue; 25 : 26 0 : switch( f->kind ) { 27 0 : case FD_METRICS_SAMPLE_U64: ok &= !!fd_pb_push_uint64( encoder, f->num, v ); break; 28 0 : case FD_METRICS_SAMPLE_NS: { 29 0 : double ns = (double)v*ns_per_tick; /* saturate: out of range is undefined */ 30 0 : ok &= !!fd_pb_push_uint64( encoder, f->num, ns<0x1p64 ? (ulong)ns : ULONG_MAX ); 31 0 : break; 32 0 : } 33 0 : case FD_METRICS_SAMPLE_BOOL: ok &= !!fd_pb_push_bool( encoder, f->num, 1 ); break; 34 0 : case FD_METRICS_SAMPLE_STATE: if( FD_LIKELY( v<f->map_cnt ) ) ok &= !!fd_pb_push_int32( encoder, f->num, (int)f->map[ v ] ); break; 35 0 : default: FD_LOG_CRIT(( "unknown metrics sample kind %u", f->kind )); 36 0 : } 37 0 : } 38 0 : return ok ? 0 : -1; 39 0 : }