proc_psi.go 3.3 KB

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