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.
 
 
 

220 lines
6.5 KiB

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