diff options
author | gVisor bot <gvisor-bot@google.com> | 2019-09-06 02:29:31 +0000 |
---|---|---|
committer | gVisor bot <gvisor-bot@google.com> | 2019-09-06 02:29:31 +0000 |
commit | 9f3cc11c3be3b1ec32dd0c2177101797c6447910 (patch) | |
tree | 8661affe1ac372a10234ff81df0e966037c983ff /runsc/specutils | |
parent | fc961d4a53eee63e428ee7da3106fb84d197a0e6 (diff) | |
parent | 0bfffbcb0163cc7ebb0cbf47fa91247d208502be (diff) |
Merge 0bfffbcb (automated)
Diffstat (limited to 'runsc/specutils')
-rw-r--r-- | runsc/specutils/specutils.go | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/runsc/specutils/specutils.go b/runsc/specutils/specutils.go index 2eec92349..df435f88d 100644 --- a/runsc/specutils/specutils.go +++ b/runsc/specutils/specutils.go @@ -23,6 +23,7 @@ import ( "os" "path" "path/filepath" + "strconv" "strings" "syscall" "time" @@ -503,3 +504,41 @@ func RetryEintr(f func() (uintptr, uintptr, error)) (uintptr, uintptr, error) { } } } + +// GetOOMScoreAdj reads the given process' oom_score_adj +func GetOOMScoreAdj(pid int) (int, error) { + data, err := ioutil.ReadFile(fmt.Sprintf("/proc/%d/oom_score_adj", pid)) + if err != nil { + return 0, err + } + return strconv.Atoi(strings.TrimSpace(string(data))) +} + +// GetParentPid gets the parent process ID of the specified PID. +func GetParentPid(pid int) (int, error) { + data, err := ioutil.ReadFile(fmt.Sprintf("/proc/%d/stat", pid)) + if err != nil { + return 0, err + } + + var cpid string + var name string + var state string + var ppid int + // Parse after the binary name. + _, err = fmt.Sscanf(string(data), + "%v %v %v %d", + // cpid is ignored. + &cpid, + // name is ignored. + &name, + // state is ignored. + &state, + &ppid) + + if err != nil { + return 0, err + } + + return ppid, nil +} |