pids.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package fs
  2. import (
  3. "math"
  4. "strconv"
  5. "github.com/opencontainers/runc/libcontainer/cgroups"
  6. "github.com/opencontainers/runc/libcontainer/cgroups/fscommon"
  7. "github.com/opencontainers/runc/libcontainer/configs"
  8. )
  9. type PidsGroup struct{}
  10. func (s *PidsGroup) Name() string {
  11. return "pids"
  12. }
  13. func (s *PidsGroup) Apply(path string, _ *configs.Resources, pid int) error {
  14. return apply(path, pid)
  15. }
  16. func (s *PidsGroup) Set(path string, r *configs.Resources) error {
  17. if r.PidsLimit != 0 {
  18. // "max" is the fallback value.
  19. limit := "max"
  20. if r.PidsLimit > 0 {
  21. limit = strconv.FormatInt(r.PidsLimit, 10)
  22. }
  23. if err := cgroups.WriteFile(path, "pids.max", limit); err != nil {
  24. return err
  25. }
  26. }
  27. return nil
  28. }
  29. func (s *PidsGroup) GetStats(path string, stats *cgroups.Stats) error {
  30. if !cgroups.PathExists(path) {
  31. return nil
  32. }
  33. current, err := fscommon.GetCgroupParamUint(path, "pids.current")
  34. if err != nil {
  35. return err
  36. }
  37. max, err := fscommon.GetCgroupParamUint(path, "pids.max")
  38. if err != nil {
  39. return err
  40. }
  41. // If no limit is set, read from pids.max returns "max", which is
  42. // converted to MaxUint64 by GetCgroupParamUint. Historically, we
  43. // represent "no limit" for pids as 0, thus this conversion.
  44. if max == math.MaxUint64 {
  45. max = 0
  46. }
  47. stats.PidsStats.Current = current
  48. stats.PidsStats.Limit = max
  49. return nil
  50. }