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 (
	
	
	
	
	
	

	
	
)
CPUStat shows how much time the cpu spend in various stages.
SoftIRQStat represent the softirq statistics as exported in the procfs stat file. A nice introduction can be found at https://0xax.gitbooks.io/linux-insides/content/interrupts/interrupts-9.html It is possible to get per-cpu stats by reading /proc/softirqs
Stat represents kernel/system statistics.
Boot time in seconds since the Epoch.
Summed up cpu statistics.
Per-CPU statistics.
Number of times interrupts were handled, which contains numbered and unnumbered IRQs.
Number of times a numbered IRQ was triggered.
Number of times a context switch happened.
Number of times a process was created.
Number of processes currently running.
Number of processes currently blocked (waiting for IO).
Number of times a softirq was scheduled.
Detailed softirq statistics.
Parse a cpu statistics line and returns the CPUStat struct plus the cpu id (or -1 for the overall sum).
func ( string) (CPUStat, int64, error) {
	 := CPUStat{}
	var  string

	,  := fmt.Sscanf(, "%s %f %f %f %f %f %f %f %f %f %f",
		&,
		&.User, &.Nice, &.System, &.Idle,
		&.Iowait, &.IRQ, &.SoftIRQ, &.Steal,
		&.Guest, &.GuestNice)

	if  != nil &&  != io.EOF {
		return CPUStat{}, -1, fmt.Errorf("couldn't parse %s (cpu): %s", , )
	}
	if  == 0 {
		return CPUStat{}, -1, fmt.Errorf("couldn't parse %s (cpu): 0 elements parsed", )
	}

	.User /= userHZ
	.Nice /= userHZ
	.System /= userHZ
	.Idle /= userHZ
	.Iowait /= userHZ
	.IRQ /= userHZ
	.SoftIRQ /= userHZ
	.Steal /= userHZ
	.Guest /= userHZ
	.GuestNice /= userHZ

	if  == "cpu" {
		return , -1, nil
	}

	,  := strconv.ParseInt([3:], 10, 64)
	if  != nil {
		return CPUStat{}, -1, fmt.Errorf("couldn't parse %s (cpu/cpuid): %s", , )
	}

	return , , nil
}
Parse a softirq line.
func ( string) (SoftIRQStat, uint64, error) {
	 := SoftIRQStat{}
	var  uint64
	var  string

	,  := fmt.Sscanf(, "%s %d %d %d %d %d %d %d %d %d %d %d",
		&, &,
		&.Hi, &.Timer, &.NetTx, &.NetRx,
		&.Block, &.BlockIoPoll,
		&.Tasklet, &.Sched,
		&.Hrtimer, &.Rcu)

	if  != nil {
		return SoftIRQStat{}, 0, fmt.Errorf("couldn't parse %s (softirq): %s", , )
	}

	return , , nil
}
NewStat returns information about current cpu/process statistics. See https://www.kernel.org/doc/Documentation/filesystems/proc.txt Deprecated: use fs.Stat() instead
func () (Stat, error) {
	,  := NewFS(fs.DefaultProcMountPoint)
	if  != nil {
		return Stat{}, 
	}
	return .Stat()
}
NewStat returns information about current cpu/process statistics. See https://www.kernel.org/doc/Documentation/filesystems/proc.txt Deprecated: use fs.Stat() instead
func ( FS) () (Stat, error) {
	return .Stat()
}
Stat returns information about current cpu/process statistics. See https://www.kernel.org/doc/Documentation/filesystems/proc.txt
func ( FS) () (Stat, error) {
	 := .proc.Path("stat")
	,  := util.ReadFileNoStat()
	if  != nil {
		return Stat{}, 
	}

	 := Stat{}

	 := bufio.NewScanner(bytes.NewReader())
	for .Scan() {
		 := .Text()
require at least <key> <value>
		if len() < 2 {
			continue
		}
		switch {
		case [0] == "btime":
			if .BootTime,  = strconv.ParseUint([1], 10, 64);  != nil {
				return Stat{}, fmt.Errorf("couldn't parse %s (btime): %s", [1], )
			}
		case [0] == "intr":
			if .IRQTotal,  = strconv.ParseUint([1], 10, 64);  != nil {
				return Stat{}, fmt.Errorf("couldn't parse %s (intr): %s", [1], )
			}
			 := [2:]
			.IRQ = make([]uint64, len())
			for ,  := range  {
				if .IRQ[],  = strconv.ParseUint(, 10, 64);  != nil {
					return Stat{}, fmt.Errorf("couldn't parse %s (intr%d): %s", , , )
				}
			}
		case [0] == "ctxt":
			if .ContextSwitches,  = strconv.ParseUint([1], 10, 64);  != nil {
				return Stat{}, fmt.Errorf("couldn't parse %s (ctxt): %s", [1], )
			}
		case [0] == "processes":
			if .ProcessCreated,  = strconv.ParseUint([1], 10, 64);  != nil {
				return Stat{}, fmt.Errorf("couldn't parse %s (processes): %s", [1], )
			}
		case [0] == "procs_running":
			if .ProcessesRunning,  = strconv.ParseUint([1], 10, 64);  != nil {
				return Stat{}, fmt.Errorf("couldn't parse %s (procs_running): %s", [1], )
			}
		case [0] == "procs_blocked":
			if .ProcessesBlocked,  = strconv.ParseUint([1], 10, 64);  != nil {
				return Stat{}, fmt.Errorf("couldn't parse %s (procs_blocked): %s", [1], )
			}
		case [0] == "softirq":
			, ,  := parseSoftIRQStat()
			if  != nil {
				return Stat{}, 
			}
			.SoftIRQTotal = 
			.SoftIRQ = 
		case strings.HasPrefix([0], "cpu"):
			, ,  := parseCPUStat()
			if  != nil {
				return Stat{}, 
			}
			if  == -1 {
				.CPUTotal = 
			} else {
				for int64(len(.CPU)) <=  {
					.CPU = append(.CPU, CPUStat{})
				}
				.CPU[] = 
			}
		}
	}

	if  := .Err();  != nil {
		return Stat{}, fmt.Errorf("couldn't parse %s: %s", , )
	}

	return , nil