blob: 46fe26626376476ad6c78925f9f04f113cf68088 [file] [log] [blame]
khenaidooab1f7bd2019-11-14 14:00:27 -05001// Copyright 2019 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
16// The PSI / pressure interface is described at
17// https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/accounting/psi.txt
18// Each resource (cpu, io, memory, ...) is exposed as a single file.
19// Each file may contain up to two lines, one for "some" pressure and one for "full" pressure.
20// Each line contains several averages (over n seconds) and a total in µs.
21//
22// Example io pressure file:
23// > some avg10=0.06 avg60=0.21 avg300=0.99 total=8537362
24// > full avg10=0.00 avg60=0.13 avg300=0.96 total=8183134
25
26import (
27 "fmt"
28 "io"
29 "io/ioutil"
30 "os"
31 "strings"
32)
33
34const lineFormat = "avg10=%f avg60=%f avg300=%f total=%d"
35
36// PSILine is a single line of values as returned by /proc/pressure/*
37// The Avg entries are averages over n seconds, as a percentage
38// The Total line is in microseconds
39type PSILine struct {
40 Avg10 float64
41 Avg60 float64
42 Avg300 float64
43 Total uint64
44}
45
46// PSIStats represent pressure stall information from /proc/pressure/*
47// Some indicates the share of time in which at least some tasks are stalled
48// Full indicates the share of time in which all non-idle tasks are stalled simultaneously
49type PSIStats struct {
50 Some *PSILine
51 Full *PSILine
52}
53
54// PSIStatsForResource reads pressure stall information for the specified
55// resource from /proc/pressure/<resource>. At time of writing this can be
56// either "cpu", "memory" or "io".
57func (fs FS) PSIStatsForResource(resource string) (PSIStats, error) {
58 file, err := os.Open(fs.proc.Path(fmt.Sprintf("%s/%s", "pressure", resource)))
59 if err != nil {
60 return PSIStats{}, fmt.Errorf("psi_stats: unavailable for %s", resource)
61 }
62
63 defer file.Close()
64 return parsePSIStats(resource, file)
65}
66
67// parsePSIStats parses the specified file for pressure stall information
68func parsePSIStats(resource string, file io.Reader) (PSIStats, error) {
69 psiStats := PSIStats{}
70 stats, err := ioutil.ReadAll(file)
71 if err != nil {
72 return psiStats, fmt.Errorf("psi_stats: unable to read data for %s", resource)
73 }
74
75 for _, l := range strings.Split(string(stats), "\n") {
76 prefix := strings.Split(l, " ")[0]
77 switch prefix {
78 case "some":
79 psi := PSILine{}
80 _, err := fmt.Sscanf(l, fmt.Sprintf("some %s", lineFormat), &psi.Avg10, &psi.Avg60, &psi.Avg300, &psi.Total)
81 if err != nil {
82 return PSIStats{}, err
83 }
84 psiStats.Some = &psi
85 case "full":
86 psi := PSILine{}
87 _, err := fmt.Sscanf(l, fmt.Sprintf("full %s", lineFormat), &psi.Avg10, &psi.Avg60, &psi.Avg300, &psi.Total)
88 if err != nil {
89 return PSIStats{}, err
90 }
91 psiStats.Full = &psi
92 default:
93 // If we encounter a line with an unknown prefix, ignore it and move on
94 // Should new measurement types be added in the future we'll simply ignore them instead
95 // of erroring on retrieval
96 continue
97 }
98 }
99
100 return psiStats, nil
101}