@@ -24,7 +24,7 @@ const entryToNode = new Map<Types.Events.Event, Helpers.TreeHelpers.TraceEntryNo
2424// events matched by thread id.
2525const preprocessedData = new Map < Types . Events . ProcessID , Map < Types . Events . ProfileID , PreprocessedData > > ( ) ;
2626
27- function buildProfileCalls ( ) : void {
27+ function parseCPUProfileData ( parseOptions : Types . Configuration . ParseOptions ) : void {
2828 for ( const [ processId , profiles ] of preprocessedData ) {
2929 for ( const [ profileId , preProcessedData ] of profiles ) {
3030 const threadId = preProcessedData . threadId ;
@@ -44,58 +44,65 @@ function buildProfileCalls(): void {
4444 profileTree,
4545 profileId,
4646 } ;
47-
4847 const dataByThread = Platform . MapUtilities . getWithDefault ( profilesInProcess , processId , ( ) => new Map ( ) ) ;
49- profileModel . forEachFrame ( openFrameCallback , closeFrameCallback ) ;
5048 dataByThread . set ( threadId , finalizedData ) ;
5149
52- function openFrameCallback (
53- depth : number , node : CPUProfile . ProfileTreeModel . ProfileNode , sampleIndex : number ,
54- timeStampMilliseconds : number ) : void {
55- if ( threadId === undefined ) {
56- return ;
57- }
58- const ts = Helpers . Timing . milliToMicro ( Types . Timing . Milli ( timeStampMilliseconds ) ) ;
59- const nodeId = node . id as Helpers . TreeHelpers . TraceEntryNodeId ;
60-
61- const profileCall = Helpers . Trace . makeProfileCall ( node , profileId , sampleIndex , ts , processId , threadId ) ;
62- finalizedData . profileCalls . push ( profileCall ) ;
63- indexStack . push ( finalizedData . profileCalls . length - 1 ) ;
64- const traceEntryNode = Helpers . TreeHelpers . makeEmptyTraceEntryNode ( profileCall , nodeId ) ;
65- entryToNode . set ( profileCall , traceEntryNode ) ;
66- traceEntryNode . depth = depth ;
67- if ( indexStack . length === 1 ) {
68- // First call in the stack is a root call.
69- finalizedData . profileTree ?. roots . add ( traceEntryNode ) ;
70- }
50+ // Only need to build pure JS ProfileCalls if we're parsing a CPU Profile, otherwise SamplesIntegrator does the work.
51+ if ( parseOptions . isCPUProfile ) {
52+ buildProfileCallsForCPUProfile ( ) ;
7153 }
72- function closeFrameCallback (
73- _depth : number , _node : CPUProfile . ProfileTreeModel . ProfileNode , _sampleIndex : number ,
74- _timeStampMillis : number , durMs : number , selfTimeMs : number ) : void {
75- const profileCallIndex = indexStack . pop ( ) ;
76- const profileCall = profileCallIndex !== undefined && finalizedData . profileCalls [ profileCallIndex ] ;
77- if ( ! profileCall ) {
78- return ;
79- }
80- const { callFrame, ts, pid, tid} = profileCall ;
81- const traceEntryNode = entryToNode . get ( profileCall ) ;
82- if ( callFrame === undefined || ts === undefined || pid === undefined || profileId === undefined ||
83- tid === undefined || traceEntryNode === undefined ) {
84- return ;
54+
55+ function buildProfileCallsForCPUProfile ( ) : void {
56+ profileModel . forEachFrame ( openFrameCallback , closeFrameCallback ) ;
57+
58+ function openFrameCallback (
59+ depth : number , node : CPUProfile . ProfileTreeModel . ProfileNode , sampleIndex : number ,
60+ timeStampMilliseconds : number ) : void {
61+ if ( threadId === undefined ) {
62+ return ;
63+ }
64+ const ts = Helpers . Timing . milliToMicro ( Types . Timing . Milli ( timeStampMilliseconds ) ) ;
65+ const nodeId = node . id as Helpers . TreeHelpers . TraceEntryNodeId ;
66+
67+ const profileCall = Helpers . Trace . makeProfileCall ( node , profileId , sampleIndex , ts , processId , threadId ) ;
68+ finalizedData . profileCalls . push ( profileCall ) ;
69+ indexStack . push ( finalizedData . profileCalls . length - 1 ) ;
70+ const traceEntryNode = Helpers . TreeHelpers . makeEmptyTraceEntryNode ( profileCall , nodeId ) ;
71+ entryToNode . set ( profileCall , traceEntryNode ) ;
72+ traceEntryNode . depth = depth ;
73+ if ( indexStack . length === 1 ) {
74+ // First call in the stack is a root call.
75+ finalizedData . profileTree ?. roots . add ( traceEntryNode ) ;
76+ }
8577 }
86- const dur = Helpers . Timing . milliToMicro ( Types . Timing . Milli ( durMs ) ) ;
87- const selfTime = Helpers . Timing . milliToMicro ( Types . Timing . Milli ( selfTimeMs ) ) ;
88- profileCall . dur = dur ;
89- traceEntryNode . selfTime = selfTime ;
78+ function closeFrameCallback (
79+ _depth : number , _node : CPUProfile . ProfileTreeModel . ProfileNode , _sampleIndex : number ,
80+ _timeStampMillis : number , durMs : number , selfTimeMs : number ) : void {
81+ const profileCallIndex = indexStack . pop ( ) ;
82+ const profileCall = profileCallIndex !== undefined && finalizedData . profileCalls [ profileCallIndex ] ;
83+ if ( ! profileCall ) {
84+ return ;
85+ }
86+ const { callFrame, ts, pid, tid} = profileCall ;
87+ const traceEntryNode = entryToNode . get ( profileCall ) ;
88+ if ( callFrame === undefined || ts === undefined || pid === undefined || profileId === undefined ||
89+ tid === undefined || traceEntryNode === undefined ) {
90+ return ;
91+ }
92+ const dur = Helpers . Timing . milliToMicro ( Types . Timing . Milli ( durMs ) ) ;
93+ const selfTime = Helpers . Timing . milliToMicro ( Types . Timing . Milli ( selfTimeMs ) ) ;
94+ profileCall . dur = dur ;
95+ traceEntryNode . selfTime = selfTime ;
9096
91- const parentIndex = indexStack . at ( - 1 ) ;
92- const parent = parentIndex !== undefined && finalizedData . profileCalls . at ( parentIndex ) ;
93- const parentNode = parent && entryToNode . get ( parent ) ;
94- if ( ! parentNode ) {
95- return ;
97+ const parentIndex = indexStack . at ( - 1 ) ;
98+ const parent = parentIndex !== undefined && finalizedData . profileCalls . at ( parentIndex ) ;
99+ const parentNode = parent && entryToNode . get ( parent ) ;
100+ if ( ! parentNode ) {
101+ return ;
102+ }
103+ traceEntryNode . parent = parentNode ;
104+ parentNode . children . push ( traceEntryNode ) ;
96105 }
97- traceEntryNode . parent = parentNode ;
98- parentNode . children . push ( traceEntryNode ) ;
99106 }
100107 }
101108 }
@@ -183,8 +190,8 @@ export function handleEvent(event: Types.Events.Event): void {
183190 }
184191}
185192
186- export async function finalize ( ) : Promise < void > {
187- buildProfileCalls ( ) ;
193+ export async function finalize ( parseOptions : Types . Configuration . ParseOptions = { } ) : Promise < void > {
194+ parseCPUProfileData ( parseOptions ) ;
188195}
189196
190197export function data ( ) : SamplesHandlerData {
0 commit comments