cubefs
244 строки · 7.1 Кб
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
14package procfs15
16import (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.
29type CPUStat struct {30User float6431Nice float6432System float6433Idle float6434Iowait float6435IRQ float6436SoftIRQ float6437Steal float6438Guest float6439GuestNice float6440}
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`.
45type SoftIRQStat struct {46Hi uint6447Timer uint6448NetTx uint6449NetRx uint6450Block uint6451BlockIoPoll uint6452Tasklet uint6453Sched uint6454Hrtimer uint6455Rcu uint6456}
57
58// Stat represents kernel/system statistics.
59type Stat struct {60// Boot time in seconds since the Epoch.61BootTime uint6462// Summed up cpu statistics.63CPUTotal CPUStat
64// Per-CPU statistics.65CPU []CPUStat66// Number of times interrupts were handled, which contains numbered and unnumbered IRQs.67IRQTotal uint6468// Number of times a numbered IRQ was triggered.69IRQ []uint6470// Number of times a context switch happened.71ContextSwitches uint6472// Number of times a process was created.73ProcessCreated uint6474// Number of processes currently running.75ProcessesRunning uint6476// Number of processes currently blocked (waiting for IO).77ProcessesBlocked uint6478// Number of times a softirq was scheduled.79SoftIRQTotal uint6480// Detailed softirq statistics.81SoftIRQ SoftIRQStat
82}
83
84// Parse a cpu statistics line and returns the CPUStat struct plus the cpu id (or -1 for the overall sum).
85func parseCPUStat(line string) (CPUStat, int64, error) {86cpuStat := CPUStat{}87var cpu string88
89count, 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
95if err != nil && err != io.EOF {96return CPUStat{}, -1, fmt.Errorf("couldn't parse %q (cpu): %w", line, err)97}98if count == 0 {99return CPUStat{}, -1, fmt.Errorf("couldn't parse %q (cpu): 0 elements parsed", line)100}101
102cpuStat.User /= userHZ103cpuStat.Nice /= userHZ104cpuStat.System /= userHZ105cpuStat.Idle /= userHZ106cpuStat.Iowait /= userHZ107cpuStat.IRQ /= userHZ108cpuStat.SoftIRQ /= userHZ109cpuStat.Steal /= userHZ110cpuStat.Guest /= userHZ111cpuStat.GuestNice /= userHZ112
113if cpu == "cpu" {114return cpuStat, -1, nil115}116
117cpuID, err := strconv.ParseInt(cpu[3:], 10, 64)118if err != nil {119return CPUStat{}, -1, fmt.Errorf("couldn't parse %q (cpu/cpuid): %w", line, err)120}121
122return cpuStat, cpuID, nil123}
124
125// Parse a softirq line.
126func parseSoftIRQStat(line string) (SoftIRQStat, uint64, error) {127softIRQStat := SoftIRQStat{}128var total uint64129var prefix string130
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
138if err != nil {139return SoftIRQStat{}, 0, fmt.Errorf("couldn't parse %q (softirq): %w", line, err)140}141
142return softIRQStat, total, nil143}
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.
149func NewStat() (Stat, error) {150fs, err := NewFS(fs.DefaultProcMountPoint)151if err != nil {152return Stat{}, err153}154return 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.
161func (fs FS) NewStat() (Stat, error) {162return fs.Stat()163}
164
165// Stat returns information about current cpu/process statistics.
166// See: https://www.kernel.org/doc/Documentation/filesystems/proc.txt
167func (fs FS) Stat() (Stat, error) {168fileName := fs.proc.Path("stat")169data, err := util.ReadFileNoStat(fileName)170if err != nil {171return Stat{}, err172}173
174stat := Stat{}175
176scanner := bufio.NewScanner(bytes.NewReader(data))177for scanner.Scan() {178line := scanner.Text()179parts := strings.Fields(scanner.Text())180// require at least <key> <value>181if len(parts) < 2 {182continue183}184switch {185case parts[0] == "btime":186if stat.BootTime, err = strconv.ParseUint(parts[1], 10, 64); err != nil {187return Stat{}, fmt.Errorf("couldn't parse %q (btime): %w", parts[1], err)188}189case parts[0] == "intr":190if stat.IRQTotal, err = strconv.ParseUint(parts[1], 10, 64); err != nil {191return Stat{}, fmt.Errorf("couldn't parse %q (intr): %w", parts[1], err)192}193numberedIRQs := parts[2:]194stat.IRQ = make([]uint64, len(numberedIRQs))195for i, count := range numberedIRQs {196if stat.IRQ[i], err = strconv.ParseUint(count, 10, 64); err != nil {197return Stat{}, fmt.Errorf("couldn't parse %q (intr%d): %w", count, i, err)198}199}200case parts[0] == "ctxt":201if stat.ContextSwitches, err = strconv.ParseUint(parts[1], 10, 64); err != nil {202return Stat{}, fmt.Errorf("couldn't parse %q (ctxt): %w", parts[1], err)203}204case parts[0] == "processes":205if stat.ProcessCreated, err = strconv.ParseUint(parts[1], 10, 64); err != nil {206return Stat{}, fmt.Errorf("couldn't parse %q (processes): %w", parts[1], err)207}208case parts[0] == "procs_running":209if stat.ProcessesRunning, err = strconv.ParseUint(parts[1], 10, 64); err != nil {210return Stat{}, fmt.Errorf("couldn't parse %q (procs_running): %w", parts[1], err)211}212case parts[0] == "procs_blocked":213if stat.ProcessesBlocked, err = strconv.ParseUint(parts[1], 10, 64); err != nil {214return Stat{}, fmt.Errorf("couldn't parse %q (procs_blocked): %w", parts[1], err)215}216case parts[0] == "softirq":217softIRQStats, total, err := parseSoftIRQStat(line)218if err != nil {219return Stat{}, err220}221stat.SoftIRQTotal = total222stat.SoftIRQ = softIRQStats223case strings.HasPrefix(parts[0], "cpu"):224cpuStat, cpuID, err := parseCPUStat(line)225if err != nil {226return Stat{}, err227}228if cpuID == -1 {229stat.CPUTotal = cpuStat230} else {231for int64(len(stat.CPU)) <= cpuID {232stat.CPU = append(stat.CPU, CPUStat{})233}234stat.CPU[cpuID] = cpuStat235}236}237}238
239if err := scanner.Err(); err != nil {240return Stat{}, fmt.Errorf("couldn't parse %q: %w", fileName, err)241}242
243return stat, nil244}
245