|
| 1 | +// Copyright (c) 2022 Gitpod GmbH. All rights reserved. |
| 2 | +// Licensed under the GNU Affero General Public License (AGPL). |
| 3 | +// See License-AGPL.txt in the project root for license information. |
| 4 | + |
| 5 | +package cpulimit |
| 6 | + |
| 7 | +import ( |
| 8 | + "bufio" |
| 9 | + "math" |
| 10 | + "os" |
| 11 | + "path/filepath" |
| 12 | + "strconv" |
| 13 | + "strings" |
| 14 | + "time" |
| 15 | + |
| 16 | + "golang.org/x/xerrors" |
| 17 | +) |
| 18 | + |
| 19 | +type CgroupV2CFSController string |
| 20 | + |
| 21 | +func (basePath CgroupV2CFSController) Usage() (CPUTime, error) { |
| 22 | + usage, err := basePath.getFlatKeyedValue("usage_usec") |
| 23 | + if err != nil { |
| 24 | + return 0, err |
| 25 | + } |
| 26 | + |
| 27 | + return CPUTime(time.Duration(usage) * time.Microsecond), nil |
| 28 | +} |
| 29 | + |
| 30 | +func (basePath CgroupV2CFSController) SetLimit(limit Bandwidth) (changed bool, err error) { |
| 31 | + quota, period, err := basePath.readCpuMax() |
| 32 | + if err != nil { |
| 33 | + return false, xerrors.Errorf("failed to read cpu max from %s: %w", basePath, err) |
| 34 | + } |
| 35 | + |
| 36 | + target := limit.Quota(period) |
| 37 | + if quota == target { |
| 38 | + return false, nil |
| 39 | + } |
| 40 | + |
| 41 | + err = basePath.writeQuota(target) |
| 42 | + if err != nil { |
| 43 | + return false, xerrors.Errorf("cannot set CFS quota of %d (period is %d, parent quota is %d): %w", |
| 44 | + target.Microseconds(), period.Microseconds(), basePath.readParentQuota().Microseconds(), err) |
| 45 | + } |
| 46 | + |
| 47 | + return true, nil |
| 48 | +} |
| 49 | + |
| 50 | +func (basePath CgroupV2CFSController) NrThrottled() (uint64, error) { |
| 51 | + throttled, err := basePath.getFlatKeyedValue("nr_throttled") |
| 52 | + if err != nil { |
| 53 | + return 0, err |
| 54 | + } |
| 55 | + |
| 56 | + return uint64(throttled), nil |
| 57 | +} |
| 58 | + |
| 59 | +func (basePath CgroupV2CFSController) readCpuMax() (time.Duration, time.Duration, error) { |
| 60 | + cpuMaxPath := filepath.Join(string(basePath), "cpu.max") |
| 61 | + cpuMax, err := os.ReadFile(cpuMaxPath) |
| 62 | + if err != nil { |
| 63 | + return 0, 0, xerrors.Errorf("unable to read cpu.max: %w", err) |
| 64 | + } |
| 65 | + |
| 66 | + parts := strings.Fields(string(cpuMax)) |
| 67 | + if len(parts) < 2 { |
| 68 | + return 0, 0, xerrors.Errorf("cpu.max did not have expected number of fields: %s", parts) |
| 69 | + } |
| 70 | + |
| 71 | + var quota int64 |
| 72 | + if parts[0] == "max" { |
| 73 | + quota = math.MaxInt64 |
| 74 | + } else { |
| 75 | + quota, err = strconv.ParseInt(parts[0], 10, 64) |
| 76 | + if err != nil { |
| 77 | + return 0, 0, xerrors.Errorf("could not parse quota of %s: %w", parts[0], err) |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + period, err := strconv.ParseInt(parts[1], 10, 64) |
| 82 | + if err != nil { |
| 83 | + return 0, 0, xerrors.Errorf("could not parse period of %s: %w", parts[1], err) |
| 84 | + } |
| 85 | + |
| 86 | + return time.Duration(quota) * time.Microsecond, time.Duration(period) * time.Microsecond, nil |
| 87 | +} |
| 88 | + |
| 89 | +func (basePath CgroupV2CFSController) writeQuota(quota time.Duration) error { |
| 90 | + cpuMaxPath := filepath.Join(string(basePath), "cpu.max") |
| 91 | + return os.WriteFile(cpuMaxPath, []byte(strconv.FormatInt(quota.Microseconds(), 10)), 0644) |
| 92 | +} |
| 93 | + |
| 94 | +func (basePath CgroupV2CFSController) readParentQuota() time.Duration { |
| 95 | + parent := CgroupV2CFSController(filepath.Dir(string(basePath))) |
| 96 | + quota, _, err := parent.readCpuMax() |
| 97 | + if err != nil { |
| 98 | + return time.Duration(0) |
| 99 | + } |
| 100 | + |
| 101 | + return time.Duration(quota) |
| 102 | +} |
| 103 | + |
| 104 | +func (basePath CgroupV2CFSController) getFlatKeyedValue(key string) (int64, error) { |
| 105 | + stats, err := os.Open(filepath.Join(string(basePath), "cpu.stat")) |
| 106 | + if err != nil { |
| 107 | + return 0, xerrors.Errorf("cannot read cpu.stat: %w", err) |
| 108 | + } |
| 109 | + defer stats.Close() |
| 110 | + |
| 111 | + scanner := bufio.NewScanner(stats) |
| 112 | + for scanner.Scan() { |
| 113 | + entry := scanner.Text() |
| 114 | + if !strings.HasPrefix(entry, key) { |
| 115 | + continue |
| 116 | + } |
| 117 | + |
| 118 | + r, err := strconv.ParseInt(strings.TrimSpace(strings.TrimPrefix(entry, key)), 10, 64) |
| 119 | + if err != nil { |
| 120 | + return 0, xerrors.Errorf("cannot parse cpu.stat: %s: %w", entry, err) |
| 121 | + } |
| 122 | + return int64(r), nil |
| 123 | + } |
| 124 | + return 0, xerrors.Errorf("cpu.stat did not contain nr_throttled") |
| 125 | +} |
0 commit comments