blob: dd8aa56885ec3d340b030e5957ae9d2dd573f78a [file] [log] [blame]
Abhay Kumara61c5222025-11-10 07:32:50 +00001// 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 "math/bits"
19 "sort"
20 "strconv"
21 "strings"
22
23 "github.com/prometheus/procfs/internal/util"
24)
25
26// ProcStatus provides status information about the process,
27// read from /proc/[pid]/status.
28type ProcStatus struct {
29 // The process ID.
30 PID int
31 // The process name.
32 Name string
33
34 // Thread group ID.
35 TGID int
36 // List of Pid namespace.
37 NSpids []uint64
38
39 // Peak virtual memory size.
40 VmPeak uint64 // nolint:revive
41 // Virtual memory size.
42 VmSize uint64 // nolint:revive
43 // Locked memory size.
44 VmLck uint64 // nolint:revive
45 // Pinned memory size.
46 VmPin uint64 // nolint:revive
47 // Peak resident set size.
48 VmHWM uint64 // nolint:revive
49 // Resident set size (sum of RssAnnon RssFile and RssShmem).
50 VmRSS uint64 // nolint:revive
51 // Size of resident anonymous memory.
52 RssAnon uint64 // nolint:revive
53 // Size of resident file mappings.
54 RssFile uint64 // nolint:revive
55 // Size of resident shared memory.
56 RssShmem uint64 // nolint:revive
57 // Size of data segments.
58 VmData uint64 // nolint:revive
59 // Size of stack segments.
60 VmStk uint64 // nolint:revive
61 // Size of text segments.
62 VmExe uint64 // nolint:revive
63 // Shared library code size.
64 VmLib uint64 // nolint:revive
65 // Page table entries size.
66 VmPTE uint64 // nolint:revive
67 // Size of second-level page tables.
68 VmPMD uint64 // nolint:revive
69 // Swapped-out virtual memory size by anonymous private.
70 VmSwap uint64 // nolint:revive
71 // Size of hugetlb memory portions
72 HugetlbPages uint64
73
74 // Number of voluntary context switches.
75 VoluntaryCtxtSwitches uint64
76 // Number of involuntary context switches.
77 NonVoluntaryCtxtSwitches uint64
78
79 // UIDs of the process (Real, effective, saved set, and filesystem UIDs)
80 UIDs [4]uint64
81 // GIDs of the process (Real, effective, saved set, and filesystem GIDs)
82 GIDs [4]uint64
83
84 // CpusAllowedList: List of cpu cores processes are allowed to run on.
85 CpusAllowedList []uint64
86}
87
88// NewStatus returns the current status information of the process.
89func (p Proc) NewStatus() (ProcStatus, error) {
90 data, err := util.ReadFileNoStat(p.path("status"))
91 if err != nil {
92 return ProcStatus{}, err
93 }
94
95 s := ProcStatus{PID: p.PID}
96
97 lines := strings.Split(string(data), "\n")
98 for _, line := range lines {
99 if !bytes.Contains([]byte(line), []byte(":")) {
100 continue
101 }
102
103 kv := strings.SplitN(line, ":", 2)
104
105 // removes spaces
106 k := strings.TrimSpace(kv[0])
107 v := strings.TrimSpace(kv[1])
108 // removes "kB"
109 v = strings.TrimSuffix(v, " kB")
110
111 // value to int when possible
112 // we can skip error check here, 'cause vKBytes is not used when value is a string
113 vKBytes, _ := strconv.ParseUint(v, 10, 64)
114 // convert kB to B
115 vBytes := vKBytes * 1024
116
117 err = s.fillStatus(k, v, vKBytes, vBytes)
118 if err != nil {
119 return ProcStatus{}, err
120 }
121 }
122
123 return s, nil
124}
125
126func (s *ProcStatus) fillStatus(k string, vString string, vUint uint64, vUintBytes uint64) error {
127 switch k {
128 case "Tgid":
129 s.TGID = int(vUint)
130 case "Name":
131 s.Name = vString
132 case "Uid":
133 var err error
134 for i, v := range strings.Split(vString, "\t") {
135 s.UIDs[i], err = strconv.ParseUint(v, 10, bits.UintSize)
136 if err != nil {
137 return err
138 }
139 }
140 case "Gid":
141 var err error
142 for i, v := range strings.Split(vString, "\t") {
143 s.GIDs[i], err = strconv.ParseUint(v, 10, bits.UintSize)
144 if err != nil {
145 return err
146 }
147 }
148 case "NSpid":
149 nspids, err := calcNSPidsList(vString)
150 if err != nil {
151 return err
152 }
153 s.NSpids = nspids
154 case "VmPeak":
155 s.VmPeak = vUintBytes
156 case "VmSize":
157 s.VmSize = vUintBytes
158 case "VmLck":
159 s.VmLck = vUintBytes
160 case "VmPin":
161 s.VmPin = vUintBytes
162 case "VmHWM":
163 s.VmHWM = vUintBytes
164 case "VmRSS":
165 s.VmRSS = vUintBytes
166 case "RssAnon":
167 s.RssAnon = vUintBytes
168 case "RssFile":
169 s.RssFile = vUintBytes
170 case "RssShmem":
171 s.RssShmem = vUintBytes
172 case "VmData":
173 s.VmData = vUintBytes
174 case "VmStk":
175 s.VmStk = vUintBytes
176 case "VmExe":
177 s.VmExe = vUintBytes
178 case "VmLib":
179 s.VmLib = vUintBytes
180 case "VmPTE":
181 s.VmPTE = vUintBytes
182 case "VmPMD":
183 s.VmPMD = vUintBytes
184 case "VmSwap":
185 s.VmSwap = vUintBytes
186 case "HugetlbPages":
187 s.HugetlbPages = vUintBytes
188 case "voluntary_ctxt_switches":
189 s.VoluntaryCtxtSwitches = vUint
190 case "nonvoluntary_ctxt_switches":
191 s.NonVoluntaryCtxtSwitches = vUint
192 case "Cpus_allowed_list":
193 s.CpusAllowedList = calcCpusAllowedList(vString)
194 }
195
196 return nil
197}
198
199// TotalCtxtSwitches returns the total context switch.
200func (s ProcStatus) TotalCtxtSwitches() uint64 {
201 return s.VoluntaryCtxtSwitches + s.NonVoluntaryCtxtSwitches
202}
203
204func calcCpusAllowedList(cpuString string) []uint64 {
205 s := strings.Split(cpuString, ",")
206
207 var g []uint64
208
209 for _, cpu := range s {
210 // parse cpu ranges, example: 1-3=[1,2,3]
211 if l := strings.Split(strings.TrimSpace(cpu), "-"); len(l) > 1 {
212 startCPU, _ := strconv.ParseUint(l[0], 10, 64)
213 endCPU, _ := strconv.ParseUint(l[1], 10, 64)
214
215 for i := startCPU; i <= endCPU; i++ {
216 g = append(g, i)
217 }
218 } else if len(l) == 1 {
219 cpu, _ := strconv.ParseUint(l[0], 10, 64)
220 g = append(g, cpu)
221 }
222
223 }
224
225 sort.Slice(g, func(i, j int) bool { return g[i] < g[j] })
226 return g
227}
228
229func calcNSPidsList(nspidsString string) ([]uint64, error) {
230 s := strings.Split(nspidsString, "\t")
231 var nspids []uint64
232
233 for _, nspid := range s {
234 nspid, err := strconv.ParseUint(nspid, 10, 64)
235 if err != nil {
236 return nil, err
237 }
238 nspids = append(nspids, nspid)
239 }
240
241 return nspids, nil
242}