1// Copyright 2018 The Prometheus Authors
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14package procfs
15
16import (
17	"bytes"
18	"io/ioutil"
19	"os"
20	"strconv"
21	"strings"
22)
23
24// ProcStatus provides status information about the process,
25// read from /proc/[pid]/stat.
26type ProcStatus struct {
27	// The process ID.
28	PID int
29	// The process name.
30	Name string
31
32	// Thread group ID.
33	TGID int
34
35	// Peak virtual memory size.
36	VmPeak uint64
37	// Virtual memory size.
38	VmSize uint64
39	// Locked memory size.
40	VmLck uint64
41	// Pinned memory size.
42	VmPin uint64
43	// Peak resident set size.
44	VmHWM uint64
45	// Resident set size (sum of RssAnnon RssFile and RssShmem).
46	VmRSS uint64
47	// Size of resident anonymous memory.
48	RssAnon uint64
49	// Size of resident file mappings.
50	RssFile uint64
51	// Size of resident shared memory.
52	RssShmem uint64
53	// Size of data segments.
54	VmData uint64
55	// Size of stack segments.
56	VmStk uint64
57	// Size of text segments.
58	VmExe uint64
59	// Shared library code size.
60	VmLib uint64
61	// Page table entries size.
62	VmPTE uint64
63	// Size of second-level page tables.
64	VmPMD uint64
65	// Swapped-out virtual memory size by anonymous private.
66	VmSwap uint64
67	// Size of hugetlb memory portions
68	HugetlbPages uint64
69
70	// Number of voluntary context switches.
71	VoluntaryCtxtSwitches uint64
72	// Number of involuntary context switches.
73	NonVoluntaryCtxtSwitches uint64
74}
75
76// NewStatus returns the current status information of the process.
77func (p Proc) NewStatus() (ProcStatus, error) {
78	f, err := os.Open(p.path("status"))
79	if err != nil {
80		return ProcStatus{}, err
81	}
82	defer f.Close()
83
84	data, err := ioutil.ReadAll(f)
85	if err != nil {
86		return ProcStatus{}, err
87	}
88
89	s := ProcStatus{PID: p.PID}
90
91	lines := strings.Split(string(data), "\n")
92	for _, line := range lines {
93		if !bytes.Contains([]byte(line), []byte(":")) {
94			continue
95		}
96
97		kv := strings.SplitN(line, ":", 2)
98
99		// removes spaces
100		k := string(strings.TrimSpace(kv[0]))
101		v := string(strings.TrimSpace(kv[1]))
102		// removes "kB"
103		v = string(bytes.Trim([]byte(v), " kB"))
104
105		// value to int when possible
106		// we can skip error check here, 'cause vKBytes is not used when value is a string
107		vKBytes, _ := strconv.ParseUint(v, 10, 64)
108		// convert kB to B
109		vBytes := vKBytes * 1024
110
111		s.fillStatus(k, v, vKBytes, vBytes)
112	}
113
114	return s, nil
115}
116
117func (s *ProcStatus) fillStatus(k string, vString string, vUint uint64, vUintBytes uint64) {
118	switch k {
119	case "Tgid":
120		s.TGID = int(vUint)
121	case "Name":
122		s.Name = vString
123	case "VmPeak":
124		s.VmPeak = vUintBytes
125	case "VmSize":
126		s.VmSize = vUintBytes
127	case "VmLck":
128		s.VmLck = vUintBytes
129	case "VmPin":
130		s.VmPin = vUintBytes
131	case "VmHWM":
132		s.VmHWM = vUintBytes
133	case "VmRSS":
134		s.VmRSS = vUintBytes
135	case "RssAnon":
136		s.RssAnon = vUintBytes
137	case "RssFile":
138		s.RssFile = vUintBytes
139	case "RssShmem":
140		s.RssShmem = vUintBytes
141	case "VmData":
142		s.VmData = vUintBytes
143	case "VmStk":
144		s.VmStk = vUintBytes
145	case "VmExe":
146		s.VmExe = vUintBytes
147	case "VmLib":
148		s.VmLib = vUintBytes
149	case "VmPTE":
150		s.VmPTE = vUintBytes
151	case "VmPMD":
152		s.VmPMD = vUintBytes
153	case "VmSwap":
154		s.VmSwap = vUintBytes
155	case "HugetlbPages":
156		s.HugetlbPages = vUintBytes
157	case "voluntary_ctxt_switches":
158		s.VoluntaryCtxtSwitches = vUint
159	case "nonvoluntary_ctxt_switches":
160		s.NonVoluntaryCtxtSwitches = vUint
161	}
162}
163
164// TotalCtxtSwitches returns the total context switch.
165func (s ProcStatus) TotalCtxtSwitches() uint64 {
166	return s.VoluntaryCtxtSwitches + s.NonVoluntaryCtxtSwitches
167}
168