本站源代码
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.

199 lines
5.4KB

  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. "io/ioutil"
  18. "os"
  19. "github.com/prometheus/procfs/internal/fs"
  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 uint
  80. // Amount of time that this process's waited-for children have been
  81. // scheduled in kernel mode, measured in clock ticks.
  82. CSTime uint
  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. proc fs.FS
  99. }
  100. // NewStat returns the current status information of the process.
  101. //
  102. // Deprecated: use p.Stat() instead
  103. func (p Proc) NewStat() (ProcStat, error) {
  104. return p.Stat()
  105. }
  106. // Stat returns the current status information of the process.
  107. func (p Proc) Stat() (ProcStat, error) {
  108. f, err := os.Open(p.path("stat"))
  109. if err != nil {
  110. return ProcStat{}, err
  111. }
  112. defer f.Close()
  113. data, err := ioutil.ReadAll(f)
  114. if err != nil {
  115. return ProcStat{}, err
  116. }
  117. var (
  118. ignore int
  119. s = ProcStat{PID: p.PID, proc: p.fs}
  120. l = bytes.Index(data, []byte("("))
  121. r = bytes.LastIndex(data, []byte(")"))
  122. )
  123. if l < 0 || r < 0 {
  124. return ProcStat{}, fmt.Errorf(
  125. "unexpected format, couldn't extract comm: %s",
  126. data,
  127. )
  128. }
  129. s.Comm = string(data[l+1 : r])
  130. _, err = fmt.Fscan(
  131. bytes.NewBuffer(data[r+2:]),
  132. &s.State,
  133. &s.PPID,
  134. &s.PGRP,
  135. &s.Session,
  136. &s.TTY,
  137. &s.TPGID,
  138. &s.Flags,
  139. &s.MinFlt,
  140. &s.CMinFlt,
  141. &s.MajFlt,
  142. &s.CMajFlt,
  143. &s.UTime,
  144. &s.STime,
  145. &s.CUTime,
  146. &s.CSTime,
  147. &s.Priority,
  148. &s.Nice,
  149. &s.NumThreads,
  150. &ignore,
  151. &s.Starttime,
  152. &s.VSize,
  153. &s.RSS,
  154. )
  155. if err != nil {
  156. return ProcStat{}, err
  157. }
  158. return s, nil
  159. }
  160. // VirtualMemory returns the virtual memory size in bytes.
  161. func (s ProcStat) VirtualMemory() uint {
  162. return s.VSize
  163. }
  164. // ResidentMemory returns the resident memory size in bytes.
  165. func (s ProcStat) ResidentMemory() int {
  166. return s.RSS * os.Getpagesize()
  167. }
  168. // StartTime returns the unix timestamp of the process in seconds.
  169. func (s ProcStat) StartTime() (float64, error) {
  170. fs := FS{proc: s.proc}
  171. stat, err := fs.Stat()
  172. if err != nil {
  173. return 0, err
  174. }
  175. return float64(stat.BootTime) + (float64(s.Starttime) / userHZ), nil
  176. }
  177. // CPUTime returns the total CPU user and system time in seconds.
  178. func (s ProcStat) CPUTime() float64 {
  179. return float64(s.UTime+s.STime) / userHZ
  180. }
上海开阖软件有限公司 沪ICP备12045867号-1