cubefs
170 строк · 4.4 Кб
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"strconv"
19"strings"
20
21"github.com/prometheus/procfs/internal/util"
22)
23
24// ProcStatus provides status information about the process,
25// read from /proc/[pid]/stat.
26type ProcStatus struct {
27// The process ID.
28PID int
29// The process name.
30Name string
31
32// Thread group ID.
33TGID int
34
35// Peak virtual memory size.
36VmPeak uint64 // nolint:revive
37// Virtual memory size.
38VmSize uint64 // nolint:revive
39// Locked memory size.
40VmLck uint64 // nolint:revive
41// Pinned memory size.
42VmPin uint64 // nolint:revive
43// Peak resident set size.
44VmHWM uint64 // nolint:revive
45// Resident set size (sum of RssAnnon RssFile and RssShmem).
46VmRSS uint64 // nolint:revive
47// Size of resident anonymous memory.
48RssAnon uint64 // nolint:revive
49// Size of resident file mappings.
50RssFile uint64 // nolint:revive
51// Size of resident shared memory.
52RssShmem uint64 // nolint:revive
53// Size of data segments.
54VmData uint64 // nolint:revive
55// Size of stack segments.
56VmStk uint64 // nolint:revive
57// Size of text segments.
58VmExe uint64 // nolint:revive
59// Shared library code size.
60VmLib uint64 // nolint:revive
61// Page table entries size.
62VmPTE uint64 // nolint:revive
63// Size of second-level page tables.
64VmPMD uint64 // nolint:revive
65// Swapped-out virtual memory size by anonymous private.
66VmSwap uint64 // nolint:revive
67// Size of hugetlb memory portions
68HugetlbPages uint64
69
70// Number of voluntary context switches.
71VoluntaryCtxtSwitches uint64
72// Number of involuntary context switches.
73NonVoluntaryCtxtSwitches uint64
74
75// UIDs of the process (Real, effective, saved set, and filesystem UIDs)
76UIDs [4]string
77// GIDs of the process (Real, effective, saved set, and filesystem GIDs)
78GIDs [4]string
79}
80
81// NewStatus returns the current status information of the process.
82func (p Proc) NewStatus() (ProcStatus, error) {
83data, err := util.ReadFileNoStat(p.path("status"))
84if err != nil {
85return ProcStatus{}, err
86}
87
88s := ProcStatus{PID: p.PID}
89
90lines := strings.Split(string(data), "\n")
91for _, line := range lines {
92if !bytes.Contains([]byte(line), []byte(":")) {
93continue
94}
95
96kv := strings.SplitN(line, ":", 2)
97
98// removes spaces
99k := string(strings.TrimSpace(kv[0]))
100v := string(strings.TrimSpace(kv[1]))
101// removes "kB"
102v = string(bytes.Trim([]byte(v), " kB"))
103
104// value to int when possible
105// we can skip error check here, 'cause vKBytes is not used when value is a string
106vKBytes, _ := strconv.ParseUint(v, 10, 64)
107// convert kB to B
108vBytes := vKBytes * 1024
109
110s.fillStatus(k, v, vKBytes, vBytes)
111}
112
113return s, nil
114}
115
116func (s *ProcStatus) fillStatus(k string, vString string, vUint uint64, vUintBytes uint64) {
117switch k {
118case "Tgid":
119s.TGID = int(vUint)
120case "Name":
121s.Name = vString
122case "Uid":
123copy(s.UIDs[:], strings.Split(vString, "\t"))
124case "Gid":
125copy(s.GIDs[:], strings.Split(vString, "\t"))
126case "VmPeak":
127s.VmPeak = vUintBytes
128case "VmSize":
129s.VmSize = vUintBytes
130case "VmLck":
131s.VmLck = vUintBytes
132case "VmPin":
133s.VmPin = vUintBytes
134case "VmHWM":
135s.VmHWM = vUintBytes
136case "VmRSS":
137s.VmRSS = vUintBytes
138case "RssAnon":
139s.RssAnon = vUintBytes
140case "RssFile":
141s.RssFile = vUintBytes
142case "RssShmem":
143s.RssShmem = vUintBytes
144case "VmData":
145s.VmData = vUintBytes
146case "VmStk":
147s.VmStk = vUintBytes
148case "VmExe":
149s.VmExe = vUintBytes
150case "VmLib":
151s.VmLib = vUintBytes
152case "VmPTE":
153s.VmPTE = vUintBytes
154case "VmPMD":
155s.VmPMD = vUintBytes
156case "VmSwap":
157s.VmSwap = vUintBytes
158case "HugetlbPages":
159s.HugetlbPages = vUintBytes
160case "voluntary_ctxt_switches":
161s.VoluntaryCtxtSwitches = vUint
162case "nonvoluntary_ctxt_switches":
163s.NonVoluntaryCtxtSwitches = vUint
164}
165}
166
167// TotalCtxtSwitches returns the total context switch.
168func (s ProcStatus) TotalCtxtSwitches() uint64 {
169return s.VoluntaryCtxtSwitches + s.NonVoluntaryCtxtSwitches
170}
171