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