25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 

245 satır
7.1 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 []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. stat := Stat{}
  154. scanner := bufio.NewScanner(bytes.NewReader(data))
  155. for scanner.Scan() {
  156. line := scanner.Text()
  157. parts := strings.Fields(scanner.Text())
  158. // require at least <key> <value>
  159. if len(parts) < 2 {
  160. continue
  161. }
  162. switch {
  163. case parts[0] == "btime":
  164. if stat.BootTime, err = strconv.ParseUint(parts[1], 10, 64); err != nil {
  165. return Stat{}, fmt.Errorf("couldn't parse %q (btime): %w", parts[1], err)
  166. }
  167. case parts[0] == "intr":
  168. if stat.IRQTotal, err = strconv.ParseUint(parts[1], 10, 64); err != nil {
  169. return Stat{}, fmt.Errorf("couldn't parse %q (intr): %w", parts[1], err)
  170. }
  171. numberedIRQs := parts[2:]
  172. stat.IRQ = make([]uint64, len(numberedIRQs))
  173. for i, count := range numberedIRQs {
  174. if stat.IRQ[i], err = strconv.ParseUint(count, 10, 64); err != nil {
  175. return Stat{}, fmt.Errorf("couldn't parse %q (intr%d): %w", count, i, err)
  176. }
  177. }
  178. case parts[0] == "ctxt":
  179. if stat.ContextSwitches, err = strconv.ParseUint(parts[1], 10, 64); err != nil {
  180. return Stat{}, fmt.Errorf("couldn't parse %q (ctxt): %w", parts[1], err)
  181. }
  182. case parts[0] == "processes":
  183. if stat.ProcessCreated, err = strconv.ParseUint(parts[1], 10, 64); err != nil {
  184. return Stat{}, fmt.Errorf("couldn't parse %q (processes): %w", parts[1], err)
  185. }
  186. case parts[0] == "procs_running":
  187. if stat.ProcessesRunning, err = strconv.ParseUint(parts[1], 10, 64); err != nil {
  188. return Stat{}, fmt.Errorf("couldn't parse %q (procs_running): %w", parts[1], err)
  189. }
  190. case parts[0] == "procs_blocked":
  191. if stat.ProcessesBlocked, err = strconv.ParseUint(parts[1], 10, 64); err != nil {
  192. return Stat{}, fmt.Errorf("couldn't parse %q (procs_blocked): %w", parts[1], err)
  193. }
  194. case parts[0] == "softirq":
  195. softIRQStats, total, err := parseSoftIRQStat(line)
  196. if err != nil {
  197. return Stat{}, err
  198. }
  199. stat.SoftIRQTotal = total
  200. stat.SoftIRQ = softIRQStats
  201. case strings.HasPrefix(parts[0], "cpu"):
  202. cpuStat, cpuID, err := parseCPUStat(line)
  203. if err != nil {
  204. return Stat{}, err
  205. }
  206. if cpuID == -1 {
  207. stat.CPUTotal = cpuStat
  208. } else {
  209. for int64(len(stat.CPU)) <= cpuID {
  210. stat.CPU = append(stat.CPU, CPUStat{})
  211. }
  212. stat.CPU[cpuID] = cpuStat
  213. }
  214. }
  215. }
  216. if err := scanner.Err(); err != nil {
  217. return Stat{}, fmt.Errorf("couldn't parse %q: %w", fileName, err)
  218. }
  219. return stat, nil
  220. }