summaryrefslogtreecommitdiff
path: root/ui/metrics/proc/status_linux.go
blob: dc0f943dd681a35406dcd4c14a72220d63dbbc98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package proc

import (
	"io/ioutil"
	"path/filepath"
	"strconv"
	"strings"

	"android/soong/finder/fs"
)

// NewProcStatus returns an instance of the ProcStatus that contains memory
// information of the process. The memory information is extracted from the
// "/proc/<pid>/status" text file. This is only available for Linux
// distribution that supports /proc.
func NewProcStatus(pid int, fileSystem fs.FileSystem) (*ProcStatus, error) {
	statusFname := filepath.Join("/proc", strconv.Itoa(pid), "status")
	r, err := fileSystem.Open(statusFname)
	if err != nil {
		return &ProcStatus{}, err
	}
	defer r.Close()

	data, err := ioutil.ReadAll(r)
	if err != nil {
		return &ProcStatus{}, err
	}

	s := &ProcStatus{
		pid: pid,
	}

	for _, l := range strings.Split(string(data), "\n") {
		// If the status file does not contain "key: values", just skip the line
		// as the information we are looking for is not needed.
		if !strings.Contains(l, ":") {
			continue
		}

		// At this point, we're only considering entries that has key, single value pairs.
		kv := strings.SplitN(l, ":", 2)
		fillProcStatus(s, strings.TrimSpace(kv[0]), strings.TrimSpace(kv[1]))
	}

	return s, nil
}