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.
 
 

255 lines
7.3 KiB

  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("couldn't parse %q (cpu): %w", line, err)
  88. }
  89. if count == 0 {
  90. return CPUStat{}, -1, fmt.Errorf("couldn't parse %q (cpu): 0 elements parsed", 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("couldn't parse %q (cpu/cpuid): %w", 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("couldn't parse %q (softirq): %w", 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. for scanner.Scan() {
  169. line := scanner.Text()
  170. parts := strings.Fields(scanner.Text())
  171. // require at least <key> <value>
  172. if len(parts) < 2 {
  173. continue
  174. }
  175. switch {
  176. case parts[0] == "btime":
  177. if stat.BootTime, err = strconv.ParseUint(parts[1], 10, 64); err != nil {
  178. return Stat{}, fmt.Errorf("couldn't parse %q (btime): %w", parts[1], err)
  179. }
  180. case parts[0] == "intr":
  181. if stat.IRQTotal, err = strconv.ParseUint(parts[1], 10, 64); err != nil {
  182. return Stat{}, fmt.Errorf("couldn't parse %q (intr): %w", parts[1], err)
  183. }
  184. numberedIRQs := parts[2:]
  185. stat.IRQ = make([]uint64, len(numberedIRQs))
  186. for i, count := range numberedIRQs {
  187. if stat.IRQ[i], err = strconv.ParseUint(count, 10, 64); err != nil {
  188. return Stat{}, fmt.Errorf("couldn't parse %q (intr%d): %w", count, i, err)
  189. }
  190. }
  191. case parts[0] == "ctxt":
  192. if stat.ContextSwitches, err = strconv.ParseUint(parts[1], 10, 64); err != nil {
  193. return Stat{}, fmt.Errorf("couldn't parse %q (ctxt): %w", parts[1], err)
  194. }
  195. case parts[0] == "processes":
  196. if stat.ProcessCreated, err = strconv.ParseUint(parts[1], 10, 64); err != nil {
  197. return Stat{}, fmt.Errorf("couldn't parse %q (processes): %w", parts[1], err)
  198. }
  199. case parts[0] == "procs_running":
  200. if stat.ProcessesRunning, err = strconv.ParseUint(parts[1], 10, 64); err != nil {
  201. return Stat{}, fmt.Errorf("couldn't parse %q (procs_running): %w", parts[1], err)
  202. }
  203. case parts[0] == "procs_blocked":
  204. if stat.ProcessesBlocked, err = strconv.ParseUint(parts[1], 10, 64); err != nil {
  205. return Stat{}, fmt.Errorf("couldn't parse %q (procs_blocked): %w", parts[1], err)
  206. }
  207. case parts[0] == "softirq":
  208. softIRQStats, total, err := parseSoftIRQStat(line)
  209. if err != nil {
  210. return Stat{}, err
  211. }
  212. stat.SoftIRQTotal = total
  213. stat.SoftIRQ = softIRQStats
  214. case strings.HasPrefix(parts[0], "cpu"):
  215. cpuStat, cpuID, err := parseCPUStat(line)
  216. if err != nil {
  217. return Stat{}, err
  218. }
  219. if cpuID == -1 {
  220. stat.CPUTotal = cpuStat
  221. } else {
  222. stat.CPU[cpuID] = cpuStat
  223. }
  224. }
  225. }
  226. if err := scanner.Err(); err != nil {
  227. return Stat{}, fmt.Errorf("couldn't parse %q: %w", fileName, err)
  228. }
  229. return stat, nil
  230. }