proc_stat.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. package procfs
  14. import (
  15. "bytes"
  16. "fmt"
  17. "os"
  18. "github.com/prometheus/procfs/internal/util"
  19. )
  20. // Originally, this USER_HZ value was dynamically retrieved via a sysconf call
  21. // which required cgo. However, that caused a lot of problems regarding
  22. // cross-compilation. Alternatives such as running a binary to determine the
  23. // value, or trying to derive it in some other way were all problematic. After
  24. // much research it was determined that USER_HZ is actually hardcoded to 100 on
  25. // all Go-supported platforms as of the time of this writing. This is why we
  26. // decided to hardcode it here as well. It is not impossible that there could
  27. // be systems with exceptions, but they should be very exotic edge cases, and
  28. // in that case, the worst outcome will be two misreported metrics.
  29. //
  30. // See also the following discussions:
  31. //
  32. // - https://github.com/prometheus/node_exporter/issues/52
  33. // - https://github.com/prometheus/procfs/pull/2
  34. // - http://stackoverflow.com/questions/17410841/how-does-user-hz-solve-the-jiffy-scaling-issue
  35. const userHZ = 100
  36. // ProcStat provides status information about the process,
  37. // read from /proc/[pid]/stat.
  38. type ProcStat struct {
  39. // The process ID.
  40. PID int
  41. // The filename of the executable.
  42. Comm string
  43. // The process state.
  44. State string
  45. // The PID of the parent of this process.
  46. PPID int
  47. // The process group ID of the process.
  48. PGRP int
  49. // The session ID of the process.
  50. Session int
  51. // The controlling terminal of the process.
  52. TTY int
  53. // The ID of the foreground process group of the controlling terminal of
  54. // the process.
  55. TPGID int
  56. // The kernel flags word of the process.
  57. Flags uint
  58. // The number of minor faults the process has made which have not required
  59. // loading a memory page from disk.
  60. MinFlt uint
  61. // The number of minor faults that the process's waited-for children have
  62. // made.
  63. CMinFlt uint
  64. // The number of major faults the process has made which have required
  65. // loading a memory page from disk.
  66. MajFlt uint
  67. // The number of major faults that the process's waited-for children have
  68. // made.
  69. CMajFlt uint
  70. // Amount of time that this process has been scheduled in user mode,
  71. // measured in clock ticks.
  72. UTime uint
  73. // Amount of time that this process has been scheduled in kernel mode,
  74. // measured in clock ticks.
  75. STime uint
  76. // Amount of time that this process's waited-for children have been
  77. // scheduled in user mode, measured in clock ticks.
  78. CUTime int
  79. // Amount of time that this process's waited-for children have been
  80. // scheduled in kernel mode, measured in clock ticks.
  81. CSTime int
  82. // For processes running a real-time scheduling policy, this is the negated
  83. // scheduling priority, minus one.
  84. Priority int
  85. // The nice value, a value in the range 19 (low priority) to -20 (high
  86. // priority).
  87. Nice int
  88. // Number of threads in this process.
  89. NumThreads int
  90. // The time the process started after system boot, the value is expressed
  91. // in clock ticks.
  92. Starttime uint64
  93. // Virtual memory size in bytes.
  94. VSize uint
  95. // Resident set size in pages.
  96. RSS int
  97. // Soft limit in bytes on the rss of the process.
  98. RSSLimit uint64
  99. // CPU number last executed on.
  100. Processor uint
  101. // Real-time scheduling priority, a number in the range 1 to 99 for processes
  102. // scheduled under a real-time policy, or 0, for non-real-time processes.
  103. RTPriority uint
  104. // Scheduling policy.
  105. Policy uint
  106. // Aggregated block I/O delays, measured in clock ticks (centiseconds).
  107. DelayAcctBlkIOTicks uint64
  108. // Guest time of the process (time spent running a virtual CPU for a guest
  109. // operating system), measured in clock ticks.
  110. GuestTime int
  111. // Guest time of the process's children, measured in clock ticks.
  112. CGuestTime int
  113. proc FS
  114. }
  115. // NewStat returns the current status information of the process.
  116. //
  117. // Deprecated: Use p.Stat() instead.
  118. func (p Proc) NewStat() (ProcStat, error) {
  119. return p.Stat()
  120. }
  121. // Stat returns the current status information of the process.
  122. func (p Proc) Stat() (ProcStat, error) {
  123. data, err := util.ReadFileNoStat(p.path("stat"))
  124. if err != nil {
  125. return ProcStat{}, err
  126. }
  127. var (
  128. ignoreInt64 int64
  129. ignoreUint64 uint64
  130. s = ProcStat{PID: p.PID, proc: p.fs}
  131. l = bytes.Index(data, []byte("("))
  132. r = bytes.LastIndex(data, []byte(")"))
  133. )
  134. if l < 0 || r < 0 {
  135. return ProcStat{}, fmt.Errorf("%w: unexpected format, couldn't extract comm %q", ErrFileParse, data)
  136. }
  137. s.Comm = string(data[l+1 : r])
  138. // Check the following resources for the details about the particular stat
  139. // fields and their data types:
  140. // * https://man7.org/linux/man-pages/man5/proc.5.html
  141. // * https://man7.org/linux/man-pages/man3/scanf.3.html
  142. _, err = fmt.Fscan(
  143. bytes.NewBuffer(data[r+2:]),
  144. &s.State,
  145. &s.PPID,
  146. &s.PGRP,
  147. &s.Session,
  148. &s.TTY,
  149. &s.TPGID,
  150. &s.Flags,
  151. &s.MinFlt,
  152. &s.CMinFlt,
  153. &s.MajFlt,
  154. &s.CMajFlt,
  155. &s.UTime,
  156. &s.STime,
  157. &s.CUTime,
  158. &s.CSTime,
  159. &s.Priority,
  160. &s.Nice,
  161. &s.NumThreads,
  162. &ignoreInt64,
  163. &s.Starttime,
  164. &s.VSize,
  165. &s.RSS,
  166. &s.RSSLimit,
  167. &ignoreUint64,
  168. &ignoreUint64,
  169. &ignoreUint64,
  170. &ignoreUint64,
  171. &ignoreUint64,
  172. &ignoreUint64,
  173. &ignoreUint64,
  174. &ignoreUint64,
  175. &ignoreUint64,
  176. &ignoreUint64,
  177. &ignoreUint64,
  178. &ignoreUint64,
  179. &ignoreInt64,
  180. &s.Processor,
  181. &s.RTPriority,
  182. &s.Policy,
  183. &s.DelayAcctBlkIOTicks,
  184. &s.GuestTime,
  185. &s.CGuestTime,
  186. )
  187. if err != nil {
  188. return ProcStat{}, err
  189. }
  190. return s, nil
  191. }
  192. // VirtualMemory returns the virtual memory size in bytes.
  193. func (s ProcStat) VirtualMemory() uint {
  194. return s.VSize
  195. }
  196. // ResidentMemory returns the resident memory size in bytes.
  197. func (s ProcStat) ResidentMemory() int {
  198. return s.RSS * os.Getpagesize()
  199. }
  200. // StartTime returns the unix timestamp of the process in seconds.
  201. func (s ProcStat) StartTime() (float64, error) {
  202. stat, err := s.proc.Stat()
  203. if err != nil {
  204. return 0, err
  205. }
  206. return float64(stat.BootTime) + (float64(s.Starttime) / userHZ), nil
  207. }
  208. // CPUTime returns the total CPU user and system time in seconds.
  209. func (s ProcStat) CPUTime() float64 {
  210. return float64(s.UTime+s.STime) / userHZ
  211. }