Repository: opencontainers/runc
File: libcontainer/cgroups/fs2/cpu.go (Go)

Code before:
	}

	if r.CPUIdle != nil {
		if err := cgroups.WriteFile(dirPath, "cpu.idle", strconv.FormatInt(*r.CPUIdle, 10)); err != nil {
			return err
		}
	}

	// NOTE: .CpuShares is not used here. Conversion is the caller's responsibility.
	if r.CpuWeight != 0 {
		if err := cgroups.WriteFile(dirPath, "cpu.weight", strconv.FormatUint(r.CpuWeight, 10)); err != nil {
			return err
		}
	}

	var burst string
	if r.CpuBurst != 0 {
		burst = strconv.FormatUint(r.CpuBurst, 10)
		if err := cgroups.WriteFile(dirPath, "cpu.max.burst", burst); err != nil {
			// Sometimes when the burst to be set is larger
			// than the current one, it is rejected by the kernel
			// (EINVAL) as old_quota/new_burst exceeds the parent
			// cgroup quota limit. If this happens and the quota is
			// going to be set, ignore the error for now and retry
			// after setting the quota.
			if !errors.Is(err, unix.EINVAL) || r.CpuQuota == 0 {
				return err
			}
		} else {
			burst = ""
		}
	}
	if r.CpuQuota != 0 || r.CpuPeriod != 0 {
		str := "max"
		if r.CpuQuota > 0 {
			str = strconv.FormatInt(r.CpuQuota, 10)
		}
		period := r.CpuPeriod
		if period == 0 {
			// This default value is documented in
			// https://www.kernel.org/doc/html/latest/admin-guide/cgroup-v2.html
			period = 100000
		}
		str += " " + strconv.FormatUint(period, 10)
		if err := cgroups.WriteFile(dirPath, "cpu.max", str); err != nil {
			return err
		}
		if burst != "" {
			if err := cgroups.WriteFile(dirPath, "cpu.max.burst", burst); err != nil {
				return err
			}

Review comment (bug):
I don't know whether there is a case of `unix.ENOENT` in `cgroupv2` or not? I think there is a high probability that there is no this type case. But maybe I'm wrong.

Code after:
	}

	if r.CPUIdle != nil {
		if err := cgroups.WriteFile(dirPath, "cpu.idle", strconv.FormatInt(*r.CPUIdle, 10)); err != nil {
			return err
		}
	}

	// NOTE: .CpuShares is not used here. Conversion is the caller's responsibility.
	if r.CpuWeight != 0 {
		if err := cgroups.WriteFile(dirPath, "cpu.weight", strconv.FormatUint(r.CpuWeight, 10)); err != nil {
			return err
		}
	}

	var burst string
	if r.CpuBurst != nil {
		burst = strconv.FormatUint(*r.CpuBurst, 10)
		if err := cgroups.WriteFile(dirPath, "cpu.max.burst", burst); err != nil {
			// Sometimes when the burst to be set is larger
			// than the current one, it is rejected by the kernel
			// (EINVAL) as old_quota/new_burst exceeds the parent
			// cgroup quota limit. If this happens and the quota is
			// going to be set, ignore the error for now and retry
			// after setting the quota.
			if !errors.Is(err, unix.EINVAL) || r.CpuQuota == 0 {
				return err
			}
		} else {
			burst = ""
		}
	}
	if r.CpuQuota != 0 || r.CpuPeriod != 0 {
		str := "max"
		if r.CpuQuota > 0 {
			str = strconv.FormatInt(r.CpuQuota, 10)
		}
		period := r.CpuPeriod
		if period == 0 {
			// This default value is documented in
			// https://www.kernel.org/doc/html/latest/admin-guide/cgroup-v2.html
			period = 100000
		}
		str += " " + strconv.FormatUint(period, 10)
		if err := cgroups.WriteFile(dirPath, "cpu.max", str); err != nil {
			return err
		}
		if burst != "" {
			if err := cgroups.WriteFile(dirPath, "cpu.max.burst", burst); err != nil {
				return err
			}