proc_stat.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package procfs
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. )
  8. // Originally, this USER_HZ value was dynamically retrieved via a sysconf call
  9. // which required cgo. However, that caused a lot of problems regarding
  10. // cross-compilation. Alternatives such as running a binary to determine the
  11. // value, or trying to derive it in some other way were all problematic. After
  12. // much research it was determined that USER_HZ is actually hardcoded to 100 on
  13. // all Go-supported platforms as of the time of this writing. This is why we
  14. // decided to hardcode it here as well. It is not impossible that there could
  15. // be systems with exceptions, but they should be very exotic edge cases, and
  16. // in that case, the worst outcome will be two misreported metrics.
  17. //
  18. // See also the following discussions:
  19. //
  20. // - https://github.com/prometheus/node_exporter/issues/52
  21. // - https://github.com/prometheus/procfs/pull/2
  22. // - http://stackoverflow.com/questions/17410841/how-does-user-hz-solve-the-jiffy-scaling-issue
  23. const userHZ = 100
  24. // ProcStat provides status information about the process,
  25. // read from /proc/[pid]/stat.
  26. type ProcStat struct {
  27. // The process ID.
  28. PID int
  29. // The filename of the executable.
  30. Comm string
  31. // The process state.
  32. State string
  33. // The PID of the parent of this process.
  34. PPID int
  35. // The process group ID of the process.
  36. PGRP int
  37. // The session ID of the process.
  38. Session int
  39. // The controlling terminal of the process.
  40. TTY int
  41. // The ID of the foreground process group of the controlling terminal of
  42. // the process.
  43. TPGID int
  44. // The kernel flags word of the process.
  45. Flags uint
  46. // The number of minor faults the process has made which have not required
  47. // loading a memory page from disk.
  48. MinFlt uint
  49. // The number of minor faults that the process's waited-for children have
  50. // made.
  51. CMinFlt uint
  52. // The number of major faults the process has made which have required
  53. // loading a memory page from disk.
  54. MajFlt uint
  55. // The number of major faults that the process's waited-for children have
  56. // made.
  57. CMajFlt uint
  58. // Amount of time that this process has been scheduled in user mode,
  59. // measured in clock ticks.
  60. UTime uint
  61. // Amount of time that this process has been scheduled in kernel mode,
  62. // measured in clock ticks.
  63. STime uint
  64. // Amount of time that this process's waited-for children have been
  65. // scheduled in user mode, measured in clock ticks.
  66. CUTime uint
  67. // Amount of time that this process's waited-for children have been
  68. // scheduled in kernel mode, measured in clock ticks.
  69. CSTime uint
  70. // For processes running a real-time scheduling policy, this is the negated
  71. // scheduling priority, minus one.
  72. Priority int
  73. // The nice value, a value in the range 19 (low priority) to -20 (high
  74. // priority).
  75. Nice int
  76. // Number of threads in this process.
  77. NumThreads int
  78. // The time the process started after system boot, the value is expressed
  79. // in clock ticks.
  80. Starttime uint64
  81. // Virtual memory size in bytes.
  82. VSize int
  83. // Resident set size in pages.
  84. RSS int
  85. fs FS
  86. }
  87. // NewStat returns the current status information of the process.
  88. func (p Proc) NewStat() (ProcStat, error) {
  89. f, err := os.Open(p.path("stat"))
  90. if err != nil {
  91. return ProcStat{}, err
  92. }
  93. defer f.Close()
  94. data, err := ioutil.ReadAll(f)
  95. if err != nil {
  96. return ProcStat{}, err
  97. }
  98. var (
  99. ignore int
  100. s = ProcStat{PID: p.PID, fs: p.fs}
  101. l = bytes.Index(data, []byte("("))
  102. r = bytes.LastIndex(data, []byte(")"))
  103. )
  104. if l < 0 || r < 0 {
  105. return ProcStat{}, fmt.Errorf(
  106. "unexpected format, couldn't extract comm: %s",
  107. data,
  108. )
  109. }
  110. s.Comm = string(data[l+1 : r])
  111. _, err = fmt.Fscan(
  112. bytes.NewBuffer(data[r+2:]),
  113. &s.State,
  114. &s.PPID,
  115. &s.PGRP,
  116. &s.Session,
  117. &s.TTY,
  118. &s.TPGID,
  119. &s.Flags,
  120. &s.MinFlt,
  121. &s.CMinFlt,
  122. &s.MajFlt,
  123. &s.CMajFlt,
  124. &s.UTime,
  125. &s.STime,
  126. &s.CUTime,
  127. &s.CSTime,
  128. &s.Priority,
  129. &s.Nice,
  130. &s.NumThreads,
  131. &ignore,
  132. &s.Starttime,
  133. &s.VSize,
  134. &s.RSS,
  135. )
  136. if err != nil {
  137. return ProcStat{}, err
  138. }
  139. return s, nil
  140. }
  141. // VirtualMemory returns the virtual memory size in bytes.
  142. func (s ProcStat) VirtualMemory() int {
  143. return s.VSize
  144. }
  145. // ResidentMemory returns the resident memory size in bytes.
  146. func (s ProcStat) ResidentMemory() int {
  147. return s.RSS * os.Getpagesize()
  148. }
  149. // StartTime returns the unix timestamp of the process in seconds.
  150. func (s ProcStat) StartTime() (float64, error) {
  151. stat, err := s.fs.NewStat()
  152. if err != nil {
  153. return 0, err
  154. }
  155. return float64(stat.BootTime) + (float64(s.Starttime) / userHZ), nil
  156. }
  157. // CPUTime returns the total CPU user and system time in seconds.
  158. func (s ProcStat) CPUTime() float64 {
  159. return float64(s.UTime+s.STime) / userHZ
  160. }