本站源代码
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

197 lines
6.3KB

  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. // +build !windows
  14. package procfs
  15. import (
  16. "bytes"
  17. "fmt"
  18. "io/ioutil"
  19. "regexp"
  20. "strings"
  21. "github.com/prometheus/procfs/internal/util"
  22. )
  23. // Zoneinfo holds info parsed from /proc/zoneinfo.
  24. type Zoneinfo struct {
  25. Node string
  26. Zone string
  27. NrFreePages *int64
  28. Min *int64
  29. Low *int64
  30. High *int64
  31. Scanned *int64
  32. Spanned *int64
  33. Present *int64
  34. Managed *int64
  35. NrActiveAnon *int64
  36. NrInactiveAnon *int64
  37. NrIsolatedAnon *int64
  38. NrAnonPages *int64
  39. NrAnonTransparentHugepages *int64
  40. NrActiveFile *int64
  41. NrInactiveFile *int64
  42. NrIsolatedFile *int64
  43. NrFilePages *int64
  44. NrSlabReclaimable *int64
  45. NrSlabUnreclaimable *int64
  46. NrMlockStack *int64
  47. NrKernelStack *int64
  48. NrMapped *int64
  49. NrDirty *int64
  50. NrWriteback *int64
  51. NrUnevictable *int64
  52. NrShmem *int64
  53. NrDirtied *int64
  54. NrWritten *int64
  55. NumaHit *int64
  56. NumaMiss *int64
  57. NumaForeign *int64
  58. NumaInterleave *int64
  59. NumaLocal *int64
  60. NumaOther *int64
  61. Protection []*int64
  62. }
  63. var nodeZoneRE = regexp.MustCompile(`(\d+), zone\s+(\w+)`)
  64. // Zoneinfo parses an zoneinfo-file (/proc/zoneinfo) and returns a slice of
  65. // structs containing the relevant info. More information available here:
  66. // https://www.kernel.org/doc/Documentation/sysctl/vm.txt
  67. func (fs FS) Zoneinfo() ([]Zoneinfo, error) {
  68. data, err := ioutil.ReadFile(fs.proc.Path("zoneinfo"))
  69. if err != nil {
  70. return nil, fmt.Errorf("error reading zoneinfo %s: %s", fs.proc.Path("zoneinfo"), err)
  71. }
  72. zoneinfo, err := parseZoneinfo(data)
  73. if err != nil {
  74. return nil, fmt.Errorf("error parsing zoneinfo %s: %s", fs.proc.Path("zoneinfo"), err)
  75. }
  76. return zoneinfo, nil
  77. }
  78. func parseZoneinfo(zoneinfoData []byte) ([]Zoneinfo, error) {
  79. zoneinfo := []Zoneinfo{}
  80. zoneinfoBlocks := bytes.Split(zoneinfoData, []byte("\nNode"))
  81. for _, block := range zoneinfoBlocks {
  82. var zoneinfoElement Zoneinfo
  83. lines := strings.Split(string(block), "\n")
  84. for _, line := range lines {
  85. if nodeZone := nodeZoneRE.FindStringSubmatch(line); nodeZone != nil {
  86. zoneinfoElement.Node = nodeZone[1]
  87. zoneinfoElement.Zone = nodeZone[2]
  88. continue
  89. }
  90. if strings.HasPrefix(strings.TrimSpace(line), "per-node stats") {
  91. zoneinfoElement.Zone = ""
  92. continue
  93. }
  94. parts := strings.Fields(strings.TrimSpace(line))
  95. if len(parts) < 2 {
  96. continue
  97. }
  98. vp := util.NewValueParser(parts[1])
  99. switch parts[0] {
  100. case "nr_free_pages":
  101. zoneinfoElement.NrFreePages = vp.PInt64()
  102. case "min":
  103. zoneinfoElement.Min = vp.PInt64()
  104. case "low":
  105. zoneinfoElement.Low = vp.PInt64()
  106. case "high":
  107. zoneinfoElement.High = vp.PInt64()
  108. case "scanned":
  109. zoneinfoElement.Scanned = vp.PInt64()
  110. case "spanned":
  111. zoneinfoElement.Spanned = vp.PInt64()
  112. case "present":
  113. zoneinfoElement.Present = vp.PInt64()
  114. case "managed":
  115. zoneinfoElement.Managed = vp.PInt64()
  116. case "nr_active_anon":
  117. zoneinfoElement.NrActiveAnon = vp.PInt64()
  118. case "nr_inactive_anon":
  119. zoneinfoElement.NrInactiveAnon = vp.PInt64()
  120. case "nr_isolated_anon":
  121. zoneinfoElement.NrIsolatedAnon = vp.PInt64()
  122. case "nr_anon_pages":
  123. zoneinfoElement.NrAnonPages = vp.PInt64()
  124. case "nr_anon_transparent_hugepages":
  125. zoneinfoElement.NrAnonTransparentHugepages = vp.PInt64()
  126. case "nr_active_file":
  127. zoneinfoElement.NrActiveFile = vp.PInt64()
  128. case "nr_inactive_file":
  129. zoneinfoElement.NrInactiveFile = vp.PInt64()
  130. case "nr_isolated_file":
  131. zoneinfoElement.NrIsolatedFile = vp.PInt64()
  132. case "nr_file_pages":
  133. zoneinfoElement.NrFilePages = vp.PInt64()
  134. case "nr_slab_reclaimable":
  135. zoneinfoElement.NrSlabReclaimable = vp.PInt64()
  136. case "nr_slab_unreclaimable":
  137. zoneinfoElement.NrSlabUnreclaimable = vp.PInt64()
  138. case "nr_mlock_stack":
  139. zoneinfoElement.NrMlockStack = vp.PInt64()
  140. case "nr_kernel_stack":
  141. zoneinfoElement.NrKernelStack = vp.PInt64()
  142. case "nr_mapped":
  143. zoneinfoElement.NrMapped = vp.PInt64()
  144. case "nr_dirty":
  145. zoneinfoElement.NrDirty = vp.PInt64()
  146. case "nr_writeback":
  147. zoneinfoElement.NrWriteback = vp.PInt64()
  148. case "nr_unevictable":
  149. zoneinfoElement.NrUnevictable = vp.PInt64()
  150. case "nr_shmem":
  151. zoneinfoElement.NrShmem = vp.PInt64()
  152. case "nr_dirtied":
  153. zoneinfoElement.NrDirtied = vp.PInt64()
  154. case "nr_written":
  155. zoneinfoElement.NrWritten = vp.PInt64()
  156. case "numa_hit":
  157. zoneinfoElement.NumaHit = vp.PInt64()
  158. case "numa_miss":
  159. zoneinfoElement.NumaMiss = vp.PInt64()
  160. case "numa_foreign":
  161. zoneinfoElement.NumaForeign = vp.PInt64()
  162. case "numa_interleave":
  163. zoneinfoElement.NumaInterleave = vp.PInt64()
  164. case "numa_local":
  165. zoneinfoElement.NumaLocal = vp.PInt64()
  166. case "numa_other":
  167. zoneinfoElement.NumaOther = vp.PInt64()
  168. case "protection:":
  169. protectionParts := strings.Split(line, ":")
  170. protectionValues := strings.Replace(protectionParts[1], "(", "", 1)
  171. protectionValues = strings.Replace(protectionValues, ")", "", 1)
  172. protectionValues = strings.TrimSpace(protectionValues)
  173. protectionStringMap := strings.Split(protectionValues, ", ")
  174. val, err := util.ParsePInt64s(protectionStringMap)
  175. if err == nil {
  176. zoneinfoElement.Protection = val
  177. }
  178. }
  179. }
  180. zoneinfo = append(zoneinfo, zoneinfoElement)
  181. }
  182. return zoneinfo, nil
  183. }
上海开阖软件有限公司 沪ICP备12045867号-1