cpu.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. Copyright The containerd Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package cgroup2
  14. import (
  15. "math"
  16. "strconv"
  17. "strings"
  18. )
  19. type CPUMax string
  20. func NewCPUMax(quota *int64, period *uint64) CPUMax {
  21. max := "max"
  22. if quota != nil {
  23. max = strconv.FormatInt(*quota, 10)
  24. }
  25. return CPUMax(strings.Join([]string{max, strconv.FormatUint(*period, 10)}, " "))
  26. }
  27. type CPU struct {
  28. Weight *uint64
  29. Max CPUMax
  30. Cpus string
  31. Mems string
  32. }
  33. func (c CPUMax) extractQuotaAndPeriod() (int64, uint64) {
  34. var (
  35. quota int64
  36. period uint64
  37. )
  38. values := strings.Split(string(c), " ")
  39. if values[0] == "max" {
  40. quota = math.MaxInt64
  41. } else {
  42. quota, _ = strconv.ParseInt(values[0], 10, 64)
  43. }
  44. period, _ = strconv.ParseUint(values[1], 10, 64)
  45. return quota, period
  46. }
  47. func (r *CPU) Values() (o []Value) {
  48. if r.Weight != nil {
  49. o = append(o, Value{
  50. filename: "cpu.weight",
  51. value: *r.Weight,
  52. })
  53. }
  54. if r.Max != "" {
  55. o = append(o, Value{
  56. filename: "cpu.max",
  57. value: r.Max,
  58. })
  59. }
  60. if r.Cpus != "" {
  61. o = append(o, Value{
  62. filename: "cpuset.cpus",
  63. value: r.Cpus,
  64. })
  65. }
  66. if r.Mems != "" {
  67. o = append(o, Value{
  68. filename: "cpuset.mems",
  69. value: r.Mems,
  70. })
  71. }
  72. return o
  73. }