// Copyright 2023 TiKV Project Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build linux

package cgroup

import (
	"math"
	"os"
	"runtime"
	"strings"
)

// GetCgroupCPU returns the CPU usage and quota for the current cgroup.
func GetCgroupCPU() (CPUUsage, error) {
	cpuUsage, err := getCgroupCPUHelper("/")
	cpuUsage.NumCPU = runtime.NumCPU()
	return cpuUsage, err
}

// CPUQuotaToGOMAXPROCS converts the CPU quota applied to the calling process
// to a valid GOMAXPROCS value.
func CPUQuotaToGOMAXPROCS(minValue int) (int, CPUQuotaStatus, error) {
	quota, err := GetCgroupCPU()
	if err != nil {
		return -1, CPUQuotaUndefined, err
	}
	maxProcs := int(math.Ceil(quota.CPUShares()))
	if minValue > 0 && maxProcs < minValue {
		return minValue, CPUQuotaMinUsed, nil
	}
	return maxProcs, CPUQuotaUsed, nil
}

// GetCPUPeriodAndQuota returns CPU period and quota time of cgroup.
func GetCPUPeriodAndQuota() (period int64, quota int64, err error) {
	return getCgroupCPUPeriodAndQuota("/")
}

// InContainer returns true if the process is running in a container.
func InContainer() bool {
	v, err := os.ReadFile(procPathCGroup)
	if err != nil {
		return false
	}
	if strings.Contains(string(v), "docker") ||
		strings.Contains(string(v), "kubepods") ||
		strings.Contains(string(v), "containerd") {
		return true
	}
	return false
}
