github.com/kotovmak/go-admin@v1.1.1/modules/system/application.go (about)

     1  package system
     2  
     3  import (
     4  	"fmt"
     5  	"runtime"
     6  	"time"
     7  
     8  	"github.com/kotovmak/go-admin/modules/config"
     9  	"github.com/kotovmak/go-admin/modules/language"
    10  	"github.com/kotovmak/go-admin/modules/utils"
    11  )
    12  
    13  var (
    14  	startTime = time.Now()
    15  )
    16  
    17  type AppStatus struct {
    18  	Uptime       string
    19  	NumGoroutine int
    20  
    21  	// General statistics.
    22  	MemAllocated string // bytes allocated and still in use
    23  	MemTotal     string // bytes allocated (even if freed)
    24  	MemSys       string // bytes obtained from system (sum of XxxSys below)
    25  	Lookups      uint64 // number of pointer lookups
    26  	MemMallocs   uint64 // number of mallocs
    27  	MemFrees     uint64 // number of frees
    28  
    29  	// Main allocation heap statistics.
    30  	HeapAlloc    string // bytes allocated and still in use
    31  	HeapSys      string // bytes obtained from system
    32  	HeapIdle     string // bytes in idle spans
    33  	HeapInuse    string // bytes in non-idle span
    34  	HeapReleased string // bytes released to the OS
    35  	HeapObjects  uint64 // total number of allocated objects
    36  
    37  	// Low-level fixed-size structure allocator statistics.
    38  	//	Inuse is bytes used now.
    39  	//	Sys is bytes obtained from system.
    40  	StackInuse  string // bootstrap stacks
    41  	StackSys    string
    42  	MSpanInuse  string // mspan structures
    43  	MSpanSys    string
    44  	MCacheInuse string // mcache structures
    45  	MCacheSys   string
    46  	BuckHashSys string // profiling bucket hash table
    47  	GCSys       string // GC metadata
    48  	OtherSys    string // other system allocations
    49  
    50  	// Garbage collector statistics.
    51  	NextGC       string // next run in HeapAlloc time (bytes)
    52  	LastGC       string // last run in absolute time (ns)
    53  	PauseTotalNs string
    54  	PauseNs      string // circular buffer of recent GC pause times, most recent at [(NumGC+255)%256]
    55  	NumGC        uint32
    56  }
    57  
    58  func GetAppStatus() AppStatus {
    59  	var app AppStatus
    60  	app.Uptime = utils.TimeSincePro(startTime, language.Lang[config.GetLanguage()])
    61  
    62  	m := new(runtime.MemStats)
    63  	runtime.ReadMemStats(m)
    64  	app.NumGoroutine = runtime.NumGoroutine()
    65  
    66  	app.MemAllocated = utils.FileSize(m.Alloc)
    67  	app.MemTotal = utils.FileSize(m.TotalAlloc)
    68  	app.MemSys = utils.FileSize(m.Sys)
    69  	app.Lookups = m.Lookups
    70  	app.MemMallocs = m.Mallocs
    71  	app.MemFrees = m.Frees
    72  
    73  	app.HeapAlloc = utils.FileSize(m.HeapAlloc)
    74  	app.HeapSys = utils.FileSize(m.HeapSys)
    75  	app.HeapIdle = utils.FileSize(m.HeapIdle)
    76  	app.HeapInuse = utils.FileSize(m.HeapInuse)
    77  	app.HeapReleased = utils.FileSize(m.HeapReleased)
    78  	app.HeapObjects = m.HeapObjects
    79  
    80  	app.StackInuse = utils.FileSize(m.StackInuse)
    81  	app.StackSys = utils.FileSize(m.StackSys)
    82  	app.MSpanInuse = utils.FileSize(m.MSpanInuse)
    83  	app.MSpanSys = utils.FileSize(m.MSpanSys)
    84  	app.MCacheInuse = utils.FileSize(m.MCacheInuse)
    85  	app.MCacheSys = utils.FileSize(m.MCacheSys)
    86  	app.BuckHashSys = utils.FileSize(m.BuckHashSys)
    87  	app.GCSys = utils.FileSize(m.GCSys)
    88  	app.OtherSys = utils.FileSize(m.OtherSys)
    89  
    90  	app.NextGC = utils.FileSize(m.NextGC)
    91  	app.LastGC = fmt.Sprintf("%.1fs", float64(time.Now().UnixNano()-int64(m.LastGC))/1000/1000/1000)
    92  	app.PauseTotalNs = fmt.Sprintf("%.1fs", float64(m.PauseTotalNs)/1000/1000/1000)
    93  	app.PauseNs = fmt.Sprintf("%.3fs", float64(m.PauseNs[(m.NumGC+255)%256])/1000/1000/1000)
    94  	app.NumGC = m.NumGC
    95  
    96  	return app
    97  }
    98  
    99  type SysStatus struct {
   100  	CpuLogicalCore int
   101  	CpuCore        int
   102  	OSPlatform     string
   103  	OSFamily       string
   104  	OSVersion      string
   105  	Load1          float64
   106  	Load5          float64
   107  	Load15         float64
   108  	MemTotal       string
   109  	MemAvailable   string
   110  	MemUsed        string
   111  }