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.

proc_stat.go 5.2KB

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