MCPcopy Index your code
hub / github.com/coder/envbox / readCPUQuotaCGroupV2

Function readCPUQuotaCGroupV2

xunix/sys.go:59–94  ·  view source on GitHub ↗
(ctx context.Context)

Source from the content-addressed store, hash-verified

57}
58
59func readCPUQuotaCGroupV2(ctx context.Context) (CPUQuota, error) {
60 fs := GetFS(ctx)
61 self, err := ReadCGroupSelf(ctx)
62 if err != nil {
63 return CPUQuota{}, xerrors.Errorf("determine own cgroup: %w", err)
64 }
65
66 maxStr, err := afero.ReadFile(fs, filepath.Join("/sys/fs/cgroup/", self, "cpu.max"))
67 if err != nil {
68 return CPUQuota{}, xerrors.Errorf("read cpu.max outside container: %w", err)
69 }
70
71 list := strings.Split(string(bytes.TrimSpace(maxStr)), " ")
72 if len(list) != 2 {
73 return CPUQuota{}, xerrors.Errorf("expected cpu.max to have exactly two entries, got: %s", string(maxStr))
74 }
75
76 var quota int
77 var period int
78
79 if list[0] == "max" {
80 quota = -1
81 } else {
82 quota, err = strconv.Atoi(list[0])
83 if err != nil {
84 return CPUQuota{}, xerrors.Errorf("quota %s not an int: %w", list[0], err)
85 }
86 }
87
88 period, err = strconv.Atoi(list[1])
89 if err != nil {
90 return CPUQuota{}, xerrors.Errorf("period %s not an int: %w", list[1], err)
91 }
92
93 return CPUQuota{Quota: quota, Period: period, CGroup: CGroupV2}, nil
94}
95
96func readCPUQuotaCGroupV1(ctx context.Context) (CPUQuota, error) {
97 fs := GetFS(ctx)

Callers 1

ReadCPUQuotaFunction · 0.85

Calls 3

GetFSFunction · 0.85
ReadCGroupSelfFunction · 0.85
ErrorfMethod · 0.65

Tested by

no test coverage detected