25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

166 lines
3.8 KiB

  1. // Copyright 2020 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. "bufio"
  17. "errors"
  18. "fmt"
  19. "os"
  20. "regexp"
  21. "strconv"
  22. "strings"
  23. "github.com/prometheus/procfs/internal/util"
  24. )
  25. var (
  26. // match the header line before each mapped zone in /proc/pid/smaps
  27. procSMapsHeaderLine = regexp.MustCompile(`^[a-f0-9].*$`)
  28. )
  29. type ProcSMapsRollup struct {
  30. // Amount of the mapping that is currently resident in RAM
  31. Rss uint64
  32. // Process's proportional share of this mapping
  33. Pss uint64
  34. // Size in bytes of clean shared pages
  35. SharedClean uint64
  36. // Size in bytes of dirty shared pages
  37. SharedDirty uint64
  38. // Size in bytes of clean private pages
  39. PrivateClean uint64
  40. // Size in bytes of dirty private pages
  41. PrivateDirty uint64
  42. // Amount of memory currently marked as referenced or accessed
  43. Referenced uint64
  44. // Amount of memory that does not belong to any file
  45. Anonymous uint64
  46. // Amount would-be-anonymous memory currently on swap
  47. Swap uint64
  48. // Process's proportional memory on swap
  49. SwapPss uint64
  50. }
  51. // ProcSMapsRollup reads from /proc/[pid]/smaps_rollup to get summed memory information of the
  52. // process.
  53. //
  54. // If smaps_rollup does not exists (require kernel >= 4.15), the content of /proc/pid/smaps will
  55. // we read and summed.
  56. func (p Proc) ProcSMapsRollup() (ProcSMapsRollup, error) {
  57. data, err := util.ReadFileNoStat(p.path("smaps_rollup"))
  58. if err != nil && os.IsNotExist(err) {
  59. return p.procSMapsRollupManual()
  60. }
  61. if err != nil {
  62. return ProcSMapsRollup{}, err
  63. }
  64. lines := strings.Split(string(data), "\n")
  65. smaps := ProcSMapsRollup{}
  66. // skip first line which don't contains information we need
  67. lines = lines[1:]
  68. for _, line := range lines {
  69. if line == "" {
  70. continue
  71. }
  72. if err := smaps.parseLine(line); err != nil {
  73. return ProcSMapsRollup{}, err
  74. }
  75. }
  76. return smaps, nil
  77. }
  78. // Read /proc/pid/smaps and do the roll-up in Go code.
  79. func (p Proc) procSMapsRollupManual() (ProcSMapsRollup, error) {
  80. file, err := os.Open(p.path("smaps"))
  81. if err != nil {
  82. return ProcSMapsRollup{}, err
  83. }
  84. defer file.Close()
  85. smaps := ProcSMapsRollup{}
  86. scan := bufio.NewScanner(file)
  87. for scan.Scan() {
  88. line := scan.Text()
  89. if procSMapsHeaderLine.MatchString(line) {
  90. continue
  91. }
  92. if err := smaps.parseLine(line); err != nil {
  93. return ProcSMapsRollup{}, err
  94. }
  95. }
  96. return smaps, nil
  97. }
  98. func (s *ProcSMapsRollup) parseLine(line string) error {
  99. kv := strings.SplitN(line, ":", 2)
  100. if len(kv) != 2 {
  101. fmt.Println(line)
  102. return errors.New("invalid net/dev line, missing colon")
  103. }
  104. k := kv[0]
  105. if k == "VmFlags" {
  106. return nil
  107. }
  108. v := strings.TrimSpace(kv[1])
  109. v = strings.TrimRight(v, " kB")
  110. vKBytes, err := strconv.ParseUint(v, 10, 64)
  111. if err != nil {
  112. return err
  113. }
  114. vBytes := vKBytes * 1024
  115. s.addValue(k, v, vKBytes, vBytes)
  116. return nil
  117. }
  118. func (s *ProcSMapsRollup) addValue(k string, vString string, vUint uint64, vUintBytes uint64) {
  119. switch k {
  120. case "Rss":
  121. s.Rss += vUintBytes
  122. case "Pss":
  123. s.Pss += vUintBytes
  124. case "Shared_Clean":
  125. s.SharedClean += vUintBytes
  126. case "Shared_Dirty":
  127. s.SharedDirty += vUintBytes
  128. case "Private_Clean":
  129. s.PrivateClean += vUintBytes
  130. case "Private_Dirty":
  131. s.PrivateDirty += vUintBytes
  132. case "Referenced":
  133. s.Referenced += vUintBytes
  134. case "Anonymous":
  135. s.Anonymous += vUintBytes
  136. case "Swap":
  137. s.Swap += vUintBytes
  138. case "SwapPss":
  139. s.SwapPss += vUintBytes
  140. }
  141. }