github.com/gramework/gramework@v1.8.1-0.20231027140105-82555c9057f5/init.go (about)

     1  package gramework
     2  
     3  import (
     4  	"fmt"
     5  	"runtime"
     6  	"sync"
     7  
     8  	"github.com/apex/log"
     9  	sigar "github.com/cloudfoundry/gosigar"
    10  )
    11  
    12  var checkOnce sync.Once
    13  
    14  func checks() {
    15  	checkOnce.Do(func() {
    16  		internalLog.WithFields(log.Fields{
    17  			"package": "gramework",
    18  			"version": Version,
    19  		}).Info("Initialization")
    20  		concreteSigar := sigar.ConcreteSigar{}
    21  		internalLog.WithFields(log.Fields{
    22  			"cputicks": siFmt(uint64(TicksPerSecond())),
    23  			"ram": func() string {
    24  				mem, err := concreteSigar.GetMem()
    25  				if err != nil {
    26  					return "<unknown>"
    27  				}
    28  				return fmt.Sprintf("%s used / %s total", siFmt(mem.ActualUsed), siFmt(mem.Total))
    29  			}(),
    30  			"swap": func() string {
    31  				swap, err := concreteSigar.GetSwap()
    32  				if err != nil {
    33  					return "<unknown>"
    34  				}
    35  				return fmt.Sprintf("%s used / %s total", siFmt(swap.Used), siFmt(swap.Total))
    36  			}(),
    37  		}).Info("node info")
    38  		la, err := concreteSigar.GetLoadAverage()
    39  		if err != nil {
    40  			err = la.Get() // retry
    41  		}
    42  		if err == nil {
    43  			maxLA := float64(runtime.NumCPU() + 2)
    44  			loadLog := internalLog.WithFields(log.Fields{
    45  				"one":     fmt.Sprintf("%.3f", la.One),
    46  				"five":    fmt.Sprintf("%.3f", la.Five),
    47  				"fifteen": fmt.Sprintf("%.3f", la.Fifteen),
    48  			})
    49  			if la.One >= maxLA || la.Five >= maxLA || la.Fifteen >= maxLA {
    50  				loadLog.Warn("high load average, performance may be impacted")
    51  			} else {
    52  				loadLog.Info("load average is good")
    53  			}
    54  		}
    55  
    56  		uptime := sigar.Uptime{}
    57  		err = uptime.Get()
    58  		if err != nil {
    59  			err = uptime.Get() // retry
    60  		}
    61  		if err == nil {
    62  			internalLog.WithField("uptime", uptime.Format()).Info("node uptime")
    63  		}
    64  	})
    65  }
    66  
    67  func siFmt(n uint64) string {
    68  	prefix := siRaw
    69  	x := float64(n)
    70  	for ; x > 1000; x = x / 1024 {
    71  		prefix++
    72  	}
    73  
    74  	return fmt.Sprintf("%.2f%s", x, prefix.String())
    75  }
    76  
    77  type siPrefix uint
    78  
    79  const (
    80  	siRaw siPrefix = iota
    81  	siKilo
    82  	siMega
    83  	siGiga
    84  	siTera
    85  )
    86  
    87  func (s siPrefix) String() string {
    88  	switch s {
    89  	case siRaw:
    90  		return ""
    91  	case siKilo:
    92  		return "K"
    93  	case siMega:
    94  		return "M"
    95  	case siGiga:
    96  		return "G"
    97  	case siTera:
    98  		return "T"
    99  	default:
   100  		return "T"
   101  	}
   102  }