package fs import ( "fmt" "os" "path/filepath" "strconv" "strings" "linkfog.com/public/lib/cgroup/fscommon" "linkfog.com/public/lib/l" ) var ( defaultCPU = "-1" ) func CPUSubSystemName() string { return "cpuacct" } func GetCPULimit(cgroupDir string, defaultLimit float64) float64 { // parse quota contents, err := fscommon.GetCgroupParamString(filepath.Join(cgroupDir, "cpu.cfs_quota_us")) if err != nil { l.Errorf("read cpu.cfs_quota_us file error:%v, use default value", err) return defaultLimit } if contents == defaultCPU { l.Warn("cpu.cfs_quota_us is defaultCPU, use default value") return defaultLimit } cpuQuota, err := fscommon.ParseUint(contents, 10, 64) if err != nil { l.Errorf("parse cpu.cfs_quota_us error:%v, contents:%s, use default value", err, contents) return defaultLimit } if cpuQuota == 0 { l.Warn("cpu.cfs_quota_us is 0, use default value") return defaultLimit } // parse period cpuPeriod, err := fscommon.GetCgroupParamUint(filepath.Join(cgroupDir, "cpu.cfs_period_us")) if err != nil { l.Errorf("read cpu.cfs_period_us file error:%v, use default value", err) return defaultLimit } if cpuPeriod == 0 { l.Warn("cpu.cfs_period_us is 0, use default value") return defaultLimit } return float64(cpuQuota) / float64(cpuPeriod) } func GetCPUUsage(cgroupDir string) (totalUsage uint64, perCPUUsage []uint64, err error) { totalUsage, err = fscommon.GetCgroupParamUint(filepath.Join(cgroupDir, "cpuacct.usage")) if err != nil { return } perCPUUsage, err = getPerCPUUsage(cgroupDir) if err != nil { return } if totalUsage == 0 { l.Warn("get cpu total usage is 0, use per-cpu usage accumulation value") for _, usage := range perCPUUsage { totalUsage += usage } } return } func getPerCPUUsage(cgroupDir string) ([]uint64, error) { var perCPUUsage []uint64 data, err := os.ReadFile(filepath.Join(cgroupDir, "cpuacct.usage_percpu")) if err != nil { return perCPUUsage, err } for _, value := range strings.Fields(string(data)) { value, err := strconv.ParseUint(value, 10, 64) if err != nil { return perCPUUsage, fmt.Errorf("unable to convert param value to uint64: %s", err) } perCPUUsage = append(perCPUUsage, value) } return perCPUUsage, nil }