...

Source file src/github.com/prometheus/procfs/proc_status.go

Documentation: github.com/prometheus/procfs

     1  // Copyright 2018 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  package procfs
    15  
    16  import (
    17  	"bytes"
    18  	"sort"
    19  	"strconv"
    20  	"strings"
    21  
    22  	"github.com/prometheus/procfs/internal/util"
    23  )
    24  
    25  // ProcStatus provides status information about the process,
    26  // read from /proc/[pid]/status.
    27  type ProcStatus struct {
    28  	// The process ID.
    29  	PID int
    30  	// The process name.
    31  	Name string
    32  
    33  	// Thread group ID.
    34  	TGID int
    35  	// List of Pid namespace.
    36  	NSpids []uint64
    37  
    38  	// Peak virtual memory size.
    39  	VmPeak uint64 // nolint:revive
    40  	// Virtual memory size.
    41  	VmSize uint64 // nolint:revive
    42  	// Locked memory size.
    43  	VmLck uint64 // nolint:revive
    44  	// Pinned memory size.
    45  	VmPin uint64 // nolint:revive
    46  	// Peak resident set size.
    47  	VmHWM uint64 // nolint:revive
    48  	// Resident set size (sum of RssAnnon RssFile and RssShmem).
    49  	VmRSS uint64 // nolint:revive
    50  	// Size of resident anonymous memory.
    51  	RssAnon uint64 // nolint:revive
    52  	// Size of resident file mappings.
    53  	RssFile uint64 // nolint:revive
    54  	// Size of resident shared memory.
    55  	RssShmem uint64 // nolint:revive
    56  	// Size of data segments.
    57  	VmData uint64 // nolint:revive
    58  	// Size of stack segments.
    59  	VmStk uint64 // nolint:revive
    60  	// Size of text segments.
    61  	VmExe uint64 // nolint:revive
    62  	// Shared library code size.
    63  	VmLib uint64 // nolint:revive
    64  	// Page table entries size.
    65  	VmPTE uint64 // nolint:revive
    66  	// Size of second-level page tables.
    67  	VmPMD uint64 // nolint:revive
    68  	// Swapped-out virtual memory size by anonymous private.
    69  	VmSwap uint64 // nolint:revive
    70  	// Size of hugetlb memory portions
    71  	HugetlbPages uint64
    72  
    73  	// Number of voluntary context switches.
    74  	VoluntaryCtxtSwitches uint64
    75  	// Number of involuntary context switches.
    76  	NonVoluntaryCtxtSwitches uint64
    77  
    78  	// UIDs of the process (Real, effective, saved set, and filesystem UIDs)
    79  	UIDs [4]string
    80  	// GIDs of the process (Real, effective, saved set, and filesystem GIDs)
    81  	GIDs [4]string
    82  
    83  	// CpusAllowedList: List of cpu cores processes are allowed to run on.
    84  	CpusAllowedList []uint64
    85  }
    86  
    87  // NewStatus returns the current status information of the process.
    88  func (p Proc) NewStatus() (ProcStatus, error) {
    89  	data, err := util.ReadFileNoStat(p.path("status"))
    90  	if err != nil {
    91  		return ProcStatus{}, err
    92  	}
    93  
    94  	s := ProcStatus{PID: p.PID}
    95  
    96  	lines := strings.Split(string(data), "\n")
    97  	for _, line := range lines {
    98  		if !bytes.Contains([]byte(line), []byte(":")) {
    99  			continue
   100  		}
   101  
   102  		kv := strings.SplitN(line, ":", 2)
   103  
   104  		// removes spaces
   105  		k := strings.TrimSpace(kv[0])
   106  		v := strings.TrimSpace(kv[1])
   107  		// removes "kB"
   108  		v = strings.TrimSuffix(v, " kB")
   109  
   110  		// value to int when possible
   111  		// we can skip error check here, 'cause vKBytes is not used when value is a string
   112  		vKBytes, _ := strconv.ParseUint(v, 10, 64)
   113  		// convert kB to B
   114  		vBytes := vKBytes * 1024
   115  
   116  		s.fillStatus(k, v, vKBytes, vBytes)
   117  	}
   118  
   119  	return s, nil
   120  }
   121  
   122  func (s *ProcStatus) fillStatus(k string, vString string, vUint uint64, vUintBytes uint64) {
   123  	switch k {
   124  	case "Tgid":
   125  		s.TGID = int(vUint)
   126  	case "Name":
   127  		s.Name = vString
   128  	case "Uid":
   129  		copy(s.UIDs[:], strings.Split(vString, "\t"))
   130  	case "Gid":
   131  		copy(s.GIDs[:], strings.Split(vString, "\t"))
   132  	case "NSpid":
   133  		s.NSpids = calcNSPidsList(vString)
   134  	case "VmPeak":
   135  		s.VmPeak = vUintBytes
   136  	case "VmSize":
   137  		s.VmSize = vUintBytes
   138  	case "VmLck":
   139  		s.VmLck = vUintBytes
   140  	case "VmPin":
   141  		s.VmPin = vUintBytes
   142  	case "VmHWM":
   143  		s.VmHWM = vUintBytes
   144  	case "VmRSS":
   145  		s.VmRSS = vUintBytes
   146  	case "RssAnon":
   147  		s.RssAnon = vUintBytes
   148  	case "RssFile":
   149  		s.RssFile = vUintBytes
   150  	case "RssShmem":
   151  		s.RssShmem = vUintBytes
   152  	case "VmData":
   153  		s.VmData = vUintBytes
   154  	case "VmStk":
   155  		s.VmStk = vUintBytes
   156  	case "VmExe":
   157  		s.VmExe = vUintBytes
   158  	case "VmLib":
   159  		s.VmLib = vUintBytes
   160  	case "VmPTE":
   161  		s.VmPTE = vUintBytes
   162  	case "VmPMD":
   163  		s.VmPMD = vUintBytes
   164  	case "VmSwap":
   165  		s.VmSwap = vUintBytes
   166  	case "HugetlbPages":
   167  		s.HugetlbPages = vUintBytes
   168  	case "voluntary_ctxt_switches":
   169  		s.VoluntaryCtxtSwitches = vUint
   170  	case "nonvoluntary_ctxt_switches":
   171  		s.NonVoluntaryCtxtSwitches = vUint
   172  	case "Cpus_allowed_list":
   173  		s.CpusAllowedList = calcCpusAllowedList(vString)
   174  	}
   175  
   176  }
   177  
   178  // TotalCtxtSwitches returns the total context switch.
   179  func (s ProcStatus) TotalCtxtSwitches() uint64 {
   180  	return s.VoluntaryCtxtSwitches + s.NonVoluntaryCtxtSwitches
   181  }
   182  
   183  func calcCpusAllowedList(cpuString string) []uint64 {
   184  	s := strings.Split(cpuString, ",")
   185  
   186  	var g []uint64
   187  
   188  	for _, cpu := range s {
   189  		// parse cpu ranges, example: 1-3=[1,2,3]
   190  		if l := strings.Split(strings.TrimSpace(cpu), "-"); len(l) > 1 {
   191  			startCPU, _ := strconv.ParseUint(l[0], 10, 64)
   192  			endCPU, _ := strconv.ParseUint(l[1], 10, 64)
   193  
   194  			for i := startCPU; i <= endCPU; i++ {
   195  				g = append(g, i)
   196  			}
   197  		} else if len(l) == 1 {
   198  			cpu, _ := strconv.ParseUint(l[0], 10, 64)
   199  			g = append(g, cpu)
   200  		}
   201  
   202  	}
   203  
   204  	sort.Slice(g, func(i, j int) bool { return g[i] < g[j] })
   205  	return g
   206  }
   207  
   208  func calcNSPidsList(nspidsString string) []uint64 {
   209  	s := strings.Split(nspidsString, " ")
   210  	var nspids []uint64
   211  
   212  	for _, nspid := range s {
   213  		nspid, _ := strconv.ParseUint(nspid, 10, 64)
   214  		if nspid == 0 {
   215  			continue
   216  		}
   217  		nspids = append(nspids, nspid)
   218  	}
   219  
   220  	return nspids
   221  }
   222  

View as plain text