暂无描述

driver_focus.go 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 nil
  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. if numFilter := parseTagFilterRange(value); numFilter != nil {
  70. ui.PrintErr(name, ":Interpreted '", value, "' as range, not regexp")
  71. return func(s *profile.Sample) bool {
  72. for key, vals := range s.NumLabel {
  73. for _, val := range vals {
  74. if numFilter(val, key) {
  75. return true
  76. }
  77. }
  78. }
  79. return false
  80. }, nil
  81. }
  82. var rfx []*regexp.Regexp
  83. for _, tagf := range strings.Split(value, ",") {
  84. fx, err := regexp.Compile(tagf)
  85. if err != nil {
  86. return nil, fmt.Errorf("parsing %s regexp: %v", name, err)
  87. }
  88. rfx = append(rfx, fx)
  89. }
  90. return func(s *profile.Sample) bool {
  91. matchedrx:
  92. for _, rx := range rfx {
  93. for key, vals := range s.Label {
  94. for _, val := range vals {
  95. if rx.MatchString(key + ":" + val) {
  96. continue matchedrx
  97. }
  98. }
  99. }
  100. return false
  101. }
  102. return true
  103. }, nil
  104. }
  105. // parseTagFilterRange returns a function to checks if a value is
  106. // contained on the range described by a string. It can recognize
  107. // strings of the form:
  108. // "32kb" -- matches values == 32kb
  109. // ":64kb" -- matches values <= 64kb
  110. // "4mb:" -- matches values >= 4mb
  111. // "12kb:64mb" -- matches values between 12kb and 64mb (both included).
  112. func parseTagFilterRange(filter string) func(int64, string) bool {
  113. ranges := tagFilterRangeRx.FindAllStringSubmatch(filter, 2)
  114. if len(ranges) == 0 {
  115. return nil // No ranges were identified
  116. }
  117. v, err := strconv.ParseInt(ranges[0][1], 10, 64)
  118. if err != nil {
  119. panic(fmt.Errorf("Failed to parse int %s: %v", ranges[0][1], err))
  120. }
  121. scaledValue, unit := measurement.Scale(v, ranges[0][2], ranges[0][2])
  122. if len(ranges) == 1 {
  123. switch match := ranges[0][0]; filter {
  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. case ":" + match:
  135. return func(v int64, u string) bool {
  136. sv, su := measurement.Scale(v, u, unit)
  137. return su == unit && sv <= scaledValue
  138. }
  139. }
  140. return nil
  141. }
  142. if filter != ranges[0][0]+":"+ranges[1][0] {
  143. return nil
  144. }
  145. if v, err = strconv.ParseInt(ranges[1][1], 10, 64); err != nil {
  146. panic(fmt.Errorf("Failed to parse int %s: %v", ranges[1][1], err))
  147. }
  148. scaledValue2, unit2 := measurement.Scale(v, ranges[1][2], unit)
  149. if unit != unit2 {
  150. return nil
  151. }
  152. return func(v int64, u string) bool {
  153. sv, su := measurement.Scale(v, u, unit)
  154. return su == unit && sv >= scaledValue && sv <= scaledValue2
  155. }
  156. }
  157. func warnNoMatches(match bool, option string, ui plugin.UI) {
  158. if !match {
  159. ui.PrintErr(option + " expression matched no samples")
  160. }
  161. }