mem.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package mem
  2. import (
  3. "encoding/json"
  4. "github.com/shirou/gopsutil/internal/common"
  5. )
  6. var invoke common.Invoker = common.Invoke{}
  7. // Memory usage statistics. Total, Available and Used contain numbers of bytes
  8. // for human consumption.
  9. //
  10. // The other fields in this struct contain kernel specific values.
  11. type VirtualMemoryStat struct {
  12. // Total amount of RAM on this system
  13. Total uint64 `json:"total"`
  14. // RAM available for programs to allocate
  15. //
  16. // This value is computed from the kernel specific values.
  17. Available uint64 `json:"available"`
  18. // RAM used by programs
  19. //
  20. // This value is computed from the kernel specific values.
  21. Used uint64 `json:"used"`
  22. // Percentage of RAM used by programs
  23. //
  24. // This value is computed from the kernel specific values.
  25. UsedPercent float64 `json:"usedPercent"`
  26. // This is the kernel's notion of free memory; RAM chips whose bits nobody
  27. // cares about the value of right now. For a human consumable number,
  28. // Available is what you really want.
  29. Free uint64 `json:"free"`
  30. // OS X / BSD specific numbers:
  31. // http://www.macyourself.com/2010/02/17/what-is-free-wired-active-and-inactive-system-memory-ram/
  32. Active uint64 `json:"active"`
  33. Inactive uint64 `json:"inactive"`
  34. Wired uint64 `json:"wired"`
  35. // FreeBSD specific numbers:
  36. // https://reviews.freebsd.org/D8467
  37. Laundry uint64 `json:"laundry"`
  38. // Linux specific numbers
  39. // https://www.centos.org/docs/5/html/5.1/Deployment_Guide/s2-proc-meminfo.html
  40. // https://www.kernel.org/doc/Documentation/filesystems/proc.txt
  41. // https://www.kernel.org/doc/Documentation/vm/overcommit-accounting
  42. Buffers uint64 `json:"buffers"`
  43. Cached uint64 `json:"cached"`
  44. Writeback uint64 `json:"writeback"`
  45. Dirty uint64 `json:"dirty"`
  46. WritebackTmp uint64 `json:"writebacktmp"`
  47. Shared uint64 `json:"shared"`
  48. Slab uint64 `json:"slab"`
  49. SReclaimable uint64 `json:"sreclaimable"`
  50. SUnreclaim uint64 `json:"sunreclaim"`
  51. PageTables uint64 `json:"pagetables"`
  52. SwapCached uint64 `json:"swapcached"`
  53. CommitLimit uint64 `json:"commitlimit"`
  54. CommittedAS uint64 `json:"committedas"`
  55. HighTotal uint64 `json:"hightotal"`
  56. HighFree uint64 `json:"highfree"`
  57. LowTotal uint64 `json:"lowtotal"`
  58. LowFree uint64 `json:"lowfree"`
  59. SwapTotal uint64 `json:"swaptotal"`
  60. SwapFree uint64 `json:"swapfree"`
  61. Mapped uint64 `json:"mapped"`
  62. VMallocTotal uint64 `json:"vmalloctotal"`
  63. VMallocUsed uint64 `json:"vmallocused"`
  64. VMallocChunk uint64 `json:"vmallocchunk"`
  65. HugePagesTotal uint64 `json:"hugepagestotal"`
  66. HugePagesFree uint64 `json:"hugepagesfree"`
  67. HugePageSize uint64 `json:"hugepagesize"`
  68. }
  69. type SwapMemoryStat struct {
  70. Total uint64 `json:"total"`
  71. Used uint64 `json:"used"`
  72. Free uint64 `json:"free"`
  73. UsedPercent float64 `json:"usedPercent"`
  74. Sin uint64 `json:"sin"`
  75. Sout uint64 `json:"sout"`
  76. PgIn uint64 `json:"pgin"`
  77. PgOut uint64 `json:"pgout"`
  78. PgFault uint64 `json:"pgfault"`
  79. // Linux specific numbers
  80. // https://www.kernel.org/doc/Documentation/cgroup-v2.txt
  81. PgMajFault uint64 `json:"pgmajfault"`
  82. }
  83. func (m VirtualMemoryStat) String() string {
  84. s, _ := json.Marshal(m)
  85. return string(s)
  86. }
  87. func (m SwapMemoryStat) String() string {
  88. s, _ := json.Marshal(m)
  89. return string(s)
  90. }
  91. type SwapDevice struct {
  92. Name string `json:"name"`
  93. UsedBytes uint64 `json:"usedBytes"`
  94. FreeBytes uint64 `json:"freeBytes"`
  95. }
  96. func (m SwapDevice) String() string {
  97. s, _ := json.Marshal(m)
  98. return string(s)
  99. }