blob: dc6c14f0a4c133f3f58a2bc25c366ef6f8475e59 [file] [log] [blame]
khenaidoo59ce9dd2019-11-11 13:05:32 -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 (
khenaidoo26721882021-08-11 17:42:52 -040027 "bufio"
28 "bytes"
khenaidoo59ce9dd2019-11-11 13:05:32 -050029 "fmt"
30 "io"
khenaidoo59ce9dd2019-11-11 13:05:32 -050031 "strings"
khenaidoo26721882021-08-11 17:42:52 -040032
33 "github.com/prometheus/procfs/internal/util"
khenaidoo59ce9dd2019-11-11 13:05:32 -050034)
35
36const lineFormat = "avg10=%f avg60=%f avg300=%f total=%d"
37
38// PSILine is a single line of values as returned by /proc/pressure/*
39// The Avg entries are averages over n seconds, as a percentage
40// The Total line is in microseconds
41type PSILine struct {
42 Avg10 float64
43 Avg60 float64
44 Avg300 float64
45 Total uint64
46}
47
48// PSIStats represent pressure stall information from /proc/pressure/*
49// Some indicates the share of time in which at least some tasks are stalled
50// Full indicates the share of time in which all non-idle tasks are stalled simultaneously
51type PSIStats struct {
52 Some *PSILine
53 Full *PSILine
54}
55
56// PSIStatsForResource reads pressure stall information for the specified
57// resource from /proc/pressure/<resource>. At time of writing this can be
58// either "cpu", "memory" or "io".
59func (fs FS) PSIStatsForResource(resource string) (PSIStats, error) {
khenaidoo26721882021-08-11 17:42:52 -040060 data, err := util.ReadFileNoStat(fs.proc.Path(fmt.Sprintf("%s/%s", "pressure", resource)))
khenaidoo59ce9dd2019-11-11 13:05:32 -050061 if err != nil {
khenaidoo26721882021-08-11 17:42:52 -040062 return PSIStats{}, fmt.Errorf("psi_stats: unavailable for %q: %w", resource, err)
khenaidoo59ce9dd2019-11-11 13:05:32 -050063 }
64
khenaidoo26721882021-08-11 17:42:52 -040065 return parsePSIStats(resource, bytes.NewReader(data))
khenaidoo59ce9dd2019-11-11 13:05:32 -050066}
67
68// parsePSIStats parses the specified file for pressure stall information
khenaidoo26721882021-08-11 17:42:52 -040069func parsePSIStats(resource string, r io.Reader) (PSIStats, error) {
khenaidoo59ce9dd2019-11-11 13:05:32 -050070 psiStats := PSIStats{}
khenaidoo59ce9dd2019-11-11 13:05:32 -050071
khenaidoo26721882021-08-11 17:42:52 -040072 scanner := bufio.NewScanner(r)
73 for scanner.Scan() {
74 l := scanner.Text()
khenaidoo59ce9dd2019-11-11 13:05:32 -050075 prefix := strings.Split(l, " ")[0]
76 switch prefix {
77 case "some":
78 psi := PSILine{}
79 _, err := fmt.Sscanf(l, fmt.Sprintf("some %s", lineFormat), &psi.Avg10, &psi.Avg60, &psi.Avg300, &psi.Total)
80 if err != nil {
81 return PSIStats{}, err
82 }
83 psiStats.Some = &psi
84 case "full":
85 psi := PSILine{}
86 _, err := fmt.Sscanf(l, fmt.Sprintf("full %s", lineFormat), &psi.Avg10, &psi.Avg60, &psi.Avg300, &psi.Total)
87 if err != nil {
88 return PSIStats{}, err
89 }
90 psiStats.Full = &psi
91 default:
92 // If we encounter a line with an unknown prefix, ignore it and move on
93 // Should new measurement types be added in the future we'll simply ignore them instead
94 // of erroring on retrieval
95 continue
96 }
97 }
98
99 return psiStats, nil
100}