softirqs.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // Copyright 2022 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/util"
  22. )
  23. // Softirqs represents the softirq statistics.
  24. type Softirqs struct {
  25. Hi []uint64
  26. Timer []uint64
  27. NetTx []uint64
  28. NetRx []uint64
  29. Block []uint64
  30. IRQPoll []uint64
  31. Tasklet []uint64
  32. Sched []uint64
  33. HRTimer []uint64
  34. RCU []uint64
  35. }
  36. func (fs FS) Softirqs() (Softirqs, error) {
  37. fileName := fs.proc.Path("softirqs")
  38. data, err := util.ReadFileNoStat(fileName)
  39. if err != nil {
  40. return Softirqs{}, err
  41. }
  42. reader := bytes.NewReader(data)
  43. return parseSoftirqs(reader)
  44. }
  45. func parseSoftirqs(r io.Reader) (Softirqs, error) {
  46. var (
  47. softirqs = Softirqs{}
  48. scanner = bufio.NewScanner(r)
  49. )
  50. if !scanner.Scan() {
  51. return Softirqs{}, fmt.Errorf("softirqs empty")
  52. }
  53. for scanner.Scan() {
  54. parts := strings.Fields(scanner.Text())
  55. var err error
  56. // require at least one cpu
  57. if len(parts) < 2 {
  58. continue
  59. }
  60. switch {
  61. case parts[0] == "HI:":
  62. perCPU := parts[1:]
  63. softirqs.Hi = make([]uint64, len(perCPU))
  64. for i, count := range perCPU {
  65. if softirqs.Hi[i], err = strconv.ParseUint(count, 10, 64); err != nil {
  66. return Softirqs{}, fmt.Errorf("couldn't parse %q (HI%d): %w", count, i, err)
  67. }
  68. }
  69. case parts[0] == "TIMER:":
  70. perCPU := parts[1:]
  71. softirqs.Timer = make([]uint64, len(perCPU))
  72. for i, count := range perCPU {
  73. if softirqs.Timer[i], err = strconv.ParseUint(count, 10, 64); err != nil {
  74. return Softirqs{}, fmt.Errorf("couldn't parse %q (TIMER%d): %w", count, i, err)
  75. }
  76. }
  77. case parts[0] == "NET_TX:":
  78. perCPU := parts[1:]
  79. softirqs.NetTx = make([]uint64, len(perCPU))
  80. for i, count := range perCPU {
  81. if softirqs.NetTx[i], err = strconv.ParseUint(count, 10, 64); err != nil {
  82. return Softirqs{}, fmt.Errorf("couldn't parse %q (NET_TX%d): %w", count, i, err)
  83. }
  84. }
  85. case parts[0] == "NET_RX:":
  86. perCPU := parts[1:]
  87. softirqs.NetRx = make([]uint64, len(perCPU))
  88. for i, count := range perCPU {
  89. if softirqs.NetRx[i], err = strconv.ParseUint(count, 10, 64); err != nil {
  90. return Softirqs{}, fmt.Errorf("couldn't parse %q (NET_RX%d): %w", count, i, err)
  91. }
  92. }
  93. case parts[0] == "BLOCK:":
  94. perCPU := parts[1:]
  95. softirqs.Block = make([]uint64, len(perCPU))
  96. for i, count := range perCPU {
  97. if softirqs.Block[i], err = strconv.ParseUint(count, 10, 64); err != nil {
  98. return Softirqs{}, fmt.Errorf("couldn't parse %q (BLOCK%d): %w", count, i, err)
  99. }
  100. }
  101. case parts[0] == "IRQ_POLL:":
  102. perCPU := parts[1:]
  103. softirqs.IRQPoll = make([]uint64, len(perCPU))
  104. for i, count := range perCPU {
  105. if softirqs.IRQPoll[i], err = strconv.ParseUint(count, 10, 64); err != nil {
  106. return Softirqs{}, fmt.Errorf("couldn't parse %q (IRQ_POLL%d): %w", count, i, err)
  107. }
  108. }
  109. case parts[0] == "TASKLET:":
  110. perCPU := parts[1:]
  111. softirqs.Tasklet = make([]uint64, len(perCPU))
  112. for i, count := range perCPU {
  113. if softirqs.Tasklet[i], err = strconv.ParseUint(count, 10, 64); err != nil {
  114. return Softirqs{}, fmt.Errorf("couldn't parse %q (TASKLET%d): %w", count, i, err)
  115. }
  116. }
  117. case parts[0] == "SCHED:":
  118. perCPU := parts[1:]
  119. softirqs.Sched = make([]uint64, len(perCPU))
  120. for i, count := range perCPU {
  121. if softirqs.Sched[i], err = strconv.ParseUint(count, 10, 64); err != nil {
  122. return Softirqs{}, fmt.Errorf("couldn't parse %q (SCHED%d): %w", count, i, err)
  123. }
  124. }
  125. case parts[0] == "HRTIMER:":
  126. perCPU := parts[1:]
  127. softirqs.HRTimer = make([]uint64, len(perCPU))
  128. for i, count := range perCPU {
  129. if softirqs.HRTimer[i], err = strconv.ParseUint(count, 10, 64); err != nil {
  130. return Softirqs{}, fmt.Errorf("couldn't parse %q (HRTIMER%d): %w", count, i, err)
  131. }
  132. }
  133. case parts[0] == "RCU:":
  134. perCPU := parts[1:]
  135. softirqs.RCU = make([]uint64, len(perCPU))
  136. for i, count := range perCPU {
  137. if softirqs.RCU[i], err = strconv.ParseUint(count, 10, 64); err != nil {
  138. return Softirqs{}, fmt.Errorf("couldn't parse %q (RCU%d): %w", count, i, err)
  139. }
  140. }
  141. }
  142. }
  143. if err := scanner.Err(); err != nil {
  144. return Softirqs{}, fmt.Errorf("couldn't parse softirqs: %w", err)
  145. }
  146. return softirqs, scanner.Err()
  147. }