@paulirish/trace_engine 0.0.9 → 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 (292) hide show
  1. package/analyze-trace.mjs +1 -1
  2. package/core/platform/DevToolsPath.d.ts +23 -0
  3. package/core/platform/DevToolsPath.js +7 -0
  4. package/core/platform/DevToolsPath.js.map +1 -0
  5. package/core/platform/MimeType.d.ts +27 -0
  6. package/core/platform/MimeType.js +137 -0
  7. package/core/platform/MimeType.js.map +1 -0
  8. package/core/platform/Timing.d.ts +7 -0
  9. package/core/platform/Timing.js +13 -0
  10. package/core/platform/Timing.js.map +1 -0
  11. package/core/platform/UIString.d.ts +3 -0
  12. package/core/platform/UIString.js +5 -0
  13. package/core/platform/UIString.js.map +1 -0
  14. package/core/platform/UserVisibleError.d.ts +12 -0
  15. package/core/platform/UserVisibleError.js +23 -0
  16. package/core/platform/UserVisibleError.js.map +1 -0
  17. package/core/platform/array-utilities.d.ts +66 -0
  18. package/core/platform/array-utilities.js +199 -0
  19. package/core/platform/array-utilities.js.map +1 -0
  20. package/core/platform/brand.d.ts +14 -0
  21. package/core/platform/brand.js +5 -0
  22. package/core/platform/brand.js.map +1 -0
  23. package/core/platform/bundle-tsconfig.json +1 -0
  24. package/core/platform/date-utilities.d.ts +2 -0
  25. package/core/platform/date-utilities.js +14 -0
  26. package/core/platform/date-utilities.js.map +1 -0
  27. package/core/platform/dcheck-tsconfig.json +8 -0
  28. package/core/platform/dcheck.d.ts +4 -0
  29. package/core/platform/dcheck.js +5 -0
  30. package/core/platform/devtools_entrypoint-bundle-tsconfig-tsconfig.json +40 -0
  31. package/core/platform/devtools_entrypoint-bundle-typescript-tsconfig.json +44 -0
  32. package/core/platform/dom-utilities.d.ts +8 -0
  33. package/core/platform/dom-utilities.js +109 -0
  34. package/core/platform/dom-utilities.js.map +1 -0
  35. package/core/platform/keyboard-utilities.d.ts +17 -0
  36. package/core/platform/keyboard-utilities.js +22 -0
  37. package/core/platform/keyboard-utilities.js.map +1 -0
  38. package/core/platform/map-utilities.d.ts +18 -0
  39. package/core/platform/map-utilities.js +76 -0
  40. package/core/platform/map-utilities.js.map +1 -0
  41. package/core/platform/number-utilities.d.ts +15 -0
  42. package/core/platform/number-utilities.js +82 -0
  43. package/core/platform/number-utilities.js.map +1 -0
  44. package/core/platform/platform-tsconfig.json +59 -0
  45. package/core/platform/platform.d.ts +19 -0
  46. package/core/platform/platform.js +54 -0
  47. package/core/platform/platform.js.compressed +0 -0
  48. package/core/platform/platform.js.hash +1 -0
  49. package/core/platform/platform.js.map +1 -0
  50. package/core/platform/platform.prebundle.d.ts +15 -0
  51. package/core/platform/platform.prebundle.js +50 -0
  52. package/core/platform/platform.prebundle.js.map +1 -0
  53. package/core/platform/platform.prebundle.ts +64 -0
  54. package/core/platform/promise-utilities.d.ts +10 -0
  55. package/core/platform/promise-utilities.js +18 -0
  56. package/core/platform/promise-utilities.js.map +1 -0
  57. package/core/platform/set-utilities.d.ts +2 -0
  58. package/core/platform/set-utilities.js +23 -0
  59. package/core/platform/set-utilities.js.map +1 -0
  60. package/core/platform/string-utilities.d.ts +71 -0
  61. package/core/platform/string-utilities.js +513 -0
  62. package/core/platform/string-utilities.js.map +1 -0
  63. package/core/platform/typescript-utilities.d.ts +56 -0
  64. package/core/platform/typescript-utilities.js +25 -0
  65. package/core/platform/typescript-utilities.js.map +1 -0
  66. package/generated/protocol.d.ts +17923 -0
  67. package/generated/protocol.js +5 -0
  68. package/models/cpu_profile/CPUProfileDataModel.d.ts +77 -0
  69. package/models/cpu_profile/CPUProfileDataModel.js +508 -0
  70. package/models/cpu_profile/CPUProfileDataModel.js.map +1 -0
  71. package/models/cpu_profile/ProfileTreeModel.d.ts +29 -0
  72. package/models/cpu_profile/ProfileTreeModel.js +95 -0
  73. package/models/cpu_profile/ProfileTreeModel.js.map +1 -0
  74. package/models/cpu_profile/bundle-tsconfig.json +1 -0
  75. package/models/cpu_profile/cpu_profile-tsconfig.json +51 -0
  76. package/models/cpu_profile/cpu_profile.d.ts +3 -0
  77. package/models/cpu_profile/cpu_profile.js +7 -0
  78. package/models/cpu_profile/cpu_profile.js.map +1 -0
  79. package/models/cpu_profile/devtools_entrypoint-bundle-typescript-tsconfig.json +44 -0
  80. package/models/trace/EntriesFilter.d.ts +55 -0
  81. package/models/trace/EntriesFilter.js +243 -0
  82. package/models/trace/EntriesFilter.js.map +1 -0
  83. package/models/trace/LegacyTracingModel.d.ts +1 -0
  84. package/models/trace/LegacyTracingModel.js +1 -0
  85. package/models/trace/LegacyTracingModel.js.map +1 -0
  86. package/models/trace/ModelImpl.d.ts +110 -0
  87. package/models/trace/ModelImpl.js +175 -0
  88. package/models/trace/ModelImpl.js.map +1 -0
  89. package/models/trace/Processor.d.ts +36 -0
  90. package/models/trace/Processor.js +213 -0
  91. package/models/trace/Processor.js.map +1 -0
  92. package/models/trace/SDKServices.js +104 -0
  93. package/models/trace/SDKServices.js.map +7 -0
  94. package/models/trace/TraceProcessor.js +133 -0
  95. package/models/trace/TraceProcessor.js.map +7 -0
  96. package/models/trace/TracingManager.d.ts +1 -0
  97. package/models/trace/TracingManager.js +1 -0
  98. package/models/trace/TracingManager.js.map +1 -0
  99. package/models/trace/TreeManipulator.js +85 -0
  100. package/models/trace/TreeManipulator.js.map +7 -0
  101. package/models/trace/bundle-tsconfig.json +1 -0
  102. package/models/trace/devtools_entrypoint-bundle-typescript-tsconfig.json +44 -0
  103. package/models/trace/devtools_entrypoint-legacy-typescript-tsconfig.json +43 -0
  104. package/models/trace/extras/FetchNodes.d.ts +46 -0
  105. package/models/trace/extras/FetchNodes.js +145 -0
  106. package/models/trace/extras/FetchNodes.js.map +1 -0
  107. package/models/trace/extras/FilmStrip.d.ts +19 -0
  108. package/models/trace/extras/FilmStrip.js +44 -0
  109. package/models/trace/extras/FilmStrip.js.map +1 -0
  110. package/models/trace/extras/MainThreadActivity.d.ts +2 -0
  111. package/models/trace/extras/MainThreadActivity.js +77 -0
  112. package/models/trace/extras/MainThreadActivity.js.map +1 -0
  113. package/models/trace/extras/Metadata.d.ts +2 -0
  114. package/models/trace/extras/Metadata.js +44 -0
  115. package/models/trace/extras/Metadata.js.map +1 -0
  116. package/models/trace/extras/bundle-tsconfig.json +1 -0
  117. package/models/trace/extras/devtools_entrypoint-bundle-typescript-tsconfig.json +44 -0
  118. package/models/trace/extras/extras-tsconfig.json +59 -0
  119. package/models/trace/extras/extras.d.ts +1 -0
  120. package/models/trace/extras/extras.js +1 -0
  121. package/models/trace/extras/extras.js.map +1 -0
  122. package/models/trace/frames/TimelineFrameModel.js +392 -0
  123. package/models/trace/frames/TimelineFrameModel.js.map +7 -0
  124. package/models/trace/frames/bundle-tsconfig.json +1 -0
  125. package/models/trace/frames/devtools_entrypoint-bundle-typescript-tsconfig.json +43 -0
  126. package/models/trace/frames/frames-tsconfig.json +58 -0
  127. package/models/trace/frames/frames.js +5 -0
  128. package/models/trace/frames/frames.js.map +7 -0
  129. package/models/trace/handlers/AnimationHandler.d.ts +8 -0
  130. package/models/trace/handlers/AnimationHandler.js +32 -0
  131. package/models/trace/handlers/AnimationHandler.js.map +1 -0
  132. package/models/trace/handlers/AuctionWorkletsHandler.d.ts +8 -0
  133. package/models/trace/handlers/AuctionWorkletsHandler.js +160 -0
  134. package/models/trace/handlers/AuctionWorkletsHandler.js.map +1 -0
  135. package/models/trace/handlers/FramesHandler.d.ts +76 -0
  136. package/models/trace/handlers/FramesHandler.js +457 -0
  137. package/models/trace/handlers/FramesHandler.js.map +1 -0
  138. package/models/trace/handlers/GPUHandler.d.ts +11 -0
  139. package/models/trace/handlers/GPUHandler.js +54 -0
  140. package/models/trace/handlers/GPUHandler.js.map +1 -0
  141. package/models/trace/handlers/InitiatorsHandler.d.ts +10 -0
  142. package/models/trace/handlers/InitiatorsHandler.js +184 -0
  143. package/models/trace/handlers/InitiatorsHandler.js.map +1 -0
  144. package/models/trace/handlers/InvalidationsHandler.d.ts +10 -0
  145. package/models/trace/handlers/InvalidationsHandler.js +120 -0
  146. package/models/trace/handlers/InvalidationsHandler.js.map +1 -0
  147. package/models/trace/handlers/LargestImagePaintHandler.d.ts +5 -0
  148. package/models/trace/handlers/LargestImagePaintHandler.js +38 -0
  149. package/models/trace/handlers/LargestImagePaintHandler.js.map +1 -0
  150. package/models/trace/handlers/LargestTextPaintHandler.d.ts +5 -0
  151. package/models/trace/handlers/LargestTextPaintHandler.js +26 -0
  152. package/models/trace/handlers/LargestTextPaintHandler.js.map +1 -0
  153. package/models/trace/handlers/LayerTreeHandler.d.ts +13 -0
  154. package/models/trace/handlers/LayerTreeHandler.js +116 -0
  155. package/models/trace/handlers/LayerTreeHandler.js.map +1 -0
  156. package/models/trace/handlers/LayoutShiftsHandler.d.ts +44 -0
  157. package/models/trace/handlers/LayoutShiftsHandler.js +347 -0
  158. package/models/trace/handlers/LayoutShiftsHandler.js.map +1 -0
  159. package/models/trace/handlers/MemoryHandler.d.ts +7 -0
  160. package/models/trace/handlers/MemoryHandler.js +20 -0
  161. package/models/trace/handlers/MemoryHandler.js.map +1 -0
  162. package/models/trace/handlers/MetaHandler.d.ts +37 -0
  163. package/models/trace/handlers/MetaHandler.js +338 -0
  164. package/models/trace/handlers/MetaHandler.js.map +1 -0
  165. package/models/trace/handlers/Migration.js +27 -0
  166. package/models/trace/handlers/Migration.js.map +7 -0
  167. package/models/trace/handlers/ModelHandlers.d.ts +21 -0
  168. package/models/trace/handlers/ModelHandlers.js +25 -0
  169. package/models/trace/handlers/ModelHandlers.js.map +1 -0
  170. package/models/trace/handlers/NetworkRequestsHandler.d.ts +17 -0
  171. package/models/trace/handlers/NetworkRequestsHandler.js +361 -0
  172. package/models/trace/handlers/NetworkRequestsHandler.js.map +1 -0
  173. package/models/trace/handlers/PageLoadMetricsHandler.d.ts +67 -0
  174. package/models/trace/handlers/PageLoadMetricsHandler.js +407 -0
  175. package/models/trace/handlers/PageLoadMetricsHandler.js.map +1 -0
  176. package/models/trace/handlers/RendererHandler.d.ts +101 -0
  177. package/models/trace/handlers/RendererHandler.js +325 -0
  178. package/models/trace/handlers/RendererHandler.js.map +1 -0
  179. package/models/trace/handlers/SamplesHandler.d.ts +46 -0
  180. package/models/trace/handlers/SamplesHandler.js +215 -0
  181. package/models/trace/handlers/SamplesHandler.js.map +1 -0
  182. package/models/trace/handlers/ScreenshotsHandler.d.ts +7 -0
  183. package/models/trace/handlers/ScreenshotsHandler.js +79 -0
  184. package/models/trace/handlers/ScreenshotsHandler.js.map +1 -0
  185. package/models/trace/handlers/Threads.d.ts +33 -0
  186. package/models/trace/handlers/Threads.js +95 -0
  187. package/models/trace/handlers/Threads.js.map +1 -0
  188. package/models/trace/handlers/UberFramesHandler.js +293 -0
  189. package/models/trace/handlers/UberFramesHandler.js.map +7 -0
  190. package/models/trace/handlers/UserInteractionsHandler.d.ts +57 -0
  191. package/models/trace/handlers/UserInteractionsHandler.js +267 -0
  192. package/models/trace/handlers/UserInteractionsHandler.js.map +1 -0
  193. package/models/trace/handlers/UserTimingsHandler.d.ts +28 -0
  194. package/models/trace/handlers/UserTimingsHandler.js +108 -0
  195. package/models/trace/handlers/UserTimingsHandler.js.map +1 -0
  196. package/models/trace/handlers/WarningsHandler.d.ts +14 -0
  197. package/models/trace/handlers/WarningsHandler.js +125 -0
  198. package/models/trace/handlers/WarningsHandler.js.map +1 -0
  199. package/models/trace/handlers/WorkersHandler.d.ts +11 -0
  200. package/models/trace/handlers/WorkersHandler.js +52 -0
  201. package/models/trace/handlers/WorkersHandler.js.map +1 -0
  202. package/models/trace/handlers/bundle-tsconfig.json +1 -0
  203. package/models/trace/handlers/devtools_entrypoint-bundle-typescript-tsconfig.json +44 -0
  204. package/models/trace/handlers/handlers-tsconfig.json +79 -0
  205. package/models/trace/handlers/handlers.d.ts +3 -0
  206. package/models/trace/handlers/handlers.js +7 -0
  207. package/models/trace/handlers/handlers.js.map +1 -0
  208. package/models/trace/handlers/types.d.ts +45 -0
  209. package/models/trace/handlers/types.js +18 -0
  210. package/models/trace/handlers/types.js.map +1 -0
  211. package/models/trace/helpers/SamplesIntegrator.d.ts +49 -0
  212. package/models/trace/helpers/SamplesIntegrator.js +397 -0
  213. package/models/trace/helpers/SamplesIntegrator.js.map +1 -0
  214. package/models/trace/helpers/Timing.d.ts +26 -0
  215. package/models/trace/helpers/Timing.js +162 -0
  216. package/models/trace/helpers/Timing.js.map +1 -0
  217. package/models/trace/helpers/Trace.d.ts +37 -0
  218. package/models/trace/helpers/Trace.js +230 -0
  219. package/models/trace/helpers/Trace.js.map +1 -0
  220. package/models/trace/helpers/TreeHelpers.d.ts +90 -0
  221. package/models/trace/helpers/TreeHelpers.js +222 -0
  222. package/models/trace/helpers/TreeHelpers.js.map +1 -0
  223. package/models/trace/helpers/bundle-tsconfig.json +1 -0
  224. package/models/trace/helpers/devtools_entrypoint-bundle-typescript-tsconfig.json +44 -0
  225. package/models/trace/helpers/helpers-tsconfig.json +59 -0
  226. package/models/trace/helpers/helpers.d.ts +4 -0
  227. package/models/trace/helpers/helpers.js +8 -0
  228. package/models/trace/helpers/helpers.js.map +1 -0
  229. package/models/trace/legacy-tsconfig.json +1 -0
  230. package/models/trace/root-causes/LayoutShift.d.ts +119 -0
  231. package/models/trace/root-causes/LayoutShift.js +517 -0
  232. package/models/trace/root-causes/LayoutShift.js.map +1 -0
  233. package/models/trace/root-causes/RootCauses.d.ts +14 -0
  234. package/models/trace/root-causes/RootCauses.js +11 -0
  235. package/models/trace/root-causes/RootCauses.js.map +1 -0
  236. package/models/trace/root-causes/bundle-tsconfig.json +1 -0
  237. package/models/trace/root-causes/devtools_entrypoint-bundle-typescript-tsconfig.json +44 -0
  238. package/models/trace/root-causes/root-causes-tsconfig.json +57 -0
  239. package/models/trace/root-causes/root-causes.d.ts +1 -0
  240. package/models/trace/root-causes/root-causes.js +5 -0
  241. package/models/trace/root-causes/root-causes.js.map +1 -0
  242. package/models/trace/sdk_services/DOMNodeLookup.js +41 -0
  243. package/models/trace/sdk_services/DOMNodeLookup.js.map +7 -0
  244. package/models/trace/sdk_services/LayoutShifts.js +68 -0
  245. package/models/trace/sdk_services/LayoutShifts.js.map +7 -0
  246. package/models/trace/sdk_services/bundle-tsconfig.json +1 -0
  247. package/models/trace/sdk_services/devtools_entrypoint-bundle-typescript-tsconfig.json +41 -0
  248. package/models/trace/sdk_services/sdk_services-tsconfig.json +57 -0
  249. package/models/trace/sdk_services/sdk_services.js +7 -0
  250. package/models/trace/sdk_services/sdk_services.js.map +7 -0
  251. package/models/trace/trace-legacy.js +16 -0
  252. package/models/trace/trace-legacy.js.map +7 -0
  253. package/models/trace/trace-tsconfig.json +69 -0
  254. package/models/trace/trace.d.ts +11 -0
  255. package/models/trace/trace.js +17 -0
  256. package/models/trace/trace.js.map +1 -0
  257. package/models/trace/types/Configuration.d.ts +33 -0
  258. package/models/trace/types/Configuration.js +29 -0
  259. package/models/trace/types/Configuration.js.map +1 -0
  260. package/models/trace/types/File.d.ts +23 -0
  261. package/models/trace/types/File.js +5 -0
  262. package/models/trace/types/File.js.map +1 -0
  263. package/models/trace/types/Timing.d.ts +25 -0
  264. package/models/trace/types/Timing.js +16 -0
  265. package/models/trace/types/Timing.js.map +1 -0
  266. package/models/trace/types/TraceEvents.d.ts +1571 -0
  267. package/models/trace/types/TraceEvents.js +388 -0
  268. package/models/trace/types/TraceEvents.js.map +1 -0
  269. package/models/trace/types/bundle-tsconfig.json +1 -0
  270. package/models/trace/types/devtools_entrypoint-bundle-typescript-tsconfig.json +44 -0
  271. package/models/trace/types/types-tsconfig.json +47 -0
  272. package/models/trace/types/types.d.ts +4 -0
  273. package/models/trace/types/types.js +8 -0
  274. package/models/trace/types/types.js.map +1 -0
  275. package/models/trace/worker/Processor.js +143 -0
  276. package/models/trace/worker/Processor.js.map +7 -0
  277. package/models/trace/worker/Types.js +1 -0
  278. package/models/trace/worker/Types.js.map +7 -0
  279. package/models/trace/worker/bundle-tsconfig.json +1 -0
  280. package/models/trace/worker/devtools_entrypoint-bundle-typescript-tsconfig.json +41 -0
  281. package/models/trace/worker/devtools_entrypoint-worker_entrypoint-typescript-tsconfig.json +41 -0
  282. package/models/trace/worker/processor-tsconfig.json +45 -0
  283. package/models/trace/worker/worker.js +7 -0
  284. package/models/trace/worker/worker.js.map +7 -0
  285. package/models/trace/worker/worker_entrypoint-tsconfig.json +1 -0
  286. package/models/trace/worker/worker_entrypoint.js +36 -0
  287. package/models/trace/worker/worker_entrypoint.js.map +7 -0
  288. package/package.json +2 -2
  289. package/TracingManager.js +0 -0
  290. package/extras/extras.js +0 -0
  291. package/trace.mjs +0 -6980
  292. package/trace.mjs.map +0 -8
@@ -0,0 +1,397 @@
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
+ */
36
+ export class SamplesIntegrator {
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;
96
+ }
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;
146
+ }
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);
179
+ }
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
+ }
194
+ }
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);
201
+ }
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;
234
+ }
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;
260
+ }
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
+ }
325
+ }
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;
353
+ }
354
+ static framesAreEqual(frame1, frame2) {
355
+ return frame1.scriptId === frame2.scriptId && frame1.functionName === frame2.functionName &&
356
+ frame1.lineNumber === frame2.lineNumber;
357
+ }
358
+ static showNativeName(name, runtimeCallStatsEnabled) {
359
+ return runtimeCallStatsEnabled && Boolean(SamplesIntegrator.nativeGroup(name));
360
+ }
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;
369
+ }
370
+ static isNativeRuntimeFrame(frame) {
371
+ return frame.url === 'native V8Runtime';
372
+ }
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;
395
+ }
396
+ }
397
+ //# sourceMappingURL=SamplesIntegrator.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SamplesIntegrator.js","sourceRoot":"","sources":["../../../../../../../front_end/models/trace/helpers/SamplesIntegrator.ts"],"names":[],"mappings":"AAAA,4DAA4D;AAC5D,yEAAyE;AACzE,6BAA6B;AAI7B,OAAO,KAAK,KAAK,MAAM,mBAAmB,CAAC;AAE3C,OAAO,EAAC,0BAA0B,EAAC,MAAM,aAAa,CAAC;AACvD,OAAO,EAAC,eAAe,EAAE,kBAAkB,EAAC,MAAM,YAAY,CAAC;AAE/D;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,MAAM,OAAO,iBAAiB;IAC5B;;;;OAIG;IACH,wBAAwB,GAA6C,EAAE,CAAC;IACxE;;;;;;OAMG;IACH,eAAe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AAK,eAAe,EAAE;gBAC1E,SAAS;aACV;YACD,uBAAuB,GAAG,eAAe,CAAC;YAC1C,KAAK,CAAC,CAAC,EAAE,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC;SACvB;QACD,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC;IACnB,CAAC;CACF","sourcesContent":["// Copyright 2023 The Chromium Authors. All rights reserved.\n// Use of this source code is governed by a BSD-style license that can be\n// found in the LICENSE file.\n\nimport type * as Protocol from '../../../generated/protocol.js';\nimport type * as CPUProfile from '../../cpu_profile/cpu_profile.js';\nimport * as Types from '../types/types.js';\n\nimport {millisecondsToMicroseconds} from './Timing.js';\nimport {makeProfileCall, mergeEventsInOrder} from './Trace.js';\n\n/**\n * This is a helper that integrates CPU profiling data coming in the\n * shape of samples, with trace events. Samples indicate what the JS\n * stack trace looked at a given point in time, but they don't have\n * duration. The SamplesIntegrator task is to make an approximation\n * of what the duration of each JS call was, given the sample data and\n * given the trace events profiled during that time. At the end of its\n * execution, the SamplesIntegrator returns an array of ProfileCalls\n * (under SamplesIntegrator::buildProfileCalls()), which\n * represent JS calls, with a call frame and duration. These calls have\n * the shape of a complete trace events and can be treated as flame\n * chart entries in the timeline.\n *\n * The approach to build the profile calls consists in tracking the\n * current stack as the following events happen (in order):\n * 1. A sample was done.\n * 2. A trace event started.\n * 3. A trace event ended.\n * Depending on the event and on the data that's coming with it the\n * stack is updated by adding or removing JS calls to it and updating\n * the duration of the calls in the tracking stack.\n *\n * note: Although this approach has been implemented since long ago, and\n * is relatively efficent (adds a complexity over the trace parsing of\n * O(n) where n is the number of samples) it has proven to be faulty.\n * It might be worthwhile experimenting with improvements or with a\n * completely different approach. Improving the approach is tracked in\n * crbug.com/1417439\n */\nexport class SamplesIntegrator {\n /**\n * The result of runing the samples integrator. Holds the JS calls\n * with their approximated duration after integrating samples into the\n * trace event tree.\n */\n #constructedProfileCalls: Types.TraceEvents.SyntheticProfileCall[] = [];\n /**\n * tracks the state of the JS stack at each point in time to update\n * the profile call durations as new events arrive. This doesn't only\n * happen with new profile calls (in which case we would compare the\n * stack in them) but also with trace events (in which case we would\n * update the duration of the events we are tracking at the moment).\n */\n #currentJSStack: Types.TraceEvents.SyntheticProfileCall[] = [];\n /**\n * Process holding the CPU profile and trace events.\n */\n #processId: Types.TraceEvents.ProcessID;\n /**\n * Thread holding the CPU profile and trace events.\n */\n #threadId: Types.TraceEvents.ThreadID;\n /**\n * Tracks the depth of the JS stack at the moment a trace event starts\n * or ends. It is assumed that for the duration of a trace event, the\n * JS stack's depth cannot decrease, since JS calls that started\n * before a trace event cannot end during the trace event. So as trace\n * events arrive, we store the \"locked\" amount of JS frames that were\n * in the stack before the event came.\n */\n #lockedJsStackDepth: number[] = [];\n /**\n * Used to keep track when samples should be integrated even if they\n * are not children of invocation trace events. This is useful in\n * cases where we can be missing the start of JS invocation events if\n * we start tracing half-way through.\n */\n #fakeJSInvocation = false;\n /**\n * The parsed CPU profile, holding the tree hierarchy of JS frames and\n * the sample data.\n */\n #profileModel: CPUProfile.CPUProfileDataModel.CPUProfileDataModel;\n /**\n * Because GC nodes don't have a stack, we artificially add a stack to\n * them which corresponds to that of the previous sample. This map\n * tracks which node is used for the stack of a GC call.\n * Note that GC samples are not shown in the flamechart, however they\n * are used during the construction of for profile calls, as we can\n * infer information about the duration of the executed code when a\n * GC node is sampled.\n */\n #nodeForGC = new Map<Types.TraceEvents.SyntheticProfileCall, CPUProfile.ProfileTreeModel.ProfileNode>();\n\n #engineConfig: Types.Configuration.Configuration;\n\n constructor(\n profileModel: CPUProfile.CPUProfileDataModel.CPUProfileDataModel, pid: Types.TraceEvents.ProcessID,\n tid: Types.TraceEvents.ThreadID, configuration?: Types.Configuration.Configuration) {\n this.#profileModel = profileModel;\n this.#threadId = tid;\n this.#processId = pid;\n this.#engineConfig = configuration || Types.Configuration.DEFAULT;\n }\n\n buildProfileCalls(traceEvents: Types.TraceEvents.TraceEventData[]): Types.TraceEvents.SyntheticProfileCall[] {\n const mergedEvents = mergeEventsInOrder(traceEvents, this.callsFromProfileSamples());\n const stack = [];\n for (let i = 0; i < mergedEvents.length; i++) {\n const event = mergedEvents[i];\n // Because instant trace events have no duration, they don't provide\n // useful information for possible changes in the duration of calls\n // in the JS stack.\n if (event.ph === Types.TraceEvents.Phase.INSTANT) {\n continue;\n }\n if (stack.length === 0) {\n if (Types.TraceEvents.isProfileCall(event)) {\n this.#onProfileCall(event);\n continue;\n }\n stack.push(event);\n this.#onTraceEventStart(event);\n continue;\n }\n\n const parentEvent = stack.at(-1);\n if (parentEvent === undefined) {\n continue;\n }\n const begin = event.ts;\n const parentBegin = parentEvent.ts;\n const parentDuration = parentEvent.dur || 0;\n const parentEnd = parentBegin + parentDuration;\n\n const startsAfterParent = begin >= parentEnd;\n if (startsAfterParent) {\n this.#onTraceEventEnd(parentEvent);\n stack.pop();\n i--;\n continue;\n }\n if (Types.TraceEvents.isProfileCall(event)) {\n this.#onProfileCall(event, parentEvent);\n continue;\n }\n this.#onTraceEventStart(event);\n stack.push(event);\n }\n while (stack.length) {\n const last = stack.pop();\n if (last) {\n this.#onTraceEventEnd(last);\n }\n }\n return this.#constructedProfileCalls;\n }\n\n #onTraceEventStart(event: Types.TraceEvents.TraceEventData): void {\n // Top level events cannot be nested into JS frames so we reset\n // the stack when we find one.\n if (event.name === Types.TraceEvents.KnownEventName.RunMicrotasks ||\n event.name === Types.TraceEvents.KnownEventName.RunTask) {\n this.#lockedJsStackDepth = [];\n this.#truncateJSStack(0, event.ts);\n this.#fakeJSInvocation = false;\n }\n\n if (this.#fakeJSInvocation) {\n this.#truncateJSStack(this.#lockedJsStackDepth.pop() || 0, event.ts);\n this.#fakeJSInvocation = false;\n }\n this.#extractStackTrace(event);\n // Keep track of the call frames in the stack before the event\n // happened. For the duration of this event, these frames cannot\n // change (none can be terminated before this event finishes).\n //\n // Also, every frame that is opened after this event, is considered\n // to be a descendant of the event. So once the event finishes, the\n // frames that were opened after it, need to be closed (see\n // onEndEvent).\n //\n // TODO(crbug.com/1417439):\n // The assumption that every frame opened after an event is a\n // descendant of the event is incorrect. For example, a JS call that\n // parents a trace event might have been sampled after the event was\n // dispatched. In this case the JS call would be discarded if this\n // event isn't an invocation event, otherwise the call will be\n // considered a child of the event. In both cases, the result would\n // be incorrect.\n this.#lockedJsStackDepth.push(this.#currentJSStack.length);\n }\n\n #onProfileCall(event: Types.TraceEvents.SyntheticProfileCall, parent?: Types.TraceEvents.TraceEventData): void {\n if ((parent && Types.TraceEvents.isJSInvocationEvent(parent)) || this.#fakeJSInvocation) {\n this.#extractStackTrace(event);\n } else if (Types.TraceEvents.isProfileCall(event) && this.#currentJSStack.length === 0) {\n // Force JS Samples to show up even if we are not inside a JS\n // invocation event, because we can be missing the start of JS\n // invocation events if we start tracing half-way through. Pretend\n // we have a top-level JS invocation event.\n this.#fakeJSInvocation = true;\n const stackDepthBefore = this.#currentJSStack.length;\n this.#extractStackTrace(event);\n this.#lockedJsStackDepth.push(stackDepthBefore);\n }\n }\n\n #onTraceEventEnd(event: Types.TraceEvents.TraceEventData): void {\n // Because the event has ended, any frames that happened after\n // this event are terminated. Frames that are ancestors to this\n // event are extended to cover its ending.\n const endTime = Types.Timing.MicroSeconds(event.ts + (event.dur || 0));\n this.#truncateJSStack(this.#lockedJsStackDepth.pop() || 0, endTime);\n }\n\n /**\n * Builds the initial calls with no duration from samples. Their\n * purpose is to be merged with the trace event array being parsed so\n * that they can be traversed in order with them and their duration\n * can be updated as the SampleIntegrator callbacks are invoked.\n */\n callsFromProfileSamples(): Types.TraceEvents.SyntheticProfileCall[] {\n const samples = this.#profileModel.samples;\n const timestamps = this.#profileModel.timestamps;\n if (!samples) {\n return [];\n }\n const calls: Types.TraceEvents.SyntheticProfileCall[] = [];\n let prevNode;\n for (let i = 0; i < samples.length; i++) {\n const node = this.#profileModel.nodeByIndex(i);\n const timestamp = millisecondsToMicroseconds(Types.Timing.MilliSeconds(timestamps[i]));\n if (!node) {\n continue;\n }\n const call = makeProfileCall(node, timestamp, this.#processId, this.#threadId);\n calls.push(call);\n if (node.id === this.#profileModel.gcNode?.id && prevNode) {\n // GC samples have no stack, so we just put GC node on top of the\n // last recorded sample. Cache the previous sample for future\n // reference.\n this.#nodeForGC.set(call, prevNode);\n continue;\n }\n prevNode = node;\n }\n return calls;\n }\n\n #getStackTraceFromProfileCall(profileCall: Types.TraceEvents.SyntheticProfileCall):\n Types.TraceEvents.SyntheticProfileCall[] {\n let node = this.#profileModel.nodeById(profileCall.nodeId);\n const isGarbageCollection = node?.id === this.#profileModel.gcNode?.id;\n if (isGarbageCollection) {\n // Because GC don't have a stack, we use the stack of the previous\n // sample.\n node = this.#nodeForGC.get(profileCall) || null;\n }\n if (!node) {\n return [];\n }\n // `node.depth` is 0 based, so to set the size of the array we need\n // to add 1 to its value.\n const callFrames = new Array<Types.TraceEvents.SyntheticProfileCall>(node.depth + 1 + Number(isGarbageCollection));\n // Add the stack trace in reverse order (bottom first).\n let i = callFrames.length - 1;\n if (isGarbageCollection) {\n // Place the garbage collection call frame on top of the stack.\n callFrames[i--] = profileCall;\n }\n while (node) {\n callFrames[i--] = makeProfileCall(node, profileCall.ts, this.#processId, this.#threadId);\n node = node.parent;\n }\n return callFrames;\n }\n\n /**\n * Update tracked stack using this event's call stack.\n */\n #extractStackTrace(event: Types.TraceEvents.TraceEventData): void {\n const stackTrace =\n Types.TraceEvents.isProfileCall(event) ? this.#getStackTraceFromProfileCall(event) : this.#currentJSStack;\n SamplesIntegrator.filterStackFrames(stackTrace, this.#engineConfig);\n\n const endTime = event.ts + (event.dur || 0);\n const minFrames = Math.min(stackTrace.length, this.#currentJSStack.length);\n let i;\n // Merge a sample's stack frames with the stack frames we have\n // so far if we detect they are equivalent.\n // Graphically\n // This:\n // Current stack trace Sample\n // [-------A------] [A]\n // [-------B------] [B]\n // [-------C------] [C]\n // ^ t = x1 ^ t = x2\n\n // Becomes this:\n // New stack trace after merge\n // [--------A-------]\n // [--------B-------]\n // [--------C-------]\n // ^ t = x2\n for (i = this.#lockedJsStackDepth.at(-1) || 0; i < minFrames; ++i) {\n const newFrame = stackTrace[i].callFrame;\n const oldFrame = this.#currentJSStack[i].callFrame;\n if (!SamplesIntegrator.framesAreEqual(newFrame, oldFrame)) {\n break;\n }\n // Scoot the right edge of this callFrame to the right\n this.#currentJSStack[i].dur =\n Types.Timing.MicroSeconds(Math.max(this.#currentJSStack[i].dur || 0, endTime - this.#currentJSStack[i].ts));\n }\n\n // If there are call frames in the sample that differ with the stack\n // we have, update the stack, but keeping the common frames in place\n // Graphically\n // This:\n // Current stack trace Sample\n // [-------A------] [A]\n // [-------B------] [B]\n // [-------C------] [C]\n // [-------D------] [E]\n // ^ t = x1 ^ t = x2\n // Becomes this:\n // New stack trace after merge\n // [--------A-------]\n // [--------B-------]\n // [--------C-------]\n // [E]\n // ^ t = x2\n this.#truncateJSStack(i, event.ts);\n\n for (; i < stackTrace.length; ++i) {\n const call = stackTrace[i];\n if (call.nodeId === this.#profileModel.programNode?.id || call.nodeId === this.#profileModel.root?.id ||\n call.nodeId === this.#profileModel.idleNode?.id || call.nodeId === this.#profileModel.gcNode?.id) {\n // Skip (root), (program) and (idle) frames, since this are not\n // relevant for web profiling and we don't want to show them in\n // the timeline.\n continue;\n }\n this.#currentJSStack.push(call);\n this.#constructedProfileCalls.push(call);\n }\n }\n\n /**\n * When a call stack that differs from the one we are tracking has\n * been detected in the samples, the latter is \"truncated\" by\n * setting the ending time of its call frames and removing the top\n * call frames that aren't shared with the new call stack. This way,\n * we can update the tracked stack with the new call frames on top.\n * @param depth the amount of call frames from bottom to top that\n * should be kept in the tracking stack trace. AKA amount of shared\n * call frames between two stacks.\n * @param time the new end of the call frames in the stack.\n */\n #truncateJSStack(depth: number, time: Types.Timing.MicroSeconds): void {\n if (this.#lockedJsStackDepth.length) {\n const lockedDepth = this.#lockedJsStackDepth.at(-1);\n if (lockedDepth && depth < lockedDepth) {\n console.error(`Child stack is shallower (${depth}) than the parent stack (${lockedDepth}) at ${time}`);\n depth = lockedDepth;\n }\n }\n if (this.#currentJSStack.length < depth) {\n console.error(`Trying to truncate higher than the current stack size at ${time}`);\n depth = this.#currentJSStack.length;\n }\n for (let k = 0; k < this.#currentJSStack.length; ++k) {\n this.#currentJSStack[k].dur = Types.Timing.MicroSeconds(Math.max(time - this.#currentJSStack[k].ts, 0));\n }\n this.#currentJSStack.length = depth;\n }\n\n static framesAreEqual(frame1: Protocol.Runtime.CallFrame, frame2: Protocol.Runtime.CallFrame): boolean {\n return frame1.scriptId === frame2.scriptId && frame1.functionName === frame2.functionName &&\n frame1.lineNumber === frame2.lineNumber;\n }\n\n static showNativeName(name: string, runtimeCallStatsEnabled: boolean): boolean {\n return runtimeCallStatsEnabled && Boolean(SamplesIntegrator.nativeGroup(name));\n }\n\n static nativeGroup(nativeName: string): 'Parse'|'Compile'|null {\n if (nativeName.startsWith('Parse')) {\n return 'Parse';\n }\n if (nativeName.startsWith('Compile') || nativeName.startsWith('Recompile')) {\n return 'Compile';\n }\n return null;\n }\n\n static isNativeRuntimeFrame(frame: Protocol.Runtime.CallFrame): boolean {\n return frame.url === 'native V8Runtime';\n }\n\n static filterStackFrames(\n stack: Types.TraceEvents.SyntheticProfileCall[], engineConfig: Types.Configuration.Configuration): void {\n const showAllEvents = engineConfig.experiments.timelineShowAllEvents;\n if (showAllEvents) {\n return;\n }\n let previousNativeFrameName: string|null = null;\n let j = 0;\n for (let i = 0; i < stack.length; ++i) {\n const frame = stack[i].callFrame;\n const nativeRuntimeFrame = SamplesIntegrator.isNativeRuntimeFrame(frame);\n if (nativeRuntimeFrame &&\n !SamplesIntegrator.showNativeName(frame.functionName, engineConfig.experiments.timelineV8RuntimeCallStats)) {\n continue;\n }\n const nativeFrameName = nativeRuntimeFrame ? SamplesIntegrator.nativeGroup(frame.functionName) : null;\n if (previousNativeFrameName && previousNativeFrameName === nativeFrameName) {\n continue;\n }\n previousNativeFrameName = nativeFrameName;\n stack[j++] = stack[i];\n }\n stack.length = j;\n }\n}\n"]}
@@ -0,0 +1,26 @@
1
+ import * as Types from '../types/types.js';
2
+ export declare const millisecondsToMicroseconds: (value: Types.Timing.MilliSeconds) => Types.Timing.MicroSeconds;
3
+ export declare const secondsToMilliseconds: (value: Types.Timing.Seconds) => Types.Timing.MilliSeconds;
4
+ export declare const secondsToMicroseconds: (value: Types.Timing.Seconds) => Types.Timing.MicroSeconds;
5
+ export declare const microSecondsToMilliseconds: (value: Types.Timing.MicroSeconds) => Types.Timing.MilliSeconds;
6
+ export declare const microSecondsToSeconds: (value: Types.Timing.MicroSeconds) => Types.Timing.Seconds;
7
+ export declare function detectBestTimeUnit(timeInMicroseconds: Types.Timing.MicroSeconds): Types.Timing.TimeUnit;
8
+ interface FormatOptions extends Intl.NumberFormatOptions {
9
+ format?: Types.Timing.TimeUnit;
10
+ }
11
+ export declare function formatMicrosecondsTime(timeInMicroseconds: Types.Timing.MicroSeconds, opts?: FormatOptions): string;
12
+ export declare function timeStampForEventAdjustedByClosestNavigation(event: Types.TraceEvents.TraceEventData, traceBounds: Types.Timing.TraceWindowMicroSeconds, navigationsByNavigationId: Map<string, Types.TraceEvents.TraceEventNavigationStart>, navigationsByFrameId: Map<string, Types.TraceEvents.TraceEventNavigationStart[]>): Types.Timing.MicroSeconds;
13
+ export interface EventTimingsData<ValueType extends Types.Timing.MicroSeconds | Types.Timing.MilliSeconds | Types.Timing.Seconds> {
14
+ startTime: ValueType;
15
+ endTime: ValueType;
16
+ duration: ValueType;
17
+ selfTime: ValueType;
18
+ }
19
+ export declare function eventTimingsMicroSeconds(event: Types.TraceEvents.TraceEventData): EventTimingsData<Types.Timing.MicroSeconds>;
20
+ export declare function eventTimingsMilliSeconds(event: Types.TraceEvents.TraceEventData): EventTimingsData<Types.Timing.MilliSeconds>;
21
+ export declare function eventTimingsSeconds(event: Types.TraceEvents.TraceEventData): EventTimingsData<Types.Timing.Seconds>;
22
+ export declare function traceWindowMilliSeconds(bounds: Types.Timing.TraceWindowMicroSeconds): Types.Timing.TraceWindowMilliSeconds;
23
+ export declare function traceWindowMillisecondsToMicroSeconds(bounds: Types.Timing.TraceWindowMilliSeconds): Types.Timing.TraceWindowMicroSeconds;
24
+ export declare function traceWindowFromMilliSeconds(min: Types.Timing.MilliSeconds, max: Types.Timing.MilliSeconds): Types.Timing.TraceWindowMicroSeconds;
25
+ export declare function traceWindowFromMicroSeconds(min: Types.Timing.MicroSeconds, max: Types.Timing.MicroSeconds): Types.Timing.TraceWindowMicroSeconds;
26
+ export {};
@@ -0,0 +1,162 @@
1
+ // Copyright 2022 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 Platform from '../../../core/platform/platform.js';
5
+ import * as Types from '../types/types.js';
6
+ import { getNavigationForTraceEvent } from './Trace.js';
7
+ export const millisecondsToMicroseconds = (value) => Types.Timing.MicroSeconds(value * 1000);
8
+ export const secondsToMilliseconds = (value) => Types.Timing.MilliSeconds(value * 1000);
9
+ export const secondsToMicroseconds = (value) => millisecondsToMicroseconds(secondsToMilliseconds(value));
10
+ export const microSecondsToMilliseconds = (value) => Types.Timing.MilliSeconds(value / 1000);
11
+ export const microSecondsToSeconds = (value) => Types.Timing.Seconds(value / 1000 / 1000);
12
+ export function detectBestTimeUnit(timeInMicroseconds) {
13
+ if (timeInMicroseconds < 1000) {
14
+ return 0 /* Types.Timing.TimeUnit.MICROSECONDS */;
15
+ }
16
+ const timeInMilliseconds = timeInMicroseconds / 1000;
17
+ if (timeInMilliseconds < 1000) {
18
+ return 1 /* Types.Timing.TimeUnit.MILLISECONDS */;
19
+ }
20
+ const timeInSeconds = timeInMilliseconds / 1000;
21
+ if (timeInSeconds < 60) {
22
+ return 2 /* Types.Timing.TimeUnit.SECONDS */;
23
+ }
24
+ return 3 /* Types.Timing.TimeUnit.MINUTES */;
25
+ }
26
+ const defaultFormatOptions = {
27
+ style: 'unit',
28
+ unit: 'millisecond',
29
+ unitDisplay: 'narrow',
30
+ };
31
+ // Create a bunch of common formatters up front, so that we're not creating
32
+ // them repeatedly during rendering.
33
+ const serialize = (value) => JSON.stringify(value);
34
+ const formatterFactory = (key) => {
35
+ // If we pass undefined as the locale, that achieves two things:
36
+ // 1. Avoids us referencing window.navigatior to fetch the locale, which is
37
+ // useful given long term we would like this engine to run in NodeJS
38
+ // environments.
39
+ // 2. Will cause the formatter to fallback to the locale of the system, which
40
+ // is likely going to be the most accurate one to use anyway.
41
+ return new Intl.NumberFormat(undefined, key ? JSON.parse(key) : {});
42
+ };
43
+ const formatters = new Map();
44
+ // Microsecond Formatter.
45
+ Platform.MapUtilities.getWithDefault(formatters, serialize({ style: 'decimal' }), formatterFactory);
46
+ // Millisecond Formatter
47
+ Platform.MapUtilities.getWithDefault(formatters, serialize(defaultFormatOptions), formatterFactory);
48
+ // Second Formatter
49
+ Platform.MapUtilities.getWithDefault(formatters, serialize({ ...defaultFormatOptions, unit: 'second' }), formatterFactory);
50
+ // Minute Formatter
51
+ Platform.MapUtilities.getWithDefault(formatters, serialize({ ...defaultFormatOptions, unit: 'minute' }), formatterFactory);
52
+ export function formatMicrosecondsTime(timeInMicroseconds, opts = {}) {
53
+ if (!opts.format) {
54
+ opts.format = detectBestTimeUnit(timeInMicroseconds);
55
+ }
56
+ const timeInMilliseconds = timeInMicroseconds / 1000;
57
+ const timeInSeconds = timeInMilliseconds / 1000;
58
+ const formatterOpts = { ...defaultFormatOptions, ...opts };
59
+ switch (opts.format) {
60
+ case 0 /* Types.Timing.TimeUnit.MICROSECONDS */: {
61
+ const formatter = Platform.MapUtilities.getWithDefault(formatters, serialize({ style: 'decimal' }), formatterFactory);
62
+ return `${formatter.format(timeInMicroseconds)}μs`;
63
+ }
64
+ case 1 /* Types.Timing.TimeUnit.MILLISECONDS */: {
65
+ const formatter = Platform.MapUtilities.getWithDefault(formatters, serialize(formatterOpts), formatterFactory);
66
+ return formatter.format(timeInMilliseconds);
67
+ }
68
+ case 2 /* Types.Timing.TimeUnit.SECONDS */: {
69
+ const formatter = Platform.MapUtilities.getWithDefault(formatters, serialize({ ...formatterOpts, unit: 'second' }), formatterFactory);
70
+ return formatter.format(timeInSeconds);
71
+ }
72
+ default: {
73
+ // Switch to mins & seconds.
74
+ const minuteFormatter = Platform.MapUtilities.getWithDefault(formatters, serialize({ ...formatterOpts, unit: 'minute' }), formatterFactory);
75
+ const secondFormatter = Platform.MapUtilities.getWithDefault(formatters, serialize({ ...formatterOpts, unit: 'second' }), formatterFactory);
76
+ const timeInMinutes = timeInSeconds / 60;
77
+ const [mins, divider, fraction] = minuteFormatter.formatToParts(timeInMinutes);
78
+ let seconds = 0;
79
+ if (divider && fraction) {
80
+ // Convert the fraction value (a string) to the nearest second.
81
+ seconds = Math.round(Number(`0.${fraction.value}`) * 60);
82
+ }
83
+ return `${minuteFormatter.format(Number(mins.value))} ${secondFormatter.format(seconds)}`;
84
+ }
85
+ }
86
+ }
87
+ export function timeStampForEventAdjustedByClosestNavigation(event, traceBounds, navigationsByNavigationId, navigationsByFrameId) {
88
+ let eventTimeStamp = event.ts - traceBounds.min;
89
+ if (event.args?.data?.navigationId) {
90
+ const navigationForEvent = navigationsByNavigationId.get(event.args.data.navigationId);
91
+ if (navigationForEvent) {
92
+ eventTimeStamp = event.ts - navigationForEvent.ts;
93
+ }
94
+ }
95
+ else if (event.args?.data?.frame) {
96
+ const navigationForEvent = getNavigationForTraceEvent(event, event.args.data.frame, navigationsByFrameId);
97
+ if (navigationForEvent) {
98
+ eventTimeStamp = event.ts - navigationForEvent.ts;
99
+ }
100
+ }
101
+ return Types.Timing.MicroSeconds(eventTimeStamp);
102
+ }
103
+ export function eventTimingsMicroSeconds(event) {
104
+ return {
105
+ startTime: event.ts,
106
+ endTime: Types.Timing.MicroSeconds(event.ts + (event.dur || Types.Timing.MicroSeconds(0))),
107
+ duration: Types.Timing.MicroSeconds(event.dur || 0),
108
+ // TODO(crbug.com/1434599): Implement selfTime calculation for events
109
+ // from the new engine.
110
+ selfTime: Types.TraceEvents.isSyntheticTraceEntry(event) ? Types.Timing.MicroSeconds(event.selfTime || 0) :
111
+ Types.Timing.MicroSeconds(event.dur || 0),
112
+ };
113
+ }
114
+ export function eventTimingsMilliSeconds(event) {
115
+ const microTimes = eventTimingsMicroSeconds(event);
116
+ return {
117
+ startTime: microSecondsToMilliseconds(microTimes.startTime),
118
+ endTime: microSecondsToMilliseconds(microTimes.endTime),
119
+ duration: microSecondsToMilliseconds(microTimes.duration),
120
+ selfTime: microSecondsToMilliseconds(microTimes.selfTime),
121
+ };
122
+ }
123
+ export function eventTimingsSeconds(event) {
124
+ const microTimes = eventTimingsMicroSeconds(event);
125
+ return {
126
+ startTime: microSecondsToSeconds(microTimes.startTime),
127
+ endTime: microSecondsToSeconds(microTimes.endTime),
128
+ duration: microSecondsToSeconds(microTimes.duration),
129
+ selfTime: microSecondsToSeconds(microTimes.selfTime),
130
+ };
131
+ }
132
+ export function traceWindowMilliSeconds(bounds) {
133
+ return {
134
+ min: microSecondsToMilliseconds(bounds.min),
135
+ max: microSecondsToMilliseconds(bounds.max),
136
+ range: microSecondsToMilliseconds(bounds.range),
137
+ };
138
+ }
139
+ export function traceWindowMillisecondsToMicroSeconds(bounds) {
140
+ return {
141
+ min: millisecondsToMicroseconds(bounds.min),
142
+ max: millisecondsToMicroseconds(bounds.max),
143
+ range: millisecondsToMicroseconds(bounds.range),
144
+ };
145
+ }
146
+ export function traceWindowFromMilliSeconds(min, max) {
147
+ const traceWindow = {
148
+ min: millisecondsToMicroseconds(min),
149
+ max: millisecondsToMicroseconds(max),
150
+ range: Types.Timing.MicroSeconds(millisecondsToMicroseconds(max) - millisecondsToMicroseconds(min)),
151
+ };
152
+ return traceWindow;
153
+ }
154
+ export function traceWindowFromMicroSeconds(min, max) {
155
+ const traceWindow = {
156
+ min,
157
+ max,
158
+ range: Types.Timing.MicroSeconds(max - min),
159
+ };
160
+ return traceWindow;
161
+ }
162
+ //# sourceMappingURL=Timing.js.map