pids.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // +build linux
  2. package fs
  3. import (
  4. "fmt"
  5. "path/filepath"
  6. "strconv"
  7. "github.com/opencontainers/runc/libcontainer/cgroups"
  8. "github.com/opencontainers/runc/libcontainer/configs"
  9. )
  10. type PidsGroup struct {
  11. }
  12. func (s *PidsGroup) Name() string {
  13. return "pids"
  14. }
  15. func (s *PidsGroup) Apply(d *cgroupData) error {
  16. _, err := d.join("pids")
  17. if err != nil && !cgroups.IsNotFound(err) {
  18. return err
  19. }
  20. return nil
  21. }
  22. func (s *PidsGroup) Set(path string, cgroup *configs.Cgroup) error {
  23. if cgroup.Resources.PidsLimit != 0 {
  24. // "max" is the fallback value.
  25. limit := "max"
  26. if cgroup.Resources.PidsLimit > 0 {
  27. limit = strconv.FormatInt(cgroup.Resources.PidsLimit, 10)
  28. }
  29. if err := writeFile(path, "pids.max", limit); err != nil {
  30. return err
  31. }
  32. }
  33. return nil
  34. }
  35. func (s *PidsGroup) Remove(d *cgroupData) error {
  36. return removePath(d.path("pids"))
  37. }
  38. func (s *PidsGroup) GetStats(path string, stats *cgroups.Stats) error {
  39. current, err := getCgroupParamUint(path, "pids.current")
  40. if err != nil {
  41. return fmt.Errorf("failed to parse pids.current - %s", err)
  42. }
  43. maxString, err := getCgroupParamString(path, "pids.max")
  44. if err != nil {
  45. return fmt.Errorf("failed to parse pids.max - %s", err)
  46. }
  47. // Default if pids.max == "max" is 0 -- which represents "no limit".
  48. var max uint64
  49. if maxString != "max" {
  50. max, err = parseUint(maxString, 10, 64)
  51. if err != nil {
  52. return fmt.Errorf("failed to parse pids.max - unable to parse %q as a uint from Cgroup file %q", maxString, filepath.Join(path, "pids.max"))
  53. }
  54. }
  55. stats.PidsStats.Current = current
  56. stats.PidsStats.Limit = max
  57. return nil
  58. }