profile_view.mjs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. import { ConsArray } from "./consarray.mjs";
  28. /**
  29. * Creates a Profile View builder object.
  30. *
  31. * @param {number} samplingRate Number of ms between profiler ticks.
  32. * @constructor
  33. */
  34. export function ViewBuilder(samplingRate) {
  35. this.samplingRate = samplingRate;
  36. };
  37. /**
  38. * Builds a profile view for the specified call tree.
  39. *
  40. * @param {CallTree} callTree A call tree.
  41. * @param {boolean} opt_bottomUpViewWeights Whether remapping
  42. * of self weights for a bottom up view is needed.
  43. */
  44. ViewBuilder.prototype.buildView = function(
  45. callTree, opt_bottomUpViewWeights) {
  46. let head;
  47. const samplingRate = this.samplingRate;
  48. const createViewNode = this.createViewNode;
  49. callTree.traverse(function(node, viewParent) {
  50. const totalWeight = node.totalWeight * samplingRate;
  51. let selfWeight = node.selfWeight * samplingRate;
  52. if (opt_bottomUpViewWeights === true) {
  53. if (viewParent === head) {
  54. selfWeight = totalWeight;
  55. } else {
  56. selfWeight = 0;
  57. }
  58. }
  59. const viewNode = createViewNode(node.label, totalWeight, selfWeight, head);
  60. if (viewParent) {
  61. viewParent.addChild(viewNode);
  62. } else {
  63. head = viewNode;
  64. }
  65. return viewNode;
  66. });
  67. const view = this.createView(head);
  68. return view;
  69. };
  70. /**
  71. * Factory method for a profile view.
  72. *
  73. * @param {ProfileView.Node} head View head node.
  74. * @return {ProfileView} Profile view.
  75. */
  76. ViewBuilder.prototype.createView = head => new ProfileView(head);
  77. /**
  78. * Factory method for a profile view node.
  79. *
  80. * @param {string} internalFuncName A fully qualified function name.
  81. * @param {number} totalTime Amount of time that application spent in the
  82. * corresponding function and its descendants (not that depending on
  83. * profile they can be either callees or callers.)
  84. * @param {number} selfTime Amount of time that application spent in the
  85. * corresponding function only.
  86. * @param {ProfileView.Node} head Profile view head.
  87. * @return {ProfileView.Node} Profile view node.
  88. */
  89. ViewBuilder.prototype.createViewNode = (
  90. funcName, totalTime, selfTime, head) =>
  91. new ProfileView.Node(
  92. funcName, totalTime, selfTime, head)
  93. ;
  94. /**
  95. * Creates a Profile View object. It allows to perform sorting
  96. * and filtering actions on the profile.
  97. *
  98. * @param {ProfileView.Node} head Head (root) node.
  99. * @constructor
  100. */
  101. export function ProfileView(head) {
  102. this.head = head;
  103. };
  104. /**
  105. * Sorts the profile view using the specified sort function.
  106. *
  107. * @param {function(ProfileView.Node,
  108. * ProfileView.Node):number} sortFunc A sorting
  109. * functions. Must comply with Array.sort sorting function requirements.
  110. */
  111. ProfileView.prototype.sort = function(sortFunc) {
  112. this.traverse(function (node) {
  113. node.sortChildren(sortFunc);
  114. });
  115. };
  116. /**
  117. * Traverses profile view nodes in preorder.
  118. *
  119. * @param {function(ProfileView.Node)} f Visitor function.
  120. */
  121. ProfileView.prototype.traverse = function(f) {
  122. const nodesToTraverse = new ConsArray();
  123. nodesToTraverse.concat([this.head]);
  124. while (!nodesToTraverse.atEnd()) {
  125. const node = nodesToTraverse.next();
  126. f(node);
  127. nodesToTraverse.concat(node.children);
  128. }
  129. };
  130. /**
  131. * Constructs a Profile View node object. Each node object corresponds to
  132. * a function call.
  133. *
  134. * @param {string} internalFuncName A fully qualified function name.
  135. * @param {number} totalTime Amount of time that application spent in the
  136. * corresponding function and its descendants (not that depending on
  137. * profile they can be either callees or callers.)
  138. * @param {number} selfTime Amount of time that application spent in the
  139. * corresponding function only.
  140. * @param {ProfileView.Node} head Profile view head.
  141. * @constructor
  142. */
  143. ProfileView.Node = function(
  144. internalFuncName, totalTime, selfTime, head) {
  145. this.internalFuncName = internalFuncName;
  146. this.totalTime = totalTime;
  147. this.selfTime = selfTime;
  148. this.head = head;
  149. this.parent = null;
  150. this.children = [];
  151. };
  152. /**
  153. * Returns a share of the function's total time in its parent's total time.
  154. */
  155. ProfileView.Node.prototype.__defineGetter__(
  156. 'parentTotalPercent',
  157. function() { return this.totalTime /
  158. (this.parent ? this.parent.totalTime : this.totalTime) * 100.0; });
  159. /**
  160. * Adds a child to the node.
  161. *
  162. * @param {ProfileView.Node} node Child node.
  163. */
  164. ProfileView.Node.prototype.addChild = function(node) {
  165. node.parent = this;
  166. this.children.push(node);
  167. };
  168. /**
  169. * Sorts all the node's children recursively.
  170. *
  171. * @param {function(ProfileView.Node,
  172. * ProfileView.Node):number} sortFunc A sorting
  173. * functions. Must comply with Array.sort sorting function requirements.
  174. */
  175. ProfileView.Node.prototype.sortChildren = function(
  176. sortFunc) {
  177. this.children.sort(sortFunc);
  178. };