summaryrefslogtreecommitdiff
path: root/ui/metrics/proc/status.go
blob: f8b734c026c11015fef522b32248d3bd5d41a3fc (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// package proc contains functionality to read proc status files.
package proc

import (
	"strconv"
	"strings"
)

// ProcStatus holds information regarding the memory usage of
// an executing process. The memory sizes in each of the field
// is in bytes.
type ProcStatus struct {
	// Process PID.
	pid int

	// Peak virtual memory size.
	VmPeak uint64

	// Virtual memory size.
	VmSize uint64

	// Locked Memory size.
	VmLck uint64

	// Pinned memory size.
	VmPin uint64

	// Peak resident set size.
	VmHWM uint64

	// Resident set size (sum of RssAnon, RssFile and RssShmem).
	VmRss uint64

	// Size of resident anonymous memory.
	RssAnon uint64

	// Size of resident shared memory.
	RssShmem uint64

	// Size of data segments.
	VmData uint64

	// Size of stack segments.
	VmStk uint64

	//Size of text segments.
	VmExe uint64

	//Shared library code size.
	VmLib uint64

	// Page table entries size.
	VmPTE uint64

	// Size of second-level page tables.
	VmPMD uint64

	// Swapped-out virtual memory size by anonymous private.
	VmSwap uint64

	// Size of hugetlb memory page size.
	HugetlbPages uint64
}

// fillProcStatus takes the key and value, converts the value
// to the proper size unit and is stored in the ProcStatus.
func fillProcStatus(s *ProcStatus, key, value string) {
	v := strToUint64(value)
	switch key {
	case "VmPeak":
		s.VmPeak = v
	case "VmSize":
		s.VmSize = v
	case "VmLck":
		s.VmLck = v
	case "VmPin":
		s.VmPin = v
	case "VmHWM":
		s.VmHWM = v
	case "VmRSS":
		s.VmRss = v
	case "RssAnon":
		s.RssAnon = v
	case "RssShmem":
		s.RssShmem = v
	case "VmData":
		s.VmData = v
	case "VmStk":
		s.VmStk = v
	case "VmExe":
		s.VmExe = v
	case "VmLib":
		s.VmLib = v
	case "VmPTE":
		s.VmPTE = v
	case "VmPMD":
		s.VmPMD = v
	case "VmSwap":
		s.VmSwap = v
	case "HugetlbPages":
		s.HugetlbPages = v
	}
}

// strToUint64 takes the string and converts to unsigned 64-bit integer.
// If the string contains a memory unit such as kB and is converted to
// bytes.
func strToUint64(v string) uint64 {
	// v could be "1024 kB" so scan for the empty space and
	// split between the value and the unit.
	var separatorIndex int
	if separatorIndex = strings.IndexAny(v, " "); separatorIndex < 0 {
		separatorIndex = len(v)
	}
	value, err := strconv.ParseUint(v[:separatorIndex], 10, 64)
	if err != nil {
		return 0
	}

	var scale uint64 = 1
	switch strings.TrimSpace(v[separatorIndex:]) {
	case "kB", "KB":
		scale = 1024
	case "mB", "MB":
		scale = 1024 * 1024
	}
	return value * scale
}