Source File
proc_stat.go
Belonging Package
github.com/prometheus/procfs
// Copyright 2018 The Prometheus Authors// Licensed under the Apache License, Version 2.0 (the "License");// you may not use this file except in compliance with the License.// You may obtain a copy of the License at//// http://www.apache.org/licenses/LICENSE-2.0//// Unless required by applicable law or agreed to in writing, software// distributed under the License is distributed on an "AS IS" BASIS,// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.// See the License for the specific language governing permissions and// limitations under the License.package procfsimport ()// Originally, this USER_HZ value was dynamically retrieved via a sysconf call// which required cgo. However, that caused a lot of problems regarding// cross-compilation. Alternatives such as running a binary to determine the// value, or trying to derive it in some other way were all problematic. After// much research it was determined that USER_HZ is actually hardcoded to 100 on// all Go-supported platforms as of the time of this writing. This is why we// decided to hardcode it here as well. It is not impossible that there could// be systems with exceptions, but they should be very exotic edge cases, and// in that case, the worst outcome will be two misreported metrics.//// See also the following discussions://// - https://github.com/prometheus/node_exporter/issues/52// - https://github.com/prometheus/procfs/pull/2// - http://stackoverflow.com/questions/17410841/how-does-user-hz-solve-the-jiffy-scaling-issueconst userHZ = 100// ProcStat provides status information about the process,// read from /proc/[pid]/stat.type ProcStat struct {// The process ID.PID int// The filename of the executable.Comm string// The process state.State string// The PID of the parent of this process.PPID int// The process group ID of the process.PGRP int// The session ID of the process.Session int// The controlling terminal of the process.TTY int// The ID of the foreground process group of the controlling terminal of// the process.TPGID int// The kernel flags word of the process.Flags uint// The number of minor faults the process has made which have not required// loading a memory page from disk.MinFlt uint// The number of minor faults that the process's waited-for children have// made.CMinFlt uint// The number of major faults the process has made which have required// loading a memory page from disk.MajFlt uint// The number of major faults that the process's waited-for children have// made.CMajFlt uint// Amount of time that this process has been scheduled in user mode,// measured in clock ticks.UTime uint// Amount of time that this process has been scheduled in kernel mode,// measured in clock ticks.STime uint// Amount of time that this process's waited-for children have been// scheduled in user mode, measured in clock ticks.CUTime int// Amount of time that this process's waited-for children have been// scheduled in kernel mode, measured in clock ticks.CSTime int// For processes running a real-time scheduling policy, this is the negated// scheduling priority, minus one.Priority int// The nice value, a value in the range 19 (low priority) to -20 (high// priority).Nice int// Number of threads in this process.NumThreads int// The time the process started after system boot, the value is expressed// in clock ticks.Starttime uint64// Virtual memory size in bytes.VSize uint// Resident set size in pages.RSS int// Soft limit in bytes on the rss of the process.RSSLimit uint64// CPU number last executed on.Processor uint// Real-time scheduling priority, a number in the range 1 to 99 for processes// scheduled under a real-time policy, or 0, for non-real-time processes.RTPriority uint// Scheduling policy.Policy uint// Aggregated block I/O delays, measured in clock ticks (centiseconds).DelayAcctBlkIOTicks uint64// Guest time of the process (time spent running a virtual CPU for a guest// operating system), measured in clock ticks.GuestTime int// Guest time of the process's children, measured in clock ticks.CGuestTime intproc FS}// NewStat returns the current status information of the process.//// Deprecated: Use p.Stat() instead.func ( Proc) () (ProcStat, error) {return .Stat()}// Stat returns the current status information of the process.func ( Proc) () (ProcStat, error) {, := util.ReadFileNoStat(.path("stat"))if != nil {return ProcStat{},}var (int64uint64= ProcStat{PID: .PID, proc: .fs}= bytes.Index(, []byte("("))= bytes.LastIndex(, []byte(")")))if < 0 || < 0 {return ProcStat{}, fmt.Errorf("%w: unexpected format, couldn't extract comm %q", ErrFileParse, )}.Comm = string([+1 : ])// Check the following resources for the details about the particular stat// fields and their data types:// * https://man7.org/linux/man-pages/man5/proc.5.html// * https://man7.org/linux/man-pages/man3/scanf.3.html_, = fmt.Fscan(bytes.NewBuffer([+2:]),&.State,&.PPID,&.PGRP,&.Session,&.TTY,&.TPGID,&.Flags,&.MinFlt,&.CMinFlt,&.MajFlt,&.CMajFlt,&.UTime,&.STime,&.CUTime,&.CSTime,&.Priority,&.Nice,&.NumThreads,&,&.Starttime,&.VSize,&.RSS,&.RSSLimit,&,&,&,&,&,&,&,&,&,&,&,&,&,&.Processor,&.RTPriority,&.Policy,&.DelayAcctBlkIOTicks,&.GuestTime,&.CGuestTime,)if != nil {return ProcStat{},}return , nil}// VirtualMemory returns the virtual memory size in bytes.func ( ProcStat) () uint {return .VSize}// ResidentMemory returns the resident memory size in bytes.func ( ProcStat) () int {return .RSS * os.Getpagesize()}// StartTime returns the unix timestamp of the process in seconds.func ( ProcStat) () (float64, error) {, := .proc.Stat()if != nil {return 0,}return float64(.BootTime) + (float64(.Starttime) / userHZ), nil}// CPUTime returns the total CPU user and system time in seconds.func ( ProcStat) () float64 {return float64(.UTime+.STime) / userHZ}
![]() |
The pages are generated with Golds v0.8.2. (GOOS=linux GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds. |