schedstat.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. import (
  15. "bufio"
  16. "errors"
  17. "os"
  18. "regexp"
  19. "strconv"
  20. )
  21. var (
  22. cpuLineRE = regexp.MustCompile(`cpu(\d+) (\d+) (\d+) (\d+) (\d+) (\d+) (\d+) (\d+) (\d+) (\d+)`)
  23. procLineRE = regexp.MustCompile(`(\d+) (\d+) (\d+)`)
  24. )
  25. // Schedstat contains scheduler statistics from /proc/schedstat
  26. //
  27. // See
  28. // https://www.kernel.org/doc/Documentation/scheduler/sched-stats.txt
  29. // for a detailed description of what these numbers mean.
  30. //
  31. // Note the current kernel documentation claims some of the time units are in
  32. // jiffies when they are actually in nanoseconds since 2.6.23 with the
  33. // introduction of CFS. A fix to the documentation is pending. See
  34. // https://lore.kernel.org/patchwork/project/lkml/list/?series=403473
  35. type Schedstat struct {
  36. CPUs []*SchedstatCPU
  37. }
  38. // SchedstatCPU contains the values from one "cpu<N>" line.
  39. type SchedstatCPU struct {
  40. CPUNum string
  41. RunningNanoseconds uint64
  42. WaitingNanoseconds uint64
  43. RunTimeslices uint64
  44. }
  45. // ProcSchedstat contains the values from `/proc/<pid>/schedstat`.
  46. type ProcSchedstat struct {
  47. RunningNanoseconds uint64
  48. WaitingNanoseconds uint64
  49. RunTimeslices uint64
  50. }
  51. // Schedstat reads data from `/proc/schedstat`.
  52. func (fs FS) Schedstat() (*Schedstat, error) {
  53. file, err := os.Open(fs.proc.Path("schedstat"))
  54. if err != nil {
  55. return nil, err
  56. }
  57. defer file.Close()
  58. stats := &Schedstat{}
  59. scanner := bufio.NewScanner(file)
  60. for scanner.Scan() {
  61. match := cpuLineRE.FindStringSubmatch(scanner.Text())
  62. if match != nil {
  63. cpu := &SchedstatCPU{}
  64. cpu.CPUNum = match[1]
  65. cpu.RunningNanoseconds, err = strconv.ParseUint(match[8], 10, 64)
  66. if err != nil {
  67. continue
  68. }
  69. cpu.WaitingNanoseconds, err = strconv.ParseUint(match[9], 10, 64)
  70. if err != nil {
  71. continue
  72. }
  73. cpu.RunTimeslices, err = strconv.ParseUint(match[10], 10, 64)
  74. if err != nil {
  75. continue
  76. }
  77. stats.CPUs = append(stats.CPUs, cpu)
  78. }
  79. }
  80. return stats, nil
  81. }
  82. func parseProcSchedstat(contents string) (ProcSchedstat, error) {
  83. var (
  84. stats ProcSchedstat
  85. err error
  86. )
  87. match := procLineRE.FindStringSubmatch(contents)
  88. if match != nil {
  89. stats.RunningNanoseconds, err = strconv.ParseUint(match[1], 10, 64)
  90. if err != nil {
  91. return stats, err
  92. }
  93. stats.WaitingNanoseconds, err = strconv.ParseUint(match[2], 10, 64)
  94. if err != nil {
  95. return stats, err
  96. }
  97. stats.RunTimeslices, err = strconv.ParseUint(match[3], 10, 64)
  98. return stats, err
  99. }
  100. return stats, errors.New("could not parse schedstat")
  101. }