cubefs
166 строк · 3.9 Кб
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
14//go:build !windows
15// +build !windows
16
17package procfs18
19import (20"bufio"21"errors"22"fmt"23"os"24"regexp"25"strconv"26"strings"27
28"github.com/prometheus/procfs/internal/util"29)
30
31var (32// match the header line before each mapped zone in `/proc/pid/smaps`.33procSMapsHeaderLine = regexp.MustCompile(`^[a-f0-9].*$`)34)
35
36type ProcSMapsRollup struct {37// Amount of the mapping that is currently resident in RAM.38Rss uint6439// Process's proportional share of this mapping.40Pss uint6441// Size in bytes of clean shared pages.42SharedClean uint6443// Size in bytes of dirty shared pages.44SharedDirty uint6445// Size in bytes of clean private pages.46PrivateClean uint6447// Size in bytes of dirty private pages.48PrivateDirty uint6449// Amount of memory currently marked as referenced or accessed.50Referenced uint6451// Amount of memory that does not belong to any file.52Anonymous uint6453// Amount would-be-anonymous memory currently on swap.54Swap uint6455// Process's proportional memory on swap.56SwapPss uint6457}
58
59// ProcSMapsRollup reads from /proc/[pid]/smaps_rollup to get summed memory information of the
60// process.
61//
62// If smaps_rollup does not exists (require kernel >= 4.15), the content of /proc/pid/smaps will
63// we read and summed.
64func (p Proc) ProcSMapsRollup() (ProcSMapsRollup, error) {65data, err := util.ReadFileNoStat(p.path("smaps_rollup"))66if err != nil && os.IsNotExist(err) {67return p.procSMapsRollupManual()68}69if err != nil {70return ProcSMapsRollup{}, err71}72
73lines := strings.Split(string(data), "\n")74smaps := ProcSMapsRollup{}75
76// skip first line which don't contains information we need77lines = lines[1:]78for _, line := range lines {79if line == "" {80continue81}82
83if err := smaps.parseLine(line); err != nil {84return ProcSMapsRollup{}, err85}86}87
88return smaps, nil89}
90
91// Read /proc/pid/smaps and do the roll-up in Go code.
92func (p Proc) procSMapsRollupManual() (ProcSMapsRollup, error) {93file, err := os.Open(p.path("smaps"))94if err != nil {95return ProcSMapsRollup{}, err96}97defer file.Close()98
99smaps := ProcSMapsRollup{}100scan := bufio.NewScanner(file)101
102for scan.Scan() {103line := scan.Text()104
105if procSMapsHeaderLine.MatchString(line) {106continue107}108
109if err := smaps.parseLine(line); err != nil {110return ProcSMapsRollup{}, err111}112}113
114return smaps, nil115}
116
117func (s *ProcSMapsRollup) parseLine(line string) error {118kv := strings.SplitN(line, ":", 2)119if len(kv) != 2 {120fmt.Println(line)121return errors.New("invalid net/dev line, missing colon")122}123
124k := kv[0]125if k == "VmFlags" {126return nil127}128
129v := strings.TrimSpace(kv[1])130v = strings.TrimRight(v, " kB")131
132vKBytes, err := strconv.ParseUint(v, 10, 64)133if err != nil {134return err135}136vBytes := vKBytes * 1024137
138s.addValue(k, v, vKBytes, vBytes)139
140return nil141}
142
143func (s *ProcSMapsRollup) addValue(k string, vString string, vUint uint64, vUintBytes uint64) {144switch k {145case "Rss":146s.Rss += vUintBytes147case "Pss":148s.Pss += vUintBytes149case "Shared_Clean":150s.SharedClean += vUintBytes151case "Shared_Dirty":152s.SharedDirty += vUintBytes153case "Private_Clean":154s.PrivateClean += vUintBytes155case "Private_Dirty":156s.PrivateDirty += vUintBytes157case "Referenced":158s.Referenced += vUintBytes159case "Anonymous":160s.Anonymous += vUintBytes161case "Swap":162s.Swap += vUintBytes163case "SwapPss":164s.SwapPss += vUintBytes165}166}
167