123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- package procfs
- import (
- "bytes"
- "strconv"
- "strings"
- "github.com/prometheus/procfs/internal/util"
- )
- type ProcStatus struct {
-
- PID int
-
- Name string
-
- TGID int
-
- VmPeak uint64
-
- VmSize uint64
-
- VmLck uint64
-
- VmPin uint64
-
- VmHWM uint64
-
- VmRSS uint64
-
- RssAnon uint64
-
- RssFile uint64
-
- RssShmem uint64
-
- VmData uint64
-
- VmStk uint64
-
- VmExe uint64
-
- VmLib uint64
-
- VmPTE uint64
-
- VmPMD uint64
-
- VmSwap uint64
-
- HugetlbPages uint64
-
- VoluntaryCtxtSwitches uint64
-
- NonVoluntaryCtxtSwitches uint64
-
- UIDs [4]string
- }
- func (p Proc) NewStatus() (ProcStatus, error) {
- data, err := util.ReadFileNoStat(p.path("status"))
- if err != nil {
- return ProcStatus{}, err
- }
- s := ProcStatus{PID: p.PID}
- lines := strings.Split(string(data), "\n")
- for _, line := range lines {
- if !bytes.Contains([]byte(line), []byte(":")) {
- continue
- }
- kv := strings.SplitN(line, ":", 2)
-
- k := string(strings.TrimSpace(kv[0]))
- v := string(strings.TrimSpace(kv[1]))
-
- v = string(bytes.Trim([]byte(v), " kB"))
-
-
- vKBytes, _ := strconv.ParseUint(v, 10, 64)
-
- vBytes := vKBytes * 1024
- s.fillStatus(k, v, vKBytes, vBytes)
- }
- return s, nil
- }
- func (s *ProcStatus) fillStatus(k string, vString string, vUint uint64, vUintBytes uint64) {
- switch k {
- case "Tgid":
- s.TGID = int(vUint)
- case "Name":
- s.Name = vString
- case "Uid":
- copy(s.UIDs[:], strings.Split(vString, "\t"))
- case "VmPeak":
- s.VmPeak = vUintBytes
- case "VmSize":
- s.VmSize = vUintBytes
- case "VmLck":
- s.VmLck = vUintBytes
- case "VmPin":
- s.VmPin = vUintBytes
- case "VmHWM":
- s.VmHWM = vUintBytes
- case "VmRSS":
- s.VmRSS = vUintBytes
- case "RssAnon":
- s.RssAnon = vUintBytes
- case "RssFile":
- s.RssFile = vUintBytes
- case "RssShmem":
- s.RssShmem = vUintBytes
- case "VmData":
- s.VmData = vUintBytes
- case "VmStk":
- s.VmStk = vUintBytes
- case "VmExe":
- s.VmExe = vUintBytes
- case "VmLib":
- s.VmLib = vUintBytes
- case "VmPTE":
- s.VmPTE = vUintBytes
- case "VmPMD":
- s.VmPMD = vUintBytes
- case "VmSwap":
- s.VmSwap = vUintBytes
- case "HugetlbPages":
- s.HugetlbPages = vUintBytes
- case "voluntary_ctxt_switches":
- s.VoluntaryCtxtSwitches = vUint
- case "nonvoluntary_ctxt_switches":
- s.NonVoluntaryCtxtSwitches = vUint
- }
- }
- func (s ProcStatus) TotalCtxtSwitches() uint64 {
- return s.VoluntaryCtxtSwitches + s.NonVoluntaryCtxtSwitches
- }
|