khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 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 | "io/ioutil" |
| 20 | "os" |
| 21 | |
| 22 | "github.com/prometheus/procfs/internal/fs" |
| 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 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 NewStat() instead |
| 110 | func (p Proc) NewStat() (ProcStat, error) { |
| 111 | return p.Stat() |
| 112 | } |
| 113 | |
| 114 | // Stat returns the current status information of the process. |
| 115 | func (p Proc) Stat() (ProcStat, error) { |
| 116 | f, err := os.Open(p.path("stat")) |
| 117 | if err != nil { |
| 118 | return ProcStat{}, err |
| 119 | } |
| 120 | defer f.Close() |
| 121 | |
| 122 | data, err := ioutil.ReadAll(f) |
| 123 | if err != nil { |
| 124 | return ProcStat{}, err |
| 125 | } |
| 126 | |
| 127 | var ( |
| 128 | ignore int |
| 129 | |
| 130 | s = ProcStat{PID: p.PID, proc: p.fs} |
| 131 | l = bytes.Index(data, []byte("(")) |
| 132 | r = bytes.LastIndex(data, []byte(")")) |
| 133 | ) |
| 134 | |
| 135 | if l < 0 || r < 0 { |
| 136 | return ProcStat{}, fmt.Errorf( |
| 137 | "unexpected format, couldn't extract comm: %s", |
| 138 | data, |
| 139 | ) |
| 140 | } |
| 141 | |
| 142 | s.Comm = string(data[l+1 : r]) |
| 143 | _, err = fmt.Fscan( |
| 144 | bytes.NewBuffer(data[r+2:]), |
| 145 | &s.State, |
| 146 | &s.PPID, |
| 147 | &s.PGRP, |
| 148 | &s.Session, |
| 149 | &s.TTY, |
| 150 | &s.TPGID, |
| 151 | &s.Flags, |
| 152 | &s.MinFlt, |
| 153 | &s.CMinFlt, |
| 154 | &s.MajFlt, |
| 155 | &s.CMajFlt, |
| 156 | &s.UTime, |
| 157 | &s.STime, |
| 158 | &s.CUTime, |
| 159 | &s.CSTime, |
| 160 | &s.Priority, |
| 161 | &s.Nice, |
| 162 | &s.NumThreads, |
| 163 | &ignore, |
| 164 | &s.Starttime, |
| 165 | &s.VSize, |
| 166 | &s.RSS, |
| 167 | ) |
| 168 | if err != nil { |
| 169 | return ProcStat{}, err |
| 170 | } |
| 171 | |
| 172 | return s, nil |
| 173 | } |
| 174 | |
| 175 | // VirtualMemory returns the virtual memory size in bytes. |
| 176 | func (s ProcStat) VirtualMemory() uint { |
| 177 | return s.VSize |
| 178 | } |
| 179 | |
| 180 | // ResidentMemory returns the resident memory size in bytes. |
| 181 | func (s ProcStat) ResidentMemory() int { |
| 182 | return s.RSS * os.Getpagesize() |
| 183 | } |
| 184 | |
| 185 | // StartTime returns the unix timestamp of the process in seconds. |
| 186 | func (s ProcStat) StartTime() (float64, error) { |
| 187 | fs := FS{proc: s.proc} |
| 188 | stat, err := fs.Stat() |
| 189 | if err != nil { |
| 190 | return 0, err |
| 191 | } |
| 192 | return float64(stat.BootTime) + (float64(s.Starttime) / userHZ), nil |
| 193 | } |
| 194 | |
| 195 | // CPUTime returns the total CPU user and system time in seconds. |
| 196 | func (s ProcStat) CPUTime() float64 { |
| 197 | return float64(s.UTime+s.STime) / userHZ |
| 198 | } |