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	"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
40const userHZ = 100
41
42// ProcStat provides status information about the process,
43// read from /proc/[pid]/stat.
44type 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 uint
85	// Amount of time that this process's waited-for children have been
86	// scheduled in kernel mode, measured in clock ticks.
87	CSTime uint
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
104	proc fs.FS
105}
106
107// NewStat returns the current status information of the process.
108//
109// Deprecated: use p.Stat() instead
110func (p Proc) NewStat() (ProcStat, error) {
111	return p.Stat()
112}
113
114// Stat returns the current status information of the process.
115func (p Proc) Stat() (ProcStat, error) {
116	data, err := util.ReadFileNoStat(p.path("stat"))
117	if err != nil {
118		return ProcStat{}, err
119	}
120
121	var (
122		ignore int
123
124		s = ProcStat{PID: p.PID, proc: p.fs}
125		l = bytes.Index(data, []byte("("))
126		r = bytes.LastIndex(data, []byte(")"))
127	)
128
129	if l < 0 || r < 0 {
130		return ProcStat{}, fmt.Errorf("unexpected format, couldn't extract comm %q", data)
131	}
132
133	s.Comm = string(data[l+1 : r])
134	_, err = fmt.Fscan(
135		bytes.NewBuffer(data[r+2:]),
136		&s.State,
137		&s.PPID,
138		&s.PGRP,
139		&s.Session,
140		&s.TTY,
141		&s.TPGID,
142		&s.Flags,
143		&s.MinFlt,
144		&s.CMinFlt,
145		&s.MajFlt,
146		&s.CMajFlt,
147		&s.UTime,
148		&s.STime,
149		&s.CUTime,
150		&s.CSTime,
151		&s.Priority,
152		&s.Nice,
153		&s.NumThreads,
154		&ignore,
155		&s.Starttime,
156		&s.VSize,
157		&s.RSS,
158	)
159	if err != nil {
160		return ProcStat{}, err
161	}
162
163	return s, nil
164}
165
166// VirtualMemory returns the virtual memory size in bytes.
167func (s ProcStat) VirtualMemory() uint {
168	return s.VSize
169}
170
171// ResidentMemory returns the resident memory size in bytes.
172func (s ProcStat) ResidentMemory() int {
173	return s.RSS * os.Getpagesize()
174}
175
176// StartTime returns the unix timestamp of the process in seconds.
177func (s ProcStat) StartTime() (float64, error) {
178	fs := FS{proc: s.proc}
179	stat, err := fs.Stat()
180	if err != nil {
181		return 0, err
182	}
183	return float64(stat.BootTime) + (float64(s.Starttime) / userHZ), nil
184}
185
186// CPUTime returns the total CPU user and system time in seconds.
187func (s ProcStat) CPUTime() float64 {
188	return float64(s.UTime+s.STime) / userHZ
189}
190