proc_smaps.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. //go:build !windows
  14. // +build !windows
  15. package procfs
  16. import (
  17. "bufio"
  18. "errors"
  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. return errors.New("invalid net/dev line, missing colon")
  102. }
  103. k := kv[0]
  104. if k == "VmFlags" {
  105. return nil
  106. }
  107. v := strings.TrimSpace(kv[1])
  108. v = strings.TrimSuffix(v, " kB")
  109. vKBytes, err := strconv.ParseUint(v, 10, 64)
  110. if err != nil {
  111. return err
  112. }
  113. vBytes := vKBytes * 1024
  114. s.addValue(k, vBytes)
  115. return nil
  116. }
  117. func (s *ProcSMapsRollup) addValue(k string, vUintBytes uint64) {
  118. switch k {
  119. case "Rss":
  120. s.Rss += vUintBytes
  121. case "Pss":
  122. s.Pss += vUintBytes
  123. case "Shared_Clean":
  124. s.SharedClean += vUintBytes
  125. case "Shared_Dirty":
  126. s.SharedDirty += vUintBytes
  127. case "Private_Clean":
  128. s.PrivateClean += vUintBytes
  129. case "Private_Dirty":
  130. s.PrivateDirty += vUintBytes
  131. case "Referenced":
  132. s.Referenced += vUintBytes
  133. case "Anonymous":
  134. s.Anonymous += vUintBytes
  135. case "Swap":
  136. s.Swap += vUintBytes
  137. case "SwapPss":
  138. s.SwapPss += vUintBytes
  139. }
  140. }