@@ -458,12 +458,12 @@ export class SamplesIntegrator {
458458 return runtimeCallStatsEnabled && Boolean ( SamplesIntegrator . nativeGroup ( name ) ) ;
459459 }
460460
461- static nativeGroup ( nativeName : string ) : 'Parse' | 'Compile' | null {
461+ static nativeGroup ( nativeName : string ) : SamplesIntegrator . NativeGroups | null {
462462 if ( nativeName . startsWith ( 'Parse' ) ) {
463- return 'Parse' ;
463+ return SamplesIntegrator . NativeGroups . PARSE ;
464464 }
465465 if ( nativeName . startsWith ( 'Compile' ) || nativeName . startsWith ( 'Recompile' ) ) {
466- return 'Compile' ;
466+ return SamplesIntegrator . NativeGroups . COMPILE ;
467467 }
468468 return null ;
469469 }
@@ -496,4 +496,59 @@ export class SamplesIntegrator {
496496 }
497497 stack . length = j ;
498498 }
499+
500+ static createFakeTraceFromCpuProfile ( profile : Protocol . Profiler . Profile , tid : Types . Events . ThreadID ) :
501+ Types . File . TraceFile {
502+ const events : Types . Events . Event [ ] = [ ] ;
503+
504+ const threadName = `Thread ${ tid } ` ;
505+ appendEvent ( 'TracingStartedInPage' , { data : { sessionId : '1' } } , 0 , 0 , Types . Events . Phase . METADATA ) ;
506+ appendEvent ( Types . Events . Name . THREAD_NAME , { name : threadName } , 0 , 0 , Types . Events . Phase . METADATA , '__metadata' ) ;
507+ if ( ! profile ) {
508+ return { traceEvents : events , metadata : { } } ;
509+ }
510+
511+ // Append a root to show the start time of the profile (which is earlier than first sample), so the Performance
512+ // panel won't truncate this time period.
513+ // 'JSRoot' doesn't exist in the new engine and is not the name of an actual trace event, but changing it might break other trace processing tools that rely on this, so we stick with this name.
514+ // TODO(crbug.com/341234884): consider removing this or clarify why it's required.
515+ appendEvent (
516+ 'JSRoot' , { } , profile . startTime , profile . endTime - profile . startTime , Types . Events . Phase . COMPLETE , 'toplevel' ) ;
517+
518+ // TODO: create a `Profile` event instead, as `cpuProfile` is legacy
519+ appendEvent ( 'CpuProfile' , { data : { cpuProfile : profile } } , profile . endTime , 0 , Types . Events . Phase . COMPLETE ) ;
520+ return {
521+ traceEvents : events ,
522+ metadata : {
523+ dataOrigin : Types . File . DataOrigin . CPU_PROFILE ,
524+ }
525+ } ;
526+
527+ function appendEvent (
528+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
529+ name : string , args : any , ts : number , dur ?: number , ph ?: Types . Events . Phase , cat ?: string ) : Types . Events . Event {
530+ const event : Types . Events . Event = {
531+ cat : cat || 'disabled-by-default-devtools.timeline' ,
532+ name,
533+ ph : ph || Types . Events . Phase . COMPLETE ,
534+ pid : Types . Events . ProcessID ( 1 ) ,
535+ tid,
536+ ts : Types . Timing . Micro ( ts ) ,
537+ args,
538+ } ;
539+
540+ if ( dur ) {
541+ event . dur = Types . Timing . Micro ( dur ) ;
542+ }
543+ events . push ( event ) ;
544+ return event ;
545+ }
546+ }
547+ }
548+
549+ export namespace SamplesIntegrator {
550+ export const enum NativeGroups {
551+ COMPILE = 'Compile' ,
552+ PARSE = 'Parse' ,
553+ }
499554}
0 commit comments