@paulirish/trace_engine 0.0.10 → 0.0.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (205) hide show
  1. package/analyze-trace.mjs +1 -1
  2. package/core/platform/DevToolsPath.d.ts +4 -13
  3. package/core/platform/DevToolsPath.js +7 -4
  4. package/core/platform/DevToolsPath.js.map +1 -7
  5. package/core/platform/MimeType.d.ts +27 -0
  6. package/core/platform/MimeType.js +119 -86
  7. package/core/platform/MimeType.js.map +1 -7
  8. package/core/platform/Timing.d.ts +7 -0
  9. package/core/platform/Timing.js +7 -4
  10. package/core/platform/Timing.js.map +1 -7
  11. package/core/platform/UIString.d.ts +2 -5
  12. package/core/platform/UIString.js +5 -2
  13. package/core/platform/UIString.js.map +1 -7
  14. package/core/platform/UserVisibleError.js +19 -10
  15. package/core/platform/UserVisibleError.js.map +1 -7
  16. package/core/platform/array-utilities.d.ts +48 -10
  17. package/core/platform/array-utilities.js +160 -124
  18. package/core/platform/array-utilities.js.map +1 -7
  19. package/core/platform/brand.d.ts +14 -0
  20. package/core/platform/brand.js +5 -1
  21. package/core/platform/brand.js.map +1 -7
  22. package/core/platform/date-utilities.js +10 -6
  23. package/core/platform/date-utilities.js.map +1 -7
  24. package/core/platform/dom-utilities.d.ts +3 -1
  25. package/core/platform/dom-utilities.js +94 -83
  26. package/core/platform/dom-utilities.js.map +1 -7
  27. package/core/platform/keyboard-utilities.d.ts +2 -0
  28. package/core/platform/keyboard-utilities.js +15 -24
  29. package/core/platform/keyboard-utilities.js.map +1 -7
  30. package/core/platform/map-utilities.d.ts +4 -0
  31. package/core/platform/map-utilities.js +66 -60
  32. package/core/platform/map-utilities.js.map +1 -7
  33. package/core/platform/number-utilities.js +66 -55
  34. package/core/platform/number-utilities.js.map +1 -7
  35. package/core/platform/platform.d.ts +5 -1
  36. package/core/platform/platform.js +54 -37
  37. package/core/platform/platform.js.map +1 -7
  38. package/core/platform/promise-utilities.d.ts +10 -0
  39. package/core/platform/promise-utilities.js +16 -8
  40. package/core/platform/promise-utilities.js.map +1 -7
  41. package/core/platform/set-utilities.js +20 -17
  42. package/core/platform/set-utilities.js.map +1 -7
  43. package/core/platform/string-utilities.d.ts +32 -1
  44. package/core/platform/string-utilities.js +453 -379
  45. package/core/platform/string-utilities.js.map +1 -7
  46. package/core/platform/typescript-utilities.d.ts +5 -5
  47. package/core/platform/typescript-utilities.js +19 -7
  48. package/core/platform/typescript-utilities.js.map +1 -7
  49. package/generated/protocol.d.ts +2081 -347
  50. package/generated/protocol.js +5 -2230
  51. package/models/cpu_profile/CPUProfileDataModel.d.ts +77 -0
  52. package/models/cpu_profile/CPUProfileDataModel.js +492 -359
  53. package/models/cpu_profile/CPUProfileDataModel.js.map +1 -7
  54. package/models/cpu_profile/ProfileTreeModel.d.ts +29 -0
  55. package/models/cpu_profile/ProfileTreeModel.js +87 -82
  56. package/models/cpu_profile/ProfileTreeModel.js.map +1 -7
  57. package/models/cpu_profile/cpu_profile.d.ts +3 -0
  58. package/models/cpu_profile/cpu_profile.js +7 -7
  59. package/models/cpu_profile/cpu_profile.js.map +1 -7
  60. package/models/trace/EntriesFilter.d.ts +55 -0
  61. package/models/trace/EntriesFilter.js +227 -166
  62. package/models/trace/EntriesFilter.js.map +1 -7
  63. package/models/trace/LegacyTracingModel.js.map +1 -7
  64. package/models/trace/ModelImpl.d.ts +110 -0
  65. package/models/trace/ModelImpl.js +161 -102
  66. package/models/trace/ModelImpl.js.map +1 -7
  67. package/models/trace/Processor.d.ts +36 -0
  68. package/models/trace/Processor.js +197 -163
  69. package/models/trace/Processor.js.map +1 -7
  70. package/models/trace/TracingManager.js.map +1 -7
  71. package/models/trace/extras/FetchNodes.d.ts +46 -0
  72. package/models/trace/extras/FetchNodes.js +132 -91
  73. package/models/trace/extras/FetchNodes.js.map +1 -7
  74. package/models/trace/extras/FilmStrip.d.ts +19 -0
  75. package/models/trace/extras/FilmStrip.js +38 -31
  76. package/models/trace/extras/FilmStrip.js.map +1 -7
  77. package/models/trace/extras/MainThreadActivity.d.ts +2 -0
  78. package/models/trace/extras/MainThreadActivity.js +72 -56
  79. package/models/trace/extras/MainThreadActivity.js.map +1 -7
  80. package/models/trace/extras/Metadata.d.ts +2 -0
  81. package/models/trace/extras/Metadata.js +42 -26
  82. package/models/trace/extras/Metadata.js.map +1 -7
  83. package/models/trace/extras/extras.js.map +1 -7
  84. package/models/trace/handlers/AnimationHandler.d.ts +8 -0
  85. package/models/trace/handlers/AnimationHandler.js +22 -20
  86. package/models/trace/handlers/AnimationHandler.js.map +1 -7
  87. package/models/trace/handlers/AuctionWorkletsHandler.d.ts +8 -0
  88. package/models/trace/handlers/AuctionWorkletsHandler.js +143 -89
  89. package/models/trace/handlers/AuctionWorkletsHandler.js.map +1 -7
  90. package/models/trace/handlers/FramesHandler.d.ts +76 -0
  91. package/models/trace/handlers/FramesHandler.js +424 -355
  92. package/models/trace/handlers/FramesHandler.js.map +1 -7
  93. package/models/trace/handlers/GPUHandler.d.ts +11 -0
  94. package/models/trace/handlers/GPUHandler.js +41 -37
  95. package/models/trace/handlers/GPUHandler.js.map +1 -7
  96. package/models/trace/handlers/InitiatorsHandler.d.ts +10 -0
  97. package/models/trace/handlers/InitiatorsHandler.js +164 -113
  98. package/models/trace/handlers/InitiatorsHandler.js.map +1 -7
  99. package/models/trace/handlers/InvalidationsHandler.d.ts +10 -0
  100. package/models/trace/handlers/InvalidationsHandler.js +101 -79
  101. package/models/trace/handlers/InvalidationsHandler.js.map +1 -7
  102. package/models/trace/handlers/LargestImagePaintHandler.d.ts +5 -0
  103. package/models/trace/handlers/LargestImagePaintHandler.js +32 -12
  104. package/models/trace/handlers/LargestImagePaintHandler.js.map +1 -7
  105. package/models/trace/handlers/LargestTextPaintHandler.d.ts +5 -0
  106. package/models/trace/handlers/LargestTextPaintHandler.js +20 -12
  107. package/models/trace/handlers/LargestTextPaintHandler.js.map +1 -7
  108. package/models/trace/handlers/LayerTreeHandler.d.ts +13 -0
  109. package/models/trace/handlers/LayerTreeHandler.js +96 -70
  110. package/models/trace/handlers/LayerTreeHandler.js.map +1 -7
  111. package/models/trace/handlers/LayoutShiftsHandler.d.ts +44 -0
  112. package/models/trace/handlers/LayoutShiftsHandler.js +304 -227
  113. package/models/trace/handlers/LayoutShiftsHandler.js.map +1 -7
  114. package/models/trace/handlers/MemoryHandler.d.ts +7 -0
  115. package/models/trace/handlers/MemoryHandler.js +14 -11
  116. package/models/trace/handlers/MemoryHandler.js.map +1 -7
  117. package/models/trace/handlers/MetaHandler.d.ts +37 -0
  118. package/models/trace/handlers/MetaHandler.js +314 -226
  119. package/models/trace/handlers/MetaHandler.js.map +1 -7
  120. package/models/trace/handlers/ModelHandlers.d.ts +21 -0
  121. package/models/trace/handlers/ModelHandlers.js +25 -22
  122. package/models/trace/handlers/ModelHandlers.js.map +1 -7
  123. package/models/trace/handlers/NetworkRequestsHandler.d.ts +17 -0
  124. package/models/trace/handlers/NetworkRequestsHandler.js +342 -218
  125. package/models/trace/handlers/NetworkRequestsHandler.js.map +1 -7
  126. package/models/trace/handlers/PageLoadMetricsHandler.d.ts +67 -0
  127. package/models/trace/handlers/PageLoadMetricsHandler.js +357 -284
  128. package/models/trace/handlers/PageLoadMetricsHandler.js.map +1 -7
  129. package/models/trace/handlers/RendererHandler.d.ts +101 -0
  130. package/models/trace/handlers/RendererHandler.js +295 -191
  131. package/models/trace/handlers/RendererHandler.js.map +1 -7
  132. package/models/trace/handlers/SamplesHandler.d.ts +46 -0
  133. package/models/trace/handlers/SamplesHandler.js +195 -158
  134. package/models/trace/handlers/SamplesHandler.js.map +1 -7
  135. package/models/trace/handlers/ScreenshotsHandler.d.ts +7 -0
  136. package/models/trace/handlers/ScreenshotsHandler.js +63 -41
  137. package/models/trace/handlers/ScreenshotsHandler.js.map +1 -7
  138. package/models/trace/handlers/Threads.d.ts +33 -0
  139. package/models/trace/handlers/Threads.js +85 -67
  140. package/models/trace/handlers/Threads.js.map +1 -7
  141. package/models/trace/handlers/UserInteractionsHandler.d.ts +57 -0
  142. package/models/trace/handlers/UserInteractionsHandler.js +240 -141
  143. package/models/trace/handlers/UserInteractionsHandler.js.map +1 -7
  144. package/models/trace/handlers/UserTimingsHandler.d.ts +28 -0
  145. package/models/trace/handlers/UserTimingsHandler.js +91 -80
  146. package/models/trace/handlers/UserTimingsHandler.js.map +1 -7
  147. package/models/trace/handlers/WarningsHandler.d.ts +14 -0
  148. package/models/trace/handlers/WarningsHandler.js +100 -62
  149. package/models/trace/handlers/WarningsHandler.js.map +1 -7
  150. package/models/trace/handlers/WorkersHandler.d.ts +11 -0
  151. package/models/trace/handlers/WorkersHandler.js +40 -38
  152. package/models/trace/handlers/WorkersHandler.js.map +1 -7
  153. package/models/trace/handlers/handlers.d.ts +3 -0
  154. package/models/trace/handlers/handlers.js +7 -4
  155. package/models/trace/handlers/handlers.js.map +1 -7
  156. package/models/trace/handlers/types.d.ts +45 -0
  157. package/models/trace/handlers/types.js +15 -15
  158. package/models/trace/handlers/types.js.map +1 -7
  159. package/models/trace/helpers/SamplesIntegrator.d.ts +49 -0
  160. package/models/trace/helpers/SamplesIntegrator.js +381 -204
  161. package/models/trace/helpers/SamplesIntegrator.js.map +1 -7
  162. package/models/trace/helpers/Timing.d.ts +26 -0
  163. package/models/trace/helpers/Timing.js +131 -110
  164. package/models/trace/helpers/Timing.js.map +1 -7
  165. package/models/trace/helpers/Trace.d.ts +37 -0
  166. package/models/trace/helpers/Trace.js +200 -166
  167. package/models/trace/helpers/Trace.js.map +1 -7
  168. package/models/trace/helpers/TreeHelpers.d.ts +90 -0
  169. package/models/trace/helpers/TreeHelpers.js +203 -100
  170. package/models/trace/helpers/TreeHelpers.js.map +1 -7
  171. package/models/trace/helpers/helpers.d.ts +4 -0
  172. package/models/trace/helpers/helpers.js +8 -5
  173. package/models/trace/helpers/helpers.js.map +1 -7
  174. package/models/trace/root-causes/LayoutShift.d.ts +119 -0
  175. package/models/trace/root-causes/LayoutShift.js +470 -323
  176. package/models/trace/root-causes/LayoutShift.js.map +1 -7
  177. package/models/trace/root-causes/RootCauses.d.ts +14 -0
  178. package/models/trace/root-causes/RootCauses.js +9 -6
  179. package/models/trace/root-causes/RootCauses.js.map +1 -7
  180. package/models/trace/root-causes/root-causes.d.ts +1 -0
  181. package/models/trace/root-causes/root-causes.js +5 -2
  182. package/models/trace/root-causes/root-causes.js.map +1 -7
  183. package/models/trace/trace.d.ts +11 -0
  184. package/models/trace/trace.js +17 -23
  185. package/models/trace/trace.js.map +1 -7
  186. package/models/trace/types/Configuration.d.ts +33 -0
  187. package/models/trace/types/Configuration.js +25 -14
  188. package/models/trace/types/Configuration.js.map +1 -7
  189. package/models/trace/types/File.d.ts +23 -0
  190. package/models/trace/types/File.js +5 -6
  191. package/models/trace/types/File.js.map +1 -7
  192. package/models/trace/types/Timing.d.ts +25 -0
  193. package/models/trace/types/Timing.js +10 -11
  194. package/models/trace/types/Timing.js.map +1 -7
  195. package/models/trace/types/TraceEvents.d.ts +1571 -0
  196. package/models/trace/types/TraceEvents.js +174 -381
  197. package/models/trace/types/TraceEvents.js.map +1 -7
  198. package/models/trace/types/types.d.ts +4 -0
  199. package/models/trace/types/types.js +8 -5
  200. package/models/trace/types/types.js.map +1 -7
  201. package/package.json +1 -1
  202. package/TracingManager.js +0 -0
  203. package/extras/extras.js +0 -0
  204. package/trace.mjs +0 -6980
  205. package/trace.mjs.map +0 -8
@@ -1,220 +1,397 @@
1
- import * as Types from "../types/types.js";
2
- import { millisecondsToMicroseconds } from "./Timing.js";
3
- import { makeProfileCall, mergeEventsInOrder } from "./Trace.js";
1
+ // Copyright 2023 The Chromium Authors. All rights reserved.
2
+ // Use of this source code is governed by a BSD-style license that can be
3
+ // found in the LICENSE file.
4
+ import * as Types from '../types/types.js';
5
+ import { millisecondsToMicroseconds } from './Timing.js';
6
+ import { makeProfileCall, mergeEventsInOrder } from './Trace.js';
7
+ /**
8
+ * This is a helper that integrates CPU profiling data coming in the
9
+ * shape of samples, with trace events. Samples indicate what the JS
10
+ * stack trace looked at a given point in time, but they don't have
11
+ * duration. The SamplesIntegrator task is to make an approximation
12
+ * of what the duration of each JS call was, given the sample data and
13
+ * given the trace events profiled during that time. At the end of its
14
+ * execution, the SamplesIntegrator returns an array of ProfileCalls
15
+ * (under SamplesIntegrator::buildProfileCalls()), which
16
+ * represent JS calls, with a call frame and duration. These calls have
17
+ * the shape of a complete trace events and can be treated as flame
18
+ * chart entries in the timeline.
19
+ *
20
+ * The approach to build the profile calls consists in tracking the
21
+ * current stack as the following events happen (in order):
22
+ * 1. A sample was done.
23
+ * 2. A trace event started.
24
+ * 3. A trace event ended.
25
+ * Depending on the event and on the data that's coming with it the
26
+ * stack is updated by adding or removing JS calls to it and updating
27
+ * the duration of the calls in the tracking stack.
28
+ *
29
+ * note: Although this approach has been implemented since long ago, and
30
+ * is relatively efficent (adds a complexity over the trace parsing of
31
+ * O(n) where n is the number of samples) it has proven to be faulty.
32
+ * It might be worthwhile experimenting with improvements or with a
33
+ * completely different approach. Improving the approach is tracked in
34
+ * crbug.com/1417439
35
+ */
4
36
  export class SamplesIntegrator {
5
- #constructedProfileCalls = [];
6
- #currentJSStack = [];
7
- #processId;
8
- #threadId;
9
- #lockedJsStackDepth = [];
10
- #fakeJSInvocation = false;
11
- #profileModel;
12
- #nodeForGC = /* @__PURE__ */ new Map();
13
- #engineConfig;
14
- constructor(profileModel, pid, tid, configuration) {
15
- this.#profileModel = profileModel;
16
- this.#threadId = tid;
17
- this.#processId = pid;
18
- this.#engineConfig = configuration || Types.Configuration.DEFAULT;
19
- }
20
- buildProfileCalls(traceEvents) {
21
- const mergedEvents = mergeEventsInOrder(traceEvents, this.callsFromProfileSamples());
22
- const stack = [];
23
- for (let i = 0; i < mergedEvents.length; i++) {
24
- const event = mergedEvents[i];
25
- if (event.ph === Types.TraceEvents.Phase.INSTANT) {
26
- continue;
27
- }
28
- if (stack.length === 0) {
29
- if (Types.TraceEvents.isProfileCall(event)) {
30
- this.#onProfileCall(event);
31
- continue;
32
- }
33
- stack.push(event);
34
- this.#onTraceEventStart(event);
35
- continue;
36
- }
37
- const parentEvent = stack.at(-1);
38
- if (parentEvent === void 0) {
39
- continue;
40
- }
41
- const begin = event.ts;
42
- const parentBegin = parentEvent.ts;
43
- const parentDuration = parentEvent.dur || 0;
44
- const parentEnd = parentBegin + parentDuration;
45
- const startsAfterParent = begin >= parentEnd;
46
- if (startsAfterParent) {
47
- this.#onTraceEventEnd(parentEvent);
48
- stack.pop();
49
- i--;
50
- continue;
51
- }
52
- if (Types.TraceEvents.isProfileCall(event)) {
53
- this.#onProfileCall(event, parentEvent);
54
- continue;
55
- }
56
- this.#onTraceEventStart(event);
57
- stack.push(event);
37
+ /**
38
+ * The result of runing the samples integrator. Holds the JS calls
39
+ * with their approximated duration after integrating samples into the
40
+ * trace event tree.
41
+ */
42
+ #constructedProfileCalls = [];
43
+ /**
44
+ * tracks the state of the JS stack at each point in time to update
45
+ * the profile call durations as new events arrive. This doesn't only
46
+ * happen with new profile calls (in which case we would compare the
47
+ * stack in them) but also with trace events (in which case we would
48
+ * update the duration of the events we are tracking at the moment).
49
+ */
50
+ #currentJSStack = [];
51
+ /**
52
+ * Process holding the CPU profile and trace events.
53
+ */
54
+ #processId;
55
+ /**
56
+ * Thread holding the CPU profile and trace events.
57
+ */
58
+ #threadId;
59
+ /**
60
+ * Tracks the depth of the JS stack at the moment a trace event starts
61
+ * or ends. It is assumed that for the duration of a trace event, the
62
+ * JS stack's depth cannot decrease, since JS calls that started
63
+ * before a trace event cannot end during the trace event. So as trace
64
+ * events arrive, we store the "locked" amount of JS frames that were
65
+ * in the stack before the event came.
66
+ */
67
+ #lockedJsStackDepth = [];
68
+ /**
69
+ * Used to keep track when samples should be integrated even if they
70
+ * are not children of invocation trace events. This is useful in
71
+ * cases where we can be missing the start of JS invocation events if
72
+ * we start tracing half-way through.
73
+ */
74
+ #fakeJSInvocation = false;
75
+ /**
76
+ * The parsed CPU profile, holding the tree hierarchy of JS frames and
77
+ * the sample data.
78
+ */
79
+ #profileModel;
80
+ /**
81
+ * Because GC nodes don't have a stack, we artificially add a stack to
82
+ * them which corresponds to that of the previous sample. This map
83
+ * tracks which node is used for the stack of a GC call.
84
+ * Note that GC samples are not shown in the flamechart, however they
85
+ * are used during the construction of for profile calls, as we can
86
+ * infer information about the duration of the executed code when a
87
+ * GC node is sampled.
88
+ */
89
+ #nodeForGC = new Map();
90
+ #engineConfig;
91
+ constructor(profileModel, pid, tid, configuration) {
92
+ this.#profileModel = profileModel;
93
+ this.#threadId = tid;
94
+ this.#processId = pid;
95
+ this.#engineConfig = configuration || Types.Configuration.DEFAULT;
58
96
  }
59
- while (stack.length) {
60
- const last = stack.pop();
61
- if (last) {
62
- this.#onTraceEventEnd(last);
63
- }
64
- }
65
- return this.#constructedProfileCalls;
66
- }
67
- #onTraceEventStart(event) {
68
- if (event.name === Types.TraceEvents.KnownEventName.RunMicrotasks || event.name === Types.TraceEvents.KnownEventName.RunTask) {
69
- this.#lockedJsStackDepth = [];
70
- this.#truncateJSStack(0, event.ts);
71
- this.#fakeJSInvocation = false;
72
- }
73
- if (this.#fakeJSInvocation) {
74
- this.#truncateJSStack(this.#lockedJsStackDepth.pop() || 0, event.ts);
75
- this.#fakeJSInvocation = false;
76
- }
77
- this.#extractStackTrace(event);
78
- this.#lockedJsStackDepth.push(this.#currentJSStack.length);
79
- }
80
- #onProfileCall(event, parent) {
81
- if (parent && Types.TraceEvents.isJSInvocationEvent(parent) || this.#fakeJSInvocation) {
82
- this.#extractStackTrace(event);
83
- } else if (Types.TraceEvents.isProfileCall(event) && this.#currentJSStack.length === 0) {
84
- this.#fakeJSInvocation = true;
85
- const stackDepthBefore = this.#currentJSStack.length;
86
- this.#extractStackTrace(event);
87
- this.#lockedJsStackDepth.push(stackDepthBefore);
88
- }
89
- }
90
- #onTraceEventEnd(event) {
91
- const endTime = Types.Timing.MicroSeconds(event.ts + (event.dur || 0));
92
- this.#truncateJSStack(this.#lockedJsStackDepth.pop() || 0, endTime);
93
- }
94
- callsFromProfileSamples() {
95
- const samples = this.#profileModel.samples;
96
- const timestamps = this.#profileModel.timestamps;
97
- if (!samples) {
98
- return [];
99
- }
100
- const calls = [];
101
- let prevNode;
102
- for (let i = 0; i < samples.length; i++) {
103
- const node = this.#profileModel.nodeByIndex(i);
104
- const timestamp = millisecondsToMicroseconds(Types.Timing.MilliSeconds(timestamps[i]));
105
- if (!node) {
106
- continue;
107
- }
108
- const call = makeProfileCall(node, timestamp, this.#processId, this.#threadId);
109
- calls.push(call);
110
- if (node.id === this.#profileModel.gcNode?.id && prevNode) {
111
- this.#nodeForGC.set(call, prevNode);
112
- continue;
113
- }
114
- prevNode = node;
115
- }
116
- return calls;
117
- }
118
- #getStackTraceFromProfileCall(profileCall) {
119
- let node = this.#profileModel.nodeById(profileCall.nodeId);
120
- const isGarbageCollection = node?.id === this.#profileModel.gcNode?.id;
121
- if (isGarbageCollection) {
122
- node = this.#nodeForGC.get(profileCall) || null;
97
+ buildProfileCalls(traceEvents) {
98
+ const mergedEvents = mergeEventsInOrder(traceEvents, this.callsFromProfileSamples());
99
+ const stack = [];
100
+ for (let i = 0; i < mergedEvents.length; i++) {
101
+ const event = mergedEvents[i];
102
+ // Because instant trace events have no duration, they don't provide
103
+ // useful information for possible changes in the duration of calls
104
+ // in the JS stack.
105
+ if (event.ph === "I" /* Types.TraceEvents.Phase.INSTANT */) {
106
+ continue;
107
+ }
108
+ if (stack.length === 0) {
109
+ if (Types.TraceEvents.isProfileCall(event)) {
110
+ this.#onProfileCall(event);
111
+ continue;
112
+ }
113
+ stack.push(event);
114
+ this.#onTraceEventStart(event);
115
+ continue;
116
+ }
117
+ const parentEvent = stack.at(-1);
118
+ if (parentEvent === undefined) {
119
+ continue;
120
+ }
121
+ const begin = event.ts;
122
+ const parentBegin = parentEvent.ts;
123
+ const parentDuration = parentEvent.dur || 0;
124
+ const parentEnd = parentBegin + parentDuration;
125
+ const startsAfterParent = begin >= parentEnd;
126
+ if (startsAfterParent) {
127
+ this.#onTraceEventEnd(parentEvent);
128
+ stack.pop();
129
+ i--;
130
+ continue;
131
+ }
132
+ if (Types.TraceEvents.isProfileCall(event)) {
133
+ this.#onProfileCall(event, parentEvent);
134
+ continue;
135
+ }
136
+ this.#onTraceEventStart(event);
137
+ stack.push(event);
138
+ }
139
+ while (stack.length) {
140
+ const last = stack.pop();
141
+ if (last) {
142
+ this.#onTraceEventEnd(last);
143
+ }
144
+ }
145
+ return this.#constructedProfileCalls;
123
146
  }
124
- if (!node) {
125
- return [];
147
+ #onTraceEventStart(event) {
148
+ // Top level events cannot be nested into JS frames so we reset
149
+ // the stack when we find one.
150
+ if (event.name === "RunMicrotasks" /* Types.TraceEvents.KnownEventName.RunMicrotasks */ ||
151
+ event.name === "RunTask" /* Types.TraceEvents.KnownEventName.RunTask */) {
152
+ this.#lockedJsStackDepth = [];
153
+ this.#truncateJSStack(0, event.ts);
154
+ this.#fakeJSInvocation = false;
155
+ }
156
+ if (this.#fakeJSInvocation) {
157
+ this.#truncateJSStack(this.#lockedJsStackDepth.pop() || 0, event.ts);
158
+ this.#fakeJSInvocation = false;
159
+ }
160
+ this.#extractStackTrace(event);
161
+ // Keep track of the call frames in the stack before the event
162
+ // happened. For the duration of this event, these frames cannot
163
+ // change (none can be terminated before this event finishes).
164
+ //
165
+ // Also, every frame that is opened after this event, is considered
166
+ // to be a descendant of the event. So once the event finishes, the
167
+ // frames that were opened after it, need to be closed (see
168
+ // onEndEvent).
169
+ //
170
+ // TODO(crbug.com/1417439):
171
+ // The assumption that every frame opened after an event is a
172
+ // descendant of the event is incorrect. For example, a JS call that
173
+ // parents a trace event might have been sampled after the event was
174
+ // dispatched. In this case the JS call would be discarded if this
175
+ // event isn't an invocation event, otherwise the call will be
176
+ // considered a child of the event. In both cases, the result would
177
+ // be incorrect.
178
+ this.#lockedJsStackDepth.push(this.#currentJSStack.length);
126
179
  }
127
- const callFrames = new Array(node.depth + 1 + Number(isGarbageCollection));
128
- let i = callFrames.length - 1;
129
- if (isGarbageCollection) {
130
- callFrames[i--] = profileCall;
180
+ #onProfileCall(event, parent) {
181
+ if ((parent && Types.TraceEvents.isJSInvocationEvent(parent)) || this.#fakeJSInvocation) {
182
+ this.#extractStackTrace(event);
183
+ }
184
+ else if (Types.TraceEvents.isProfileCall(event) && this.#currentJSStack.length === 0) {
185
+ // Force JS Samples to show up even if we are not inside a JS
186
+ // invocation event, because we can be missing the start of JS
187
+ // invocation events if we start tracing half-way through. Pretend
188
+ // we have a top-level JS invocation event.
189
+ this.#fakeJSInvocation = true;
190
+ const stackDepthBefore = this.#currentJSStack.length;
191
+ this.#extractStackTrace(event);
192
+ this.#lockedJsStackDepth.push(stackDepthBefore);
193
+ }
131
194
  }
132
- while (node) {
133
- callFrames[i--] = makeProfileCall(node, profileCall.ts, this.#processId, this.#threadId);
134
- node = node.parent;
195
+ #onTraceEventEnd(event) {
196
+ // Because the event has ended, any frames that happened after
197
+ // this event are terminated. Frames that are ancestors to this
198
+ // event are extended to cover its ending.
199
+ const endTime = Types.Timing.MicroSeconds(event.ts + (event.dur || 0));
200
+ this.#truncateJSStack(this.#lockedJsStackDepth.pop() || 0, endTime);
135
201
  }
136
- return callFrames;
137
- }
138
- #extractStackTrace(event) {
139
- const stackTrace = Types.TraceEvents.isProfileCall(event) ? this.#getStackTraceFromProfileCall(event) : this.#currentJSStack;
140
- SamplesIntegrator.filterStackFrames(stackTrace, this.#engineConfig);
141
- const endTime = event.ts + (event.dur || 0);
142
- const minFrames = Math.min(stackTrace.length, this.#currentJSStack.length);
143
- let i;
144
- for (i = this.#lockedJsStackDepth.at(-1) || 0; i < minFrames; ++i) {
145
- const newFrame = stackTrace[i].callFrame;
146
- const oldFrame = this.#currentJSStack[i].callFrame;
147
- if (!SamplesIntegrator.framesAreEqual(newFrame, oldFrame)) {
148
- break;
149
- }
150
- this.#currentJSStack[i].dur = Types.Timing.MicroSeconds(Math.max(this.#currentJSStack[i].dur || 0, endTime - this.#currentJSStack[i].ts));
202
+ /**
203
+ * Builds the initial calls with no duration from samples. Their
204
+ * purpose is to be merged with the trace event array being parsed so
205
+ * that they can be traversed in order with them and their duration
206
+ * can be updated as the SampleIntegrator callbacks are invoked.
207
+ */
208
+ callsFromProfileSamples() {
209
+ const samples = this.#profileModel.samples;
210
+ const timestamps = this.#profileModel.timestamps;
211
+ if (!samples) {
212
+ return [];
213
+ }
214
+ const calls = [];
215
+ let prevNode;
216
+ for (let i = 0; i < samples.length; i++) {
217
+ const node = this.#profileModel.nodeByIndex(i);
218
+ const timestamp = millisecondsToMicroseconds(Types.Timing.MilliSeconds(timestamps[i]));
219
+ if (!node) {
220
+ continue;
221
+ }
222
+ const call = makeProfileCall(node, timestamp, this.#processId, this.#threadId);
223
+ calls.push(call);
224
+ if (node.id === this.#profileModel.gcNode?.id && prevNode) {
225
+ // GC samples have no stack, so we just put GC node on top of the
226
+ // last recorded sample. Cache the previous sample for future
227
+ // reference.
228
+ this.#nodeForGC.set(call, prevNode);
229
+ continue;
230
+ }
231
+ prevNode = node;
232
+ }
233
+ return calls;
151
234
  }
152
- this.#truncateJSStack(i, event.ts);
153
- for (; i < stackTrace.length; ++i) {
154
- const call = stackTrace[i];
155
- if (call.nodeId === this.#profileModel.programNode?.id || call.nodeId === this.#profileModel.root?.id || call.nodeId === this.#profileModel.idleNode?.id || call.nodeId === this.#profileModel.gcNode?.id) {
156
- continue;
157
- }
158
- this.#currentJSStack.push(call);
159
- this.#constructedProfileCalls.push(call);
235
+ #getStackTraceFromProfileCall(profileCall) {
236
+ let node = this.#profileModel.nodeById(profileCall.nodeId);
237
+ const isGarbageCollection = node?.id === this.#profileModel.gcNode?.id;
238
+ if (isGarbageCollection) {
239
+ // Because GC don't have a stack, we use the stack of the previous
240
+ // sample.
241
+ node = this.#nodeForGC.get(profileCall) || null;
242
+ }
243
+ if (!node) {
244
+ return [];
245
+ }
246
+ // `node.depth` is 0 based, so to set the size of the array we need
247
+ // to add 1 to its value.
248
+ const callFrames = new Array(node.depth + 1 + Number(isGarbageCollection));
249
+ // Add the stack trace in reverse order (bottom first).
250
+ let i = callFrames.length - 1;
251
+ if (isGarbageCollection) {
252
+ // Place the garbage collection call frame on top of the stack.
253
+ callFrames[i--] = profileCall;
254
+ }
255
+ while (node) {
256
+ callFrames[i--] = makeProfileCall(node, profileCall.ts, this.#processId, this.#threadId);
257
+ node = node.parent;
258
+ }
259
+ return callFrames;
160
260
  }
161
- }
162
- #truncateJSStack(depth, time) {
163
- if (this.#lockedJsStackDepth.length) {
164
- const lockedDepth = this.#lockedJsStackDepth.at(-1);
165
- if (lockedDepth && depth < lockedDepth) {
166
- console.error(`Child stack is shallower (${depth}) than the parent stack (${lockedDepth}) at ${time}`);
167
- depth = lockedDepth;
168
- }
261
+ /**
262
+ * Update tracked stack using this event's call stack.
263
+ */
264
+ #extractStackTrace(event) {
265
+ const stackTrace = Types.TraceEvents.isProfileCall(event) ? this.#getStackTraceFromProfileCall(event) : this.#currentJSStack;
266
+ SamplesIntegrator.filterStackFrames(stackTrace, this.#engineConfig);
267
+ const endTime = event.ts + (event.dur || 0);
268
+ const minFrames = Math.min(stackTrace.length, this.#currentJSStack.length);
269
+ let i;
270
+ // Merge a sample's stack frames with the stack frames we have
271
+ // so far if we detect they are equivalent.
272
+ // Graphically
273
+ // This:
274
+ // Current stack trace Sample
275
+ // [-------A------] [A]
276
+ // [-------B------] [B]
277
+ // [-------C------] [C]
278
+ // ^ t = x1 ^ t = x2
279
+ // Becomes this:
280
+ // New stack trace after merge
281
+ // [--------A-------]
282
+ // [--------B-------]
283
+ // [--------C-------]
284
+ // ^ t = x2
285
+ for (i = this.#lockedJsStackDepth.at(-1) || 0; i < minFrames; ++i) {
286
+ const newFrame = stackTrace[i].callFrame;
287
+ const oldFrame = this.#currentJSStack[i].callFrame;
288
+ if (!SamplesIntegrator.framesAreEqual(newFrame, oldFrame)) {
289
+ break;
290
+ }
291
+ // Scoot the right edge of this callFrame to the right
292
+ this.#currentJSStack[i].dur =
293
+ Types.Timing.MicroSeconds(Math.max(this.#currentJSStack[i].dur || 0, endTime - this.#currentJSStack[i].ts));
294
+ }
295
+ // If there are call frames in the sample that differ with the stack
296
+ // we have, update the stack, but keeping the common frames in place
297
+ // Graphically
298
+ // This:
299
+ // Current stack trace Sample
300
+ // [-------A------] [A]
301
+ // [-------B------] [B]
302
+ // [-------C------] [C]
303
+ // [-------D------] [E]
304
+ // ^ t = x1 ^ t = x2
305
+ // Becomes this:
306
+ // New stack trace after merge
307
+ // [--------A-------]
308
+ // [--------B-------]
309
+ // [--------C-------]
310
+ // [E]
311
+ // ^ t = x2
312
+ this.#truncateJSStack(i, event.ts);
313
+ for (; i < stackTrace.length; ++i) {
314
+ const call = stackTrace[i];
315
+ if (call.nodeId === this.#profileModel.programNode?.id || call.nodeId === this.#profileModel.root?.id ||
316
+ call.nodeId === this.#profileModel.idleNode?.id || call.nodeId === this.#profileModel.gcNode?.id) {
317
+ // Skip (root), (program) and (idle) frames, since this are not
318
+ // relevant for web profiling and we don't want to show them in
319
+ // the timeline.
320
+ continue;
321
+ }
322
+ this.#currentJSStack.push(call);
323
+ this.#constructedProfileCalls.push(call);
324
+ }
169
325
  }
170
- if (this.#currentJSStack.length < depth) {
171
- console.error(`Trying to truncate higher than the current stack size at ${time}`);
172
- depth = this.#currentJSStack.length;
326
+ /**
327
+ * When a call stack that differs from the one we are tracking has
328
+ * been detected in the samples, the latter is "truncated" by
329
+ * setting the ending time of its call frames and removing the top
330
+ * call frames that aren't shared with the new call stack. This way,
331
+ * we can update the tracked stack with the new call frames on top.
332
+ * @param depth the amount of call frames from bottom to top that
333
+ * should be kept in the tracking stack trace. AKA amount of shared
334
+ * call frames between two stacks.
335
+ * @param time the new end of the call frames in the stack.
336
+ */
337
+ #truncateJSStack(depth, time) {
338
+ if (this.#lockedJsStackDepth.length) {
339
+ const lockedDepth = this.#lockedJsStackDepth.at(-1);
340
+ if (lockedDepth && depth < lockedDepth) {
341
+ console.error(`Child stack is shallower (${depth}) than the parent stack (${lockedDepth}) at ${time}`);
342
+ depth = lockedDepth;
343
+ }
344
+ }
345
+ if (this.#currentJSStack.length < depth) {
346
+ console.error(`Trying to truncate higher than the current stack size at ${time}`);
347
+ depth = this.#currentJSStack.length;
348
+ }
349
+ for (let k = 0; k < this.#currentJSStack.length; ++k) {
350
+ this.#currentJSStack[k].dur = Types.Timing.MicroSeconds(Math.max(time - this.#currentJSStack[k].ts, 0));
351
+ }
352
+ this.#currentJSStack.length = depth;
173
353
  }
174
- for (let k = 0; k < this.#currentJSStack.length; ++k) {
175
- this.#currentJSStack[k].dur = Types.Timing.MicroSeconds(Math.max(time - this.#currentJSStack[k].ts, 0));
354
+ static framesAreEqual(frame1, frame2) {
355
+ return frame1.scriptId === frame2.scriptId && frame1.functionName === frame2.functionName &&
356
+ frame1.lineNumber === frame2.lineNumber;
176
357
  }
177
- this.#currentJSStack.length = depth;
178
- }
179
- static framesAreEqual(frame1, frame2) {
180
- return frame1.scriptId === frame2.scriptId && frame1.functionName === frame2.functionName && frame1.lineNumber === frame2.lineNumber;
181
- }
182
- static showNativeName(name, runtimeCallStatsEnabled) {
183
- return runtimeCallStatsEnabled && Boolean(SamplesIntegrator.nativeGroup(name));
184
- }
185
- static nativeGroup(nativeName) {
186
- if (nativeName.startsWith("Parse")) {
187
- return "Parse";
358
+ static showNativeName(name, runtimeCallStatsEnabled) {
359
+ return runtimeCallStatsEnabled && Boolean(SamplesIntegrator.nativeGroup(name));
188
360
  }
189
- if (nativeName.startsWith("Compile") || nativeName.startsWith("Recompile")) {
190
- return "Compile";
361
+ static nativeGroup(nativeName) {
362
+ if (nativeName.startsWith('Parse')) {
363
+ return 'Parse';
364
+ }
365
+ if (nativeName.startsWith('Compile') || nativeName.startsWith('Recompile')) {
366
+ return 'Compile';
367
+ }
368
+ return null;
191
369
  }
192
- return null;
193
- }
194
- static isNativeRuntimeFrame(frame) {
195
- return frame.url === "native V8Runtime";
196
- }
197
- static filterStackFrames(stack, engineConfig) {
198
- const showAllEvents = engineConfig.experiments.timelineShowAllEvents;
199
- if (showAllEvents) {
200
- return;
370
+ static isNativeRuntimeFrame(frame) {
371
+ return frame.url === 'native V8Runtime';
201
372
  }
202
- let previousNativeFrameName = null;
203
- let j = 0;
204
- for (let i = 0; i < stack.length; ++i) {
205
- const frame = stack[i].callFrame;
206
- const nativeRuntimeFrame = SamplesIntegrator.isNativeRuntimeFrame(frame);
207
- if (nativeRuntimeFrame && !SamplesIntegrator.showNativeName(frame.functionName, engineConfig.experiments.timelineV8RuntimeCallStats)) {
208
- continue;
209
- }
210
- const nativeFrameName = nativeRuntimeFrame ? SamplesIntegrator.nativeGroup(frame.functionName) : null;
211
- if (previousNativeFrameName && previousNativeFrameName === nativeFrameName) {
212
- continue;
213
- }
214
- previousNativeFrameName = nativeFrameName;
215
- stack[j++] = stack[i];
373
+ static filterStackFrames(stack, engineConfig) {
374
+ const showAllEvents = engineConfig.experiments.timelineShowAllEvents;
375
+ if (showAllEvents) {
376
+ return;
377
+ }
378
+ let previousNativeFrameName = null;
379
+ let j = 0;
380
+ for (let i = 0; i < stack.length; ++i) {
381
+ const frame = stack[i].callFrame;
382
+ const nativeRuntimeFrame = SamplesIntegrator.isNativeRuntimeFrame(frame);
383
+ if (nativeRuntimeFrame &&
384
+ !SamplesIntegrator.showNativeName(frame.functionName, engineConfig.experiments.timelineV8RuntimeCallStats)) {
385
+ continue;
386
+ }
387
+ const nativeFrameName = nativeRuntimeFrame ? SamplesIntegrator.nativeGroup(frame.functionName) : null;
388
+ if (previousNativeFrameName && previousNativeFrameName === nativeFrameName) {
389
+ continue;
390
+ }
391
+ previousNativeFrameName = nativeFrameName;
392
+ stack[j++] = stack[i];
393
+ }
394
+ stack.length = j;
216
395
  }
217
- stack.length = j;
218
- }
219
396
  }
220
- //# sourceMappingURL=SamplesIntegrator.js.map
397
+ //# sourceMappingURL=SamplesIntegrator.js.map