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.
 
 
 

179 lines
5.0 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. // Implements methods to remove frames from profiles.
  15. package profile
  16. import (
  17. "fmt"
  18. "regexp"
  19. "strings"
  20. )
  21. var (
  22. reservedNames = []string{"(anonymous namespace)", "operator()"}
  23. bracketRx = func() *regexp.Regexp {
  24. var quotedNames []string
  25. for _, name := range append(reservedNames, "(") {
  26. quotedNames = append(quotedNames, regexp.QuoteMeta(name))
  27. }
  28. return regexp.MustCompile(strings.Join(quotedNames, "|"))
  29. }()
  30. )
  31. // simplifyFunc does some primitive simplification of function names.
  32. func simplifyFunc(f string) string {
  33. // Account for leading '.' on the PPC ELF v1 ABI.
  34. funcName := strings.TrimPrefix(f, ".")
  35. // Account for unsimplified names -- try to remove the argument list by trimming
  36. // starting from the first '(', but skipping reserved names that have '('.
  37. for _, ind := range bracketRx.FindAllStringSubmatchIndex(funcName, -1) {
  38. foundReserved := false
  39. for _, res := range reservedNames {
  40. if funcName[ind[0]:ind[1]] == res {
  41. foundReserved = true
  42. break
  43. }
  44. }
  45. if !foundReserved {
  46. funcName = funcName[:ind[0]]
  47. break
  48. }
  49. }
  50. return funcName
  51. }
  52. // Prune removes all nodes beneath a node matching dropRx, and not
  53. // matching keepRx. If the root node of a Sample matches, the sample
  54. // will have an empty stack.
  55. func (p *Profile) Prune(dropRx, keepRx *regexp.Regexp) {
  56. prune := make(map[uint64]bool)
  57. pruneBeneath := make(map[uint64]bool)
  58. for _, loc := range p.Location {
  59. var i int
  60. for i = len(loc.Line) - 1; i >= 0; i-- {
  61. if fn := loc.Line[i].Function; fn != nil && fn.Name != "" {
  62. funcName := simplifyFunc(fn.Name)
  63. if dropRx.MatchString(funcName) {
  64. if keepRx == nil || !keepRx.MatchString(funcName) {
  65. break
  66. }
  67. }
  68. }
  69. }
  70. if i >= 0 {
  71. // Found matching entry to prune.
  72. pruneBeneath[loc.ID] = true
  73. // Remove the matching location.
  74. if i == len(loc.Line)-1 {
  75. // Matched the top entry: prune the whole location.
  76. prune[loc.ID] = true
  77. } else {
  78. loc.Line = loc.Line[i+1:]
  79. }
  80. }
  81. }
  82. // Prune locs from each Sample
  83. for _, sample := range p.Sample {
  84. // Scan from the root to the leaves to find the prune location.
  85. // Do not prune frames before the first user frame, to avoid
  86. // pruning everything.
  87. foundUser := false
  88. for i := len(sample.Location) - 1; i >= 0; i-- {
  89. id := sample.Location[i].ID
  90. if !prune[id] && !pruneBeneath[id] {
  91. foundUser = true
  92. continue
  93. }
  94. if !foundUser {
  95. continue
  96. }
  97. if prune[id] {
  98. sample.Location = sample.Location[i+1:]
  99. break
  100. }
  101. if pruneBeneath[id] {
  102. sample.Location = sample.Location[i:]
  103. break
  104. }
  105. }
  106. }
  107. }
  108. // RemoveUninteresting prunes and elides profiles using built-in
  109. // tables of uninteresting function names.
  110. func (p *Profile) RemoveUninteresting() error {
  111. var keep, drop *regexp.Regexp
  112. var err error
  113. if p.DropFrames != "" {
  114. if drop, err = regexp.Compile("^(" + p.DropFrames + ")$"); err != nil {
  115. return fmt.Errorf("failed to compile regexp %s: %v", p.DropFrames, err)
  116. }
  117. if p.KeepFrames != "" {
  118. if keep, err = regexp.Compile("^(" + p.KeepFrames + ")$"); err != nil {
  119. return fmt.Errorf("failed to compile regexp %s: %v", p.KeepFrames, err)
  120. }
  121. }
  122. p.Prune(drop, keep)
  123. }
  124. return nil
  125. }
  126. // PruneFrom removes all nodes beneath the lowest node matching dropRx, not including itself.
  127. //
  128. // Please see the example below to understand this method as well as
  129. // the difference from Prune method.
  130. //
  131. // A sample contains Location of [A,B,C,B,D] where D is the top frame and there's no inline.
  132. //
  133. // PruneFrom(A) returns [A,B,C,B,D] because there's no node beneath A.
  134. // Prune(A, nil) returns [B,C,B,D] by removing A itself.
  135. //
  136. // PruneFrom(B) returns [B,C,B,D] by removing all nodes beneath the first B when scanning from the bottom.
  137. // Prune(B, nil) returns [D] because a matching node is found by scanning from the root.
  138. func (p *Profile) PruneFrom(dropRx *regexp.Regexp) {
  139. pruneBeneath := make(map[uint64]bool)
  140. for _, loc := range p.Location {
  141. for i := 0; i < len(loc.Line); i++ {
  142. if fn := loc.Line[i].Function; fn != nil && fn.Name != "" {
  143. funcName := simplifyFunc(fn.Name)
  144. if dropRx.MatchString(funcName) {
  145. // Found matching entry to prune.
  146. pruneBeneath[loc.ID] = true
  147. loc.Line = loc.Line[i:]
  148. break
  149. }
  150. }
  151. }
  152. }
  153. // Prune locs from each Sample
  154. for _, sample := range p.Sample {
  155. // Scan from the bottom leaf to the root to find the prune location.
  156. for i, loc := range sample.Location {
  157. if pruneBeneath[loc.ID] {
  158. sample.Location = sample.Location[i:]
  159. break
  160. }
  161. }
  162. }
  163. }