proc_psi.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. "fmt"
  25. "io"
  26. "io/ioutil"
  27. "os"
  28. "strings"
  29. )
  30. const lineFormat = "avg10=%f avg60=%f avg300=%f total=%d"
  31. // PSILine is a single line of values as returned by /proc/pressure/*
  32. // The Avg entries are averages over n seconds, as a percentage
  33. // The Total line is in microseconds
  34. type PSILine struct {
  35. Avg10 float64
  36. Avg60 float64
  37. Avg300 float64
  38. Total uint64
  39. }
  40. // PSIStats represent pressure stall information from /proc/pressure/*
  41. // Some indicates the share of time in which at least some tasks are stalled
  42. // Full indicates the share of time in which all non-idle tasks are stalled simultaneously
  43. type PSIStats struct {
  44. Some *PSILine
  45. Full *PSILine
  46. }
  47. // PSIStatsForResource reads pressure stall information for the specified
  48. // resource from /proc/pressure/<resource>. At time of writing this can be
  49. // either "cpu", "memory" or "io".
  50. func (fs FS) PSIStatsForResource(resource string) (PSIStats, error) {
  51. file, err := os.Open(fs.proc.Path(fmt.Sprintf("%s/%s", "pressure", resource)))
  52. if err != nil {
  53. return PSIStats{}, fmt.Errorf("psi_stats: unavailable for %s", resource)
  54. }
  55. defer file.Close()
  56. return parsePSIStats(resource, file)
  57. }
  58. // parsePSIStats parses the specified file for pressure stall information
  59. func parsePSIStats(resource string, file io.Reader) (PSIStats, error) {
  60. psiStats := PSIStats{}
  61. stats, err := ioutil.ReadAll(file)
  62. if err != nil {
  63. return psiStats, fmt.Errorf("psi_stats: unable to read data for %s", resource)
  64. }
  65. for _, l := range strings.Split(string(stats), "\n") {
  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. }