@@ -35,6 +35,9 @@ import type {
3535 SampleFilterSpec ,
3636 ProfileLogsResult ,
3737 ThreadSelectResult ,
38+ CounterSummary ,
39+ CounterListResult ,
40+ CounterInfoResult ,
3841} from './protocol' ;
3942import { truncateFunctionName } from '../../src/profile-query/function-list' ;
4043import { describeSpec } from '../../src/profile-query/filter-stack' ;
@@ -428,6 +431,10 @@ Name: ${result.name}\n`;
428431 if ( process . remainingThreads ) {
429432 output += ` + ${ process . remainingThreads . count } more threads with combined CPU time ${ process . remainingThreads . combinedCpuMs . toFixed ( 3 ) } ms and max CPU time ${ process . remainingThreads . maxCpuMs . toFixed ( 3 ) } ms (use --all to see all)\n` ;
430433 }
434+
435+ for ( const counter of process . counters ?? [ ] ) {
436+ output += ` ${ counter . counterHandle } : ${ counter . label } ${ formatCounterStats ( counter ) } \n` ;
437+ }
431438 }
432439
433440 if ( result . remainingProcesses ) {
@@ -451,6 +458,85 @@ Name: ${result.name}\n`;
451458 return output ;
452459}
453460
461+ function formatCounterStatInline (
462+ stat : CounterSummary [ 'stats' ] [ number ]
463+ ) : string {
464+ const value = stat . carbon
465+ ? `${ stat . formattedValue } (${ stat . carbon } )`
466+ : stat . formattedValue ;
467+ return `${ stat . label } : ${ value } ` ;
468+ }
469+
470+ /** The ` - stat; stat [N samples]` trailer shared by counter list and profile info. */
471+ function formatCounterStats ( counter : CounterSummary ) : string {
472+ const stats =
473+ counter . stats . length > 0
474+ ? ` - ${ counter . stats . map ( formatCounterStatInline ) . join ( '; ' ) } `
475+ : '' ;
476+ return `${ stats } [${ counter . rangeSampleCount } samples]` ;
477+ }
478+
479+ function formatCounterSummaryLine ( counter : CounterSummary ) : string {
480+ return ` ${ counter . counterHandle } : ${ counter . label } (${ counter . category } )${ formatCounterStats ( counter ) } ` ;
481+ }
482+
483+ /**
484+ * Format a CounterListResult as plain text.
485+ */
486+ export function formatCounterListResult (
487+ result : WithContext < CounterListResult >
488+ ) : string {
489+ const contextHeader = formatContextHeader ( result . context ) ;
490+ if ( result . counters . length === 0 ) {
491+ return `${ contextHeader } \n\nNo counters in this profile.` ;
492+ }
493+ const lines = result . counters . map ( formatCounterSummaryLine ) ;
494+ return `${ contextHeader } \n\nCounters (${ result . counters . length } ):\n${ lines . join ( '\n' ) } ` ;
495+ }
496+
497+ /**
498+ * Format a CounterInfoResult as plain text.
499+ */
500+ export function formatCounterInfoResult (
501+ result : WithContext < CounterInfoResult >
502+ ) : string {
503+ const contextHeader = formatContextHeader ( result . context ) ;
504+ const lines = [
505+ contextHeader ,
506+ '' ,
507+ `Counter ${ result . counterHandle } : ${ result . label } ` ,
508+ ` Name: ${ result . name } ` ,
509+ ` Category: ${ result . category } ` ,
510+ ] ;
511+ if ( result . description ) {
512+ lines . push ( ` Description: ${ result . description } ` ) ;
513+ }
514+ lines . push ( ` Unit: ${ result . unit || '(none)' } ` ) ;
515+ lines . push ( ` Graph type: ${ result . graphType } ` ) ;
516+ lines . push (
517+ ` Main thread: ${ result . mainThreadHandle } (${ result . mainThreadName } )`
518+ ) ;
519+ lines . push (
520+ ` Samples: ${ result . sampleCount } total, ${ result . rangeSampleCount } in current range`
521+ ) ;
522+ if ( result . rangeStart !== null && result . rangeEnd !== null ) {
523+ const zeroAt = result . context . rootRange . start ;
524+ lines . push (
525+ ` Time span: ${ formatDuration ( result . rangeStart - zeroAt ) } → ${ formatDuration ( result . rangeEnd - zeroAt ) } `
526+ ) ;
527+ }
528+ if ( result . stats . length > 0 ) {
529+ lines . push ( ' Stats (current range):' ) ;
530+ for ( const stat of result . stats ) {
531+ const value = stat . carbon
532+ ? `${ stat . formattedValue } (${ stat . carbon } )`
533+ : stat . formattedValue ;
534+ lines . push ( ` ${ stat . label } : ${ value } ` ) ;
535+ }
536+ }
537+ return lines . join ( '\n' ) ;
538+ }
539+
454540/**
455541 * Helper function to format a call tree node recursively.
456542 *
0 commit comments