index.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package systeminfo
  2. import (
  3. "fmt"
  4. "runtime"
  5. "time"
  6. )
  7. var initTime = time.Now()
  8. type SystemInfo struct {
  9. Uptime string
  10. NumGoroutine int
  11. // General statistics.
  12. MemAllocated string // bytes allocated and still in use
  13. MemTotal string // bytes allocated (even if freed)
  14. MemSys string // bytes obtained from system (sum of XxxSys below)
  15. Lookups uint64 // number of pointer lookups
  16. MemMallocs uint64 // number of mallocs
  17. MemFrees uint64 // number of frees
  18. // Main allocation heap statistics.
  19. HeapAlloc string // bytes allocated and still in use
  20. HeapSys string // bytes obtained from system
  21. HeapIdle string // bytes in idle spans
  22. HeapInuse string // bytes in non-idle span
  23. HeapReleased string // bytes released to the OS
  24. HeapObjects uint64 // total number of allocated objects
  25. // Low-level fixed-size structure allocator statistics.
  26. // Inuse is bytes used now.
  27. // Sys is bytes obtained from system.
  28. StackInuse string // bootstrap stacks
  29. StackSys string
  30. MSpanInuse string // mspan structures
  31. MSpanSys string
  32. MCacheInuse string // mcache structures
  33. MCacheSys string
  34. BuckHashSys string // profiling bucket hash table
  35. GCSys string // GC metadata
  36. OtherSys string // other system allocations
  37. // Garbage collector statistics.
  38. NextGC string // next run in HeapAlloc time (bytes)
  39. LastGC string // last run in absolute time (ns)
  40. PauseTotalNs string
  41. PauseNs string // circular buffer of recent GC pause times, most recent at [(NumGC+255)%256]
  42. NumGC uint32
  43. }
  44. func GetSystemInfo() (sysStatus *SystemInfo) {
  45. sysStatus = &SystemInfo{}
  46. sysStatus.Uptime = TimeSincePro(initTime)
  47. m := new(runtime.MemStats)
  48. runtime.ReadMemStats(m)
  49. sysStatus.NumGoroutine = runtime.NumGoroutine()
  50. sysStatus.MemAllocated = FileSize(int64(m.Alloc))
  51. sysStatus.MemTotal = FileSize(int64(m.TotalAlloc))
  52. sysStatus.MemSys = FileSize(int64(m.Sys))
  53. sysStatus.Lookups = m.Lookups
  54. sysStatus.MemMallocs = m.Mallocs
  55. sysStatus.MemFrees = m.Frees
  56. sysStatus.HeapAlloc = FileSize(int64(m.HeapAlloc))
  57. sysStatus.HeapSys = FileSize(int64(m.HeapSys))
  58. sysStatus.HeapIdle = FileSize(int64(m.HeapIdle))
  59. sysStatus.HeapInuse = FileSize(int64(m.HeapInuse))
  60. sysStatus.HeapReleased = FileSize(int64(m.HeapReleased))
  61. sysStatus.HeapObjects = m.HeapObjects
  62. sysStatus.StackInuse = FileSize(int64(m.StackInuse))
  63. sysStatus.StackSys = FileSize(int64(m.StackSys))
  64. sysStatus.MSpanInuse = FileSize(int64(m.MSpanInuse))
  65. sysStatus.MSpanSys = FileSize(int64(m.MSpanSys))
  66. sysStatus.MCacheInuse = FileSize(int64(m.MCacheInuse))
  67. sysStatus.MCacheSys = FileSize(int64(m.MCacheSys))
  68. sysStatus.BuckHashSys = FileSize(int64(m.BuckHashSys))
  69. sysStatus.GCSys = FileSize(int64(m.GCSys))
  70. sysStatus.OtherSys = FileSize(int64(m.OtherSys))
  71. sysStatus.NextGC = FileSize(int64(m.NextGC))
  72. sysStatus.LastGC = fmt.Sprintf("%.1fs", float64(time.Now().UnixNano()-int64(m.LastGC))/1000/1000/1000)
  73. sysStatus.PauseTotalNs = fmt.Sprintf("%.1fs", float64(m.PauseTotalNs)/1000/1000/1000)
  74. sysStatus.PauseNs = fmt.Sprintf("%.3fs", float64(m.PauseNs[(m.NumGC+255)%256])/1000/1000/1000)
  75. sysStatus.NumGC = m.NumGC
  76. return
  77. }