You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

proc_psi.go 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. // NewPSIStatsForResource reads pressure stall information for the specified
  48. // resource. At time of writing this can be either "cpu", "memory" or "io".
  49. func NewPSIStatsForResource(resource string) (PSIStats, error) {
  50. fs, err := NewFS(DefaultMountPoint)
  51. if err != nil {
  52. return PSIStats{}, err
  53. }
  54. return fs.NewPSIStatsForResource(resource)
  55. }
  56. // NewPSIStatsForResource reads pressure stall information from /proc/pressure/<resource>
  57. func (fs FS) NewPSIStatsForResource(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. defer file.Close()
  63. return parsePSIStats(resource, file)
  64. }
  65. // parsePSIStats parses the specified file for pressure stall information
  66. func parsePSIStats(resource string, file io.Reader) (PSIStats, error) {
  67. psiStats := PSIStats{}
  68. stats, err := ioutil.ReadAll(file)
  69. if err != nil {
  70. return psiStats, fmt.Errorf("psi_stats: unable to read data for %s", resource)
  71. }
  72. for _, l := range strings.Split(string(stats), "\n") {
  73. prefix := strings.Split(l, " ")[0]
  74. switch prefix {
  75. case "some":
  76. psi := PSILine{}
  77. _, err := fmt.Sscanf(l, fmt.Sprintf("some %s", lineFormat), &psi.Avg10, &psi.Avg60, &psi.Avg300, &psi.Total)
  78. if err != nil {
  79. return PSIStats{}, err
  80. }
  81. psiStats.Some = &psi
  82. case "full":
  83. psi := PSILine{}
  84. _, err := fmt.Sscanf(l, fmt.Sprintf("full %s", lineFormat), &psi.Avg10, &psi.Avg60, &psi.Avg300, &psi.Total)
  85. if err != nil {
  86. return PSIStats{}, err
  87. }
  88. psiStats.Full = &psi
  89. default:
  90. // If we encounter a line with an unknown prefix, ignore it and move on
  91. // Should new measurement types be added in the future we'll simply ignore them instead
  92. // of erroring on retrieval
  93. continue
  94. }
  95. }
  96. return psiStats, nil
  97. }