meminfo.go 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package linux
  2. import (
  3. "io/ioutil"
  4. "reflect"
  5. "strconv"
  6. "strings"
  7. )
  8. type MemInfo struct {
  9. MemTotal uint64 `json:"mem_total"`
  10. MemFree uint64 `json:"mem_free"`
  11. MemAvailable uint64 `json:"mem_available"`
  12. Buffers uint64 `json:"buffers"`
  13. Cached uint64 `json:"cached"`
  14. SwapCached uint64 `json:"swap_cached"`
  15. Active uint64 `json:"active"`
  16. Inactive uint64 `json:"inactive"`
  17. ActiveAnon uint64 `json:"active_anon" field:"Active(anon)"`
  18. InactiveAnon uint64 `json:"inactive_anon" field:"Inactive(anon)"`
  19. ActiveFile uint64 `json:"active_file" field:"Active(file)"`
  20. InactiveFile uint64 `json:"inactive_file" field:"Inactive(file)"`
  21. Unevictable uint64 `json:"unevictable"`
  22. Mlocked uint64 `json:"mlocked"`
  23. SwapTotal uint64 `json:"swap_total"`
  24. SwapFree uint64 `json:"swap_free"`
  25. Dirty uint64 `json:"dirty"`
  26. Writeback uint64 `json:"write_back"`
  27. AnonPages uint64 `json:"anon_pages"`
  28. Mapped uint64 `json:"mapped"`
  29. Shmem uint64 `json:"shmem"`
  30. Slab uint64 `json:"slab"`
  31. SReclaimable uint64 `json:"s_reclaimable"`
  32. SUnreclaim uint64 `json:"s_unclaim"`
  33. KernelStack uint64 `json:"kernel_stack"`
  34. PageTables uint64 `json:"page_tables"`
  35. NFS_Unstable uint64 `json:"nfs_unstable"`
  36. Bounce uint64 `json:"bounce"`
  37. WritebackTmp uint64 `json:"writeback_tmp"`
  38. CommitLimit uint64 `json:"commit_limit"`
  39. Committed_AS uint64 `json:"committed_as"`
  40. VmallocTotal uint64 `json:"vmalloc_total"`
  41. VmallocUsed uint64 `json:"vmalloc_used"`
  42. VmallocChunk uint64 `json:"vmalloc_chunk"`
  43. HardwareCorrupted uint64 `json:"hardware_corrupted"`
  44. AnonHugePages uint64 `json:"anon_huge_pages"`
  45. HugePages_Total uint64 `json:"huge_pages_total"`
  46. HugePages_Free uint64 `json:"huge_pages_free"`
  47. HugePages_Rsvd uint64 `json:"huge_pages_rsvd"`
  48. HugePages_Surp uint64 `json:"huge_pages_surp"`
  49. Hugepagesize uint64 `json:"hugepagesize"`
  50. DirectMap4k uint64 `json:"direct_map_4k"`
  51. DirectMap2M uint64 `json:"direct_map_2M"`
  52. DirectMap1G uint64 `json:"direct_map_1G"`
  53. }
  54. func ReadMemInfo(path string) (*MemInfo, error) {
  55. data, err := ioutil.ReadFile(path)
  56. if err != nil {
  57. return nil, err
  58. }
  59. lines := strings.Split(string(data), "\n")
  60. // Maps a meminfo metric to its value (i.e. MemTotal --> 100000)
  61. statMap := make(map[string]uint64)
  62. var info = MemInfo{}
  63. for _, line := range lines {
  64. fields := strings.SplitN(line, ":", 2)
  65. if len(fields) < 2 {
  66. continue
  67. }
  68. valFields := strings.Fields(fields[1])
  69. val, _ := strconv.ParseUint(valFields[0], 10, 64)
  70. statMap[fields[0]] = val
  71. }
  72. elem := reflect.ValueOf(&info).Elem()
  73. typeOfElem := elem.Type()
  74. for i := 0; i < elem.NumField(); i++ {
  75. val, ok := statMap[typeOfElem.Field(i).Name]
  76. if ok {
  77. elem.Field(i).SetUint(val)
  78. continue
  79. }
  80. val, ok = statMap[typeOfElem.Field(i).Tag.Get("field")]
  81. if ok {
  82. elem.Field(i).SetUint(val)
  83. }
  84. }
  85. return &info, nil
  86. }