stat.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. "bufio"
  16. "bytes"
  17. "fmt"
  18. "io"
  19. "strconv"
  20. "strings"
  21. "github.com/prometheus/procfs/internal/fs"
  22. "github.com/prometheus/procfs/internal/util"
  23. )
  24. // CPUStat shows how much time the cpu spend in various stages.
  25. type CPUStat struct {
  26. User float64
  27. Nice float64
  28. System float64
  29. Idle float64
  30. Iowait float64
  31. IRQ float64
  32. SoftIRQ float64
  33. Steal float64
  34. Guest float64
  35. GuestNice float64
  36. }
  37. // SoftIRQStat represent the softirq statistics as exported in the procfs stat file.
  38. // A nice introduction can be found at https://0xax.gitbooks.io/linux-insides/content/interrupts/interrupts-9.html
  39. // It is possible to get per-cpu stats by reading `/proc/softirqs`.
  40. type SoftIRQStat struct {
  41. Hi uint64
  42. Timer uint64
  43. NetTx uint64
  44. NetRx uint64
  45. Block uint64
  46. BlockIoPoll uint64
  47. Tasklet uint64
  48. Sched uint64
  49. Hrtimer uint64
  50. Rcu uint64
  51. }
  52. // Stat represents kernel/system statistics.
  53. type Stat struct {
  54. // Boot time in seconds since the Epoch.
  55. BootTime uint64
  56. // Summed up cpu statistics.
  57. CPUTotal CPUStat
  58. // Per-CPU statistics.
  59. CPU map[int64]CPUStat
  60. // Number of times interrupts were handled, which contains numbered and unnumbered IRQs.
  61. IRQTotal uint64
  62. // Number of times a numbered IRQ was triggered.
  63. IRQ []uint64
  64. // Number of times a context switch happened.
  65. ContextSwitches uint64
  66. // Number of times a process was created.
  67. ProcessCreated uint64
  68. // Number of processes currently running.
  69. ProcessesRunning uint64
  70. // Number of processes currently blocked (waiting for IO).
  71. ProcessesBlocked uint64
  72. // Number of times a softirq was scheduled.
  73. SoftIRQTotal uint64
  74. // Detailed softirq statistics.
  75. SoftIRQ SoftIRQStat
  76. }
  77. // Parse a cpu statistics line and returns the CPUStat struct plus the cpu id (or -1 for the overall sum).
  78. func parseCPUStat(line string) (CPUStat, int64, error) {
  79. cpuStat := CPUStat{}
  80. var cpu string
  81. count, err := fmt.Sscanf(line, "%s %f %f %f %f %f %f %f %f %f %f",
  82. &cpu,
  83. &cpuStat.User, &cpuStat.Nice, &cpuStat.System, &cpuStat.Idle,
  84. &cpuStat.Iowait, &cpuStat.IRQ, &cpuStat.SoftIRQ, &cpuStat.Steal,
  85. &cpuStat.Guest, &cpuStat.GuestNice)
  86. if err != nil && err != io.EOF {
  87. return CPUStat{}, -1, fmt.Errorf("%s: couldn't parse %q (cpu): %w", ErrFileParse, line, err)
  88. }
  89. if count == 0 {
  90. return CPUStat{}, -1, fmt.Errorf("%w: couldn't parse %q (cpu): 0 elements parsed", ErrFileParse, line)
  91. }
  92. cpuStat.User /= userHZ
  93. cpuStat.Nice /= userHZ
  94. cpuStat.System /= userHZ
  95. cpuStat.Idle /= userHZ
  96. cpuStat.Iowait /= userHZ
  97. cpuStat.IRQ /= userHZ
  98. cpuStat.SoftIRQ /= userHZ
  99. cpuStat.Steal /= userHZ
  100. cpuStat.Guest /= userHZ
  101. cpuStat.GuestNice /= userHZ
  102. if cpu == "cpu" {
  103. return cpuStat, -1, nil
  104. }
  105. cpuID, err := strconv.ParseInt(cpu[3:], 10, 64)
  106. if err != nil {
  107. return CPUStat{}, -1, fmt.Errorf("%s: couldn't parse %q (cpu/cpuid): %w", ErrFileParse, line, err)
  108. }
  109. return cpuStat, cpuID, nil
  110. }
  111. // Parse a softirq line.
  112. func parseSoftIRQStat(line string) (SoftIRQStat, uint64, error) {
  113. softIRQStat := SoftIRQStat{}
  114. var total uint64
  115. var prefix string
  116. _, err := fmt.Sscanf(line, "%s %d %d %d %d %d %d %d %d %d %d %d",
  117. &prefix, &total,
  118. &softIRQStat.Hi, &softIRQStat.Timer, &softIRQStat.NetTx, &softIRQStat.NetRx,
  119. &softIRQStat.Block, &softIRQStat.BlockIoPoll,
  120. &softIRQStat.Tasklet, &softIRQStat.Sched,
  121. &softIRQStat.Hrtimer, &softIRQStat.Rcu)
  122. if err != nil {
  123. return SoftIRQStat{}, 0, fmt.Errorf("%s: couldn't parse %q (softirq): %w", ErrFileParse, line, err)
  124. }
  125. return softIRQStat, total, nil
  126. }
  127. // NewStat returns information about current cpu/process statistics.
  128. // See https://www.kernel.org/doc/Documentation/filesystems/proc.txt
  129. //
  130. // Deprecated: Use fs.Stat() instead.
  131. func NewStat() (Stat, error) {
  132. fs, err := NewFS(fs.DefaultProcMountPoint)
  133. if err != nil {
  134. return Stat{}, err
  135. }
  136. return fs.Stat()
  137. }
  138. // NewStat returns information about current cpu/process statistics.
  139. // See: https://www.kernel.org/doc/Documentation/filesystems/proc.txt
  140. //
  141. // Deprecated: Use fs.Stat() instead.
  142. func (fs FS) NewStat() (Stat, error) {
  143. return fs.Stat()
  144. }
  145. // Stat returns information about current cpu/process statistics.
  146. // See: https://www.kernel.org/doc/Documentation/filesystems/proc.txt
  147. func (fs FS) Stat() (Stat, error) {
  148. fileName := fs.proc.Path("stat")
  149. data, err := util.ReadFileNoStat(fileName)
  150. if err != nil {
  151. return Stat{}, err
  152. }
  153. procStat, err := parseStat(bytes.NewReader(data), fileName)
  154. if err != nil {
  155. return Stat{}, err
  156. }
  157. return procStat, nil
  158. }
  159. // parseStat parses the metrics from /proc/[pid]/stat.
  160. func parseStat(r io.Reader, fileName string) (Stat, error) {
  161. var (
  162. scanner = bufio.NewScanner(r)
  163. stat = Stat{
  164. CPU: make(map[int64]CPUStat),
  165. }
  166. err error
  167. )
  168. // Increase default scanner buffer to handle very long `intr` lines.
  169. buf := make([]byte, 0, 8*1024)
  170. scanner.Buffer(buf, 1024*1024)
  171. for scanner.Scan() {
  172. line := scanner.Text()
  173. parts := strings.Fields(scanner.Text())
  174. // require at least <key> <value>
  175. if len(parts) < 2 {
  176. continue
  177. }
  178. switch {
  179. case parts[0] == "btime":
  180. if stat.BootTime, err = strconv.ParseUint(parts[1], 10, 64); err != nil {
  181. return Stat{}, fmt.Errorf("%s: couldn't parse %q (btime): %w", ErrFileParse, parts[1], err)
  182. }
  183. case parts[0] == "intr":
  184. if stat.IRQTotal, err = strconv.ParseUint(parts[1], 10, 64); err != nil {
  185. return Stat{}, fmt.Errorf("%s: couldn't parse %q (intr): %w", ErrFileParse, parts[1], err)
  186. }
  187. numberedIRQs := parts[2:]
  188. stat.IRQ = make([]uint64, len(numberedIRQs))
  189. for i, count := range numberedIRQs {
  190. if stat.IRQ[i], err = strconv.ParseUint(count, 10, 64); err != nil {
  191. return Stat{}, fmt.Errorf("%s: couldn't parse %q (intr%d): %w", ErrFileParse, count, i, err)
  192. }
  193. }
  194. case parts[0] == "ctxt":
  195. if stat.ContextSwitches, err = strconv.ParseUint(parts[1], 10, 64); err != nil {
  196. return Stat{}, fmt.Errorf("%s: couldn't parse %q (ctxt): %w", ErrFileParse, parts[1], err)
  197. }
  198. case parts[0] == "processes":
  199. if stat.ProcessCreated, err = strconv.ParseUint(parts[1], 10, 64); err != nil {
  200. return Stat{}, fmt.Errorf("%s: couldn't parse %q (processes): %w", ErrFileParse, parts[1], err)
  201. }
  202. case parts[0] == "procs_running":
  203. if stat.ProcessesRunning, err = strconv.ParseUint(parts[1], 10, 64); err != nil {
  204. return Stat{}, fmt.Errorf("%s: couldn't parse %q (procs_running): %w", ErrFileParse, parts[1], err)
  205. }
  206. case parts[0] == "procs_blocked":
  207. if stat.ProcessesBlocked, err = strconv.ParseUint(parts[1], 10, 64); err != nil {
  208. return Stat{}, fmt.Errorf("%s: couldn't parse %q (procs_blocked): %w", ErrFileParse, parts[1], err)
  209. }
  210. case parts[0] == "softirq":
  211. softIRQStats, total, err := parseSoftIRQStat(line)
  212. if err != nil {
  213. return Stat{}, err
  214. }
  215. stat.SoftIRQTotal = total
  216. stat.SoftIRQ = softIRQStats
  217. case strings.HasPrefix(parts[0], "cpu"):
  218. cpuStat, cpuID, err := parseCPUStat(line)
  219. if err != nil {
  220. return Stat{}, err
  221. }
  222. if cpuID == -1 {
  223. stat.CPUTotal = cpuStat
  224. } else {
  225. stat.CPU[cpuID] = cpuStat
  226. }
  227. }
  228. }
  229. if err := scanner.Err(); err != nil {
  230. return Stat{}, fmt.Errorf("%s: couldn't parse %q: %w", ErrFileParse, fileName, err)
  231. }
  232. return stat, nil
  233. }