profile_view.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. // Copyright 2009 the V8 project authors. All rights reserved.
  2. // Redistribution and use in source and binary forms, with or without
  3. // modification, are permitted provided that the following conditions are
  4. // met:
  5. //
  6. // * Redistributions of source code must retain the above copyright
  7. // notice, this list of conditions and the following disclaimer.
  8. // * Redistributions in binary form must reproduce the above
  9. // copyright notice, this list of conditions and the following
  10. // disclaimer in the documentation and/or other materials provided
  11. // with the distribution.
  12. // * Neither the name of Google Inc. nor the names of its
  13. // contributors may be used to endorse or promote products derived
  14. // from this software without specific prior written permission.
  15. //
  16. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  19. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  20. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  21. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  22. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  26. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. /**
  28. * Creates a Profile View builder object.
  29. *
  30. * @param {number} samplingRate Number of ms between profiler ticks.
  31. * @constructor
  32. */
  33. function ViewBuilder(samplingRate) {
  34. this.samplingRate = samplingRate;
  35. };
  36. /**
  37. * Builds a profile view for the specified call tree.
  38. *
  39. * @param {CallTree} callTree A call tree.
  40. * @param {boolean} opt_bottomUpViewWeights Whether remapping
  41. * of self weights for a bottom up view is needed.
  42. */
  43. ViewBuilder.prototype.buildView = function(
  44. callTree, opt_bottomUpViewWeights) {
  45. var head;
  46. var samplingRate = this.samplingRate;
  47. var createViewNode = this.createViewNode;
  48. callTree.traverse(function(node, viewParent) {
  49. var totalWeight = node.totalWeight * samplingRate;
  50. var selfWeight = node.selfWeight * samplingRate;
  51. if (opt_bottomUpViewWeights === true) {
  52. if (viewParent === head) {
  53. selfWeight = totalWeight;
  54. } else {
  55. selfWeight = 0;
  56. }
  57. }
  58. var viewNode = createViewNode(node.label, totalWeight, selfWeight, head);
  59. if (viewParent) {
  60. viewParent.addChild(viewNode);
  61. } else {
  62. head = viewNode;
  63. }
  64. return viewNode;
  65. });
  66. var view = this.createView(head);
  67. return view;
  68. };
  69. /**
  70. * Factory method for a profile view.
  71. *
  72. * @param {ProfileView.Node} head View head node.
  73. * @return {ProfileView} Profile view.
  74. */
  75. ViewBuilder.prototype.createView = function(head) {
  76. return new ProfileView(head);
  77. };
  78. /**
  79. * Factory method for a profile view node.
  80. *
  81. * @param {string} internalFuncName A fully qualified function name.
  82. * @param {number} totalTime Amount of time that application spent in the
  83. * corresponding function and its descendants (not that depending on
  84. * profile they can be either callees or callers.)
  85. * @param {number} selfTime Amount of time that application spent in the
  86. * corresponding function only.
  87. * @param {ProfileView.Node} head Profile view head.
  88. * @return {ProfileView.Node} Profile view node.
  89. */
  90. ViewBuilder.prototype.createViewNode = function(
  91. funcName, totalTime, selfTime, head) {
  92. return new ProfileView.Node(
  93. funcName, totalTime, selfTime, head);
  94. };
  95. /**
  96. * Creates a Profile View object. It allows to perform sorting
  97. * and filtering actions on the profile.
  98. *
  99. * @param {ProfileView.Node} head Head (root) node.
  100. * @constructor
  101. */
  102. function ProfileView(head) {
  103. this.head = head;
  104. };
  105. /**
  106. * Sorts the profile view using the specified sort function.
  107. *
  108. * @param {function(ProfileView.Node,
  109. * ProfileView.Node):number} sortFunc A sorting
  110. * functions. Must comply with Array.sort sorting function requirements.
  111. */
  112. ProfileView.prototype.sort = function(sortFunc) {
  113. this.traverse(function (node) {
  114. node.sortChildren(sortFunc);
  115. });
  116. };
  117. /**
  118. * Traverses profile view nodes in preorder.
  119. *
  120. * @param {function(ProfileView.Node)} f Visitor function.
  121. */
  122. ProfileView.prototype.traverse = function(f) {
  123. var nodesToTraverse = new ConsArray();
  124. nodesToTraverse.concat([this.head]);
  125. while (!nodesToTraverse.atEnd()) {
  126. var node = nodesToTraverse.next();
  127. f(node);
  128. nodesToTraverse.concat(node.children);
  129. }
  130. };
  131. /**
  132. * Constructs a Profile View node object. Each node object corresponds to
  133. * a function call.
  134. *
  135. * @param {string} internalFuncName A fully qualified function name.
  136. * @param {number} totalTime Amount of time that application spent in the
  137. * corresponding function and its descendants (not that depending on
  138. * profile they can be either callees or callers.)
  139. * @param {number} selfTime Amount of time that application spent in the
  140. * corresponding function only.
  141. * @param {ProfileView.Node} head Profile view head.
  142. * @constructor
  143. */
  144. ProfileView.Node = function(
  145. internalFuncName, totalTime, selfTime, head) {
  146. this.internalFuncName = internalFuncName;
  147. this.totalTime = totalTime;
  148. this.selfTime = selfTime;
  149. this.head = head;
  150. this.parent = null;
  151. this.children = [];
  152. };
  153. /**
  154. * Returns a share of the function's total time in application's total time.
  155. */
  156. ProfileView.Node.prototype.__defineGetter__(
  157. 'totalPercent',
  158. function() { return this.totalTime /
  159. (this.head ? this.head.totalTime : this.totalTime) * 100.0; });
  160. /**
  161. * Returns a share of the function's self time in application's total time.
  162. */
  163. ProfileView.Node.prototype.__defineGetter__(
  164. 'selfPercent',
  165. function() { return this.selfTime /
  166. (this.head ? this.head.totalTime : this.totalTime) * 100.0; });
  167. /**
  168. * Returns a share of the function's total time in its parent's total time.
  169. */
  170. ProfileView.Node.prototype.__defineGetter__(
  171. 'parentTotalPercent',
  172. function() { return this.totalTime /
  173. (this.parent ? this.parent.totalTime : this.totalTime) * 100.0; });
  174. /**
  175. * Adds a child to the node.
  176. *
  177. * @param {ProfileView.Node} node Child node.
  178. */
  179. ProfileView.Node.prototype.addChild = function(node) {
  180. node.parent = this;
  181. this.children.push(node);
  182. };
  183. /**
  184. * Sorts all the node's children recursively.
  185. *
  186. * @param {function(ProfileView.Node,
  187. * ProfileView.Node):number} sortFunc A sorting
  188. * functions. Must comply with Array.sort sorting function requirements.
  189. */
  190. ProfileView.Node.prototype.sortChildren = function(
  191. sortFunc) {
  192. this.children.sort(sortFunc);
  193. };