Bez popisu

prune.go 4.5KB

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