1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
use std::collections::HashMap;
use regex::Regex;
use crate::ir::{FrameCategory, FrameId, FrameKind, ProfileIR};
use super::{CalleeStats, CallerCalleeAnalyzer, CallerStats};
/// Statistics for a single function
#[derive(Debug, Clone)]
pub struct FunctionStats {
/// Frame ID
pub frame_id: FrameId,
/// Function name
pub name: String,
/// Source location
pub location: String,
/// Category
pub category: FrameCategory,
/// Self time (time spent directly in this function)
pub self_time: u64,
/// Total time (including callees)
pub total_time: u64,
/// Number of samples where this was the leaf
pub self_samples: u32,
/// Number of samples where this appeared in the stack
pub total_samples: u32,
/// Maximum recursion depth observed (0 = not recursive)
pub max_recursion_depth: u32,
/// Samples where this function was called recursively
pub recursive_samples: u32,
/// Timestamp when this function first appeared in samples (microseconds)
pub first_seen_us: u64,
/// Timestamp when this function last appeared in samples (microseconds)
pub last_seen_us: u64,
}
impl FunctionStats {
/// Calculate self time as percentage of total profile time
#[expect(clippy::cast_precision_loss)]
pub fn self_percent(&self, total: u64) -> f64 {
if total == 0 {
0.0
} else {
(self.self_time as f64 / total as f64) * 100.0
}
}
/// Calculate total time as percentage of total profile time
#[expect(clippy::cast_precision_loss)]
pub fn total_percent(&self, total: u64) -> f64 {
if total == 0 {
0.0
} else {
(self.total_time as f64 / total as f64) * 100.0
}
}
/// Average self time per sample (microseconds)
#[expect(clippy::cast_precision_loss)]
pub fn avg_time_per_sample(&self) -> f64 {
if self.self_samples == 0 {
0.0
} else {
self.self_time as f64 / self.self_samples as f64
}
}
/// Classify the function's performance pattern
pub fn performance_pattern(&self, total_samples: usize) -> PerformancePattern {
let call_frequency = if total_samples > 0 {
self.self_samples as f64 / total_samples as f64
} else {
0.0
};
let avg_time = self.avg_time_per_sample();
// High frequency = appears in >1% of samples
// High cost = >1ms average per sample
let high_frequency = call_frequency > 0.01;
let high_cost = avg_time > 1000.0; // 1ms in microseconds
match (high_frequency, high_cost) {
(true, true) => PerformancePattern::CriticalPath,
(false, true) => PerformancePattern::ExpensiveOperation,
(true, false) => PerformancePattern::FrequentlyCalled,
(false, false) => PerformancePattern::Normal,
}
}
/// Check if function shows recursive behavior
pub fn is_recursive(&self) -> bool {
self.max_recursion_depth > 0
}
/// Wall clock span from first to last appearance (microseconds)
pub fn active_span_us(&self) -> u64 {
self.last_seen_us.saturating_sub(self.first_seen_us)
}
/// Estimated time spent in async operations (span minus CPU time)
pub fn async_wait_us(&self) -> u64 {
self.active_span_us().saturating_sub(self.total_time)
}
/// Ratio of active span to CPU time (>2.0 suggests async-heavy)
#[expect(clippy::cast_precision_loss)]
pub fn async_ratio(&self) -> f64 {
if self.total_time == 0 {
return 0.0;
}
self.active_span_us() as f64 / self.total_time as f64
}
/// Whether this function appears to be async-heavy
/// Async-heavy if: span > 2x CPU time AND async wait > 100ms
pub fn is_async_heavy(&self) -> bool {
self.async_ratio() > 2.0 && self.async_wait_us() > 100_000
}
}
/// Classification of function performance patterns
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PerformancePattern {
/// High frequency + high cost per call - critical optimization target
CriticalPath,
/// Low frequency but expensive per call - optimize the operation itself
ExpensiveOperation,
/// High frequency but cheap per call - "death by 1000 cuts", reduce call count
FrequentlyCalled,
/// Neither particularly frequent nor expensive
Normal,
}
/// Time breakdown by category (self time - when category is at leaf)
#[derive(Debug, Clone, Default)]
pub struct CategoryBreakdown {
pub app: u64,
pub deps: u64,
pub node_internal: u64,
pub v8_internal: u64,
pub native: u64,
}
/// Inclusive time breakdown by category (when category appears anywhere in stack)
#[derive(Debug, Clone, Default)]
pub struct CategoryBreakdownInclusive {
pub app: u64,
pub deps: u64,
pub node_internal: u64,
pub v8_internal: u64,
pub native: u64,
}
impl CategoryBreakdownInclusive {
/// Get total time
pub fn total(&self) -> u64 {
self.app + self.deps + self.node_internal + self.v8_internal + self.native
}
}
/// Time spent when one category calls another
#[derive(Debug, Clone, Default)]
pub struct CategoryCallFlow {
/// Map from (caller, callee) -> time spent in callee when called by caller
pub calls: std::collections::HashMap<(FrameCategory, FrameCategory), u64>,
}
impl CategoryCallFlow {
/// Get time spent when caller calls callee
pub fn get(&self, caller: FrameCategory, callee: FrameCategory) -> u64 {
self.calls.get(&(caller, callee)).copied().unwrap_or(0)
}
/// Get all callees for a given caller category, sorted by time descending
pub fn callees_for(&self, caller: FrameCategory) -> Vec<(FrameCategory, u64)> {
let mut result: Vec<_> = self
.calls
.iter()
.filter(|((c, _), _)| *c == caller)
.map(|((_, callee), &time)| (*callee, time))
.collect();
result.sort_by(|a, b| b.1.cmp(&a.1));
result
}
}
impl CategoryBreakdown {
/// Get total time
pub fn total(&self) -> u64 {
self.app + self.deps + self.node_internal + self.v8_internal + self.native
}
/// Get percentage for a category
#[expect(clippy::cast_precision_loss)]
pub fn percent(&self, category: FrameCategory) -> f64 {
let total = self.total();
if total == 0 {
return 0.0;
}
let value = match category {
FrameCategory::App => self.app,
FrameCategory::Deps => self.deps,
FrameCategory::NodeInternal => self.node_internal,
FrameCategory::V8Internal => self.v8_internal,
FrameCategory::Native => self.native,
};
(value as f64 / total as f64) * 100.0
}
}
/// A hot path (frequently executed call stack)
#[derive(Debug, Clone)]
pub struct HotPath {
/// Frame IDs from root to leaf
pub frames: Vec<FrameId>,
/// Total time spent in this exact path
pub time: u64,
/// Percentage of total time
pub percent: f64,
/// Number of samples with this exact path
pub sample_count: u32,
/// First sample timestamp where this path appeared (microseconds)
pub first_seen_us: u64,
/// Last sample timestamp where this path appeared (microseconds)
pub last_seen_us: u64,
}
impl HotPath {
/// Get the wall-clock span from first to last appearance (microseconds)
pub fn active_span_us(&self) -> u64 {
self.last_seen_us.saturating_sub(self.first_seen_us)
}
/// Get estimated async wait time (span minus CPU time)
pub fn async_wait_us(&self) -> u64 {
self.active_span_us().saturating_sub(self.time)
}
/// Get ratio of active span to CPU time (>1 suggests async operations)
pub fn async_ratio(&self) -> f64 {
if self.time == 0 {
return 0.0;
}
self.active_span_us() as f64 / self.time as f64
}
/// Check if this path is async-heavy (span > 2x CPU time AND > 100ms wait)
pub fn is_async_heavy(&self) -> bool {
self.async_ratio() > 2.0 && self.async_wait_us() > 100_000
}
}
/// Statistics aggregated by source file
#[derive(Debug, Clone)]
pub struct FileStats {
/// Source file path
pub file: String,
/// Self time in this file
pub self_time: u64,
/// Total time (inclusive) in this file
pub total_time: u64,
/// Number of calls (sample appearances)
pub call_count: u32,
/// Primary category of frames in this file
pub category: FrameCategory,
}
/// Statistics aggregated by npm package
#[derive(Debug, Clone)]
pub struct PackageStats {
/// Package name (e.g., "vitest@4.0.15")
pub package: String,
/// Total time spent in this package
pub time: u64,
/// Percentage of all dependency time
pub percent_of_deps: f64,
/// Top function by self time in this package
pub top_function: String,
/// Top function location
pub top_function_location: String,
}
/// Detailed analysis for a hot function (caller/callee attribution)
#[derive(Debug, Clone)]
pub struct HotFunctionDetail {
/// Frame ID of the hot function
pub frame_id: FrameId,
/// Function name
pub name: String,
/// Source location
pub location: String,
/// Self time of this function
pub self_time: u64,
/// Total (inclusive) time of this function
pub total_time: u64,
/// Top callers (functions that call this one)
pub callers: Vec<CallerStats>,
/// Top callees (functions called by this one)
pub callees: Vec<CalleeStats>,
/// First sample timestamp where this function appeared (microseconds)
pub first_seen_us: u64,
/// Last sample timestamp where this function appeared (microseconds)
pub last_seen_us: u64,
}
impl HotFunctionDetail {
/// Get the wall-clock span from first to last appearance (microseconds)
pub fn active_span_us(&self) -> u64 {
self.last_seen_us.saturating_sub(self.first_seen_us)
}
/// Check if this function is async-heavy (span > 2x CPU time AND > 100ms wait)
pub fn is_async_heavy(&self) -> bool {
let async_wait = self.active_span_us().saturating_sub(self.total_time);
let ratio = if self.total_time > 0 {
self.active_span_us() as f64 / self.total_time as f64
} else {
0.0
};
ratio > 2.0 && async_wait > 100_000
}
}
/// Profile metadata for the report header
#[derive(Debug, Clone, Default)]
pub struct ProfileMetadata {
/// Source profile file name
pub source_file: Option<String>,
/// Profile duration in milliseconds (CPU time from samples)
pub duration_ms: f64,
/// Wall-clock duration in milliseconds (actual elapsed time)
pub wall_time_ms: Option<f64>,
/// Total sample count
pub sample_count: usize,
/// Average sample interval in milliseconds
pub sample_interval_ms: f64,
/// Whether internal frames are filtered
pub internals_filtered: bool,
/// Number of external sourcemaps loaded
pub sourcemaps_loaded: usize,
/// Number of inline sourcemaps found
pub sourcemaps_inline: usize,
/// Focused package name (if package filter is active)
pub focus_package: Option<String>,
/// Number of profiles merged (1 = single, >1 = merged from multiple processes)
pub profiles_merged: usize,
/// Categories being filtered to (empty = all categories)
pub filter_categories: Vec<FrameCategory>,
}
impl ProfileMetadata {
/// Calculate CPU utilization (CPU time / wall time)
/// Returns a value between 0.0 and 1.0+ (can exceed 1.0 for merged profiles)
#[expect(clippy::cast_precision_loss)]
pub fn cpu_utilization(&self) -> Option<f64> {
self.wall_time_ms.map(|wall| {
if wall > 0.0 {
self.duration_ms / wall
} else {
0.0
}
})
}
}
/// Time-based phase of profile execution
#[derive(Debug, Clone)]
pub struct PhaseAnalysis {
/// Startup phase (first 10% of samples or first 500ms, whichever is smaller)
pub startup: PhaseStats,
/// Steady state (middle portion)
pub steady_state: PhaseStats,
/// Total profile duration in microseconds
pub total_duration_us: u64,
}
/// Statistics for a single phase
#[derive(Debug, Clone)]
pub struct PhaseStats {
/// Phase name
pub name: String,
/// Start time (microseconds from profile start)
pub start_us: u64,
/// End time (microseconds from profile start)
pub end_us: u64,
/// Number of samples in this phase
pub sample_count: usize,
/// Top functions by self time in this phase
pub top_functions: Vec<PhaseFunctionStats>,
/// Category breakdown for this phase
pub category_breakdown: CategoryBreakdown,
}
/// Function stats within a specific phase
#[derive(Debug, Clone)]
pub struct PhaseFunctionStats {
/// Function name
pub name: String,
/// Location
pub location: String,
/// Self time in this phase
pub self_time: u64,
/// Percentage of phase time
pub percent: f64,
/// Category
pub category: FrameCategory,
}
/// Functions detected with recursive call patterns
#[derive(Debug, Clone)]
pub struct RecursiveFunctionStats {
/// Function name
pub name: String,
/// Location
pub location: String,
/// Maximum recursion depth observed
pub max_depth: u32,
/// Number of samples with recursion
pub recursive_samples: u32,
/// Total samples for this function
pub total_samples: u32,
/// Time spent in recursive calls
pub recursive_time: u64,
/// Total self time
pub total_self_time: u64,
}
/// GC analysis with allocation hotspots
#[derive(Debug, Clone)]
pub struct GcAnalysis {
/// Total GC time in microseconds
pub total_time: u64,
/// Number of samples where GC was active
pub sample_count: u32,
/// Average GC pause duration (time / samples)
pub avg_pause_us: u64,
/// Functions frequently on stack during GC (likely allocators)
pub allocation_hotspots: Vec<AllocationHotspot>,
/// GC time during startup phase
pub startup_gc_time: u64,
/// GC time during steady state
pub steady_gc_time: u64,
}
/// Function that appears frequently during GC (likely allocating)
#[derive(Debug, Clone)]
pub struct AllocationHotspot {
/// Function name
pub name: String,
/// Location
pub location: String,
/// Category
pub category: FrameCategory,
/// Times this function was on stack during GC
pub gc_samples: u32,
/// Total samples for this function
pub total_samples: u32,
/// Percentage of GC samples where this function appeared
pub gc_correlation: f64,
}
/// Result of CPU profile analysis
#[derive(Debug)]
pub struct CpuAnalysis {
/// Total profile time in microseconds
pub total_time: u64,
/// Total number of samples
pub total_samples: usize,
/// Per-function statistics (sorted by self time)
pub functions: Vec<FunctionStats>,
/// Per-function statistics sorted by total (inclusive) time
pub functions_by_total: Vec<FunctionStats>,
/// Time breakdown by category (self time)
pub category_breakdown: CategoryBreakdown,
/// Time breakdown by category (inclusive time)
pub category_breakdown_inclusive: CategoryBreakdownInclusive,
/// Call flow between categories
pub category_call_flow: CategoryCallFlow,
/// Hot paths (top call stacks by time)
pub hot_paths: Vec<HotPath>,
/// Statistics aggregated by source file
pub file_stats: Vec<FileStats>,
/// Statistics aggregated by npm package
pub package_stats: Vec<PackageStats>,
/// Detailed caller/callee analysis for top hot functions
pub hot_function_details: Vec<HotFunctionDetail>,
/// Time spent in GC frames (microseconds)
pub gc_time: u64,
/// Enhanced GC analysis with allocation hotspots
pub gc_analysis: Option<GcAnalysis>,
/// Time spent in native addon frames (microseconds)
pub native_time: u64,
/// Profile metadata
pub metadata: ProfileMetadata,
/// Phase analysis (startup vs steady state)
pub phase_analysis: Option<PhaseAnalysis>,
/// Functions with recursive call patterns
pub recursive_functions: Vec<RecursiveFunctionStats>,
}
/// Analyzer for CPU profiles
pub struct CpuAnalyzer {
/// Minimum percentage to include in results
min_percent: f64,
/// Maximum number of functions to return
top_n: usize,
/// Whether to include internal frames
include_internals: bool,
/// Categories to include (empty = all)
filter_categories: Vec<FrameCategory>,
/// Focus on a specific npm package
filter_package: Option<String>,
/// Focus on functions matching this pattern (regex)
focus_pattern: Option<Regex>,
/// Exclude functions matching this pattern (regex)
exclude_pattern: Option<Regex>,
}
impl CpuAnalyzer {
/// Create a new analyzer with default settings
pub fn new() -> Self {
Self {
min_percent: 0.0,
top_n: 50,
include_internals: false,
filter_categories: vec![],
filter_package: None,
focus_pattern: None,
exclude_pattern: None,
}
}
/// Set minimum percentage threshold
pub fn min_percent(mut self, percent: f64) -> Self {
self.min_percent = percent;
self
}
/// Set maximum number of results
pub fn top_n(mut self, n: usize) -> Self {
self.top_n = n;
self
}
/// Include internal frames (Node.js, V8, Native)
pub fn include_internals(mut self, include: bool) -> Self {
self.include_internals = include;
self
}
/// Filter to specific categories
pub fn filter_categories(mut self, categories: Vec<FrameCategory>) -> Self {
self.filter_categories = categories;
self
}
/// Focus analysis on a specific npm package
pub fn filter_package(mut self, package: String) -> Self {
self.filter_package = Some(package);
self
}
/// Focus on functions matching this regex pattern
pub fn focus(mut self, pattern: Regex) -> Self {
self.focus_pattern = Some(pattern);
self
}
/// Exclude functions matching this regex pattern
pub fn exclude(mut self, pattern: Regex) -> Self {
self.exclude_pattern = Some(pattern);
self
}
/// Check if a file path belongs to the target package
fn is_in_package(file: &str, package: &str) -> bool {
// Handle various node_modules path formats:
// - node_modules/package-name/
// - node_modules/@scope/package-name/
// - .pnpm/package-name@version/node_modules/package-name/
// - .pnpm/@scope+package-name@version/node_modules/@scope/package-name/
// Direct match in path
if file.contains(&format!("/{package}/"))
|| file.contains(&format!("/{package}:"))
|| file.ends_with(&format!("/{package}"))
{
return true;
}
// For pnpm, also check the .pnpm directory format
// e.g., .pnpm/prettier-plugin-tailwindcss@0.5.0/
if file.contains(".pnpm/") {
let pnpm_pattern = format!(".pnpm/{package}@");
let pnpm_scoped = format!(".pnpm/{}+", package.replace('/', "+"));
if file.contains(&pnpm_pattern) || file.contains(&pnpm_scoped) {
return true;
}
}
false
}
/// Check if a frame should be included based on all filter settings.
/// This is the single source of truth for filtering logic.
fn should_include_frame(&self, frame: &crate::ir::Frame) -> bool {
// Apply category filter
if !self.filter_categories.is_empty() && !self.filter_categories.contains(&frame.category) {
return false;
}
// Apply internals filter (unless package filter is active)
if self.filter_package.is_none() && !self.include_internals && frame.category.is_internal()
{
return false;
}
// Apply package filter
if let Some(ref pkg) = self.filter_package {
if let Some(ref file) = frame.file {
if !Self::is_in_package(file, pkg) {
return false;
}
} else {
return false;
}
}
let name = frame.display_name();
let location = frame.location();
// Apply focus pattern (must match name or location)
if let Some(ref pattern) = self.focus_pattern {
if !pattern.is_match(&name) && !pattern.is_match(&location) {
return false;
}
}
// Apply exclude pattern (must not match name or location)
if let Some(ref pattern) = self.exclude_pattern {
if pattern.is_match(&name) || pattern.is_match(&location) {
return false;
}
}
true
}
/// Analyze a profile
#[expect(clippy::cast_precision_loss)]
#[expect(clippy::too_many_lines)]
pub fn analyze(&self, profile: &ProfileIR) -> CpuAnalysis {
let total_time = profile.total_weight();
let total_samples = profile.sample_count();
// Aggregate times per frame
let mut self_times: HashMap<FrameId, u64> = HashMap::new();
let mut total_times: HashMap<FrameId, u64> = HashMap::new();
let mut self_counts: HashMap<FrameId, u32> = HashMap::new();
let mut total_counts: HashMap<FrameId, u32> = HashMap::new();
let mut category_breakdown = CategoryBreakdown::default();
let mut category_breakdown_inclusive = CategoryBreakdownInclusive::default();
let mut category_call_flow = CategoryCallFlow::default();
let mut stack_times: HashMap<Vec<FrameId>, (u64, u32, u64, u64)> = HashMap::new(); // (time, sample_count, first_seen, last_seen)
// Track GC and native time
let mut gc_time: u64 = 0;
let mut gc_samples: u32 = 0;
let mut gc_stack_frames: HashMap<FrameId, u32> = HashMap::new(); // frame_id -> gc_sample_count
let mut gc_sample_timestamps: Vec<u64> = Vec::new(); // timestamps of GC samples
let mut native_time: u64 = 0;
// Track recursion: frame_id -> (max_depth, recursive_sample_count, recursive_time)
let mut recursion_stats: HashMap<FrameId, (u32, u32, u64)> = HashMap::new();
// Track phase analysis data: (timestamp, sample_index)
let mut sample_timestamps: Vec<(u64, usize)> = Vec::new();
// Track file-level aggregations
let mut file_self_times: HashMap<String, u64> = HashMap::new();
let mut file_total_times: HashMap<String, u64> = HashMap::new();
let mut file_call_counts: HashMap<String, u32> = HashMap::new();
let mut file_categories: HashMap<String, FrameCategory> = HashMap::new();
// Track package-level aggregations (for deps only)
let mut package_times: HashMap<String, u64> = HashMap::new();
let mut package_top_funcs: HashMap<String, (String, String, u64)> = HashMap::new(); // (name, location, self_time)
// Track temporal bounds for async analysis: frame_id -> timestamp
let mut first_seen: HashMap<FrameId, u64> = HashMap::new();
let mut last_seen: HashMap<FrameId, u64> = HashMap::new();
for (sample_idx, sample) in profile.samples.iter().enumerate() {
let weight = sample.weight;
// Track sample timestamp for phase analysis
sample_timestamps.push((sample.timestamp_us, sample_idx));
if let Some(stack) = profile.get_stack(sample.stack_id) {
// Detect recursion: count occurrences of each frame in this stack
let mut frame_counts: HashMap<FrameId, u32> = HashMap::new();
for &frame_id in &stack.frames {
*frame_counts.entry(frame_id).or_default() += 1;
}
// Update recursion stats for frames that appear multiple times
for (&frame_id, &count) in &frame_counts {
if count > 1 {
let depth = count - 1; // Recursion depth is count - 1
let entry = recursion_stats.entry(frame_id).or_insert((0, 0, 0));
entry.0 = entry.0.max(depth); // Max depth
entry.1 += 1; // Recursive sample count
entry.2 += weight; // Recursive time
}
}
// Track temporal bounds for each frame in the stack (for async analysis)
let timestamp = sample.timestamp_us;
for &frame_id in &stack.frames {
// Update first_seen (only if not already set)
first_seen.entry(frame_id).or_insert(timestamp);
// Always update last_seen to track the latest appearance
last_seen.insert(frame_id, timestamp);
}
// Leaf frame gets self time
if let Some(&leaf_frame) = stack.frames.last() {
*self_times.entry(leaf_frame).or_default() += weight;
*self_counts.entry(leaf_frame).or_default() += 1;
// Attribute to category based on leaf frame
if let Some(frame) = profile.get_frame(leaf_frame) {
match frame.category {
FrameCategory::App => category_breakdown.app += weight,
FrameCategory::Deps => category_breakdown.deps += weight,
FrameCategory::NodeInternal => {
category_breakdown.node_internal += weight;
}
FrameCategory::V8Internal => category_breakdown.v8_internal += weight,
FrameCategory::Native => category_breakdown.native += weight,
}
// Track GC and native time based on FrameKind
match frame.kind {
FrameKind::GC => {
gc_time += weight;
gc_samples += 1;
gc_sample_timestamps.push(sample.timestamp_us);
// Track all frames on stack during GC (potential allocators)
for &frame_id in &stack.frames {
if frame_id != leaf_frame {
// Exclude the GC frame itself
*gc_stack_frames.entry(frame_id).or_default() += 1;
}
}
}
FrameKind::Native => native_time += weight,
_ => {}
}
// File-level self time
if let Some(file) = frame.clean_file() {
*file_self_times.entry(file.clone()).or_default() += weight;
file_categories.entry(file).or_insert(frame.category);
}
// Package-level aggregation for deps
if frame.category == FrameCategory::Deps {
if let Some(file) = frame.clean_file() {
if let Some(pkg) = Self::extract_package_name(&file) {
*package_times.entry(pkg.clone()).or_default() += weight;
// Track top function by self time for this package
let current_self =
self_times.get(&frame.id).copied().unwrap_or(0);
package_top_funcs
.entry(pkg)
.and_modify(|(name, loc, time)| {
if weight > *time {
*name = frame.display_name().to_string();
*loc = frame.location();
*time = weight;
}
})
.or_insert((
frame.display_name().to_string(),
frame.location(),
current_self,
));
}
}
}
}
}
// All frames get total time
// Track which categories appear in this stack (for inclusive breakdown)
let mut stack_has_app = false;
let mut stack_has_deps = false;
let mut stack_has_node = false;
let mut stack_has_v8 = false;
let mut stack_has_native = false;
// Track category transitions for call flow
let mut prev_category: Option<FrameCategory> = None;
for &frame_id in &stack.frames {
*total_times.entry(frame_id).or_default() += weight;
*total_counts.entry(frame_id).or_default() += 1;
// File-level total time
if let Some(frame) = profile.get_frame(frame_id) {
if let Some(file) = frame.clean_file() {
*file_total_times.entry(file.clone()).or_default() += weight;
*file_call_counts.entry(file.clone()).or_default() += 1;
file_categories.entry(file).or_insert(frame.category);
}
// Track categories in this stack
match frame.category {
FrameCategory::App => stack_has_app = true,
FrameCategory::Deps => stack_has_deps = true,
FrameCategory::NodeInternal => stack_has_node = true,
FrameCategory::V8Internal => stack_has_v8 = true,
FrameCategory::Native => stack_has_native = true,
}
// Track call flow: when category changes, record the transition
if let Some(prev) = prev_category {
if prev != frame.category {
*category_call_flow
.calls
.entry((prev, frame.category))
.or_default() += weight;
}
}
prev_category = Some(frame.category);
}
}
// Attribute inclusive time to categories that appear in this stack
if stack_has_app {
category_breakdown_inclusive.app += weight;
}
if stack_has_deps {
category_breakdown_inclusive.deps += weight;
}
if stack_has_node {
category_breakdown_inclusive.node_internal += weight;
}
if stack_has_v8 {
category_breakdown_inclusive.v8_internal += weight;
}
if stack_has_native {
category_breakdown_inclusive.native += weight;
}
// Track stack times for hot paths (time, sample_count, first_seen, last_seen)
let timestamp = sample.timestamp_us;
let entry = stack_times
.entry(stack.frames.clone())
.or_insert((0, 0, timestamp, timestamp));
entry.0 += weight;
entry.1 += 1;
// Update last_seen to track the latest appearance
entry.3 = timestamp;
}
}
// Build function stats (unfiltered for internal use)
let all_functions: Vec<FunctionStats> = profile
.frames
.iter()
.filter_map(|frame| {
let self_time = self_times.get(&frame.id).copied().unwrap_or(0);
let frame_total_time = total_times.get(&frame.id).copied().unwrap_or(0);
// Skip if no time
if self_time == 0 && frame_total_time == 0 {
return None;
}
let (max_depth, rec_samples, _) =
recursion_stats.get(&frame.id).copied().unwrap_or((0, 0, 0));
Some(FunctionStats {
frame_id: frame.id,
name: frame.display_name().to_string(),
location: frame.location(),
category: frame.category,
self_time,
total_time: frame_total_time,
self_samples: self_counts.get(&frame.id).copied().unwrap_or(0),
total_samples: total_counts.get(&frame.id).copied().unwrap_or(0),
max_recursion_depth: max_depth,
recursive_samples: rec_samples,
first_seen_us: first_seen.get(&frame.id).copied().unwrap_or(0),
last_seen_us: last_seen.get(&frame.id).copied().unwrap_or(0),
})
})
.collect();
// Apply filters for the user-facing list (using unified filter)
let mut functions: Vec<FunctionStats> = all_functions
.iter()
.filter(|func| {
// Look up original frame to use unified filter
if let Some(frame) = profile.get_frame(func.frame_id) {
if !self.should_include_frame(frame) {
return false;
}
} else {
return false;
}
// Apply min percent filter (separate from frame-based filters)
let self_pct = if total_time > 0 {
(func.self_time as f64 / total_time as f64) * 100.0
} else {
0.0
};
if self_pct < self.min_percent && self.min_percent > 0.0 {
return false;
}
true
})
.cloned()
.collect();
// Sort by self time descending
functions.sort_by(|a, b| {
b.self_time
.cmp(&a.self_time)
.then_with(|| a.name.cmp(&b.name))
});
functions.truncate(self.top_n);
// Build functions_by_total (sorted by inclusive time)
let mut functions_by_total = functions.clone();
functions_by_total.sort_by(|a, b| {
b.total_time
.cmp(&a.total_time)
.then_with(|| a.name.cmp(&b.name))
});
// Build hot paths
let mut hot_paths: Vec<HotPath> = stack_times
.into_iter()
.filter(|(frames, _)| {
// Filter out stacks with only internal frames
if !self.include_internals {
frames.iter().any(|&fid| {
profile
.get_frame(fid)
.is_some_and(|f| !f.category.is_internal())
})
} else {
true
}
})
.map(
|(frames, (time, sample_count, first_seen_us, last_seen_us))| {
let percent = if total_time > 0 {
(time as f64 / total_time as f64) * 100.0
} else {
0.0
};
HotPath {
frames,
time,
percent,
sample_count,
first_seen_us,
last_seen_us,
}
},
)
.collect();
// Sort by CPU time descending
hot_paths.sort_by(|a, b| b.time.cmp(&a.time));
// Deduplicate: remove paths that are prefixes of other paths
// (e.g., A->B->C and A->B are duplicates, keep the longer one)
let hot_paths = Self::deduplicate_prefix_paths(hot_paths);
let hot_paths: Vec<_> = hot_paths.into_iter().take(10).collect();
// Build file stats
let mut file_stats: Vec<FileStats> = file_total_times
.iter()
.map(|(file, &file_total_time)| {
let self_time = file_self_times.get(file).copied().unwrap_or(0);
let call_count = file_call_counts.get(file).copied().unwrap_or(0);
let category = file_categories
.get(file)
.copied()
.unwrap_or(FrameCategory::App);
FileStats {
file: file.clone(),
self_time,
total_time: file_total_time,
call_count,
category,
}
})
.filter(|fs| {
// Filter internal files if not including internals
if !self.include_internals && fs.category.is_internal() {
return false;
}
true
})
.collect();
file_stats.sort_by(|a, b| {
b.self_time
.cmp(&a.self_time)
.then_with(|| a.file.cmp(&b.file))
});
file_stats.truncate(20); // Top 20 files
// Build package stats
let total_deps_time = category_breakdown.deps;
let mut package_stats: Vec<PackageStats> = package_times
.iter()
.map(|(pkg, &time)| {
let (top_func, top_loc, _) = package_top_funcs
.get(pkg)
.cloned()
.unwrap_or_else(|| ("(unknown)".to_string(), "(unknown)".to_string(), 0));
let percent_of_deps = if total_deps_time > 0 {
(time as f64 / total_deps_time as f64) * 100.0
} else {
0.0
};
PackageStats {
package: pkg.clone(),
time,
percent_of_deps,
top_function: top_func,
top_function_location: top_loc,
}
})
.collect();
package_stats.sort_by(|a, b| b.time.cmp(&a.time).then_with(|| a.package.cmp(&b.package)));
package_stats.truncate(15); // Top 15 packages
// Build hot function details (caller/callee for top N functions)
let caller_callee_analyzer = CallerCalleeAnalyzer::new().top_n(5);
let hot_function_details: Vec<HotFunctionDetail> = functions
.iter()
.take(5) // Top 5 hot functions
.filter_map(|func| {
caller_callee_analyzer
.analyze(profile, func.frame_id)
.map(|analysis| HotFunctionDetail {
frame_id: func.frame_id,
name: func.name.clone(),
location: func.location.clone(),
self_time: func.self_time,
total_time: func.total_time,
callers: analysis.callers,
callees: analysis.callees,
first_seen_us: func.first_seen_us,
last_seen_us: func.last_seen_us,
})
})
.collect();
// Build recursive functions list (using unified filter)
let mut recursive_functions: Vec<RecursiveFunctionStats> = recursion_stats
.iter()
.filter_map(|(&frame_id, &(max_depth, rec_samples, rec_time))| {
if max_depth == 0 {
return None;
}
let frame = profile.get_frame(frame_id)?;
// Use unified filter method
if !self.should_include_frame(frame) {
return None;
}
let total_self = self_times.get(&frame_id).copied().unwrap_or(0);
// Use total_counts (samples where function appears anywhere in stack)
// not self_counts (samples where function is at leaf)
let total_samp = total_counts.get(&frame_id).copied().unwrap_or(0);
Some(RecursiveFunctionStats {
name: frame.display_name().to_string(),
location: frame.location(),
max_depth,
recursive_samples: rec_samples,
total_samples: total_samp,
recursive_time: rec_time,
total_self_time: total_self,
})
})
.collect();
recursive_functions.sort_by(|a, b| {
b.recursive_time
.cmp(&a.recursive_time)
.then_with(|| a.name.cmp(&b.name))
});
recursive_functions.truncate(10); // Top 10 recursive functions
// Build phase analysis (startup vs steady state)
let phase_analysis = if !sample_timestamps.is_empty() && total_time > 0 {
// Define startup as first 500ms or first 10% of samples, whichever is smaller
let startup_threshold_us = 500_000u64; // 500ms
let startup_sample_threshold = total_samples / 10;
let startup_end_idx = sample_timestamps
.iter()
.position(|(ts, _)| *ts > startup_threshold_us)
.unwrap_or(sample_timestamps.len())
.min(startup_sample_threshold.max(1));
let startup_end_time = if startup_end_idx < sample_timestamps.len() {
sample_timestamps[startup_end_idx].0
} else {
total_time
};
// Compute stats for each phase (using unified filter)
let startup_stats = self.compute_phase_stats(
profile,
&profile.samples[..startup_end_idx.min(profile.samples.len())],
"Startup",
0,
startup_end_time,
);
let steady_stats = self.compute_phase_stats(
profile,
&profile.samples[startup_end_idx.min(profile.samples.len())..],
"Steady State",
startup_end_time,
total_time,
);
Some(PhaseAnalysis {
startup: startup_stats,
steady_state: steady_stats,
total_duration_us: total_time,
})
} else {
None
};
// Build GC analysis if GC samples exist
let gc_analysis = if gc_samples > 0 {
// Calculate average pause
let avg_pause_us = gc_time / u64::from(gc_samples);
// Build allocation hotspots (functions frequently on stack during GC)
let mut hotspots: Vec<AllocationHotspot> = gc_stack_frames
.iter()
.filter_map(|(&frame_id, &gc_count)| {
let frame = profile.get_frame(frame_id)?;
let total_count = total_counts.get(&frame_id).copied().unwrap_or(0);
// Only include if function appears in significant portion of GC samples
// and is user code (App or Deps), not internals
if gc_count < 2
|| (frame.category != FrameCategory::App
&& frame.category != FrameCategory::Deps)
{
return None;
}
let gc_correlation = f64::from(gc_count) / f64::from(gc_samples) * 100.0;
Some(AllocationHotspot {
name: frame.display_name().to_string(),
location: frame.location(),
category: frame.category,
gc_samples: gc_count,
total_samples: total_count,
gc_correlation,
})
})
.collect();
// Sort by GC correlation (most correlated first), then by name for stable ordering
hotspots.sort_by(|a, b| {
b.gc_correlation
.partial_cmp(&a.gc_correlation)
.unwrap_or(std::cmp::Ordering::Equal)
.then_with(|| a.name.cmp(&b.name))
});
hotspots.truncate(10);
// Calculate GC time in startup vs steady state
let startup_end_time = phase_analysis.as_ref().map_or(0, |p| p.startup.end_us);
let startup_gc_time: u64 = gc_sample_timestamps
.iter()
.filter(|&&ts| ts <= startup_end_time)
.count() as u64
* (gc_time / u64::from(gc_samples).max(1));
let steady_gc_time = gc_time.saturating_sub(startup_gc_time);
Some(GcAnalysis {
total_time: gc_time,
sample_count: gc_samples,
avg_pause_us,
allocation_hotspots: hotspots,
startup_gc_time,
steady_gc_time,
})
} else {
None
};
// Build metadata
let duration_ms = total_time as f64 / 1000.0;
let wall_time_ms = profile.duration_us.map(|us| us as f64 / 1000.0);
let sample_interval_ms = if total_samples > 0 {
duration_ms / total_samples as f64
} else {
0.0
};
let metadata = ProfileMetadata {
source_file: profile.source_file.clone(),
duration_ms,
wall_time_ms,
sample_count: total_samples,
sample_interval_ms,
internals_filtered: !self.include_internals,
sourcemaps_loaded: profile.sourcemaps_resolved,
sourcemaps_inline: 0,
focus_package: self.filter_package.clone(),
profiles_merged: profile.profiles_merged,
filter_categories: self.filter_categories.clone(),
};
CpuAnalysis {
total_time,
total_samples,
functions,
functions_by_total,
category_breakdown,
category_breakdown_inclusive,
category_call_flow,
hot_paths,
file_stats,
package_stats,
hot_function_details,
gc_time,
gc_analysis,
native_time,
metadata,
phase_analysis,
recursive_functions,
}
}
/// Compute statistics for a phase of the profile
#[expect(clippy::cast_precision_loss)]
fn compute_phase_stats(
&self,
profile: &ProfileIR,
samples: &[crate::ir::Sample],
name: &str,
start_us: u64,
end_us: u64,
) -> PhaseStats {
let mut self_times: HashMap<FrameId, u64> = HashMap::new();
let mut category_breakdown = CategoryBreakdown::default();
for sample in samples {
let weight = sample.weight;
if let Some(stack) = profile.get_stack(sample.stack_id) {
if let Some(&leaf_frame) = stack.frames.last() {
*self_times.entry(leaf_frame).or_default() += weight;
if let Some(frame) = profile.get_frame(leaf_frame) {
match frame.category {
FrameCategory::App => category_breakdown.app += weight,
FrameCategory::Deps => category_breakdown.deps += weight,
FrameCategory::NodeInternal => {
category_breakdown.node_internal += weight;
}
FrameCategory::V8Internal => category_breakdown.v8_internal += weight,
FrameCategory::Native => category_breakdown.native += weight,
}
}
}
}
}
let total_phase_time = category_breakdown.total();
// Get top functions for this phase (using unified filter)
let mut top_functions: Vec<PhaseFunctionStats> = self_times
.iter()
.filter_map(|(&frame_id, &self_time)| {
let frame = profile.get_frame(frame_id)?;
// Use unified filter method
if !self.should_include_frame(frame) {
return None;
}
let percent = if total_phase_time > 0 {
(self_time as f64 / total_phase_time as f64) * 100.0
} else {
0.0
};
Some(PhaseFunctionStats {
name: frame.display_name().to_string(),
location: frame.location(),
self_time,
percent,
category: frame.category,
})
})
.collect();
top_functions.sort_by(|a, b| b.self_time.cmp(&a.self_time));
top_functions.truncate(5); // Top 5 functions per phase
PhaseStats {
name: name.to_string(),
start_us,
end_us,
sample_count: samples.len(),
top_functions,
category_breakdown,
}
}
/// Extract package name from a node_modules path
/// Handles both regular packages (lodash) and scoped packages (@vitest/runner)
fn extract_package_name(path: &str) -> Option<String> {
// Look for node_modules in the path
let parts: Vec<&str> = path.split("node_modules/").collect();
if parts.len() < 2 {
return None;
}
let after_node_modules = parts.last()?;
let path_parts: Vec<&str> = after_node_modules.split('/').collect();
if path_parts.is_empty() {
return None;
}
// Handle scoped packages (@org/package)
if path_parts[0].starts_with('@') && path_parts.len() >= 2 {
// For pnpm paths like ".pnpm/@vitest+runner@4.0.15/node_modules/@vitest/runner"
// We want the final package name
let scoped_name = format!("{}/{}", path_parts[0], path_parts[1]);
Some(scoped_name)
} else {
// Regular package, extract up to first @ for version or / for path
let pkg = path_parts[0];
// Handle pnpm format: package@version
if let Some(at_pos) = pkg.find('@') {
if at_pos > 0 {
Some(pkg[..at_pos].to_string())
} else {
Some(pkg.to_string())
}
} else {
Some(pkg.to_string())
}
}
}
/// Deduplicate hot paths by removing paths that are prefixes of other paths.
/// For example, if we have A->B->C and A->B, keep only A->B->C since
/// the shorter path is just a partial capture of the same call chain.
fn deduplicate_prefix_paths(mut paths: Vec<HotPath>) -> Vec<HotPath> {
if paths.len() <= 1 {
return paths;
}
// Sort by length descending so longer paths come first
paths.sort_by(|a, b| b.frames.len().cmp(&a.frames.len()));
let mut result: Vec<HotPath> = Vec::new();
for path in paths {
// Check if this path is a prefix of any path we've already kept
let is_prefix = result.iter().any(|kept| {
// path is a prefix of kept if path.frames matches the start of kept.frames
path.frames.len() < kept.frames.len()
&& path.frames.iter().zip(&kept.frames).all(|(a, b)| a == b)
});
if !is_prefix {
result.push(path);
}
}
// Re-sort by CPU time descending
result.sort_by(|a, b| b.time.cmp(&a.time));
result
}
}
impl Default for CpuAnalyzer {
fn default() -> Self {
Self::new()
}
}