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.
 
 

151 lines
3.9 KiB

  1. // Copyright 2019 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. // For the proc file format details,
  24. // See:
  25. // * Linux 2.6.23 https://elixir.bootlin.com/linux/v2.6.23/source/net/core/dev.c#L2343
  26. // * Linux 2.6.39 https://elixir.bootlin.com/linux/v2.6.39/source/net/core/dev.c#L4086
  27. // * Linux 4.18 https://elixir.bootlin.com/linux/v4.18/source/net/core/net-procfs.c#L162
  28. // * Linux 5.14 https://elixir.bootlin.com/linux/v5.14/source/net/core/net-procfs.c#L169
  29. // SoftnetStat contains a single row of data from /proc/net/softnet_stat.
  30. type SoftnetStat struct {
  31. // Number of processed packets.
  32. Processed uint32
  33. // Number of dropped packets.
  34. Dropped uint32
  35. // Number of times processing packets ran out of quota.
  36. TimeSqueezed uint32
  37. // Number of collision occur while obtaining device lock while transmitting.
  38. CPUCollision uint32
  39. // Number of times cpu woken up received_rps.
  40. ReceivedRps uint32
  41. // number of times flow limit has been reached.
  42. FlowLimitCount uint32
  43. // Softnet backlog status.
  44. SoftnetBacklogLen uint32
  45. // CPU id owning this softnet_data.
  46. Index uint32
  47. // softnet_data's Width.
  48. Width int
  49. }
  50. var softNetProcFile = "net/softnet_stat"
  51. // NetSoftnetStat reads data from /proc/net/softnet_stat.
  52. func (fs FS) NetSoftnetStat() ([]SoftnetStat, error) {
  53. b, err := util.ReadFileNoStat(fs.proc.Path(softNetProcFile))
  54. if err != nil {
  55. return nil, err
  56. }
  57. entries, err := parseSoftnet(bytes.NewReader(b))
  58. if err != nil {
  59. return nil, fmt.Errorf("failed to parse /proc/net/softnet_stat: %w", err)
  60. }
  61. return entries, nil
  62. }
  63. func parseSoftnet(r io.Reader) ([]SoftnetStat, error) {
  64. const minColumns = 9
  65. s := bufio.NewScanner(r)
  66. var stats []SoftnetStat
  67. for s.Scan() {
  68. columns := strings.Fields(s.Text())
  69. width := len(columns)
  70. softnetStat := SoftnetStat{}
  71. if width < minColumns {
  72. return nil, fmt.Errorf("%d columns were detected, but at least %d were expected", width, minColumns)
  73. }
  74. // Linux 2.6.23 https://elixir.bootlin.com/linux/v2.6.23/source/net/core/dev.c#L2347
  75. if width >= minColumns {
  76. us, err := parseHexUint32s(columns[0:9])
  77. if err != nil {
  78. return nil, err
  79. }
  80. softnetStat.Processed = us[0]
  81. softnetStat.Dropped = us[1]
  82. softnetStat.TimeSqueezed = us[2]
  83. softnetStat.CPUCollision = us[8]
  84. }
  85. // Linux 2.6.39 https://elixir.bootlin.com/linux/v2.6.39/source/net/core/dev.c#L4086
  86. if width >= 10 {
  87. us, err := parseHexUint32s(columns[9:10])
  88. if err != nil {
  89. return nil, err
  90. }
  91. softnetStat.ReceivedRps = us[0]
  92. }
  93. // Linux 4.18 https://elixir.bootlin.com/linux/v4.18/source/net/core/net-procfs.c#L162
  94. if width >= 11 {
  95. us, err := parseHexUint32s(columns[10:11])
  96. if err != nil {
  97. return nil, err
  98. }
  99. softnetStat.FlowLimitCount = us[0]
  100. }
  101. // Linux 5.14 https://elixir.bootlin.com/linux/v5.14/source/net/core/net-procfs.c#L169
  102. if width >= 13 {
  103. us, err := parseHexUint32s(columns[11:13])
  104. if err != nil {
  105. return nil, err
  106. }
  107. softnetStat.SoftnetBacklogLen = us[0]
  108. softnetStat.Index = us[1]
  109. }
  110. softnetStat.Width = width
  111. stats = append(stats, softnetStat)
  112. }
  113. return stats, nil
  114. }
  115. func parseHexUint32s(ss []string) ([]uint32, error) {
  116. us := make([]uint32, 0, len(ss))
  117. for _, s := range ss {
  118. u, err := strconv.ParseUint(s, 16, 32)
  119. if err != nil {
  120. return nil, err
  121. }
  122. us = append(us, uint32(u))
  123. }
  124. return us, nil
  125. }