stat.go 6.7 KB

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