Line data Source code
1 : #include "helper.h"
2 :
3 : #include <stdio.h>
4 :
5 0 : #define PRINT( ... ) do { \
6 0 : int n = snprintf( *buf, *buf_sz, __VA_ARGS__ ); \
7 0 : if( FD_UNLIKELY( n<0 ) ) FD_LOG_ERR(( "snprintf failed" )); \
8 0 : if( FD_UNLIKELY( (ulong)n>=*buf_sz ) ) FD_LOG_ERR(( "snprintf truncated" )); \
9 0 : *buf += n; *buf_sz -= (ulong)n; \
10 0 : } while(0)
11 :
12 : void
13 : printf_age( char ** buf,
14 : ulong * buf_sz,
15 0 : long _dt ) {
16 0 : if( FD_UNLIKELY( _dt< 0L ) ) { PRINT( " invalid" ); return; }
17 0 : if( FD_UNLIKELY( _dt==0L ) ) { PRINT( " 0s" ); return; }
18 0 : ulong rem = (ulong)_dt;
19 0 : ulong ns = rem % 1000UL; rem /= 1000UL; if( !rem /*no u*/ ) { PRINT( " %3lun", ns ); return; }
20 0 : ulong us = rem % 1000UL; rem /= 1000UL; if( !rem /*no m*/ ) { PRINT( " %3lu.%03luu", us, ns ); return; }
21 0 : ulong ms = rem % 1000UL; rem /= 1000UL; if( !rem /*no s*/ ) { PRINT( "%3lu.%03lu%02lum", ms, us, ns/10UL ); return; }
22 0 : ulong s = rem % 60UL; rem /= 60UL; if( !rem /*no m*/ ) { PRINT( "%2lu.%03lu%03lus", s, ms, us ); return; }
23 0 : ulong m = rem % 60UL; rem /= 60UL; if( !rem /*no h*/ ) { PRINT( "%2lu:%02lu.%03lu%1lu", m, s, ms, us/100UL ); return; }
24 0 : ulong h = rem % 24UL; rem /= 24UL; if( !rem /*no d*/ ) { PRINT( "%2lu:%02lu:%02lu.%1lu", h, m, s, ms/100UL ); return; }
25 0 : ulong d = rem % 7UL; rem /= 7UL; if( !rem /*no w*/ ) { PRINT( " %1lud %2lu:%02lu", d, h, m ); return; }
26 0 : ulong w = rem; if( w<=99UL ) { PRINT( "%2luw %1lud %2luh", w, d, h ); return; }
27 0 : /* note that this can handle LONG_MAX fine */ PRINT( "%6luw %1lud", w, d );
28 0 : }
29 :
30 : void
31 : printf_stale( char ** buf,
32 : ulong * buf_sz,
33 : long age,
34 0 : long expire ) {
35 0 : if( FD_UNLIKELY( age>expire ) ) {
36 0 : PRINT( TEXT_YELLOW );
37 0 : printf_age( buf, buf_sz, age );
38 0 : PRINT( TEXT_NORMAL );
39 0 : return;
40 0 : }
41 0 : PRINT( TEXT_GREEN " -" TEXT_NORMAL );
42 0 : }
43 :
44 : void
45 : printf_heart( char ** buf,
46 : ulong * buf_sz,
47 : long hb_now,
48 0 : long hb_then ) {
49 0 : long dt = hb_now - hb_then;
50 0 : PRINT( "%s", (dt>0L) ? (TEXT_GREEN " -" TEXT_NORMAL) :
51 0 : (!dt) ? (TEXT_RED " NONE" TEXT_NORMAL) :
52 0 : (TEXT_BLUE "RESET" TEXT_NORMAL) );
53 0 : }
54 :
55 : char const *
56 0 : sig_color( ulong sig ) {
57 0 : switch( sig ) {
58 0 : case FD_CNC_SIGNAL_BOOT: return TEXT_BLUE; /* Blue -> waiting for tile to start */
59 0 : case FD_CNC_SIGNAL_HALT: return TEXT_YELLOW; /* Yellow -> waiting for tile to process */
60 0 : case FD_CNC_SIGNAL_RUN: return TEXT_GREEN; /* Green -> Normal */
61 0 : case FD_CNC_SIGNAL_FAIL: return TEXT_RED; /* Red -> Definitely abnormal */
62 0 : default: break; /* Unknown, don't colorize */
63 0 : }
64 0 : return TEXT_NORMAL;
65 0 : }
66 :
67 : void
68 : printf_sig( char ** buf,
69 : ulong * buf_sz,
70 : ulong sig_now,
71 0 : ulong sig_then ) {
72 0 : char buf0[ FD_CNC_SIGNAL_CSTR_BUF_MAX ];
73 0 : char buf1[ FD_CNC_SIGNAL_CSTR_BUF_MAX ];
74 0 : PRINT( "%s%4s" TEXT_NORMAL "(%s%4s" TEXT_NORMAL ")",
75 0 : sig_color( sig_now ), fd_cnc_signal_cstr( sig_now, buf0 ),
76 0 : sig_color( sig_then ), fd_cnc_signal_cstr( sig_then, buf1 ) );
77 0 : }
78 :
79 : void
80 : printf_err_bool( char ** buf,
81 : ulong * buf_sz,
82 : ulong err_now,
83 0 : ulong err_then ) {
84 0 : PRINT( "%5s(%5s)", err_now ? TEXT_RED "err" TEXT_NORMAL : TEXT_GREEN " -" TEXT_NORMAL,
85 0 : err_then ? TEXT_RED "err" TEXT_NORMAL : TEXT_GREEN " -" TEXT_NORMAL );
86 0 : }
87 :
88 : void
89 : printf_err_cnt( char ** buf,
90 : ulong * buf_sz,
91 : ulong cnt_now,
92 0 : ulong cnt_then ) {
93 0 : long delta = (long)(cnt_now - cnt_then);
94 : char const * color = (!delta) ? TEXT_GREEN /* no new error counts */
95 : : (delta>0L) ? TEXT_RED /* new error counts */
96 : : (cnt_now) ? TEXT_YELLOW /* decrease of existing error counts?? */
97 0 : : TEXT_BLUE; /* reset of the error counter */
98 0 : if( delta> 99999L ) PRINT( "%10u(%s>+99999" TEXT_NORMAL ")", (uint)cnt_now, color );
99 0 : else if( delta<-99999L ) PRINT( "%10u(%s<-99999" TEXT_NORMAL ")", (uint)cnt_now, color );
100 0 : else PRINT( "%10u(%s %+6li" TEXT_NORMAL ")", (uint)cnt_now, color, delta );
101 0 : }
102 :
103 : void
104 : printf_seq( char ** buf,
105 : ulong * buf_sz,
106 : ulong seq_now,
107 0 : ulong seq_then ) {
108 0 : long delta = (long)(seq_now - seq_then);
109 : char const * color = (!delta) ? TEXT_YELLOW /* no sequence numbers published */
110 : : (delta>0L) ? TEXT_GREEN /* new sequence numbers published */
111 : : (seq_now) ? TEXT_RED /* sequence number went backward */
112 0 : : TEXT_BLUE; /* sequence number reset */
113 0 : if( delta> 99999L ) PRINT( "%10lu(%s>+99999" TEXT_NORMAL ")", seq_now, color );
114 0 : else if( delta<-99999L ) PRINT( "%10lu(%s<-99999" TEXT_NORMAL ")", seq_now, color );
115 0 : else PRINT( "%10lu(%s %+6li" TEXT_NORMAL ")", seq_now, color, delta );
116 0 : }
117 :
118 : void
119 : printf_rate( char ** buf,
120 : ulong * buf_sz,
121 : double cvt,
122 : double overhead,
123 : ulong cnt_now,
124 : ulong cnt_then,
125 0 : long dt ) {
126 0 : if( FD_UNLIKELY( !((0.< cvt ) & (cvt<=DBL_MAX)) |
127 0 : !((0.<=overhead) & (cvt<=DBL_MAX)) |
128 0 : (cnt_now<cnt_then) |
129 0 : (dt<=0L) ) ) {
130 0 : PRINT( TEXT_RED " invalid" TEXT_NORMAL );
131 0 : return;
132 0 : }
133 0 : double rate = cvt*(overhead+(double)(cnt_now-cnt_then)) / (double)dt;
134 0 : if( FD_UNLIKELY( !((0.<=rate) & (rate<=DBL_MAX)) ) ) {
135 0 : PRINT( TEXT_RED "overflow" TEXT_NORMAL );
136 0 : return;
137 0 : }
138 0 : /**/ if( rate<=9999.9 ) { PRINT( " %6.1f ", rate ); return; }
139 0 : rate *= 1e-3; if( rate<=9999.9 ) { PRINT( " %6.1fK", rate ); return; }
140 0 : rate *= 1e-3; if( rate<=9999.9 ) { PRINT( " %6.1fM", rate ); return; }
141 0 : rate *= 1e-3; if( rate<=9999.9 ) { PRINT( " %6.1fG", rate ); return; }
142 0 : rate *= 1e-3; if( rate<=9999.9 ) { PRINT( " %6.1fT", rate ); return; }
143 0 : rate *= 1e-3; if( rate<=9999.9 ) { PRINT( " %6.1fP", rate ); return; }
144 0 : rate *= 1e-3; if( rate<=9999.9 ) { PRINT( " %6.1fE", rate ); return; }
145 0 : rate *= 1e-3; if( rate<=9999.9 ) { PRINT( " %6.1fZ", rate ); return; }
146 0 : rate *= 1e-3; if( rate<=9999.9 ) { PRINT( " %6.1fY", rate ); return; }
147 0 : /**/ PRINT( ">9999.9Y" );
148 0 : }
149 :
150 : void
151 : printf_pct( char ** buf,
152 : ulong * buf_sz,
153 : ulong num_now,
154 : ulong num_then,
155 : double lhopital_num,
156 : ulong den_now,
157 : ulong den_then,
158 0 : double lhopital_den ) {
159 0 : if( FD_UNLIKELY( (num_now<num_then) |
160 0 : (den_now<den_then) |
161 0 : !((0.<=lhopital_num) & (lhopital_num<=DBL_MAX)) |
162 0 : !((0.< lhopital_den) & (lhopital_den<=DBL_MAX)) ) ) {
163 0 : PRINT( TEXT_RED " invalid" TEXT_NORMAL );
164 0 : return;
165 0 : }
166 :
167 0 : double pct = 100.*(((double)(num_now - num_then) + lhopital_num) / ((double)(den_now - den_then) + lhopital_den));
168 :
169 0 : if( FD_UNLIKELY( !((0.<=pct) & (pct<=DBL_MAX)) ) ) {
170 0 : PRINT( TEXT_RED "overflow" TEXT_NORMAL );
171 0 : return;
172 0 : }
173 :
174 0 : if( pct<=999.999 ) { PRINT( " %7.3f", pct ); return; }
175 0 : /**/ PRINT( ">999.999" );
176 0 : }
|