Açıklama Yok

driver_focus.go 6.3KB

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