本站源代码
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.

245 lines
7.0KB

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