memory.go 605 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package gupta
  2. import (
  3. "bytes"
  4. "io"
  5. "strconv"
  6. "strings"
  7. )
  8. type Memory struct {
  9. total uint64
  10. free uint64
  11. }
  12. func (m *Memory) Free() uint64 {
  13. return m.free
  14. }
  15. func (m *Memory) Total() uint64 {
  16. return m.total
  17. }
  18. func (m *Memory) Used() uint64 {
  19. return m.total - m.free
  20. }
  21. func NewMemory(r io.ReaderAt) Memory {
  22. m := Memory{}
  23. buf := make([]byte, 80)
  24. r.ReadAt(buf, 0)
  25. idx := bytes.Index(buf, []byte("MemAvailable"))
  26. line := string(buf[:idx])
  27. elements := strings.Fields(line)
  28. m.total, _ = strconv.ParseUint(elements[1], 10, 64)
  29. m.free, _ = strconv.ParseUint(elements[4], 10, 64)
  30. return m
  31. }