memory.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package cgroup
  2. import (
  3. "path"
  4. )
  5. const maxMemory = 1 << 62
  6. type MemoryStat struct {
  7. RSS uint64
  8. Cache uint64
  9. }
  10. func (cg *Cgroup) MemoryStat() (MemoryStat, error) {
  11. vars, err := readVariablesFromFile(path.Join(cgRoot, "memory", cg.subsystems["memory"], "memory.stat"))
  12. if err != nil {
  13. return MemoryStat{}, err
  14. }
  15. // Note from https://www.kernel.org/doc/Documentation/cgroup-v1/memory.txt:
  16. // Only anonymous and swap cache memory is listed as part of 'rss' stat.
  17. // This should not be confused with the true 'resident set size' or the
  18. // amount of physical memory used by the cgroup.
  19. // 'rss + mapped_file" will give you resident set size of cgroup.
  20. // (Note: file and shmem may be shared among other cgroups. In that case,
  21. // mapped_file is accounted only when the memory cgroup is owner of page
  22. // cache.)
  23. return MemoryStat{
  24. RSS: vars["rss"] + vars["mapped_file"],
  25. Cache: vars["cache"],
  26. }, nil
  27. }
  28. func (cg *Cgroup) MemoryLimitBytes() (uint64, error) {
  29. limit, err := readUintFromFile(path.Join(cgRoot, "memory", cg.subsystems["memory"], "memory.limit_in_bytes"))
  30. if err != nil {
  31. return 0, err
  32. }
  33. if limit > maxMemory {
  34. return 0, nil
  35. }
  36. return limit, nil
  37. }