cubefs

Форк
0
222 строки · 6.2 Кб
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

14
package procfs
15

16
import (
17
	"bytes"
18
	"fmt"
19
	"os"
20

21
	"github.com/prometheus/procfs/internal/fs"
22
	"github.com/prometheus/procfs/internal/util"
23
)
24

25
// Originally, this USER_HZ value was dynamically retrieved via a sysconf call
26
// which required cgo. However, that caused a lot of problems regarding
27
// cross-compilation. Alternatives such as running a binary to determine the
28
// value, or trying to derive it in some other way were all problematic.  After
29
// much research it was determined that USER_HZ is actually hardcoded to 100 on
30
// all Go-supported platforms as of the time of this writing. This is why we
31
// decided to hardcode it here as well. It is not impossible that there could
32
// be systems with exceptions, but they should be very exotic edge cases, and
33
// in that case, the worst outcome will be two misreported metrics.
34
//
35
// See also the following discussions:
36
//
37
// - https://github.com/prometheus/node_exporter/issues/52
38
// - https://github.com/prometheus/procfs/pull/2
39
// - http://stackoverflow.com/questions/17410841/how-does-user-hz-solve-the-jiffy-scaling-issue
40
const userHZ = 100
41

42
// ProcStat provides status information about the process,
43
// read from /proc/[pid]/stat.
44
type ProcStat struct {
45
	// The process ID.
46
	PID int
47
	// The filename of the executable.
48
	Comm string
49
	// The process state.
50
	State string
51
	// The PID of the parent of this process.
52
	PPID int
53
	// The process group ID of the process.
54
	PGRP int
55
	// The session ID of the process.
56
	Session int
57
	// The controlling terminal of the process.
58
	TTY int
59
	// The ID of the foreground process group of the controlling terminal of
60
	// the process.
61
	TPGID int
62
	// The kernel flags word of the process.
63
	Flags uint
64
	// The number of minor faults the process has made which have not required
65
	// loading a memory page from disk.
66
	MinFlt uint
67
	// The number of minor faults that the process's waited-for children have
68
	// made.
69
	CMinFlt uint
70
	// The number of major faults the process has made which have required
71
	// loading a memory page from disk.
72
	MajFlt uint
73
	// The number of major faults that the process's waited-for children have
74
	// made.
75
	CMajFlt uint
76
	// Amount of time that this process has been scheduled in user mode,
77
	// measured in clock ticks.
78
	UTime uint
79
	// Amount of time that this process has been scheduled in kernel mode,
80
	// measured in clock ticks.
81
	STime uint
82
	// Amount of time that this process's waited-for children have been
83
	// scheduled in user mode, measured in clock ticks.
84
	CUTime int
85
	// Amount of time that this process's waited-for children have been
86
	// scheduled in kernel mode, measured in clock ticks.
87
	CSTime int
88
	// For processes running a real-time scheduling policy, this is the negated
89
	// scheduling priority, minus one.
90
	Priority int
91
	// The nice value, a value in the range 19 (low priority) to -20 (high
92
	// priority).
93
	Nice int
94
	// Number of threads in this process.
95
	NumThreads int
96
	// The time the process started after system boot, the value is expressed
97
	// in clock ticks.
98
	Starttime uint64
99
	// Virtual memory size in bytes.
100
	VSize uint
101
	// Resident set size in pages.
102
	RSS int
103
	// Soft limit in bytes on the rss of the process.
104
	RSSLimit uint64
105
	// Real-time scheduling priority, a number in the range 1 to 99 for processes
106
	// scheduled under a real-time policy, or 0, for non-real-time processes.
107
	RTPriority uint
108
	// Scheduling policy.
109
	Policy uint
110
	// Aggregated block I/O delays, measured in clock ticks (centiseconds).
111
	DelayAcctBlkIOTicks uint64
112

113
	proc fs.FS
114
}
115

116
// NewStat returns the current status information of the process.
117
//
118
// Deprecated: Use p.Stat() instead.
119
func (p Proc) NewStat() (ProcStat, error) {
120
	return p.Stat()
121
}
122

123
// Stat returns the current status information of the process.
124
func (p Proc) Stat() (ProcStat, error) {
125
	data, err := util.ReadFileNoStat(p.path("stat"))
126
	if err != nil {
127
		return ProcStat{}, err
128
	}
129

130
	var (
131
		ignoreInt64  int64
132
		ignoreUint64 uint64
133

134
		s = ProcStat{PID: p.PID, proc: p.fs}
135
		l = bytes.Index(data, []byte("("))
136
		r = bytes.LastIndex(data, []byte(")"))
137
	)
138

139
	if l < 0 || r < 0 {
140
		return ProcStat{}, fmt.Errorf("unexpected format, couldn't extract comm %q", data)
141
	}
142

143
	s.Comm = string(data[l+1 : r])
144

145
	// Check the following resources for the details about the particular stat
146
	// fields and their data types:
147
	// * https://man7.org/linux/man-pages/man5/proc.5.html
148
	// * https://man7.org/linux/man-pages/man3/scanf.3.html
149
	_, err = fmt.Fscan(
150
		bytes.NewBuffer(data[r+2:]),
151
		&s.State,
152
		&s.PPID,
153
		&s.PGRP,
154
		&s.Session,
155
		&s.TTY,
156
		&s.TPGID,
157
		&s.Flags,
158
		&s.MinFlt,
159
		&s.CMinFlt,
160
		&s.MajFlt,
161
		&s.CMajFlt,
162
		&s.UTime,
163
		&s.STime,
164
		&s.CUTime,
165
		&s.CSTime,
166
		&s.Priority,
167
		&s.Nice,
168
		&s.NumThreads,
169
		&ignoreInt64,
170
		&s.Starttime,
171
		&s.VSize,
172
		&s.RSS,
173
		&s.RSSLimit,
174
		&ignoreUint64,
175
		&ignoreUint64,
176
		&ignoreUint64,
177
		&ignoreUint64,
178
		&ignoreUint64,
179
		&ignoreUint64,
180
		&ignoreUint64,
181
		&ignoreUint64,
182
		&ignoreUint64,
183
		&ignoreUint64,
184
		&ignoreUint64,
185
		&ignoreUint64,
186
		&ignoreInt64,
187
		&ignoreInt64,
188
		&s.RTPriority,
189
		&s.Policy,
190
		&s.DelayAcctBlkIOTicks,
191
	)
192
	if err != nil {
193
		return ProcStat{}, err
194
	}
195

196
	return s, nil
197
}
198

199
// VirtualMemory returns the virtual memory size in bytes.
200
func (s ProcStat) VirtualMemory() uint {
201
	return s.VSize
202
}
203

204
// ResidentMemory returns the resident memory size in bytes.
205
func (s ProcStat) ResidentMemory() int {
206
	return s.RSS * os.Getpagesize()
207
}
208

209
// StartTime returns the unix timestamp of the process in seconds.
210
func (s ProcStat) StartTime() (float64, error) {
211
	fs := FS{proc: s.proc}
212
	stat, err := fs.Stat()
213
	if err != nil {
214
		return 0, err
215
	}
216
	return float64(stat.BootTime) + (float64(s.Starttime) / userHZ), nil
217
}
218

219
// CPUTime returns the total CPU user and system time in seconds.
220
func (s ProcStat) CPUTime() float64 {
221
	return float64(s.UTime+s.STime) / userHZ
222
}
223

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.