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

211 lines
7.7KB

  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. "fmt"
  17. "io/ioutil"
  18. "os"
  19. "path/filepath"
  20. "strings"
  21. "github.com/prometheus/procfs/internal/util"
  22. )
  23. // The VM interface is described at
  24. // https://www.kernel.org/doc/Documentation/sysctl/vm.txt
  25. // Each setting is exposed as a single file.
  26. // Each file contains one line with a single numerical value, except lowmem_reserve_ratio which holds an array
  27. // and numa_zonelist_order (deprecated) which is a string
  28. type VM struct {
  29. AdminReserveKbytes *int64 // /proc/sys/vm/admin_reserve_kbytes
  30. BlockDump *int64 // /proc/sys/vm/block_dump
  31. CompactUnevictableAllowed *int64 // /proc/sys/vm/compact_unevictable_allowed
  32. DirtyBackgroundBytes *int64 // /proc/sys/vm/dirty_background_bytes
  33. DirtyBackgroundRatio *int64 // /proc/sys/vm/dirty_background_ratio
  34. DirtyBytes *int64 // /proc/sys/vm/dirty_bytes
  35. DirtyExpireCentisecs *int64 // /proc/sys/vm/dirty_expire_centisecs
  36. DirtyRatio *int64 // /proc/sys/vm/dirty_ratio
  37. DirtytimeExpireSeconds *int64 // /proc/sys/vm/dirtytime_expire_seconds
  38. DirtyWritebackCentisecs *int64 // /proc/sys/vm/dirty_writeback_centisecs
  39. DropCaches *int64 // /proc/sys/vm/drop_caches
  40. ExtfragThreshold *int64 // /proc/sys/vm/extfrag_threshold
  41. HugetlbShmGroup *int64 // /proc/sys/vm/hugetlb_shm_group
  42. LaptopMode *int64 // /proc/sys/vm/laptop_mode
  43. LegacyVaLayout *int64 // /proc/sys/vm/legacy_va_layout
  44. LowmemReserveRatio []*int64 // /proc/sys/vm/lowmem_reserve_ratio
  45. MaxMapCount *int64 // /proc/sys/vm/max_map_count
  46. MemoryFailureEarlyKill *int64 // /proc/sys/vm/memory_failure_early_kill
  47. MemoryFailureRecovery *int64 // /proc/sys/vm/memory_failure_recovery
  48. MinFreeKbytes *int64 // /proc/sys/vm/min_free_kbytes
  49. MinSlabRatio *int64 // /proc/sys/vm/min_slab_ratio
  50. MinUnmappedRatio *int64 // /proc/sys/vm/min_unmapped_ratio
  51. MmapMinAddr *int64 // /proc/sys/vm/mmap_min_addr
  52. NrHugepages *int64 // /proc/sys/vm/nr_hugepages
  53. NrHugepagesMempolicy *int64 // /proc/sys/vm/nr_hugepages_mempolicy
  54. NrOvercommitHugepages *int64 // /proc/sys/vm/nr_overcommit_hugepages
  55. NumaStat *int64 // /proc/sys/vm/numa_stat
  56. NumaZonelistOrder string // /proc/sys/vm/numa_zonelist_order
  57. OomDumpTasks *int64 // /proc/sys/vm/oom_dump_tasks
  58. OomKillAllocatingTask *int64 // /proc/sys/vm/oom_kill_allocating_task
  59. OvercommitKbytes *int64 // /proc/sys/vm/overcommit_kbytes
  60. OvercommitMemory *int64 // /proc/sys/vm/overcommit_memory
  61. OvercommitRatio *int64 // /proc/sys/vm/overcommit_ratio
  62. PageCluster *int64 // /proc/sys/vm/page-cluster
  63. PanicOnOom *int64 // /proc/sys/vm/panic_on_oom
  64. PercpuPagelistFraction *int64 // /proc/sys/vm/percpu_pagelist_fraction
  65. StatInterval *int64 // /proc/sys/vm/stat_interval
  66. Swappiness *int64 // /proc/sys/vm/swappiness
  67. UserReserveKbytes *int64 // /proc/sys/vm/user_reserve_kbytes
  68. VfsCachePressure *int64 // /proc/sys/vm/vfs_cache_pressure
  69. WatermarkBoostFactor *int64 // /proc/sys/vm/watermark_boost_factor
  70. WatermarkScaleFactor *int64 // /proc/sys/vm/watermark_scale_factor
  71. ZoneReclaimMode *int64 // /proc/sys/vm/zone_reclaim_mode
  72. }
  73. // VM reads the VM statistics from the specified `proc` filesystem.
  74. func (fs FS) VM() (*VM, error) {
  75. path := fs.proc.Path("sys/vm")
  76. file, err := os.Stat(path)
  77. if err != nil {
  78. return nil, err
  79. }
  80. if !file.Mode().IsDir() {
  81. return nil, fmt.Errorf("%s is not a directory", path)
  82. }
  83. files, err := ioutil.ReadDir(path)
  84. if err != nil {
  85. return nil, err
  86. }
  87. var vm VM
  88. for _, f := range files {
  89. if f.IsDir() {
  90. continue
  91. }
  92. name := filepath.Join(path, f.Name())
  93. // ignore errors on read, as there are some write only
  94. // in /proc/sys/vm
  95. value, err := util.SysReadFile(name)
  96. if err != nil {
  97. continue
  98. }
  99. vp := util.NewValueParser(value)
  100. switch f.Name() {
  101. case "admin_reserve_kbytes":
  102. vm.AdminReserveKbytes = vp.PInt64()
  103. case "block_dump":
  104. vm.BlockDump = vp.PInt64()
  105. case "compact_unevictable_allowed":
  106. vm.CompactUnevictableAllowed = vp.PInt64()
  107. case "dirty_background_bytes":
  108. vm.DirtyBackgroundBytes = vp.PInt64()
  109. case "dirty_background_ratio":
  110. vm.DirtyBackgroundRatio = vp.PInt64()
  111. case "dirty_bytes":
  112. vm.DirtyBytes = vp.PInt64()
  113. case "dirty_expire_centisecs":
  114. vm.DirtyExpireCentisecs = vp.PInt64()
  115. case "dirty_ratio":
  116. vm.DirtyRatio = vp.PInt64()
  117. case "dirtytime_expire_seconds":
  118. vm.DirtytimeExpireSeconds = vp.PInt64()
  119. case "dirty_writeback_centisecs":
  120. vm.DirtyWritebackCentisecs = vp.PInt64()
  121. case "drop_caches":
  122. vm.DropCaches = vp.PInt64()
  123. case "extfrag_threshold":
  124. vm.ExtfragThreshold = vp.PInt64()
  125. case "hugetlb_shm_group":
  126. vm.HugetlbShmGroup = vp.PInt64()
  127. case "laptop_mode":
  128. vm.LaptopMode = vp.PInt64()
  129. case "legacy_va_layout":
  130. vm.LegacyVaLayout = vp.PInt64()
  131. case "lowmem_reserve_ratio":
  132. stringSlice := strings.Fields(value)
  133. pint64Slice := make([]*int64, 0, len(stringSlice))
  134. for _, value := range stringSlice {
  135. vp := util.NewValueParser(value)
  136. pint64Slice = append(pint64Slice, vp.PInt64())
  137. }
  138. vm.LowmemReserveRatio = pint64Slice
  139. case "max_map_count":
  140. vm.MaxMapCount = vp.PInt64()
  141. case "memory_failure_early_kill":
  142. vm.MemoryFailureEarlyKill = vp.PInt64()
  143. case "memory_failure_recovery":
  144. vm.MemoryFailureRecovery = vp.PInt64()
  145. case "min_free_kbytes":
  146. vm.MinFreeKbytes = vp.PInt64()
  147. case "min_slab_ratio":
  148. vm.MinSlabRatio = vp.PInt64()
  149. case "min_unmapped_ratio":
  150. vm.MinUnmappedRatio = vp.PInt64()
  151. case "mmap_min_addr":
  152. vm.MmapMinAddr = vp.PInt64()
  153. case "nr_hugepages":
  154. vm.NrHugepages = vp.PInt64()
  155. case "nr_hugepages_mempolicy":
  156. vm.NrHugepagesMempolicy = vp.PInt64()
  157. case "nr_overcommit_hugepages":
  158. vm.NrOvercommitHugepages = vp.PInt64()
  159. case "numa_stat":
  160. vm.NumaStat = vp.PInt64()
  161. case "numa_zonelist_order":
  162. vm.NumaZonelistOrder = value
  163. case "oom_dump_tasks":
  164. vm.OomDumpTasks = vp.PInt64()
  165. case "oom_kill_allocating_task":
  166. vm.OomKillAllocatingTask = vp.PInt64()
  167. case "overcommit_kbytes":
  168. vm.OvercommitKbytes = vp.PInt64()
  169. case "overcommit_memory":
  170. vm.OvercommitMemory = vp.PInt64()
  171. case "overcommit_ratio":
  172. vm.OvercommitRatio = vp.PInt64()
  173. case "page-cluster":
  174. vm.PageCluster = vp.PInt64()
  175. case "panic_on_oom":
  176. vm.PanicOnOom = vp.PInt64()
  177. case "percpu_pagelist_fraction":
  178. vm.PercpuPagelistFraction = vp.PInt64()
  179. case "stat_interval":
  180. vm.StatInterval = vp.PInt64()
  181. case "swappiness":
  182. vm.Swappiness = vp.PInt64()
  183. case "user_reserve_kbytes":
  184. vm.UserReserveKbytes = vp.PInt64()
  185. case "vfs_cache_pressure":
  186. vm.VfsCachePressure = vp.PInt64()
  187. case "watermark_boost_factor":
  188. vm.WatermarkBoostFactor = vp.PInt64()
  189. case "watermark_scale_factor":
  190. vm.WatermarkScaleFactor = vp.PInt64()
  191. case "zone_reclaim_mode":
  192. vm.ZoneReclaimMode = vp.PInt64()
  193. }
  194. if err := vp.Err(); err != nil {
  195. return nil, err
  196. }
  197. }
  198. return &vm, nil
  199. }
上海开阖软件有限公司 沪ICP备12045867号-1