Keine Beschreibung

driver_focus.go 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. tagshow, err := compileRegexOption("tagshow", v["tagshow"].value, err)
  46. taghide, err := compileRegexOption("taghide", v["taghide"].value, err)
  47. tns, tnh := prof.FilterTagsByName(tagshow, taghide)
  48. warnNoMatches(tagshow == nil || tns, "TagShow", ui)
  49. warnNoMatches(tagignore == nil || tnh, "TagHide", ui)
  50. if prunefrom != nil {
  51. prof.PruneFrom(prunefrom)
  52. }
  53. return err
  54. }
  55. func compileRegexOption(name, value string, err error) (*regexp.Regexp, error) {
  56. if value == "" || err != nil {
  57. return nil, err
  58. }
  59. rx, err := regexp.Compile(value)
  60. if err != nil {
  61. return nil, fmt.Errorf("parsing %s regexp: %v", name, err)
  62. }
  63. return rx, nil
  64. }
  65. func compileTagFilter(name, value string, ui plugin.UI, err error) (func(*profile.Sample) bool, error) {
  66. if value == "" || err != nil {
  67. return nil, err
  68. }
  69. tagValuePair := strings.SplitN(value, "=", 2)
  70. var wantKey string
  71. if len(tagValuePair) == 2 {
  72. wantKey = tagValuePair[0]
  73. value = tagValuePair[1]
  74. } else {
  75. wantKey = ""
  76. }
  77. if numFilter := parseTagFilterRange(value); numFilter != nil {
  78. ui.PrintErr(name, ":Interpreted '", value, "' as range, not regexp")
  79. labelFilter := func(vals []int64, key string) bool {
  80. for _, val := range vals {
  81. if numFilter(val, key) {
  82. return true
  83. }
  84. }
  85. return false
  86. }
  87. if wantKey == "" {
  88. return func(s *profile.Sample) bool {
  89. for key, vals := range s.NumLabel {
  90. if labelFilter(vals, key) {
  91. return true
  92. }
  93. }
  94. return false
  95. }, nil
  96. }
  97. return func(s *profile.Sample) bool {
  98. if vals, ok := s.NumLabel[wantKey]; ok {
  99. return labelFilter(vals, wantKey)
  100. }
  101. return false
  102. }, nil
  103. }
  104. var rfx []*regexp.Regexp
  105. for _, tagf := range strings.Split(value, ",") {
  106. fx, err := regexp.Compile(tagf)
  107. if err != nil {
  108. return nil, fmt.Errorf("parsing %s regexp: %v", name, err)
  109. }
  110. rfx = append(rfx, fx)
  111. }
  112. if wantKey == "" {
  113. return func(s *profile.Sample) bool {
  114. matchedrx:
  115. for _, rx := range rfx {
  116. for key, vals := range s.Label {
  117. for _, val := range vals {
  118. // TODO: Match against val, not key:val in future
  119. if rx.MatchString(key + ":" + val) {
  120. continue matchedrx
  121. }
  122. }
  123. }
  124. return false
  125. }
  126. return true
  127. }, nil
  128. }
  129. return func(s *profile.Sample) bool {
  130. if vals, ok := s.Label[wantKey]; ok {
  131. for _, rx := range rfx {
  132. for _, val := range vals {
  133. if rx.MatchString(val) {
  134. return true
  135. }
  136. }
  137. }
  138. }
  139. return false
  140. }, nil
  141. }
  142. // parseTagFilterRange returns a function to checks if a value is
  143. // contained on the range described by a string. It can recognize
  144. // strings of the form:
  145. // "32kb" -- matches values == 32kb
  146. // ":64kb" -- matches values <= 64kb
  147. // "4mb:" -- matches values >= 4mb
  148. // "12kb:64mb" -- matches values between 12kb and 64mb (both included).
  149. func parseTagFilterRange(filter string) func(int64, string) bool {
  150. ranges := tagFilterRangeRx.FindAllStringSubmatch(filter, 2)
  151. if len(ranges) == 0 {
  152. return nil // No ranges were identified
  153. }
  154. v, err := strconv.ParseInt(ranges[0][1], 10, 64)
  155. if err != nil {
  156. panic(fmt.Errorf("Failed to parse int %s: %v", ranges[0][1], err))
  157. }
  158. scaledValue, unit := measurement.Scale(v, ranges[0][2], ranges[0][2])
  159. if len(ranges) == 1 {
  160. switch match := ranges[0][0]; filter {
  161. case match:
  162. return func(v int64, u string) bool {
  163. sv, su := measurement.Scale(v, u, unit)
  164. return su == unit && sv == scaledValue
  165. }
  166. case match + ":":
  167. return func(v int64, u string) bool {
  168. sv, su := measurement.Scale(v, u, unit)
  169. return su == unit && sv >= scaledValue
  170. }
  171. case ":" + match:
  172. return func(v int64, u string) bool {
  173. sv, su := measurement.Scale(v, u, unit)
  174. return su == unit && sv <= scaledValue
  175. }
  176. }
  177. return nil
  178. }
  179. if filter != ranges[0][0]+":"+ranges[1][0] {
  180. return nil
  181. }
  182. if v, err = strconv.ParseInt(ranges[1][1], 10, 64); err != nil {
  183. panic(fmt.Errorf("Failed to parse int %s: %v", ranges[1][1], err))
  184. }
  185. scaledValue2, unit2 := measurement.Scale(v, ranges[1][2], unit)
  186. if unit != unit2 {
  187. return nil
  188. }
  189. return func(v int64, u string) bool {
  190. sv, su := measurement.Scale(v, u, unit)
  191. return su == unit && sv >= scaledValue && sv <= scaledValue2
  192. }
  193. }
  194. func warnNoMatches(match bool, option string, ui plugin.UI) {
  195. if !match {
  196. ui.PrintErr(option + " expression matched no samples")
  197. }
  198. }