cpuProfilerModel.d.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /**
  2. * @license Copyright 2020 The Lighthouse Authors. All Rights Reserved.
  3. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
  4. * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
  5. *
  6. * MODIFICATION NOTICE:
  7. * This file is derived from `https://github.com/GoogleChrome/lighthouse/blob/0422daa9b1b8528dd8436860b153134bd0f959f1/lighthouse-core/lib/tracehouse/cpu-profile-model.js`
  8. * and has been modified by Saphal Patro (email: saphal1998@gmail.com)
  9. * The following changes have been made to the original file:
  10. * 1. Converted code to Typescript and defined necessary types
  11. * 2. Wrote a method @see collectProfileEvents to convert the Hermes Samples to Profile Chunks supported by Lighthouse Parser
  12. * 3. Modified @see constructNodes to work with the Hermes Samples and StackFrames
  13. */
  14. /**
  15. * @fileoverview
  16. *
  17. * This model converts the `Profile` and `ProfileChunk` mega trace events from the `disabled-by-default-v8.cpu_profiler`
  18. * category into B/E-style trace events that main-thread-tasks.js already knows how to parse into a task tree.
  19. *
  20. * The CPU profiler measures where time is being spent by sampling the stack (See https://www.jetbrains.com/help/profiler/Profiling_Guidelines__Choosing_the_Right_Profiling_Mode.html
  21. * for a generic description of the differences between tracing and sampling).
  22. *
  23. * A `Profile` event is a record of the stack that was being executed at different sample points in time.
  24. * It has a structure like this:
  25. *
  26. * nodes: [function A, function B, function C]
  27. * samples: [node with id 2, node with id 1, ...]
  28. * timeDeltas: [4125μs since last sample, 121μs since last sample, ...]
  29. *
  30. * Helpful prior art:
  31. * @see https://cs.chromium.org/chromium/src/third_party/devtools-frontend/src/front_end/sdk/CPUProfileDataModel.js?sq=package:chromium&g=0&l=42
  32. * @see https://github.com/v8/v8/blob/99ca333b0efba3236954b823101315aefeac51ab/tools/profile.js
  33. * @see https://github.com/jlfwong/speedscope/blob/9ed1eb192cb7e9dac43a5f25bd101af169dc654a/src/import/chrome.ts#L200
  34. */
  35. import { CPUProfileChunk, CPUProfileChunkNode, CPUProfileChunker } from '../types/CPUProfile';
  36. import { DurationEvent } from '../types/EventInterfaces';
  37. import { HermesCPUProfile, HermesSample, HermesStackFrame } from '../types/HermesProfile';
  38. export declare class CpuProfilerModel {
  39. _profile: CPUProfileChunk;
  40. _nodesById: Map<number, CPUProfileChunkNode>;
  41. _activeNodeArraysById: Map<number, number[]>;
  42. constructor(profile: CPUProfileChunk);
  43. /**
  44. * Initialization function to enable O(1) access to nodes by node ID.
  45. * @return {Map<number, CPUProfileChunkNode}
  46. */
  47. _createNodeMap(): Map<number, CPUProfileChunkNode>;
  48. /**
  49. * Initialization function to enable O(1) access to the set of active nodes in the stack by node ID.
  50. * @return Map<number, number[]>
  51. */
  52. _createActiveNodeArrays(): Map<number, number[]>;
  53. /**
  54. * Returns all the node IDs in a stack when a specific nodeId is at the top of the stack
  55. * (i.e. a stack's node ID and the node ID of all of its parents).
  56. */
  57. _getActiveNodeIds(nodeId: number): number[];
  58. /**
  59. * Generates the necessary B/E-style trace events for a single transition from stack A to stack B
  60. * at the given timestamp.
  61. *
  62. * Example:
  63. *
  64. * timestamp 1234
  65. * previousNodeIds 1,2,3
  66. * currentNodeIds 1,2,4
  67. *
  68. * yields [end 3 at ts 1234, begin 4 at ts 1234]
  69. *
  70. * @param {number} timestamp
  71. * @param {Array<number>} previousNodeIds
  72. * @param {Array<number>} currentNodeIds
  73. * @returns {Array<DurationEvent>}
  74. */
  75. _createStartEndEventsForTransition(timestamp: number, previousNodeIds: number[], currentNodeIds: number[]): DurationEvent[];
  76. /**
  77. * Creates B/E-style trace events from a CpuProfile object created by `collectProfileEvents()`
  78. * @return {DurationEvent}
  79. * @throws If the length of timeDeltas array or the samples array does not match with the length of samples in Hermes Profile
  80. */
  81. createStartEndEvents(): DurationEvent[];
  82. /**
  83. * Creates B/E-style trace events from a CpuProfile object created by `collectProfileEvents()`
  84. * @param {CPUProfileChunk} profile
  85. */
  86. static createStartEndEvents(profile: CPUProfileChunk): DurationEvent[];
  87. /**
  88. * Converts the Hermes Sample into a single CpuProfileChunk object for consumption
  89. * by `createStartEndEvents()`.
  90. *
  91. * @param {HermesCPUProfile} profile
  92. * @throws Profile must have at least one sample
  93. * @return {CPUProfileChunk}
  94. */
  95. static collectProfileEvents(profile: HermesCPUProfile): CPUProfileChunk;
  96. /**
  97. * Constructs CPUProfileChunk Nodes and the resultant samples and time deltas to be inputted into the
  98. * CPUProfileChunk object which will be processed to give createStartEndEvents()
  99. *
  100. * @param {HermesSample} samples
  101. * @param {<string, HermesStackFrame>} stackFrames
  102. * @return {CPUProfileChunker}
  103. */
  104. static constructNodes(samples: HermesSample[], stackFrames: {
  105. [key in string]: HermesStackFrame;
  106. }): CPUProfileChunker;
  107. }