mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 03:41:45 +00:00
Merge pull request #107108 from linxiulei/fix_pid
Read number of running processes from /proc/loadavg.
This commit is contained in:
commit
518a3c2f70
@ -20,8 +20,10 @@ limitations under the License.
|
|||||||
package pidlimit
|
package pidlimit
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -50,12 +52,36 @@ func Stats() (*statsapi.RlimitStats, error) {
|
|||||||
rlimit.MaxPID = &taskMax
|
rlimit.MaxPID = &taskMax
|
||||||
}
|
}
|
||||||
|
|
||||||
var info syscall.Sysinfo_t
|
// Prefer to read "/proc/loadavg" when possible because sysinfo(2)
|
||||||
syscall.Sysinfo(&info)
|
// returns truncated number when greater than 65538. See
|
||||||
procs := int64(info.Procs)
|
// https://github.com/kubernetes/kubernetes/issues/107107
|
||||||
rlimit.NumOfRunningProcesses = &procs
|
if procs, err := runningTaskCount(); err == nil {
|
||||||
|
rlimit.NumOfRunningProcesses = &procs
|
||||||
|
} else {
|
||||||
|
var info syscall.Sysinfo_t
|
||||||
|
syscall.Sysinfo(&info)
|
||||||
|
procs := int64(info.Procs)
|
||||||
|
rlimit.NumOfRunningProcesses = &procs
|
||||||
|
}
|
||||||
|
|
||||||
rlimit.Time = v1.NewTime(time.Now())
|
rlimit.Time = v1.NewTime(time.Now())
|
||||||
|
|
||||||
return rlimit, nil
|
return rlimit, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func runningTaskCount() (int64, error) {
|
||||||
|
// Example: 1.36 3.49 4.53 2/3518 3715089
|
||||||
|
bytes, err := ioutil.ReadFile("/proc/loadavg")
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
fields := strings.Fields(string(bytes))
|
||||||
|
if len(fields) < 5 {
|
||||||
|
return 0, fmt.Errorf("not enough fields in /proc/loadavg")
|
||||||
|
}
|
||||||
|
subfields := strings.Split(fields[3], "/")
|
||||||
|
if len(subfields) != 2 {
|
||||||
|
return 0, fmt.Errorf("error parsing fourth field of /proc/loadavg")
|
||||||
|
}
|
||||||
|
return strconv.ParseInt(subfields[1], 10, 64)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user