Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

223 rader
6.2 KiB

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