proc_stat.go 5.1 KB

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