eventedProfile.tsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. import {lastOfArray} from 'sentry/utils';
  2. import {CallTreeNode} from 'sentry/utils/profiling/callTreeNode';
  3. import {Frame} from 'sentry/utils/profiling/frame';
  4. import {Profile} from './profile';
  5. import {createFrameIndex} from './utils';
  6. export class EventedProfile extends Profile {
  7. appendOrderStack: CallTreeNode[] = [this.appendOrderTree];
  8. stack: Frame[] = [];
  9. lastValue = 0;
  10. static FromProfile(
  11. eventedProfile: Profiling.EventedProfile,
  12. frameIndex: ReturnType<typeof createFrameIndex>
  13. ): EventedProfile {
  14. const profile = new EventedProfile(
  15. eventedProfile.endValue - eventedProfile.startValue,
  16. eventedProfile.startValue,
  17. eventedProfile.endValue,
  18. eventedProfile.name,
  19. eventedProfile.unit,
  20. eventedProfile.threadID
  21. );
  22. // If frames are offset, we need to set lastValue to profile start, so that delta between
  23. // samples is correctly offset by the start value.
  24. profile.lastValue = Math.max(0, eventedProfile.startValue);
  25. for (const event of eventedProfile.events) {
  26. const frame = frameIndex[event.frame];
  27. if (!frame) {
  28. throw new Error(`Cannot retrieve event: ${event.frame} from frame index`);
  29. }
  30. switch (event.type) {
  31. // Open a new frame
  32. case 'O': {
  33. profile.enterFrame(frame, event.at);
  34. break;
  35. }
  36. // Close a frame
  37. case 'C': {
  38. profile.leaveFrame(frame, event.at);
  39. break;
  40. }
  41. default: {
  42. throw new TypeError(`Unknown event type ${event.type}`);
  43. }
  44. }
  45. }
  46. return profile.build();
  47. }
  48. addWeightToFrames(weight: number): void {
  49. const weightDelta = weight - this.lastValue;
  50. for (const frame of this.stack) {
  51. frame.addToTotalWeight(weightDelta);
  52. }
  53. const top = lastOfArray(this.stack);
  54. if (top) {
  55. top.addToSelfWeight(weight);
  56. }
  57. }
  58. addWeightsToNodes(value: number) {
  59. const delta = value - this.lastValue;
  60. for (const node of this.appendOrderStack) {
  61. node.addToTotalWeight(delta);
  62. }
  63. const stackTop = lastOfArray(this.appendOrderStack);
  64. if (stackTop) {
  65. stackTop.addToSelfWeight(delta);
  66. }
  67. }
  68. enterFrame(frame: Frame, at: number): void {
  69. this.addWeightToFrames(at);
  70. this.addWeightsToNodes(at);
  71. const lastTop = lastOfArray(this.appendOrderStack);
  72. if (lastTop) {
  73. const sampleDelta = at - this.lastValue;
  74. if (sampleDelta < 0) {
  75. throw new Error(
  76. 'Sample delta cannot be negative, samples may be corrupt or out of order'
  77. );
  78. }
  79. // If the sample timestamp is not the same as the same as of previous frame,
  80. // we can deduce that this is a new sample and need to push it on the stack
  81. if (sampleDelta > 0) {
  82. this.samples.push(lastTop);
  83. this.weights.push(sampleDelta);
  84. }
  85. const last = lastOfArray(lastTop.children);
  86. let node: CallTreeNode;
  87. if (last && !last.isLocked() && last.frame === frame) {
  88. node = last;
  89. } else {
  90. node = new CallTreeNode(frame, lastTop);
  91. lastTop.children.push(node);
  92. }
  93. // TODO: This is On^2, because we iterate over all frames in the stack to check if our
  94. // frame is a recursive frame. We could do this in O(1) by keeping a map of frames in the stack with their respective indexes
  95. // We check the stack in a top-down order to find the first recursive frame.
  96. let start = this.appendOrderStack.length - 1;
  97. while (start >= 0) {
  98. if (this.appendOrderStack[start].frame === node.frame) {
  99. // The recursion edge is bidirectional
  100. this.appendOrderStack[start].setRecursiveThroughNode(node);
  101. node.setRecursiveThroughNode(this.appendOrderStack[start]);
  102. break;
  103. }
  104. start--;
  105. }
  106. this.appendOrderStack.push(node);
  107. }
  108. this.stack.push(frame);
  109. this.lastValue = at;
  110. }
  111. leaveFrame(_event: Frame, at: number): void {
  112. this.addWeightToFrames(at);
  113. this.addWeightsToNodes(at);
  114. const leavingStackTop = this.appendOrderStack.pop();
  115. if (leavingStackTop === undefined) {
  116. throw new Error('Unbalanced stack');
  117. }
  118. // Lock the stack node, so we make sure we dont mutate it in the future.
  119. // The samples should be ordered by timestamp when processed so we should never
  120. // iterate over them again in the future.
  121. leavingStackTop.lock();
  122. const sampleDelta = at - this.lastValue;
  123. if (sampleDelta > 0) {
  124. this.samples.push(leavingStackTop);
  125. this.weights.push(sampleDelta);
  126. // Keep track of the minFrameDuration
  127. this.minFrameDuration = Math.min(sampleDelta, this.minFrameDuration);
  128. }
  129. this.stack.pop();
  130. this.lastValue = at;
  131. }
  132. build(): EventedProfile {
  133. if (this.appendOrderStack.length > 1) {
  134. throw new Error('Unbalanced append order stack');
  135. }
  136. this.duration = Math.max(
  137. this.duration,
  138. this.weights.reduce((a, b) => a + b, 0)
  139. );
  140. // We had no frames with duration > 0, so set min duration to timeline duration
  141. // which effectively disables any zooming on the flamegraphs
  142. if (
  143. this.minFrameDuration === Number.POSITIVE_INFINITY ||
  144. this.minFrameDuration === 0
  145. ) {
  146. this.minFrameDuration = this.duration;
  147. }
  148. return this;
  149. }
  150. }