Consider threads-max when deciding MaxPID.

Fixes kubernetes#107111
This commit is contained in:
Eric Lin 2021-12-17 20:56:23 +00:00
parent 712745cb67
commit fea15977c8
2 changed files with 19 additions and 6 deletions

View File

@ -25,7 +25,7 @@ import (
"syscall" "syscall"
"time" "time"
"k8s.io/apimachinery/pkg/apis/meta/v1" v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
statsapi "k8s.io/kubelet/pkg/apis/stats/v1alpha1" statsapi "k8s.io/kubelet/pkg/apis/stats/v1alpha1"
) )
@ -33,11 +33,22 @@ import (
func Stats() (*statsapi.RlimitStats, error) { func Stats() (*statsapi.RlimitStats, error) {
rlimit := &statsapi.RlimitStats{} rlimit := &statsapi.RlimitStats{}
if content, err := ioutil.ReadFile("/proc/sys/kernel/pid_max"); err == nil { taskMax := int64(-1)
if maxPid, err := strconv.ParseInt(string(content[:len(content)-1]), 10, 64); err == nil { // Calculate the mininum of kernel.pid_max and kernel.threads-max as they both specify the
rlimit.MaxPID = &maxPid // system-wide limit on the number of tasks.
for _, file := range []string{"/proc/sys/kernel/pid_max", "/proc/sys/kernel/threads-max"} {
if content, err := ioutil.ReadFile(file); err == nil {
if limit, err := strconv.ParseInt(string(content[:len(content)-1]), 10, 64); err == nil {
if taskMax == -1 || taskMax > limit {
taskMax = limit
} }
} }
}
}
// Both reads did not fail.
if taskMax >= 0 {
rlimit.MaxPID = &taskMax
}
var info syscall.Sysinfo_t var info syscall.Sysinfo_t
syscall.Sysinfo(&info) syscall.Sysinfo(&info)

View File

@ -65,9 +65,11 @@ type NodeStats struct {
type RlimitStats struct { type RlimitStats struct {
Time metav1.Time `json:"time"` Time metav1.Time `json:"time"`
// The max PID of OS. // The max number of extant process (threads, precisely on Linux) of OS. See RLIMIT_NPROC in getrlimit(2).
// The operating system ceiling on the number of process IDs that can be assigned.
// On Linux, tasks (either processes or threads) consume 1 PID each.
MaxPID *int64 `json:"maxpid,omitempty"` MaxPID *int64 `json:"maxpid,omitempty"`
// The number of running process in the OS. // The number of running process (threads, precisely on Linux) in the OS.
NumOfRunningProcesses *int64 `json:"curproc,omitempty"` NumOfRunningProcesses *int64 `json:"curproc,omitempty"`
} }