Originally posted by @ardan-bkennedy in https://github.com/uber-go/automaxprocs/issues/44#issuecomment-1089508688
This change has not fixed the problem. I switched my code from "github.com/emadolsky/automaxprocs/maxprocs" to v1.5.0 and I am still getting that the CPU Quota is not defined.
I noticed that in v1.5.0
https://github.com/uber-go/automaxprocs/blob/v1.5.0/internal/runtime/cpu_quota_linux.go#L35
func CPUQuotaToGOMAXPROCS(minValue int) (int, CPUQuotaStatus, error) {[Jason Lai, 5 years ago: • Import `automaxprocs/x/runtime`](https://sourcegraph.com/github.com/uber-go/automaxprocs/-/commit/572239b375b1ca6b76babeb8b0ee139dc35fd4d8)
cgroups, err := newQueryer()
if err != nil {
return -1, CPUQuotaUndefined, err
}
quota, defined, err := cgroups.CPUQuota()
if !defined || err != nil {
return -1, CPUQuotaUndefined, err
}
maxProcs := int(math.Floor(quota))
if minValue > 0 && maxProcs < minValue {
return minValue, CPUQuotaMinUsed, nil
}
return maxProcs, CPUQuotaUsed, nil
}
var (
_newCgroups2 = cg.NewCGroups2ForCurrentProcess
_newCgroups = cg.NewCGroupsForCurrentProcess
)
func newQueryer() (queryer, error) {
cgroups, err := _newCgroups2()
if err == nil {
return cgroups, nil
}
if errors.Is(err, cg.ErrNotV2) {
return _newCgroups()
}
return nil, err
}
In the emadolsky repo, it's working. I don't know enough to tell you why.
func CPUQuotaToGOMAXPROCS(minValue int) (int, CPUQuotaStatus, error) {
var quota float64
var defined bool
var err error
isV2, err := cg.IsCGroupV2()
if err != nil {
return -1, CPUQuotaUndefined, err
}
if isV2 {
quota, defined, err = cg.CPUQuotaV2()
if !defined || err != nil {
return -1, CPUQuotaUndefined, err
}
} else {
cgroups, err := cg.NewCGroupsForCurrentProcess()
if err != nil {
return -1, CPUQuotaUndefined, err
}
quota, defined, err = cgroups.CPUQuota()
if !defined || err != nil {
return -1, CPUQuotaUndefined, err
}
}
maxProcs := int(math.Floor(quota))
if minValue > 0 && maxProcs < minValue {
return minValue, CPUQuotaMinUsed, nil
}
return maxProcs, CPUQuotaUsed, nil
}