mem.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package mem
  2. import (
  3. "encoding/json"
  4. "github.com/shirou/gopsutil/v3/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. HugePagesRsvd uint64 `json:"hugePagesRsvd"`
  68. HugePagesSurp uint64 `json:"hugePagesSurp"`
  69. HugePageSize uint64 `json:"hugePageSize"`
  70. }
  71. type SwapMemoryStat struct {
  72. Total uint64 `json:"total"`
  73. Used uint64 `json:"used"`
  74. Free uint64 `json:"free"`
  75. UsedPercent float64 `json:"usedPercent"`
  76. Sin uint64 `json:"sin"`
  77. Sout uint64 `json:"sout"`
  78. PgIn uint64 `json:"pgIn"`
  79. PgOut uint64 `json:"pgOut"`
  80. PgFault uint64 `json:"pgFault"`
  81. // Linux specific numbers
  82. // https://www.kernel.org/doc/Documentation/cgroup-v2.txt
  83. PgMajFault uint64 `json:"pgMajFault"`
  84. }
  85. func (m VirtualMemoryStat) String() string {
  86. s, _ := json.Marshal(m)
  87. return string(s)
  88. }
  89. func (m SwapMemoryStat) String() string {
  90. s, _ := json.Marshal(m)
  91. return string(s)
  92. }
  93. type SwapDevice struct {
  94. Name string `json:"name"`
  95. UsedBytes uint64 `json:"usedBytes"`
  96. FreeBytes uint64 `json:"freeBytes"`
  97. }
  98. func (m SwapDevice) String() string {
  99. s, _ := json.Marshal(m)
  100. return string(s)
  101. }