설명 없음

driver_focus.go 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // Copyright 2014 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package driver
  15. import (
  16. "fmt"
  17. "regexp"
  18. "strconv"
  19. "strings"
  20. "github.com/google/pprof/internal/measurement"
  21. "github.com/google/pprof/internal/plugin"
  22. "github.com/google/pprof/profile"
  23. )
  24. var tagFilterRangeRx = regexp.MustCompile("([[:digit:]]+)([[:alpha:]]+)")
  25. // applyFocus filters samples based on the focus/ignore options
  26. func applyFocus(prof *profile.Profile, v variables, ui plugin.UI) error {
  27. focus, err := compileRegexOption("focus", v["focus"].value, nil)
  28. ignore, err := compileRegexOption("ignore", v["ignore"].value, err)
  29. hide, err := compileRegexOption("hide", v["hide"].value, err)
  30. show, err := compileRegexOption("show", v["show"].value, err)
  31. tagfocus, err := compileTagFilter("tagfocus", v["tagfocus"].value, ui, err)
  32. tagignore, err := compileTagFilter("tagignore", v["tagignore"].value, ui, err)
  33. prunefrom, err := compileRegexOption("prune_from", v["prune_from"].value, err)
  34. if err != nil {
  35. return err
  36. }
  37. fm, im, hm, hnm := prof.FilterSamplesByName(focus, ignore, hide, show)
  38. warnNoMatches(focus == nil || fm, "Focus", ui)
  39. warnNoMatches(ignore == nil || im, "Ignore", ui)
  40. warnNoMatches(hide == nil || hm, "Hide", ui)
  41. warnNoMatches(show == nil || hnm, "Show", ui)
  42. tfm, tim := prof.FilterSamplesByTag(tagfocus, tagignore)
  43. warnNoMatches(tagfocus == nil || tfm, "TagFocus", ui)
  44. warnNoMatches(tagignore == nil || tim, "TagIgnore", ui)
  45. if prunefrom != nil {
  46. prof.PruneFrom(prunefrom)
  47. }
  48. return nil
  49. }
  50. func compileRegexOption(name, value string, err error) (*regexp.Regexp, error) {
  51. if value == "" || err != nil {
  52. return nil, err
  53. }
  54. rx, err := regexp.Compile(value)
  55. if err != nil {
  56. return nil, fmt.Errorf("parsing %s regexp: %v", name, err)
  57. }
  58. return rx, nil
  59. }
  60. func compileTagFilter(name, value string, ui plugin.UI, err error) (func(*profile.Sample) bool, error) {
  61. if value == "" || err != nil {
  62. return nil, err
  63. }
  64. if numFilter := parseTagFilterRange(value); numFilter != nil {
  65. ui.PrintErr(name, ":Interpreted '", value, "' as range, not regexp")
  66. return func(s *profile.Sample) bool {
  67. for key, vals := range s.NumLabel {
  68. for _, val := range vals {
  69. if numFilter(val, key) {
  70. return true
  71. }
  72. }
  73. }
  74. return false
  75. }, nil
  76. }
  77. var rfx []*regexp.Regexp
  78. for _, tagf := range strings.Split(value, ",") {
  79. fx, err := regexp.Compile(tagf)
  80. if err != nil {
  81. return nil, fmt.Errorf("parsing %s regexp: %v", name, err)
  82. }
  83. rfx = append(rfx, fx)
  84. }
  85. return func(s *profile.Sample) bool {
  86. matchedrx:
  87. for _, rx := range rfx {
  88. for key, vals := range s.Label {
  89. for _, val := range vals {
  90. if rx.MatchString(key + ":" + val) {
  91. continue matchedrx
  92. }
  93. }
  94. }
  95. return false
  96. }
  97. return true
  98. }, nil
  99. }
  100. // parseTagFilterRange returns a function to checks if a value is
  101. // contained on the range described by a string. It can recognize
  102. // strings of the form:
  103. // "32kb" -- matches values == 32kb
  104. // ":64kb" -- matches values <= 64kb
  105. // "4mb:" -- matches values >= 4mb
  106. // "12kb:64mb" -- matches values between 12kb and 64mb (both included).
  107. func parseTagFilterRange(filter string) func(int64, string) bool {
  108. ranges := tagFilterRangeRx.FindAllStringSubmatch(filter, 2)
  109. if len(ranges) == 0 {
  110. return nil // No ranges were identified
  111. }
  112. v, err := strconv.ParseInt(ranges[0][1], 10, 64)
  113. if err != nil {
  114. panic(fmt.Errorf("Failed to parse int %s: %v", ranges[0][1], err))
  115. }
  116. scaledValue, unit := measurement.Scale(v, ranges[0][2], ranges[0][2])
  117. if len(ranges) == 1 {
  118. switch match := ranges[0][0]; filter {
  119. case match:
  120. return func(v int64, u string) bool {
  121. sv, su := measurement.Scale(v, u, unit)
  122. return su == unit && sv == scaledValue
  123. }
  124. case match + ":":
  125. return func(v int64, u string) bool {
  126. sv, su := measurement.Scale(v, u, unit)
  127. return su == unit && sv >= scaledValue
  128. }
  129. case ":" + match:
  130. return func(v int64, u string) bool {
  131. sv, su := measurement.Scale(v, u, unit)
  132. return su == unit && sv <= scaledValue
  133. }
  134. }
  135. return nil
  136. }
  137. if filter != ranges[0][0]+":"+ranges[1][0] {
  138. return nil
  139. }
  140. if v, err = strconv.ParseInt(ranges[1][1], 10, 64); err != nil {
  141. panic(fmt.Errorf("Failed to parse int %s: %v", ranges[1][1], err))
  142. }
  143. scaledValue2, unit2 := measurement.Scale(v, ranges[1][2], unit)
  144. if unit != unit2 {
  145. return nil
  146. }
  147. return func(v int64, u string) bool {
  148. sv, su := measurement.Scale(v, u, unit)
  149. return su == unit && sv >= scaledValue && sv <= scaledValue2
  150. }
  151. }
  152. func warnNoMatches(match bool, option string, ui plugin.UI) {
  153. if !match {
  154. ui.PrintErr(option + " expression matched no samples")
  155. }
  156. }