...

Source file src/github.com/prometheus/procfs/stat.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  	"bufio"
    18  	"bytes"
    19  	"fmt"
    20  	"io"
    21  	"strconv"
    22  	"strings"
    23  
    24  	"github.com/prometheus/procfs/internal/fs"
    25  	"github.com/prometheus/procfs/internal/util"
    26  )
    27  
    28  // CPUStat shows how much time the cpu spend in various stages.
    29  type CPUStat struct {
    30  	User      float64
    31  	Nice      float64
    32  	System    float64
    33  	Idle      float64
    34  	Iowait    float64
    35  	IRQ       float64
    36  	SoftIRQ   float64
    37  	Steal     float64
    38  	Guest     float64
    39  	GuestNice float64
    40  }
    41  
    42  // SoftIRQStat represent the softirq statistics as exported in the procfs stat file.
    43  // A nice introduction can be found at https://0xax.gitbooks.io/linux-insides/content/interrupts/interrupts-9.html
    44  // It is possible to get per-cpu stats by reading `/proc/softirqs`.
    45  type SoftIRQStat struct {
    46  	Hi          uint64
    47  	Timer       uint64
    48  	NetTx       uint64
    49  	NetRx       uint64
    50  	Block       uint64
    51  	BlockIoPoll uint64
    52  	Tasklet     uint64
    53  	Sched       uint64
    54  	Hrtimer     uint64
    55  	Rcu         uint64
    56  }
    57  
    58  // Stat represents kernel/system statistics.
    59  type Stat struct {
    60  	// Boot time in seconds since the Epoch.
    61  	BootTime uint64
    62  	// Summed up cpu statistics.
    63  	CPUTotal CPUStat
    64  	// Per-CPU statistics.
    65  	CPU map[int64]CPUStat
    66  	// Number of times interrupts were handled, which contains numbered and unnumbered IRQs.
    67  	IRQTotal uint64
    68  	// Number of times a numbered IRQ was triggered.
    69  	IRQ []uint64
    70  	// Number of times a context switch happened.
    71  	ContextSwitches uint64
    72  	// Number of times a process was created.
    73  	ProcessCreated uint64
    74  	// Number of processes currently running.
    75  	ProcessesRunning uint64
    76  	// Number of processes currently blocked (waiting for IO).
    77  	ProcessesBlocked uint64
    78  	// Number of times a softirq was scheduled.
    79  	SoftIRQTotal uint64
    80  	// Detailed softirq statistics.
    81  	SoftIRQ SoftIRQStat
    82  }
    83  
    84  // Parse a cpu statistics line and returns the CPUStat struct plus the cpu id (or -1 for the overall sum).
    85  func parseCPUStat(line string) (CPUStat, int64, error) {
    86  	cpuStat := CPUStat{}
    87  	var cpu string
    88  
    89  	count, err := fmt.Sscanf(line, "%s %f %f %f %f %f %f %f %f %f %f",
    90  		&cpu,
    91  		&cpuStat.User, &cpuStat.Nice, &cpuStat.System, &cpuStat.Idle,
    92  		&cpuStat.Iowait, &cpuStat.IRQ, &cpuStat.SoftIRQ, &cpuStat.Steal,
    93  		&cpuStat.Guest, &cpuStat.GuestNice)
    94  
    95  	if err != nil && err != io.EOF {
    96  		return CPUStat{}, -1, fmt.Errorf("%s: couldn't parse %q (cpu): %w", ErrFileParse, line, err)
    97  	}
    98  	if count == 0 {
    99  		return CPUStat{}, -1, fmt.Errorf("%w: couldn't parse %q (cpu): 0 elements parsed", ErrFileParse, line)
   100  	}
   101  
   102  	cpuStat.User /= userHZ
   103  	cpuStat.Nice /= userHZ
   104  	cpuStat.System /= userHZ
   105  	cpuStat.Idle /= userHZ
   106  	cpuStat.Iowait /= userHZ
   107  	cpuStat.IRQ /= userHZ
   108  	cpuStat.SoftIRQ /= userHZ
   109  	cpuStat.Steal /= userHZ
   110  	cpuStat.Guest /= userHZ
   111  	cpuStat.GuestNice /= userHZ
   112  
   113  	if cpu == "cpu" {
   114  		return cpuStat, -1, nil
   115  	}
   116  
   117  	cpuID, err := strconv.ParseInt(cpu[3:], 10, 64)
   118  	if err != nil {
   119  		return CPUStat{}, -1, fmt.Errorf("%s: couldn't parse %q (cpu/cpuid): %w", ErrFileParse, line, err)
   120  	}
   121  
   122  	return cpuStat, cpuID, nil
   123  }
   124  
   125  // Parse a softirq line.
   126  func parseSoftIRQStat(line string) (SoftIRQStat, uint64, error) {
   127  	softIRQStat := SoftIRQStat{}
   128  	var total uint64
   129  	var prefix string
   130  
   131  	_, err := fmt.Sscanf(line, "%s %d %d %d %d %d %d %d %d %d %d %d",
   132  		&prefix, &total,
   133  		&softIRQStat.Hi, &softIRQStat.Timer, &softIRQStat.NetTx, &softIRQStat.NetRx,
   134  		&softIRQStat.Block, &softIRQStat.BlockIoPoll,
   135  		&softIRQStat.Tasklet, &softIRQStat.Sched,
   136  		&softIRQStat.Hrtimer, &softIRQStat.Rcu)
   137  
   138  	if err != nil {
   139  		return SoftIRQStat{}, 0, fmt.Errorf("%s: couldn't parse %q (softirq): %w", ErrFileParse, line, err)
   140  	}
   141  
   142  	return softIRQStat, total, nil
   143  }
   144  
   145  // NewStat returns information about current cpu/process statistics.
   146  // See https://www.kernel.org/doc/Documentation/filesystems/proc.txt
   147  //
   148  // Deprecated: Use fs.Stat() instead.
   149  func NewStat() (Stat, error) {
   150  	fs, err := NewFS(fs.DefaultProcMountPoint)
   151  	if err != nil {
   152  		return Stat{}, err
   153  	}
   154  	return fs.Stat()
   155  }
   156  
   157  // NewStat returns information about current cpu/process statistics.
   158  // See: https://www.kernel.org/doc/Documentation/filesystems/proc.txt
   159  //
   160  // Deprecated: Use fs.Stat() instead.
   161  func (fs FS) NewStat() (Stat, error) {
   162  	return fs.Stat()
   163  }
   164  
   165  // Stat returns information about current cpu/process statistics.
   166  // See: https://www.kernel.org/doc/Documentation/filesystems/proc.txt
   167  func (fs FS) Stat() (Stat, error) {
   168  	fileName := fs.proc.Path("stat")
   169  	data, err := util.ReadFileNoStat(fileName)
   170  	if err != nil {
   171  		return Stat{}, err
   172  	}
   173  	procStat, err := parseStat(bytes.NewReader(data), fileName)
   174  	if err != nil {
   175  		return Stat{}, err
   176  	}
   177  	return procStat, nil
   178  }
   179  
   180  // parseStat parses the metrics from /proc/[pid]/stat.
   181  func parseStat(r io.Reader, fileName string) (Stat, error) {
   182  	var (
   183  		scanner = bufio.NewScanner(r)
   184  		stat    = Stat{
   185  			CPU: make(map[int64]CPUStat),
   186  		}
   187  		err error
   188  	)
   189  
   190  	// Increase default scanner buffer to handle very long `intr` lines.
   191  	buf := make([]byte, 0, 8*1024)
   192  	scanner.Buffer(buf, 1024*1024)
   193  
   194  	for scanner.Scan() {
   195  		line := scanner.Text()
   196  		parts := strings.Fields(scanner.Text())
   197  		// require at least <key> <value>
   198  		if len(parts) < 2 {
   199  			continue
   200  		}
   201  		switch {
   202  		case parts[0] == "btime":
   203  			if stat.BootTime, err = strconv.ParseUint(parts[1], 10, 64); err != nil {
   204  				return Stat{}, fmt.Errorf("%s: couldn't parse %q (btime): %w", ErrFileParse, parts[1], err)
   205  			}
   206  		case parts[0] == "intr":
   207  			if stat.IRQTotal, err = strconv.ParseUint(parts[1], 10, 64); err != nil {
   208  				return Stat{}, fmt.Errorf("%s: couldn't parse %q (intr): %w", ErrFileParse, parts[1], err)
   209  			}
   210  			numberedIRQs := parts[2:]
   211  			stat.IRQ = make([]uint64, len(numberedIRQs))
   212  			for i, count := range numberedIRQs {
   213  				if stat.IRQ[i], err = strconv.ParseUint(count, 10, 64); err != nil {
   214  					return Stat{}, fmt.Errorf("%s: couldn't parse %q (intr%d): %w", ErrFileParse, count, i, err)
   215  				}
   216  			}
   217  		case parts[0] == "ctxt":
   218  			if stat.ContextSwitches, err = strconv.ParseUint(parts[1], 10, 64); err != nil {
   219  				return Stat{}, fmt.Errorf("%s: couldn't parse %q (ctxt): %w", ErrFileParse, parts[1], err)
   220  			}
   221  		case parts[0] == "processes":
   222  			if stat.ProcessCreated, err = strconv.ParseUint(parts[1], 10, 64); err != nil {
   223  				return Stat{}, fmt.Errorf("%s: couldn't parse %q (processes): %w", ErrFileParse, parts[1], err)
   224  			}
   225  		case parts[0] == "procs_running":
   226  			if stat.ProcessesRunning, err = strconv.ParseUint(parts[1], 10, 64); err != nil {
   227  				return Stat{}, fmt.Errorf("%s: couldn't parse %q (procs_running): %w", ErrFileParse, parts[1], err)
   228  			}
   229  		case parts[0] == "procs_blocked":
   230  			if stat.ProcessesBlocked, err = strconv.ParseUint(parts[1], 10, 64); err != nil {
   231  				return Stat{}, fmt.Errorf("%s: couldn't parse %q (procs_blocked): %w", ErrFileParse, parts[1], err)
   232  			}
   233  		case parts[0] == "softirq":
   234  			softIRQStats, total, err := parseSoftIRQStat(line)
   235  			if err != nil {
   236  				return Stat{}, err
   237  			}
   238  			stat.SoftIRQTotal = total
   239  			stat.SoftIRQ = softIRQStats
   240  		case strings.HasPrefix(parts[0], "cpu"):
   241  			cpuStat, cpuID, err := parseCPUStat(line)
   242  			if err != nil {
   243  				return Stat{}, err
   244  			}
   245  			if cpuID == -1 {
   246  				stat.CPUTotal = cpuStat
   247  			} else {
   248  				stat.CPU[cpuID] = cpuStat
   249  			}
   250  		}
   251  	}
   252  
   253  	if err := scanner.Err(); err != nil {
   254  		return Stat{}, fmt.Errorf("%s: couldn't parse %q: %w", ErrFileParse, fileName, err)
   255  	}
   256  
   257  	return stat, nil
   258  }
   259  

View as plain text