You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

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