@@ -494,50 +494,39 @@ function topLevelTasks(trace, { pid, tid }) {
494494 return tops ;
495495}
496496
497- // Thread CPU time, spread evenly over the task, so a loaded machine descheduling the thread does not count as work .
497+ // Thread CPU time, spread evenly over the task, so a descheduled thread does not count; untimed tasks count wall time .
498498const cpuUs = ( e , a , b ) =>
499- ( Math . max ( 0 , Math . min ( b , e . ts + e . dur ) - Math . max ( a , e . ts ) ) * e . tdur ) / ( e . dur || 1 ) ;
499+ ( Math . max ( 0 , Math . min ( b , e . ts + e . dur ) - Math . max ( a , e . ts ) ) * ( e . tdur ?? e . dur ) ) / ( e . dur || 1 ) ;
500500
501501/** Main-thread CPU ms inside each frame interval, on the thread that ran the end mark; null when unknown. */
502502function mainThreadPerFrame ( { frames, mark, trace } ) {
503503 const anchor = trace . find ( ( e ) => e . name === TRACE_MARK && e . cat . includes ( "user_timing" ) ) ;
504- if ( ! anchor ) return { work : null , untimedOutside : 0 , unknown : "no end mark in the trace" } ;
504+ // Without the mark the work is unknown, which fails smoothness alone.
505+ if ( ! anchor ) return { work : null , wallTimed : 0 , unknown : "no end mark in the trace" } ;
505506 const toTrace = ( ms ) => anchor . ts + ( ms - mark ) * 1000 ;
506507 const [ from , to ] = [ toTrace ( frames [ 0 ] ) , toTrace ( frames . at ( - 1 ) ) ] ;
507- // A task cut by the trace's start or end has no thread time; outside the frames it adds nothing anyway.
508- const all = topLevelTasks ( trace , anchor ) ;
509- const tasks = all . filter ( ( e ) => e . ts < to && e . ts + e . dur > from ) ;
510- const untimedOutside = all . filter ( ( e ) => e . tdur === undefined && ! tasks . includes ( e ) ) . length ;
511- // Without the mark or thread CPU time the work is unknown, which fails smoothness alone.
512- const untimed = tasks . filter ( ( e ) => e . tdur === undefined ) ;
513- if ( untimed . length ) {
514- const [ first ] = untimed ;
515- const at = `${ first . name } ${ first . dur } us at ${ Math . round ( ( first . ts - from ) / 1000 ) } ms` ;
516- return {
517- work : null ,
518- untimedOutside,
519- unknown : `${ untimed . length } untimed task(s) in the frames, first ${ at } ` ,
520- } ;
521- }
508+ const tasks = topLevelTasks ( trace , anchor ) ;
509+ const inFrames = ( e ) => e . ts < to && e . ts + e . dur > from ;
510+ const wallTimed = tasks . filter ( ( e ) => e . tdur === undefined && inFrames ( e ) ) . length ;
522511 const work = frames . slice ( 1 ) . map ( ( t , i ) => {
523512 const [ a , b ] = [ toTrace ( frames [ i ] ) , toTrace ( t ) ] ;
524513 return tasks . reduce ( ( sum , e ) => sum + cpuUs ( e , a , b ) , 0 ) / 1000 ;
525514 } ) ;
526- return { work, untimedOutside } ;
515+ return { work, wallTimed } ;
527516}
528517
529518const hundredth = ( v ) => Math . round ( v * 100 ) / 100 ;
530519
531520export function smoothness ( rec ) {
532521 const intervals = rec . frames . slice ( 1 ) . map ( ( t , i ) => t - rec . frames [ i ] ) ;
533- const { work, untimedOutside , unknown } = mainThreadPerFrame ( rec ) ;
522+ const { work, wallTimed , unknown } = mainThreadPerFrame ( rec ) ;
534523 return {
535524 p95 : percentile ( intervals , 95 ) ,
536525 frames : intervals . length ,
537526 longTasks : rec . long . length ,
538527 intervals : intervals . map ( hundredth ) ,
539528 work : work && work . map ( hundredth ) ,
540- untimedOutside ,
529+ wallTimed ,
541530 ...( unknown && { unknown } ) ,
542531 } ;
543532}
0 commit comments