proc_stat.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. proc FS
  109. }
  110. // NewStat returns the current status information of the process.
  111. //
  112. // Deprecated: Use p.Stat() instead.
  113. func (p Proc) NewStat() (ProcStat, error) {
  114. return p.Stat()
  115. }
  116. // Stat returns the current status information of the process.
  117. func (p Proc) Stat() (ProcStat, error) {
  118. data, err := util.ReadFileNoStat(p.path("stat"))
  119. if err != nil {
  120. return ProcStat{}, err
  121. }
  122. var (
  123. ignoreInt64 int64
  124. ignoreUint64 uint64
  125. s = ProcStat{PID: p.PID, proc: p.fs}
  126. l = bytes.Index(data, []byte("("))
  127. r = bytes.LastIndex(data, []byte(")"))
  128. )
  129. if l < 0 || r < 0 {
  130. return ProcStat{}, fmt.Errorf("%w: unexpected format, couldn't extract comm %q", ErrFileParse, data)
  131. }
  132. s.Comm = string(data[l+1 : r])
  133. // Check the following resources for the details about the particular stat
  134. // fields and their data types:
  135. // * https://man7.org/linux/man-pages/man5/proc.5.html
  136. // * https://man7.org/linux/man-pages/man3/scanf.3.html
  137. _, err = fmt.Fscan(
  138. bytes.NewBuffer(data[r+2:]),
  139. &s.State,
  140. &s.PPID,
  141. &s.PGRP,
  142. &s.Session,
  143. &s.TTY,
  144. &s.TPGID,
  145. &s.Flags,
  146. &s.MinFlt,
  147. &s.CMinFlt,
  148. &s.MajFlt,
  149. &s.CMajFlt,
  150. &s.UTime,
  151. &s.STime,
  152. &s.CUTime,
  153. &s.CSTime,
  154. &s.Priority,
  155. &s.Nice,
  156. &s.NumThreads,
  157. &ignoreInt64,
  158. &s.Starttime,
  159. &s.VSize,
  160. &s.RSS,
  161. &s.RSSLimit,
  162. &ignoreUint64,
  163. &ignoreUint64,
  164. &ignoreUint64,
  165. &ignoreUint64,
  166. &ignoreUint64,
  167. &ignoreUint64,
  168. &ignoreUint64,
  169. &ignoreUint64,
  170. &ignoreUint64,
  171. &ignoreUint64,
  172. &ignoreUint64,
  173. &ignoreUint64,
  174. &ignoreInt64,
  175. &s.Processor,
  176. &s.RTPriority,
  177. &s.Policy,
  178. &s.DelayAcctBlkIOTicks,
  179. )
  180. if err != nil {
  181. return ProcStat{}, err
  182. }
  183. return s, nil
  184. }
  185. // VirtualMemory returns the virtual memory size in bytes.
  186. func (s ProcStat) VirtualMemory() uint {
  187. return s.VSize
  188. }
  189. // ResidentMemory returns the resident memory size in bytes.
  190. func (s ProcStat) ResidentMemory() int {
  191. return s.RSS * os.Getpagesize()
  192. }
  193. // StartTime returns the unix timestamp of the process in seconds.
  194. func (s ProcStat) StartTime() (float64, error) {
  195. stat, err := s.proc.Stat()
  196. if err != nil {
  197. return 0, err
  198. }
  199. return float64(stat.BootTime) + (float64(s.Starttime) / userHZ), nil
  200. }
  201. // CPUTime returns the total CPU user and system time in seconds.
  202. func (s ProcStat) CPUTime() float64 {
  203. return float64(s.UTime+s.STime) / userHZ
  204. }