LCOV - code coverage report
Current view: top level - app/fdctl/monitor - helper.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 110 0.0 %
Date: 2024-11-13 11:58:15 Functions: 0 10 0.0 %

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

Generated by: LCOV version 1.14