diff --git a/pkg/kubelet/stats/pidlimit/pidlimit_linux.go b/pkg/kubelet/stats/pidlimit/pidlimit_linux.go index 12141974299..d900be7f9fa 100644 --- a/pkg/kubelet/stats/pidlimit/pidlimit_linux.go +++ b/pkg/kubelet/stats/pidlimit/pidlimit_linux.go @@ -25,7 +25,7 @@ import ( "syscall" "time" - "k8s.io/apimachinery/pkg/apis/meta/v1" + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" statsapi "k8s.io/kubelet/pkg/apis/stats/v1alpha1" ) @@ -33,11 +33,22 @@ import ( func Stats() (*statsapi.RlimitStats, error) { rlimit := &statsapi.RlimitStats{} - if content, err := ioutil.ReadFile("/proc/sys/kernel/pid_max"); err == nil { - if maxPid, err := strconv.ParseInt(string(content[:len(content)-1]), 10, 64); err == nil { - rlimit.MaxPID = &maxPid + taskMax := int64(-1) + // Calculate the mininum of kernel.pid_max and kernel.threads-max as they both specify the + // 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 syscall.Sysinfo(&info) diff --git a/staging/src/k8s.io/kubelet/pkg/apis/stats/v1alpha1/types.go b/staging/src/k8s.io/kubelet/pkg/apis/stats/v1alpha1/types.go index 50622f5170e..5afbf452260 100644 --- a/staging/src/k8s.io/kubelet/pkg/apis/stats/v1alpha1/types.go +++ b/staging/src/k8s.io/kubelet/pkg/apis/stats/v1alpha1/types.go @@ -65,9 +65,11 @@ type NodeStats struct { type RlimitStats struct { 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"` - // 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"` }