blob: dd8aa56885ec3d340b030e5957ae9d2dd573f78a [file] [log] [blame]
khenaidooab1f7bd2019-11-14 14:00:27 -05001// 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"
Abhay Kumara2ae5992025-11-10 14:02:24 +000018 "math/bits"
19 "sort"
khenaidooab1f7bd2019-11-14 14:00:27 -050020 "strconv"
21 "strings"
khenaidood948f772021-08-11 17:49:24 -040022
23 "github.com/prometheus/procfs/internal/util"
khenaidooab1f7bd2019-11-14 14:00:27 -050024)
25
khenaidood948f772021-08-11 17:49:24 -040026// ProcStatus provides status information about the process,
Abhay Kumara2ae5992025-11-10 14:02:24 +000027// read from /proc/[pid]/status.
khenaidooab1f7bd2019-11-14 14:00:27 -050028type ProcStatus struct {
29 // The process ID.
30 PID int
31 // The process name.
32 Name string
33
khenaidood948f772021-08-11 17:49:24 -040034 // Thread group ID.
35 TGID int
Abhay Kumara2ae5992025-11-10 14:02:24 +000036 // List of Pid namespace.
37 NSpids []uint64
khenaidood948f772021-08-11 17:49:24 -040038
khenaidooab1f7bd2019-11-14 14:00:27 -050039 // Peak virtual memory size.
Abhay Kumara2ae5992025-11-10 14:02:24 +000040 VmPeak uint64 // nolint:revive
khenaidooab1f7bd2019-11-14 14:00:27 -050041 // Virtual memory size.
Abhay Kumara2ae5992025-11-10 14:02:24 +000042 VmSize uint64 // nolint:revive
khenaidooab1f7bd2019-11-14 14:00:27 -050043 // Locked memory size.
Abhay Kumara2ae5992025-11-10 14:02:24 +000044 VmLck uint64 // nolint:revive
khenaidooab1f7bd2019-11-14 14:00:27 -050045 // Pinned memory size.
Abhay Kumara2ae5992025-11-10 14:02:24 +000046 VmPin uint64 // nolint:revive
khenaidooab1f7bd2019-11-14 14:00:27 -050047 // Peak resident set size.
Abhay Kumara2ae5992025-11-10 14:02:24 +000048 VmHWM uint64 // nolint:revive
khenaidooab1f7bd2019-11-14 14:00:27 -050049 // Resident set size (sum of RssAnnon RssFile and RssShmem).
Abhay Kumara2ae5992025-11-10 14:02:24 +000050 VmRSS uint64 // nolint:revive
khenaidooab1f7bd2019-11-14 14:00:27 -050051 // Size of resident anonymous memory.
Abhay Kumara2ae5992025-11-10 14:02:24 +000052 RssAnon uint64 // nolint:revive
khenaidooab1f7bd2019-11-14 14:00:27 -050053 // Size of resident file mappings.
Abhay Kumara2ae5992025-11-10 14:02:24 +000054 RssFile uint64 // nolint:revive
khenaidooab1f7bd2019-11-14 14:00:27 -050055 // Size of resident shared memory.
Abhay Kumara2ae5992025-11-10 14:02:24 +000056 RssShmem uint64 // nolint:revive
khenaidooab1f7bd2019-11-14 14:00:27 -050057 // Size of data segments.
Abhay Kumara2ae5992025-11-10 14:02:24 +000058 VmData uint64 // nolint:revive
khenaidooab1f7bd2019-11-14 14:00:27 -050059 // Size of stack segments.
Abhay Kumara2ae5992025-11-10 14:02:24 +000060 VmStk uint64 // nolint:revive
khenaidooab1f7bd2019-11-14 14:00:27 -050061 // Size of text segments.
Abhay Kumara2ae5992025-11-10 14:02:24 +000062 VmExe uint64 // nolint:revive
khenaidooab1f7bd2019-11-14 14:00:27 -050063 // Shared library code size.
Abhay Kumara2ae5992025-11-10 14:02:24 +000064 VmLib uint64 // nolint:revive
khenaidooab1f7bd2019-11-14 14:00:27 -050065 // Page table entries size.
Abhay Kumara2ae5992025-11-10 14:02:24 +000066 VmPTE uint64 // nolint:revive
khenaidooab1f7bd2019-11-14 14:00:27 -050067 // Size of second-level page tables.
Abhay Kumara2ae5992025-11-10 14:02:24 +000068 VmPMD uint64 // nolint:revive
khenaidooab1f7bd2019-11-14 14:00:27 -050069 // Swapped-out virtual memory size by anonymous private.
Abhay Kumara2ae5992025-11-10 14:02:24 +000070 VmSwap uint64 // nolint:revive
khenaidooab1f7bd2019-11-14 14:00:27 -050071 // 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
khenaidood948f772021-08-11 17:49:24 -040078
79 // UIDs of the process (Real, effective, saved set, and filesystem UIDs)
Abhay Kumara2ae5992025-11-10 14:02:24 +000080 UIDs [4]uint64
khenaidood948f772021-08-11 17:49:24 -040081 // GIDs of the process (Real, effective, saved set, and filesystem GIDs)
Abhay Kumara2ae5992025-11-10 14:02:24 +000082 GIDs [4]uint64
83
84 // CpusAllowedList: List of cpu cores processes are allowed to run on.
85 CpusAllowedList []uint64
khenaidooab1f7bd2019-11-14 14:00:27 -050086}
87
88// NewStatus returns the current status information of the process.
89func (p Proc) NewStatus() (ProcStatus, error) {
khenaidood948f772021-08-11 17:49:24 -040090 data, err := util.ReadFileNoStat(p.path("status"))
khenaidooab1f7bd2019-11-14 14:00:27 -050091 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
Abhay Kumara2ae5992025-11-10 14:02:24 +0000106 k := strings.TrimSpace(kv[0])
107 v := strings.TrimSpace(kv[1])
khenaidooab1f7bd2019-11-14 14:00:27 -0500108 // removes "kB"
Abhay Kumara2ae5992025-11-10 14:02:24 +0000109 v = strings.TrimSuffix(v, " kB")
khenaidooab1f7bd2019-11-14 14:00:27 -0500110
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
Abhay Kumara2ae5992025-11-10 14:02:24 +0000117 err = s.fillStatus(k, v, vKBytes, vBytes)
118 if err != nil {
119 return ProcStatus{}, err
120 }
khenaidooab1f7bd2019-11-14 14:00:27 -0500121 }
122
123 return s, nil
124}
125
Abhay Kumara2ae5992025-11-10 14:02:24 +0000126func (s *ProcStatus) fillStatus(k string, vString string, vUint uint64, vUintBytes uint64) error {
khenaidooab1f7bd2019-11-14 14:00:27 -0500127 switch k {
khenaidood948f772021-08-11 17:49:24 -0400128 case "Tgid":
129 s.TGID = int(vUint)
khenaidooab1f7bd2019-11-14 14:00:27 -0500130 case "Name":
131 s.Name = vString
khenaidood948f772021-08-11 17:49:24 -0400132 case "Uid":
Abhay Kumara2ae5992025-11-10 14:02:24 +0000133 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 }
khenaidood948f772021-08-11 17:49:24 -0400140 case "Gid":
Abhay Kumara2ae5992025-11-10 14:02:24 +0000141 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
khenaidooab1f7bd2019-11-14 14:00:27 -0500154 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
Abhay Kumara2ae5992025-11-10 14:02:24 +0000192 case "Cpus_allowed_list":
193 s.CpusAllowedList = calcCpusAllowedList(vString)
khenaidooab1f7bd2019-11-14 14:00:27 -0500194 }
Abhay Kumara2ae5992025-11-10 14:02:24 +0000195
196 return nil
khenaidooab1f7bd2019-11-14 14:00:27 -0500197}
198
199// TotalCtxtSwitches returns the total context switch.
200func (s ProcStatus) TotalCtxtSwitches() uint64 {
201 return s.VoluntaryCtxtSwitches + s.NonVoluntaryCtxtSwitches
202}
Abhay Kumara2ae5992025-11-10 14:02:24 +0000203
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}