本站源代码
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.

119 lines
2.9KB

  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/schedstats
  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. func (fs FS) Schedstat() (*Schedstat, error) {
  52. file, err := os.Open(fs.proc.Path("schedstat"))
  53. if err != nil {
  54. return nil, err
  55. }
  56. defer file.Close()
  57. stats := &Schedstat{}
  58. scanner := bufio.NewScanner(file)
  59. for scanner.Scan() {
  60. match := cpuLineRE.FindStringSubmatch(scanner.Text())
  61. if match != nil {
  62. cpu := &SchedstatCPU{}
  63. cpu.CPUNum = match[1]
  64. cpu.RunningNanoseconds, err = strconv.ParseUint(match[8], 10, 64)
  65. if err != nil {
  66. continue
  67. }
  68. cpu.WaitingNanoseconds, err = strconv.ParseUint(match[9], 10, 64)
  69. if err != nil {
  70. continue
  71. }
  72. cpu.RunTimeslices, err = strconv.ParseUint(match[10], 10, 64)
  73. if err != nil {
  74. continue
  75. }
  76. stats.CPUs = append(stats.CPUs, cpu)
  77. }
  78. }
  79. return stats, nil
  80. }
  81. func parseProcSchedstat(contents string) (stats ProcSchedstat, err error) {
  82. match := procLineRE.FindStringSubmatch(contents)
  83. if match != nil {
  84. stats.RunningNanoseconds, err = strconv.ParseUint(match[1], 10, 64)
  85. if err != nil {
  86. return
  87. }
  88. stats.WaitingNanoseconds, err = strconv.ParseUint(match[2], 10, 64)
  89. if err != nil {
  90. return
  91. }
  92. stats.RunTimeslices, err = strconv.ParseUint(match[3], 10, 64)
  93. return
  94. }
  95. err = errors.New("could not parse schedstat")
  96. return
  97. }
上海开阖软件有限公司 沪ICP备12045867号-1