Без опису

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