system

package
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 7, 2023 License: Apache-2.0 Imports: 32 Imported by: 3

Documentation

Overview

Copyright 2022 The Koordinator 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.

Index

Constants

View Source
const (
	DefaultCPUCFSPeriod    int64 = 100000
	CPUShareKubeBEValue    int64 = 2
	CPUShareUnitValue      int64 = 1024
	CFSQuotaUnlimitedValue int64 = -1
	// MemoryLimitUnlimitedValue denotes the unlimited value of cgroups-v1 memory.limit_in_bytes.
	// It derives from linux PAGE_COUNTER_MAX and may be different according to the PAGE_SIZE (here we suppose `4k`).
	// https://github.com/torvalds/linux/blob/ea4424be16887a37735d6550cfd0611528dbe5d9/mm/memcontrol.c#L5337
	MemoryLimitUnlimitedValue int64 = 0x7FFFFFFFFFFFF000 // 9223372036854771712 < math.MaxInt64

	// CgroupMaxSymbolStr only appears in cgroups-v2 files, we consider the value as MaxInt64
	CgroupMaxSymbolStr string = "max"
	// CgroupMaxValueStr math.MaxInt64; writing `memory.high` with this do the same as set as "max"
	CgroupMaxValueStr string = "9223372036854775807"
)
View Source
const (
	Cgroupfs CgroupDriverType = "cgroupfs"
	Systemd  CgroupDriverType = "systemd"

	KubeRootNameSystemd       = "kubepods.slice/"
	KubeBurstableNameSystemd  = "kubepods-burstable.slice/"
	KubeBesteffortNameSystemd = "kubepods-besteffort.slice/"

	KubeRootNameCgroupfs       = "kubepods/"
	KubeBurstableNameCgroupfs  = "burstable/"
	KubeBesteffortNameCgroupfs = "besteffort/"
)
View Source
const (
	CgroupCPUDir     string = "cpu/"
	CgroupCPUSetDir  string = "cpuset/"
	CgroupCPUAcctDir string = "cpuacct/"
	CgroupMemDir     string = "memory/"
	CgroupBlkioDir   string = "blkio/"

	CgroupV2Dir = ""
)
View Source
const (
	CFSBasePeriodValue int64 = 100000
	CFSQuotaMinValue   int64 = 1000 // min value except `-1`
	CPUSharesMinValue  int64 = 2
	CPUWeightMinValue  int64 = 1
	CPUWeightMaxValue  int64 = 10000

	CPUStatName      = "cpu.stat"
	CPUSharesName    = "cpu.shares"
	CPUCFSQuotaName  = "cpu.cfs_quota_us"
	CPUCFSPeriodName = "cpu.cfs_period_us"
	CPUBVTWarpNsName = "cpu.bvt_warp_ns"
	CPUBurstName     = "cpu.cfs_burst_us"
	CPUTasksName     = "tasks"
	CPUProcsName     = "cgroup.procs"
	CPUThreadsName   = "cgroup.threads"
	CPUMaxName       = "cpu.max"
	CPUWeightName    = "cpu.weight"

	CPUSetCPUSName          = "cpuset.cpus"
	CPUSetCPUSEffectiveName = "cpuset.cpus.effective"

	CPUAcctStatName           = "cpuacct.stat"
	CPUAcctUsageName          = "cpuacct.usage"
	CPUAcctCPUPressureName    = "cpu.pressure"
	CPUAcctMemoryPressureName = "memory.pressure"
	CPUAcctIOPressureName     = "io.pressure"

	MemoryLimitName            = "memory.limit_in_bytes"
	MemoryUsageName            = "memory.usage_in_bytes"
	MemoryStatName             = "memory.stat"
	MemoryNumaStatName         = "memory.numa_stat"
	MemoryWmarkRatioName       = "memory.wmark_ratio"
	MemoryWmarkScaleFactorName = "memory.wmark_scale_factor"
	MemoryWmarkMinAdjName      = "memory.wmark_min_adj"
	MemoryMinName              = "memory.min"  // anolis os or cgroups-v2
	MemoryLowName              = "memory.low"  // anolis os or cgroups-v2
	MemoryHighName             = "memory.high" // anolis os or cgroups-v2
	MemoryMaxName              = "memory.max"
	MemoryCurrentName          = "memory.current"
	MemoryPriorityName         = "memory.priority"
	MemoryUsePriorityOomName   = "memory.use_priority_oom"
	MemoryOomGroupName         = "memory.oom.group"

	BlkioTRIopsName = "blkio.throttle.read_iops_device"
	BlkioTRBpsName  = "blkio.throttle.read_bps_device"
	BlkioTWIopsName = "blkio.throttle.write_iops_device"
	BlkioTWBpsName  = "blkio.throttle.write_bps_device"
)
View Source
const (
	DS_MODE   = "dsMode"
	HOST_MODE = "hostMode"
)
View Source
const (
	CPUInfoFileName       string = "cpuinfo"
	KernelCmdlineFileName string = "cmdline"

	ResctrlName string = "resctrl"

	ResctrlDir string = "resctrl/"
	RdtInfoDir string = "info"
	L3CatDir   string = "L3"

	ResctrlSchemataName string = "schemata"
	ResctrlCbmMaskName  string = "cbm_mask"
	ResctrlTasksName    string = "tasks"

	// L3SchemataPrefix is the prefix of l3 cat schemata
	L3SchemataPrefix = "L3"
	// MbSchemataPrefix is the prefix of mba schemata
	MbSchemataPrefix = "MB"

	// other cpu vendor like "GenuineIntel"
	AMD_VENDOR_ID = "AuthenticAMD"
)
View Source
const (
	ProcStatName    = "stat"
	ProcMemInfoName = "meminfo"
	SysctlSubDir    = "sys"

	KernelSchedGroupIdentityEnable = "kernel/sched_group_identity_enabled"
)
View Source
const (
	ProcSysVmRelativePath   = "sys/vm/"
	MemcgReaperRelativePath = "kernel/mm/memcg_reaper/"

	MinFreeKbytesFileName        = "min_free_kbytes"
	WatermarkScaleFactorFileName = "watermark_scale_factor"
	MemcgReapBackGroundFileName  = "reap_background"
)
View Source
const ErrCgroupDir = "cgroup path or file not exist"
View Source
const ErrResourceUnsupportedPrefix = "resource is unsupported"

Variables

View Source
var (
	NaturalInt64Validator = &RangeValidator{min: 0, max: math.MaxInt64}

	CPUSharesValidator                      = &RangeValidator{min: CPUSharesMinValue, max: math.MaxInt64}
	CPUBurstValidator                       = &RangeValidator{min: 0, max: 100 * 10 * 100000}
	CPUBvtWarpNsValidator                   = &RangeValidator{min: -1, max: 2}
	CPUWeightValidator                      = &RangeValidator{min: CPUWeightMinValue, max: CPUWeightMaxValue}
	MemoryWmarkRatioValidator               = &RangeValidator{min: 0, max: 100}
	MemoryPriorityValidator                 = &RangeValidator{min: 0, max: 12}
	MemoryOomGroupValidator                 = &RangeValidator{min: 0, max: 1}
	MemoryUsePriorityOomValidator           = &RangeValidator{min: 0, max: 1}
	MemoryWmarkMinAdjValidator              = &RangeValidator{min: -25, max: 50}
	MemoryWmarkScaleFactorFileNameValidator = &RangeValidator{min: 1, max: 1000}

	CPUSetCPUSValidator = &CPUSetStrValidator{}
)
View Source
var (
	DefaultFactory = NewCgroupResourceFactory()

	CPUStat      = DefaultFactory.New(CPUStatName, CgroupCPUDir)
	CPUShares    = DefaultFactory.New(CPUSharesName, CgroupCPUDir).WithValidator(CPUSharesValidator)
	CPUCFSQuota  = DefaultFactory.New(CPUCFSQuotaName, CgroupCPUDir)
	CPUCFSPeriod = DefaultFactory.New(CPUCFSPeriodName, CgroupCPUDir)
	CPUBurst     = DefaultFactory.New(CPUBurstName, CgroupCPUDir).WithValidator(CPUBurstValidator).WithCheckSupported(SupportedIfFileExists)
	CPUBVTWarpNs = DefaultFactory.New(CPUBVTWarpNsName, CgroupCPUDir).WithValidator(CPUBvtWarpNsValidator).WithCheckSupported(SupportedIfFileExists)
	CPUTasks     = DefaultFactory.New(CPUTasksName, CgroupCPUDir)
	CPUProcs     = DefaultFactory.New(CPUProcsName, CgroupCPUDir)

	CPUSet = DefaultFactory.New(CPUSetCPUSName, CgroupCPUSetDir).WithValidator(CPUSetCPUSValidator)

	CPUAcctStat           = DefaultFactory.New(CPUAcctStatName, CgroupCPUAcctDir)
	CPUAcctUsage          = DefaultFactory.New(CPUAcctUsageName, CgroupCPUAcctDir)
	CPUAcctCPUPressure    = DefaultFactory.New(CPUAcctCPUPressureName, CgroupCPUAcctDir).WithSupported(SupportedIfFileExistsInKubepods(CPUAcctCPUPressureName, CgroupCPUAcctDir))
	CPUAcctMemoryPressure = DefaultFactory.New(CPUAcctMemoryPressureName, CgroupCPUAcctDir).WithSupported(SupportedIfFileExistsInKubepods(CPUAcctMemoryPressureName, CgroupCPUAcctDir))
	CPUAcctIOPressure     = DefaultFactory.New(CPUAcctIOPressureName, CgroupCPUAcctDir).WithSupported(SupportedIfFileExistsInKubepods(CPUAcctIOPressureName, CgroupCPUAcctDir))

	MemoryLimit            = DefaultFactory.New(MemoryLimitName, CgroupMemDir)
	MemoryUsage            = DefaultFactory.New(MemoryUsageName, CgroupMemDir)
	MemoryStat             = DefaultFactory.New(MemoryStatName, CgroupMemDir)
	MemoryNumaStat         = DefaultFactory.New(MemoryNumaStatName, CgroupMemDir)
	MemoryWmarkRatio       = DefaultFactory.New(MemoryWmarkRatioName, CgroupMemDir).WithValidator(MemoryWmarkRatioValidator).WithSupported(SupportedIfFileExistsInKubepods(MemoryWmarkRatioName, CgroupMemDir))
	MemoryWmarkScaleFactor = DefaultFactory.New(MemoryWmarkScaleFactorName, CgroupMemDir).WithValidator(MemoryWmarkScaleFactorFileNameValidator).WithSupported(SupportedIfFileExistsInKubepods(MemoryWmarkScaleFactorName, CgroupMemDir))
	MemoryWmarkMinAdj      = DefaultFactory.New(MemoryWmarkMinAdjName, CgroupMemDir).WithValidator(MemoryWmarkMinAdjValidator).WithSupported(SupportedIfFileExistsInKubepods(MemoryWmarkMinAdjName, CgroupMemDir))
	MemoryMin              = DefaultFactory.New(MemoryMinName, CgroupMemDir).WithValidator(NaturalInt64Validator).WithSupported(SupportedIfFileExistsInKubepods(MemoryMinName, CgroupMemDir))
	MemoryLow              = DefaultFactory.New(MemoryLowName, CgroupMemDir).WithValidator(NaturalInt64Validator).WithSupported(SupportedIfFileExistsInKubepods(MemoryLowName, CgroupMemDir))
	MemoryHigh             = DefaultFactory.New(MemoryHighName, CgroupMemDir).WithValidator(NaturalInt64Validator).WithSupported(SupportedIfFileExistsInKubepods(MemoryHighName, CgroupMemDir))
	MemoryPriority         = DefaultFactory.New(MemoryPriorityName, CgroupMemDir).WithValidator(MemoryPriorityValidator).WithSupported(SupportedIfFileExistsInKubepods(MemoryPriorityName, CgroupMemDir))
	MemoryUsePriorityOom   = DefaultFactory.New(MemoryUsePriorityOomName, CgroupMemDir).WithValidator(MemoryUsePriorityOomValidator).WithSupported(SupportedIfFileExistsInKubepods(MemoryUsePriorityOomName, CgroupMemDir))
	MemoryOomGroup         = DefaultFactory.New(MemoryOomGroupName, CgroupMemDir).WithValidator(MemoryOomGroupValidator).WithSupported(SupportedIfFileExistsInKubepods(MemoryOomGroupName, CgroupMemDir))

	BlkioReadIops  = DefaultFactory.New(BlkioTRIopsName, CgroupBlkioDir) // TODO: add validator for blkio.throttle
	BlkioReadBps   = DefaultFactory.New(BlkioTRBpsName, CgroupBlkioDir)
	BlkioWriteIops = DefaultFactory.New(BlkioTWIopsName, CgroupBlkioDir)
	BlkioWriteBps  = DefaultFactory.New(BlkioTWBpsName, CgroupBlkioDir)

	CPUCFSQuotaV2  = DefaultFactory.NewV2(CPUCFSQuotaName, CPUMaxName)
	CPUCFSPeriodV2 = DefaultFactory.NewV2(CPUCFSPeriodName, CPUMaxName)
	CPUSharesV2    = DefaultFactory.NewV2(CPUSharesName, CPUWeightName).WithValidator(CPUWeightValidator)
	CPUStatV2      = DefaultFactory.NewV2(CPUStatName, CPUStatName)
	CPUAcctStatV2  = DefaultFactory.NewV2(CPUAcctStatName, CPUStatName)
	CPUAcctUsageV2 = DefaultFactory.NewV2(CPUAcctUsageName, CPUStatName)

	CPUAcctCPUPressureV2    = DefaultFactory.NewV2(CPUAcctCPUPressureName, CPUAcctCPUPressureName).WithSupported(SupportedIfFileExistsInKubepods(CPUAcctCPUPressureName, ""))
	CPUAcctMemoryPressureV2 = DefaultFactory.NewV2(CPUAcctMemoryPressureName, CPUAcctMemoryPressureName).WithSupported(SupportedIfFileExistsInKubepods(CPUAcctMemoryPressureName, ""))
	CPUAcctIOPressureV2     = DefaultFactory.NewV2(CPUAcctIOPressureName, CPUAcctIOPressureName).WithSupported(SupportedIfFileExistsInKubepods(CPUAcctIOPressureName, ""))

	CPUSetV2                 = DefaultFactory.NewV2(CPUSetCPUSName, CPUSetCPUSName).WithValidator(CPUSetCPUSValidator)
	CPUSetEffectiveV2        = DefaultFactory.NewV2(CPUSetCPUSEffectiveName, CPUSetCPUSEffectiveName) // TODO: unify the R/W
	CPUTasksV2               = DefaultFactory.NewV2(CPUTasksName, CPUThreadsName)
	CPUProcsV2               = DefaultFactory.NewV2(CPUProcsName, CPUProcsName)
	MemoryLimitV2            = DefaultFactory.NewV2(MemoryLimitName, MemoryMaxName)
	MemoryUsageV2            = DefaultFactory.NewV2(MemoryUsageName, MemoryCurrentName)
	MemoryStatV2             = DefaultFactory.NewV2(MemoryStatName, MemoryStatName)
	MemoryNumaStatV2         = DefaultFactory.NewV2(MemoryNumaStatName, MemoryNumaStatName)
	MemoryMinV2              = DefaultFactory.NewV2(MemoryMinName, MemoryMinName).WithValidator(NaturalInt64Validator)
	MemoryLowV2              = DefaultFactory.NewV2(MemoryLowName, MemoryLowName).WithValidator(NaturalInt64Validator)
	MemoryHighV2             = DefaultFactory.NewV2(MemoryHighName, MemoryHighName).WithValidator(NaturalInt64Validator)
	MemoryWmarkRatioV2       = DefaultFactory.NewV2(MemoryWmarkRatioName, MemoryWmarkRatioName).WithValidator(MemoryWmarkRatioValidator).WithCheckSupported(SupportedIfFileExists)
	MemoryWmarkScaleFactorV2 = DefaultFactory.NewV2(MemoryWmarkScaleFactorName, MemoryWmarkScaleFactorName).WithValidator(MemoryWmarkScaleFactorFileNameValidator).WithCheckSupported(SupportedIfFileExists)
	MemoryWmarkMinAdjV2      = DefaultFactory.NewV2(MemoryWmarkMinAdjName, MemoryWmarkMinAdjName).WithValidator(MemoryWmarkMinAdjValidator).WithCheckSupported(SupportedIfFileExists)
	MemoryPriorityV2         = DefaultFactory.NewV2(MemoryPriorityName, MemoryPriorityName).WithValidator(MemoryPriorityValidator).WithCheckSupported(SupportedIfFileExists)
	MemoryUsePriorityOomV2   = DefaultFactory.NewV2(MemoryUsePriorityOomName, MemoryUsePriorityOomName).WithValidator(MemoryUsePriorityOomValidator).WithCheckSupported(SupportedIfFileExists)
	MemoryOomGroupV2         = DefaultFactory.NewV2(MemoryOomGroupName, MemoryOomGroupName).WithValidator(MemoryOomGroupValidator).WithCheckSupported(SupportedIfFileExists)
)

for cgroup resources, we use the corresponding cgroups-v1 filename as its resource type

View Source
var (
	MinFreeKbytesValidator        = &RangeValidator{min: 10 * 1024, max: 10 * 1024 * 1024}
	WatermarkScaleFactorValidator = &RangeValidator{min: 10, max: 400}
	MemcgReapBackGroundValidator  = &RangeValidator{min: 0, max: 1}
)
View Source
var AgentMode = DS_MODE
View Source
var CgroupPathFormatter = cgroupPathFormatterInSystemd

default use Systemd cgroup path format

View Source
var (
	CommonRootDir = "" // for uni-test
)
View Source
var DefaultRegistry = NewCgroupResourceRegistry()
View Source
var ExecCmdOnHost = execCmdOnHostFn

If running in container, exec command by 'nsenter --mount=/proc/1/ns/mnt ${cmds}'. return stdout, exitcode, error

View Source
var HostSystemInfo = collectVersionInfo()
View Source
var (
	// Jiffies is the duration unit of CPU stats. Normally, it is 10ms.
	Jiffies = float64(10 * time.Millisecond)
)
View Source
var PidOf = pidOfFn

PidOf finds process(es) with a specified name (regexp match) and return their pid(s).

View Source
var UseCgroupsV2 bool

Functions

func CalcCPUThrottledRatio

func CalcCPUThrottledRatio(curPoint, prePoint *CPUStatRaw) float64

func CalculateCatL3MaskValue added in v1.1.0

func CalculateCatL3MaskValue(cbm uint, startPercent, endPercent int64) (string, error)

func CheckAndTryEnableResctrlCat

func CheckAndTryEnableResctrlCat() error

CheckAndTryEnableResctrlCat checks if resctrl and l3_cat are enabled; if not, try to enable the features by mount resctrl subsystem; See MountResctrlSubsystem() for the detail. It returns whether the resctrl cat is enabled, and the error if failed to enable or to check resctrl interfaces

func CheckIfAllSupported added in v1.1.0

func CheckIfAllSupported(checkSupportedFns ...func() (bool, string)) func() (bool, string)

func CommonFileRead

func CommonFileRead(file string) (string, error)

func CommonFileWrite

func CommonFileWrite(file string, data string) error

func CommonFileWriteIfDifferent

func CommonFileWriteIfDifferent(file string, value string) (bool, error)

func ConvertCPUSharesToWeight added in v1.1.0

func ConvertCPUSharesToWeight(s string) (int64, error)

func ConvertCPUWeightToShares added in v1.1.0

func ConvertCPUWeightToShares(v int64) (int64, error)

ConvertCPUWeightToShares converts the value of `cpu.weight` (cgroups-v2) into the value of `cpu.shares` (cgroups-v1)

func FileExists

func FileExists(path string) bool

func GetCacheInfo added in v1.1.0

func GetCacheInfo(str string) (string, int32, error)

GetCacheInfo parses the output of `lscpu -e=CACHE` into l1l2 and l3 infos e.g. - input: "1:1:1:0" - output: "1", 0, nil

func GetCgroupFilePath

func GetCgroupFilePath(cgroupTaskDir string, r Resource) string

GetCgroupFilePath gets the full path of the given cgroup dir and resource. @cgroupTaskDir kubepods.slice/kubepods-pod7712555c_ce62_454a_9e18_9ff0217b8941.slice/ @return /sys/fs/cgroup/cpu/kubepods.slice/kubepods-pod7712555c_ce62_454a_9e18_9ff0217b8941.slice/cpu.shares

func GetKubeletCommandline added in v1.1.0

func GetKubeletCommandline(port int) ([]string, error)

func GetPeriodTicks added in v1.1.0

func GetPeriodTicks(start, end time.Time) float64

func GetProcFilePath added in v1.1.0

func GetProcFilePath(procRelativePath string) string

func GetProcRootDir added in v1.2.0

func GetProcRootDir() string

func GetProcSysFilePath added in v1.1.0

func GetProcSysFilePath(file string) string

func GetResctrlGroupRootDirPath

func GetResctrlGroupRootDirPath(groupPath string) string

@groupPath BE @return /sys/fs/resctrl/BE

func GetResctrlL3CbmFilePath

func GetResctrlL3CbmFilePath() string

@return /sys/fs/resctrl/info/L3/cbm_mask

func GetResctrlSchemataFilePath

func GetResctrlSchemataFilePath(groupPath string) string

@groupPath BE @return /sys/fs/resctrl/BE/schemata

func GetResctrlSubsystemDirPath

func GetResctrlSubsystemDirPath() string

@return /sys/fs/resctrl

func GetResctrlTasksFilePath

func GetResctrlTasksFilePath(groupPath string) string

@groupPath BE @return /sys/fs/resctrl/BE/tasks

func GetSysRootDir added in v1.2.0

func GetSysRootDir() string

func GetVendorIDByCPUInfo added in v1.2.0

func GetVendorIDByCPUInfo(path string) (string, error)

return vendor_id like AuthenticAMD from cpu info, e.g. vendor_id : AuthenticAMD vendor_id : GenuineIntel

func InitCatGroupIfNotExist added in v1.1.0

func InitCatGroupIfNotExist(group string) error

func IsCgroupV2Resource added in v1.2.0

func IsCgroupV2Resource(r Resource) bool

func IsResourceUnsupportedErr added in v1.1.0

func IsResourceUnsupportedErr(err error) bool

func IsSupportResctrl added in v1.1.0

func IsSupportResctrl() (bool, error)

func IsUsingCgroupsV2 added in v1.1.0

func IsUsingCgroupsV2() bool

modify base: github.com/opencontainers/runc/libcontainer/cgroups/utils.go IsCgroup2UnifiedMode

func KubeletPortToPid added in v1.1.0

func KubeletPortToPid(port int) (int, error)

KubeletPortToPid Query pid by tcp port number with the help of go-netstat note: Due to the low efficiency of full traversal, we cache the result and verify each time

func MountResctrlSubsystem

func MountResctrlSubsystem() (bool, error)

MountResctrlSubsystem mounts resctrl fs under the sysFSRoot to enable the kernel feature on supported environment NOTE: Linux kernel (>= 4.10), Intel cpu and bare-mental host are required; Also, Intel RDT features should be enabled in kernel configurations and kernel commandline. For more info, please see https://github.com/intel/intel-cmt-cat/wiki/resctrl

func NewProcSysctl added in v1.1.0

func NewProcSysctl() utilsysctl.Interface

func ParseCPUAcctUsageV2 added in v1.1.0

func ParseCPUAcctUsageV2(content string) (uint64, error)

func ParseCPUCFSPeriodV2 added in v1.1.0

func ParseCPUCFSPeriodV2(content string) (int64, error)

func ParseCPUCFSQuotaV2 added in v1.1.0

func ParseCPUCFSQuotaV2(content string) (int64, error)

func ParseKVMap added in v1.1.0

func ParseKVMap(content string) map[string]string

ParseKVMap parses a file content into a KV map. e.g. `user 100\nsystem 20` -> `{"user": "100", "system": "20"}`

func ParseResctrlSchemataMap added in v1.1.0

func ParseResctrlSchemataMap(content string) map[string]map[int]string

ParseResctrlSchemataMap parses the content of resctrl schemata. e.g. schemata=`L3:0=fff;1=fff\nMB:0=100;1=100\n` -> `{"L3": {0: "fff", 1: "fff"}, "MB": {0: "100", 1: "100"}}`

func PathExists

func PathExists(path string) (bool, error)

func ProcCmdLine

func ProcCmdLine(procRoot string, pid int) ([]string, error)

CmdLine returns the command line args of a process.

func ReadCatL3CbmString

func ReadCatL3CbmString() (string, error)

ReadCatL3CbmString reads and returns the value of cat l3 cbm_mask

func ReadFileNoStat

func ReadFileNoStat(filename string) ([]byte, error)

ReadFileNoStat uses io.ReadAll to read contents of entire file. This is similar to io.ReadFile but without the call to os.Stat, because many files in /proc and /sys report incorrect file sizes (either 0 or 4096). Reads a max file size of 512kB. For files larger than this, a scanner should be used.

func ReadResctrlTasksMap

func ReadResctrlTasksMap(groupPath string) (map[int32]struct{}, error)

ReadResctrlTasksMap reads and returns the map of given resctrl group's task ids

func ResourceUnsupportedErr added in v1.1.0

func ResourceUnsupportedErr(msg string) error

func SetConf

func SetConf(config Config)

func SetSchedGroupIdentity added in v1.1.0

func SetSchedGroupIdentity(enable bool) error

func SetupCgroupPathFormatter

func SetupCgroupPathFormatter(driver CgroupDriverType)

func SupportedIfFileExists added in v1.1.0

func SupportedIfFileExists(r Resource, dynamicPath string) (bool, string)

func SupportedIfFileExistsInKubepods added in v1.1.0

func SupportedIfFileExistsInKubepods(filename string, subfs string) (bool, string)

func TCPSocks added in v1.1.0

func TCPSocks(fn netstat.AcceptFn) ([]netstat.SockTabEntry, error)

func ValidateResourceValue added in v1.1.0

func ValidateResourceValue(value *int64, dynamicPath string, r Resource) bool

func WorkingDirOf added in v1.1.0

func WorkingDirOf(pid int) (string, error)

return working dir of process

Types

type CPUSetStrValidator added in v1.1.0

type CPUSetStrValidator struct{}

func (*CPUSetStrValidator) Validate added in v1.1.0

func (c *CPUSetStrValidator) Validate(value string) (bool, string)

type CPUStatRaw

type CPUStatRaw struct {
	NrPeriods            int64
	NrThrottled          int64
	ThrottledNanoSeconds int64
}

func ParseCPUStatRaw added in v1.1.0

func ParseCPUStatRaw(content string) (*CPUStatRaw, error)

func ParseCPUStatRawV2 added in v1.1.0

func ParseCPUStatRawV2(content string) (*CPUStatRaw, error)

type CPUStatV2Raw added in v1.1.0

type CPUStatV2Raw struct {
	UsageUsec  int64
	UserUsec   int64
	SystemUSec int64

	NrPeriods     int64
	NrThrottled   int64
	ThrottledUSec int64
}

func ParseCPUAcctStatRawV2 added in v1.1.0

func ParseCPUAcctStatRawV2(content string) (*CPUStatV2Raw, error)

type CgroupDriverType

type CgroupDriverType string

func GuessCgroupDriverFromCgroupName

func GuessCgroupDriverFromCgroupName() CgroupDriverType

func GuessCgroupDriverFromKubeletPort added in v1.1.0

func GuessCgroupDriverFromKubeletPort(port int) (CgroupDriverType, error)

Guess Kubelet's cgroup driver from kubelet port.

  1. use KubeletPortToPid to get kubelet pid.
  2. If '--cgroup-driver' in args, that's it. else if '--config' not in args, is default driver('cgroupfs'). else go to step-3.
  3. If kubelet config is relative path, join with /proc/${pidof kubelet}/cwd. search 'cgroupDriver:' in kubelet config file, that's it.

func (CgroupDriverType) Validate

func (c CgroupDriverType) Validate() bool

type CgroupResource added in v1.1.0

type CgroupResource struct {
	Type           ResourceType
	FileName       string
	Subfs          string
	Supported      *bool
	SupportMsg     string
	CheckSupported func(r Resource, parentDir string) (isSupported bool, msg string)
	Validator      ResourceValidator
	CgroupVersion  CgroupVersion
}

func (*CgroupResource) GetCgroupVersion added in v1.2.0

func (c *CgroupResource) GetCgroupVersion() CgroupVersion

func (*CgroupResource) IsSupported added in v1.1.0

func (c *CgroupResource) IsSupported(parentDir string) (bool, string)

func (*CgroupResource) IsValid added in v1.1.0

func (c *CgroupResource) IsValid(v string) (bool, string)

func (*CgroupResource) Path added in v1.1.0

func (c *CgroupResource) Path(parentDir string) string

func (*CgroupResource) ResourceType added in v1.1.0

func (c *CgroupResource) ResourceType() ResourceType

func (*CgroupResource) SetCgroupVersion added in v1.2.0

func (c *CgroupResource) SetCgroupVersion(cv CgroupVersion)

func (*CgroupResource) WithCheckSupported added in v1.1.0

func (c *CgroupResource) WithCheckSupported(checkSupportedFn func(r Resource, parentDir string) (isSupported bool, msg string)) Resource

func (*CgroupResource) WithSupported added in v1.1.0

func (c *CgroupResource) WithSupported(isSupported bool, msg string) Resource

func (*CgroupResource) WithValidator added in v1.1.0

func (c *CgroupResource) WithValidator(validator ResourceValidator) Resource

type CgroupResourceFactory added in v1.1.0

type CgroupResourceFactory interface {
	New(filename string, subfs string) Resource // cgroup-v1 filename represents the resource type
	NewV2(t ResourceType, filename string) Resource
}

func NewCgroupResourceFactory added in v1.1.0

func NewCgroupResourceFactory() CgroupResourceFactory

type CgroupResourceRegistry added in v1.1.0

type CgroupResourceRegistry interface {
	Add(v CgroupVersion, s ...Resource)
	Get(v CgroupVersion, t ResourceType) (Resource, bool)
}

func NewCgroupResourceRegistry added in v1.1.0

func NewCgroupResourceRegistry() CgroupResourceRegistry

type CgroupResourceRegistryImpl added in v1.1.0

type CgroupResourceRegistryImpl struct {
	// contains filtered or unexported fields
}

func (*CgroupResourceRegistryImpl) Add added in v1.1.0

func (*CgroupResourceRegistryImpl) Get added in v1.1.0

type CgroupVersion added in v1.1.0

type CgroupVersion int32
const (
	CgroupVersionV1 CgroupVersion = 1
	CgroupVersionV2 CgroupVersion = 2
)

func GetCurrentCgroupVersion added in v1.1.0

func GetCurrentCgroupVersion() CgroupVersion

type Config

type Config struct {
	CgroupRootDir         string
	CgroupKubePath        string
	SysRootDir            string
	SysFSRootDir          string
	ProcRootDir           string
	VarRunRootDir         string
	NodeNameOverride      string
	RuntimeHooksConfigDir string

	ContainerdEndPoint string
	DockerEndPoint     string
}

func NewDsModeConfig

func NewDsModeConfig() *Config

func NewHostModeConfig

func NewHostModeConfig() *Config

func (*Config) InitFlags

func (c *Config) InitFlags(fs *flag.FlagSet)

type FileTestUtil

type FileTestUtil struct {
	// Temporary directory to store mock cgroup filesystem.
	TempDir string
	// contains filtered or unexported fields
}

func NewFileTestUtil

func NewFileTestUtil(t *testing.T) *FileTestUtil

NewFileTestUtil creates a new test util for the specified subsystem. NOTE: this function should be called only for testing purposes.

func (*FileTestUtil) Cleanup

func (c *FileTestUtil) Cleanup()

func (*FileTestUtil) CreateCgroupFile

func (c *FileTestUtil) CreateCgroupFile(taskDir string, r Resource)

func (*FileTestUtil) CreateFile

func (c *FileTestUtil) CreateFile(testFilePath string)

if filePath contain TempDir, createFile direct, else join with TempDir and create

func (*FileTestUtil) CreateProcSubFile

func (c *FileTestUtil) CreateProcSubFile(fileRelativePath string)

func (*FileTestUtil) MkDirAll

func (c *FileTestUtil) MkDirAll(testDir string)

if dir contain TempDir, mkdir direct, else join with TempDir and mkdir

func (*FileTestUtil) ReadCgroupFileContents

func (c *FileTestUtil) ReadCgroupFileContents(taskDir string, r Resource) string

func (*FileTestUtil) ReadCgroupFileContentsInt added in v1.2.0

func (c *FileTestUtil) ReadCgroupFileContentsInt(taskDir string, r Resource) *int64

func (*FileTestUtil) ReadFileContents

func (c *FileTestUtil) ReadFileContents(testFilePath string) string

if filePath contain TempDir, read direct, else join with TempDir and read

func (*FileTestUtil) ReadProcSubFileContents

func (c *FileTestUtil) ReadProcSubFileContents(relativeFilePath string) string

func (*FileTestUtil) SetAnolisOSResourcesSupported added in v1.1.0

func (c *FileTestUtil) SetAnolisOSResourcesSupported(supported bool)

func (*FileTestUtil) SetCgroupsV2 added in v1.1.0

func (c *FileTestUtil) SetCgroupsV2(useCgroupsV2 bool)

func (*FileTestUtil) SetResourcesSupported added in v1.1.0

func (c *FileTestUtil) SetResourcesSupported(supported bool, resources ...Resource)

func (*FileTestUtil) WriteCgroupFileContents

func (c *FileTestUtil) WriteCgroupFileContents(taskDir string, r Resource, contents string)

WriteCgroupFileContents is only intended for test functions. For specific read/write functionalities, please refer to the executor package.

func (*FileTestUtil) WriteFileContents

func (c *FileTestUtil) WriteFileContents(testFilePath, contents string)

if filePath contain TempDir, write direct, else join with TempDir and write

func (*FileTestUtil) WriteProcSubFileContents

func (c *FileTestUtil) WriteProcSubFileContents(relativeFilePath string, contents string)

type MemoryStatRaw added in v1.1.0

type MemoryStatRaw struct {
	Cache        int64
	RSS          int64
	InactiveFile int64
	ActiveFile   int64
	InactiveAnon int64
	ActiveAnon   int64
	Unevictable  int64
}

func ParseMemoryStatRaw added in v1.1.0

func ParseMemoryStatRaw(content string) (*MemoryStatRaw, error)

func ParseMemoryStatRawV2 added in v1.1.0

func ParseMemoryStatRawV2(content string) (*MemoryStatRaw, error)

func (*MemoryStatRaw) Usage added in v1.1.0

func (m *MemoryStatRaw) Usage() int64

type NumaMemoryPages added in v1.2.0

type NumaMemoryPages struct {
	NumaId   int
	PagesNum uint64
}

func ParseMemoryNumaStat added in v1.2.0

func ParseMemoryNumaStat(content string) ([]NumaMemoryPages, error)

func ParseMemoryNumaStatV2 added in v1.2.0

func ParseMemoryNumaStatV2(content string) ([]NumaMemoryPages, error)

type ProcSysctl added in v1.1.0

type ProcSysctl struct{}

ProcSysctl implements Interface by reading and writing files under /proc/sys

func (*ProcSysctl) GetSysctl added in v1.1.0

func (*ProcSysctl) GetSysctl(sysctl string) (int, error)

func (*ProcSysctl) SetSysctl added in v1.1.0

func (*ProcSysctl) SetSysctl(sysctl string, newVal int) error

SetSysctl modifies the specified sysctl flag to the new value

type RangeValidator

type RangeValidator struct {
	// contains filtered or unexported fields
}

func (*RangeValidator) Validate

func (r *RangeValidator) Validate(value string) (bool, string)

type ResctrlResource added in v1.1.0

type ResctrlResource struct {
	Type           ResourceType
	FileName       string
	Subdir         string
	CheckSupported func(r Resource, parentDir string) (isSupported bool, msg string)
	Validator      ResourceValidator
}

func (*ResctrlResource) IsSupported added in v1.1.0

func (r *ResctrlResource) IsSupported(parentDir string) (bool, string)

func (*ResctrlResource) IsValid added in v1.1.0

func (r *ResctrlResource) IsValid(v string) (bool, string)

func (*ResctrlResource) Path added in v1.1.0

func (r *ResctrlResource) Path(parentDir string) string

func (*ResctrlResource) ResourceType added in v1.1.0

func (r *ResctrlResource) ResourceType() ResourceType

func (*ResctrlResource) WithCheckSupported added in v1.1.0

func (r *ResctrlResource) WithCheckSupported(checkSupportedFn func(r Resource, parentDir string) (isSupported bool, msg string)) Resource

func (*ResctrlResource) WithSupported added in v1.1.0

func (r *ResctrlResource) WithSupported(isSupported bool, msg string) Resource

func (*ResctrlResource) WithValidator added in v1.1.0

func (r *ResctrlResource) WithValidator(validator ResourceValidator) Resource

type ResctrlSchemataRaw added in v1.1.0

type ResctrlSchemataRaw struct {
	L3    []int64
	MB    []int64
	L3Num int
}

func NewResctrlSchemataRaw added in v1.1.0

func NewResctrlSchemataRaw() *ResctrlSchemataRaw

func ReadResctrlSchemataRaw added in v1.1.0

func ReadResctrlSchemataRaw(schemataFile string, l3Num int) (*ResctrlSchemataRaw, error)

func (*ResctrlSchemataRaw) DeepCopy added in v1.1.0

func (r *ResctrlSchemataRaw) DeepCopy() *ResctrlSchemataRaw

func (*ResctrlSchemataRaw) Equal added in v1.1.0

func (*ResctrlSchemataRaw) L3Number added in v1.1.0

func (r *ResctrlSchemataRaw) L3Number() int

func (*ResctrlSchemataRaw) L3String added in v1.1.0

func (r *ResctrlSchemataRaw) L3String() string

func (*ResctrlSchemataRaw) MBString added in v1.1.0

func (r *ResctrlSchemataRaw) MBString() string

func (*ResctrlSchemataRaw) ParseResctrlSchemata added in v1.1.0

func (r *ResctrlSchemataRaw) ParseResctrlSchemata(content string, l3Num int) error

ParseResctrlSchemata parses the resctrl schemata of given cgroup, and returns the l3_cat masks and mba masks. @content `L3:0=fff;1=fff\nMB:0=100;1=100\n` (may have additional lines (e.g. ARM MPAM)) @l3Num 2 @return { L3: ["fff", "fff"], MB: ["100", "100"] }, nil

func (*ResctrlSchemataRaw) Prefix added in v1.1.0

func (r *ResctrlSchemataRaw) Prefix() string

func (*ResctrlSchemataRaw) WithL3Mask added in v1.1.0

func (r *ResctrlSchemataRaw) WithL3Mask(mask string) *ResctrlSchemataRaw

func (*ResctrlSchemataRaw) WithL3Num added in v1.1.0

func (r *ResctrlSchemataRaw) WithL3Num(l3Num int) *ResctrlSchemataRaw

func (*ResctrlSchemataRaw) WithMB added in v1.2.0

func (r *ResctrlSchemataRaw) WithMB(valueOrPercent string) *ResctrlSchemataRaw

type Resource added in v1.1.0

type Resource interface {
	// ResourceType is the type of system resource. e.g. "cpu.cfs_quota_us", "cpu.cfs_period_us", "schemata"
	ResourceType() ResourceType
	// Path is the generated system file path according to the given parent directory.
	// e.g. "/host-cgroup/kubepods/kubepods-podxxx/cpu.shares"
	Path(dynamicPath string) string
	// IsSupported checks whether the system resource is supported in current platform
	IsSupported(dynamicPath string) (bool, string)
	// IsValid checks whether the given value is valid for the system resource's content
	IsValid(v string) (bool, string)
	// WithValidator sets the ResourceValidator for the resource
	WithValidator(validator ResourceValidator) Resource
	// WithSupported sets the Supported status of the resource when it is initialized.
	WithSupported(supported bool, msg string) Resource
	// WithCheckSupported sets the check function for the Supported status of given resource and parent directory.
	WithCheckSupported(checkSupportedFn func(r Resource, dynamicPath string) (isSupported bool, msg string)) Resource
}

func GetCgroupResource added in v1.1.0

func GetCgroupResource(resourceType ResourceType) (Resource, error)

func NewCommonCgroupResource added in v1.1.0

func NewCommonCgroupResource(resourceType ResourceType, filename string, subfs string) Resource

func NewCommonResctrlResource added in v1.1.0

func NewCommonResctrlResource(filename string, subdir string) Resource

func NewCommonSystemResource added in v1.2.0

func NewCommonSystemResource(relativePath, fileName string, Rootdir func() string) Resource

type ResourceType added in v1.1.0

type ResourceType string

func GetDefaultResourceType added in v1.1.0

func GetDefaultResourceType(subfs string, filename string) ResourceType

type ResourceValidator added in v1.1.0

type ResourceValidator interface {
	Validate(value string) (isValid bool, msg string)
}

ResourceValidator validates the resource value

type SystemResource added in v1.2.0

type SystemResource struct {
	Type         ResourceType
	RootDir      func() string
	RelativePath string
	FileName     string
	Validator    ResourceValidator

	Supported      *bool
	SupportMsg     string
	CheckSupported func(r Resource, dynamicPath string) (isSupported bool, msg string)
}

func (*SystemResource) IsSupported added in v1.2.0

func (c *SystemResource) IsSupported(dynamicPath string) (bool, string)

func (*SystemResource) IsValid added in v1.2.0

func (c *SystemResource) IsValid(v string) (bool, string)

func (*SystemResource) Path added in v1.2.0

func (c *SystemResource) Path(dynamicPath string) string

func (*SystemResource) ResourceType added in v1.2.0

func (c *SystemResource) ResourceType() ResourceType

func (*SystemResource) WithCheckSupported added in v1.2.0

func (c *SystemResource) WithCheckSupported(checkSupportedFn func(r Resource, parentDir string) (isSupported bool, msg string)) Resource

func (*SystemResource) WithSupported added in v1.2.0

func (c *SystemResource) WithSupported(isSupported bool, msg string) Resource

func (*SystemResource) WithValidator added in v1.2.0

func (c *SystemResource) WithValidator(validator ResourceValidator) Resource

type VersionInfo

type VersionInfo struct {
	// Open Anolis OS (kernel): https://github.com/alibaba/cloud-kernel
	IsAnolisOS bool
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL