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
6 // http://www.apache.org/licenses/LICENSE-2.0
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.
23 // Originally, this USER_HZ value was dynamically retrieved via a sysconf call
24 // which required cgo. However, that caused a lot of problems regarding
25 // cross-compilation. Alternatives such as running a binary to determine the
26 // value, or trying to derive it in some other way were all problematic. After
27 // much research it was determined that USER_HZ is actually hardcoded to 100 on
28 // all Go-supported platforms as of the time of this writing. This is why we
29 // decided to hardcode it here as well. It is not impossible that there could
30 // be systems with exceptions, but they should be very exotic edge cases, and
31 // in that case, the worst outcome will be two misreported metrics.
33 // See also the following discussions:
35 // - https://github.com/prometheus/node_exporter/issues/52
36 // - https://github.com/prometheus/procfs/pull/2
37 // - http://stackoverflow.com/questions/17410841/how-does-user-hz-solve-the-jiffy-scaling-issue
40 // ProcStat provides status information about the process,
41 // read from /proc/[pid]/stat.
42 type ProcStat struct {
45 // The filename of the executable.
49 // The PID of the parent of this process.
51 // The process group ID of the process.
53 // The session ID of the process.
55 // The controlling terminal of the process.
57 // The ID of the foreground process group of the controlling terminal of
60 // The kernel flags word of the process.
62 // The number of minor faults the process has made which have not required
63 // loading a memory page from disk.
65 // The number of minor faults that the process's waited-for children have
68 // The number of major faults the process has made which have required
69 // loading a memory page from disk.
71 // The number of major faults that the process's waited-for children have
74 // Amount of time that this process has been scheduled in user mode,
75 // measured in clock ticks.
77 // Amount of time that this process has been scheduled in kernel mode,
78 // measured in clock ticks.
80 // Amount of time that this process's waited-for children have been
81 // scheduled in user mode, measured in clock ticks.
83 // Amount of time that this process's waited-for children have been
84 // scheduled in kernel mode, measured in clock ticks.
86 // For processes running a real-time scheduling policy, this is the negated
87 // scheduling priority, minus one.
89 // The nice value, a value in the range 19 (low priority) to -20 (high
92 // Number of threads in this process.
94 // The time the process started after system boot, the value is expressed
97 // Virtual memory size in bytes.
99 // Resident set size in pages.
105 // NewStat returns the current status information of the process.
106 func (p Proc) NewStat() (ProcStat, error) {
107 f, err := os.Open(p.path("stat"))
109 return ProcStat{}, err
113 data, err := ioutil.ReadAll(f)
115 return ProcStat{}, err
121 s = ProcStat{PID: p.PID, fs: p.fs}
122 l = bytes.Index(data, []byte("("))
123 r = bytes.LastIndex(data, []byte(")"))
127 return ProcStat{}, fmt.Errorf(
128 "unexpected format, couldn't extract comm: %s",
133 s.Comm = string(data[l+1 : r])
135 bytes.NewBuffer(data[r+2:]),
160 return ProcStat{}, err
166 // VirtualMemory returns the virtual memory size in bytes.
167 func (s ProcStat) VirtualMemory() uint {
171 // ResidentMemory returns the resident memory size in bytes.
172 func (s ProcStat) ResidentMemory() int {
173 return s.RSS * os.Getpagesize()
176 // StartTime returns the unix timestamp of the process in seconds.
177 func (s ProcStat) StartTime() (float64, error) {
178 stat, err := s.fs.NewStat()
182 return float64(stat.BootTime) + (float64(s.Starttime) / userHZ), nil
185 // CPUTime returns the total CPU user and system time in seconds.
186 func (s ProcStat) CPUTime() float64 {
187 return float64(s.UTime+s.STime) / userHZ