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 procfs

import (
	
	
	

	
	
)
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-issue
const userHZ = 100
ProcStat provides status information about the process, read from /proc/[pid]/stat.
The process ID.
The filename of the executable.
The process state.
The PID of the parent of this process.
The process group ID of the process.
The session ID of the process.
The controlling terminal of the process.
The ID of the foreground process group of the controlling terminal of the process.
The kernel flags word of the process.
The number of minor faults the process has made which have not required loading a memory page from disk.
The number of minor faults that the process's waited-for children have made.
The number of major faults the process has made which have required loading a memory page from disk.
The number of major faults that the process's waited-for children have made.
Amount of time that this process has been scheduled in user mode, measured in clock ticks.
Amount of time that this process has been scheduled in kernel mode, measured in clock ticks.
Amount of time that this process's waited-for children have been scheduled in user mode, measured in clock ticks.
Amount of time that this process's waited-for children have been scheduled in kernel mode, measured in clock ticks.
For processes running a real-time scheduling policy, this is the negated scheduling priority, minus one.
The nice value, a value in the range 19 (low priority) to -20 (high priority).
Number of threads in this process.
The time the process started after system boot, the value is expressed in clock ticks.
Virtual memory size in bytes.
Resident set size in pages.
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 (
		 int

		 = ProcStat{PID: .PID, proc: .fs}
		 = bytes.Index(, []byte("("))
		 = bytes.LastIndex(, []byte(")"))
	)

	if  < 0 ||  < 0 {
		return ProcStat{}, fmt.Errorf(
			"unexpected format, couldn't extract comm: %s",
			,
		)
	}

	.Comm = string([+1 : ])
	_,  = 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,
	)
	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) {
	 := FS{proc: .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