process_statm.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package linux
  2. import (
  3. "io/ioutil"
  4. "strconv"
  5. "strings"
  6. )
  7. // Provides information about memory usage, measured in pages.
  8. type ProcessStatm struct {
  9. Size uint64 `json:"size"` // total program size
  10. Resident uint64 `json:"resident"` // resident set size
  11. Share uint64 `json:"share"` // shared pages
  12. Text uint64 `json:"text"` // text (code)
  13. Lib uint64 `json:"lib"` // library (unused in Linux 2.6)
  14. Data uint64 `json:"data"` // data + stack
  15. Dirty uint64 `json:"dirty"` // dirty pages (unused in Linux 2.6)
  16. }
  17. func ReadProcessStatm(path string) (*ProcessStatm, error) {
  18. b, err := ioutil.ReadFile(path)
  19. if err != nil {
  20. return nil, err
  21. }
  22. s := string(b)
  23. f := strings.Fields(s)
  24. statm := ProcessStatm{}
  25. var n uint64
  26. for i := 0; i < len(f); i++ {
  27. if n, err = strconv.ParseUint(f[i], 10, 64); err != nil {
  28. return nil, err
  29. }
  30. switch i {
  31. case 0:
  32. statm.Size = n
  33. case 1:
  34. statm.Resident = n
  35. case 2:
  36. statm.Share = n
  37. case 3:
  38. statm.Text = n
  39. case 4:
  40. statm.Lib = n
  41. case 5:
  42. statm.Data = n
  43. case 6:
  44. statm.Dirty = n
  45. }
  46. }
  47. return &statm, nil
  48. }