pids.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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/cgroups/fscommon"
  9. "github.com/opencontainers/runc/libcontainer/configs"
  10. )
  11. type PidsGroup struct {
  12. }
  13. func (s *PidsGroup) Name() string {
  14. return "pids"
  15. }
  16. func (s *PidsGroup) Apply(d *cgroupData) error {
  17. _, err := d.join("pids")
  18. if err != nil && !cgroups.IsNotFound(err) {
  19. return err
  20. }
  21. return nil
  22. }
  23. func (s *PidsGroup) Set(path string, cgroup *configs.Cgroup) error {
  24. if cgroup.Resources.PidsLimit != 0 {
  25. // "max" is the fallback value.
  26. limit := "max"
  27. if cgroup.Resources.PidsLimit > 0 {
  28. limit = strconv.FormatInt(cgroup.Resources.PidsLimit, 10)
  29. }
  30. if err := fscommon.WriteFile(path, "pids.max", limit); err != nil {
  31. return err
  32. }
  33. }
  34. return nil
  35. }
  36. func (s *PidsGroup) Remove(d *cgroupData) error {
  37. return removePath(d.path("pids"))
  38. }
  39. func (s *PidsGroup) GetStats(path string, stats *cgroups.Stats) error {
  40. current, err := fscommon.GetCgroupParamUint(path, "pids.current")
  41. if err != nil {
  42. return fmt.Errorf("failed to parse pids.current - %s", err)
  43. }
  44. maxString, err := fscommon.GetCgroupParamString(path, "pids.max")
  45. if err != nil {
  46. return fmt.Errorf("failed to parse pids.max - %s", err)
  47. }
  48. // Default if pids.max == "max" is 0 -- which represents "no limit".
  49. var max uint64
  50. if maxString != "max" {
  51. max, err = fscommon.ParseUint(maxString, 10, 64)
  52. if err != nil {
  53. 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"))
  54. }
  55. }
  56. stats.PidsStats.Current = current
  57. stats.PidsStats.Limit = max
  58. return nil
  59. }