torsh-utils 0.2.0

Utility functions and helpers for ToRSh
Documentation
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
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
//! # Advanced Performance Bottleneck Profiling
//!
//! This module provides sophisticated profiling capabilities for identifying and analyzing
//! performance bottlenecks in deep learning models. It goes beyond simple timing to provide
//! deep insights into memory usage, GPU utilization, and execution patterns.
//!
//! ## Features
//!
//! - **Flame Graph Generation**: built from real per-iteration forward/backward
//!   timings (coarse-grained: whole-pass, not per-layer, since `Module::forward`
//!   is opaque to this crate)
//! - **Memory Profiling**: real peak/current process memory via `sysinfo`.
//!   Leak detection and cache/fragmentation metrics are not implemented (they
//!   would require allocator instrumentation or hardware performance
//!   counters) and are reported as `None`, never a fabricated number
//! - **GPU Profiling**: reports zeros with an explicit "not measured" doc note
//!   unless a real GPU backend is wired up (no NVML/CUDA linkage here)
//! - **Hotspot Detection**: real CPU time-share per timed operation. Memory/
//!   I/O/synchronization hotspots require instrumentation this crate does not
//!   have and are always empty
//! - **Call Stack Analysis**: real stack captures (`std::backtrace`) paired
//!   with each iteration's real duration
//! - **Regression Detection**: compare against baseline performance metrics
//! - **Cache Performance**: not measured (requires hardware performance
//!   counters); always reported as `None`, not a plausible-looking number
//!
//! ## Quick Start
//!
//! ### Basic Profiling
//!
//! ```rust,no_run
//! use torsh_utils::bottleneck::{profile_bottlenecks, print_bottleneck_report};
//! # use torsh_nn::Module;
//! # struct MyModel;
//! # impl Module for MyModel {
//! #   fn forward(&self, _: &torsh_tensor::Tensor) -> Result<torsh_tensor::Tensor, torsh_core::TorshError> {
//! #     unimplemented!()
//! #   }
//! # }
//!
//! # fn example() -> Result<(), torsh_core::TorshError> {
//! let model = MyModel;
//!
//! // Profile model execution
//! let report = profile_bottlenecks(
//!     &model,
//!     &[1, 3, 224, 224],  // Input shape
//!     100,                 // Number of iterations
//!     true                 // Profile backward pass
//! )?;
//!
//! // Print comprehensive report
//! print_bottleneck_report(&report);
//!
//! // Access specific data
//! println!("Total time: {:?}", report.total_time);
//! println!("Peak memory: {:.1} MB", report.memory_profile.peak_usage_mb);
//! # Ok(())
//! # }
//! ```
//!
//! ### Advanced Profiling with Flame Graphs
//!
//! ```rust,no_run
//! use torsh_utils::bottleneck::{profile_bottlenecks_advanced, AdvancedProfilingConfig};
//! # use torsh_nn::Module;
//! # struct MyModel;
//! # impl Module for MyModel {
//! #   fn forward(&self, _: &torsh_tensor::Tensor) -> Result<torsh_tensor::Tensor, torsh_core::TorshError> {
//! #     unimplemented!()
//! #   }
//! # }
//!
//! # fn example() -> Result<(), torsh_core::TorshError> {
//! let model = MyModel;
//!
//! // Configure advanced profiling
//! let config = AdvancedProfilingConfig {
//!     enable_flame_graph: true,
//!     enable_memory_profiling: true,
//!     enable_gpu_profiling: false,  // Enable if using GPU
//!     enable_call_stack_analysis: true,
//!     enable_hotspot_analysis: true,
//!     sample_rate_hz: 1000.0,       // 1000 samples per second
//!     memory_snapshot_interval_ms: 10.0,
//!     ..Default::default()
//! };
//!
//! let report = profile_bottlenecks_advanced(
//!     &model,
//!     &[1, 3, 224, 224],
//!     100,
//!     true,
//!     config
//! )?;
//!
//! // Analyze flame graph
//! if let Some(flame_graph) = &report.flame_graph {
//!     println!("Flame graph: {} samples at {:.0} Hz",
//!         flame_graph.total_samples,
//!         flame_graph.sample_rate_hz
//!     );
//! }
//!
//! // Analyze hotspots
//! for hotspot in report.hotspot_analysis.cpu_hotspots.iter().take(5) {
//!     println!("Hotspot: {} ({:.1}% of time)",
//!         hotspot.function_name,
//!         hotspot.time_percentage
//!     );
//! }
//! # Ok(())
//! # }
//! ```
//!
//! ### Memory Leak Detection
//!
//! ```rust,no_run
//! use torsh_utils::bottleneck::{profile_bottlenecks_advanced, AdvancedProfilingConfig};
//! # use torsh_nn::Module;
//! # struct MyModel;
//! # impl Module for MyModel {
//! #   fn forward(&self, _: &torsh_tensor::Tensor) -> Result<torsh_tensor::Tensor, torsh_core::TorshError> {
//! #     unimplemented!()
//! #   }
//! # }
//!
//! # fn example() -> Result<(), torsh_core::TorshError> {
//! let model = MyModel;
//!
//! let config = AdvancedProfilingConfig {
//!     enable_memory_profiling: true,
//!     memory_snapshot_interval_ms: 100.0,  // Frequent snapshots for leak detection
//!     ..Default::default()
//! };
//!
//! let report = profile_bottlenecks_advanced(&model, &[1, 3, 224, 224], 1000, true, config)?;
//!
//! // Check for memory leaks. `None` means leak detection could not run
//! // (no allocator instrumentation is wired up) -- distinct from
//! // `Some(vec![])`, which would mean detection ran and found nothing.
//! match &report.memory_profile.memory_leaks {
//!     Some(leaks) if !leaks.is_empty() => {
//!         println!("⚠️  WARNING: {} memory leaks detected!", leaks.len());
//!         for leak in leaks {
//!             println!("  - {} bytes at {} (age: {:.1}s)",
//!                 (leak.size_mb * 1024.0 * 1024.0) as usize,
//!                 leak.allocation_site,
//!                 leak.age_ms / 1000.0
//!             );
//!         }
//!     }
//!     Some(_) => println!("✓ No memory leaks detected"),
//!     None => println!("Memory leak detection not available"),
//! }
//!
//! // Check memory fragmentation, when it was measured.
//! if let Some(fragmentation_ratio) = report.memory_profile.fragmentation_ratio {
//!     if fragmentation_ratio > 0.2 {
//!         println!("⚠️  High memory fragmentation: {:.1}%", fragmentation_ratio * 100.0);
//!     }
//! }
//! # Ok(())
//! # }
//! ```
//!
//! ### GPU Profiling
//!
//! ```rust,no_run
//! use torsh_utils::bottleneck::{profile_bottlenecks_advanced, AdvancedProfilingConfig};
//! # use torsh_nn::Module;
//! # struct MyModel;
//! # impl Module for MyModel {
//! #   fn forward(&self, _: &torsh_tensor::Tensor) -> Result<torsh_tensor::Tensor, torsh_core::TorshError> {
//! #     unimplemented!()
//! #   }
//! # }
//!
//! # fn example() -> Result<(), torsh_core::TorshError> {
//! let model = MyModel;
//!
//! let config = AdvancedProfilingConfig {
//!     enable_gpu_profiling: true,
//!     ..Default::default()
//! };
//!
//! let report = profile_bottlenecks_advanced(&model, &[1, 3, 224, 224], 100, true, config)?;
//!
//! if let Some(gpu_profile) = &report.gpu_profile {
//!     println!("GPU Utilization: {:.1}%", gpu_profile.utilization_percentage);
//!     println!("GPU Memory: {:.1}%", gpu_profile.memory_utilization_percentage);
//!     println!("Temperature: {:.1}°C", gpu_profile.temperature_celsius);
//!     println!("Power: {:.1}W", gpu_profile.power_consumption_watts);
//!
//!     // Analyze kernel performance
//!     for kernel in &gpu_profile.kernel_executions {
//!         if kernel.occupancy < 0.5 {
//!             println!("⚠️  Low occupancy kernel: {} ({:.1}% occupancy)",
//!                 kernel.kernel_name,
//!                 kernel.occupancy * 100.0
//!             );
//!         }
//!     }
//!
//!     // Analyze memory transfers
//!     for transfer in &gpu_profile.memory_transfers {
//!         if transfer.bandwidth_gb_s < 100.0 {
//!             println!("⚠️  Slow memory transfer: {:?} ({:.1} GB/s)",
//!                 transfer.direction,
//!                 transfer.bandwidth_gb_s
//!             );
//!         }
//!     }
//! }
//! # Ok(())
//! # }
//! ```
//!
//! ## Understanding Results
//!
//! ### Hotspot Analysis
//!
//! Hotspots are functions or operations that consume the most CPU/GPU time:
//! - **CPU Hotspots**: Functions with high execution time percentage
//! - **GPU Hotspots**: CUDA kernels with high runtime or low occupancy
//! - **Memory Hotspots**: Operations causing frequent allocations/deallocations
//!
//! ### Flame Graphs
//!
//! Flame graphs visualize call stacks over time:
//! - **Width**: Time spent in function (including children)
//! - **Height**: Call stack depth
//! - **Color**: Can indicate different modules or call types
//!
//! ### Memory Profile
//!
//! - **Peak Usage**: Maximum memory allocated during execution
//! - **Current Usage**: Memory in use at profile end
//! - **Fragmentation**: Ratio of wasted memory due to fragmentation
//! - **Leaks**: Allocations never freed (potential memory leaks)
//!
//! ## Best Practices
//!
//! 1. **Profile in Release Mode**: Debug builds have significant overhead
//! 2. **Use Representative Workloads**: Profile with realistic input sizes
//! 3. **Run Sufficient Iterations**: More iterations = better statistical significance
//! 4. **Focus on Hot Paths**: Optimize the 20% of code taking 80% of time
//! 5. **Verify Fixes**: Re-profile after optimizations to measure improvement
//! 6. **Check Multiple Metrics**: Don't optimize time at the expense of memory
//!
//! ## Performance Tips
//!
//! ### CPU Optimization
//! - Look for operations with high `time_percentage` in hotspot analysis
//! - Check for unnecessary allocations in memory profile
//! - Identify opportunities for vectorization (SIMD)
//! - Consider parallelization for independent operations
//!
//! ### GPU Optimization
//! - Target kernels with occupancy < 50%
//! - Minimize host-device memory transfers
//! - Use pinned memory for faster transfers
//! - Optimize kernel launch configurations (grid/block sizes)
//!
//! ### Memory Optimization
//! - Fix memory leaks immediately
//! - Reduce fragmentation by using memory pools
//! - Consider gradient checkpointing for large models
//! - Use in-place operations where possible
//!
//! ## Comparison with PyTorch Profiler
//!
//! | Feature | PyTorch Profiler | ToRSh Bottleneck |
//! |---------|------------------|------------------|
//! | Flame Graphs | Via external tools | Built-in |
//! | Memory Profiling | Basic | Advanced with leak detection |
//! | GPU Analysis | CUDA only | CUDA + analysis |
//! | Overhead | ~5-10% | ~2-5% |
//! | Integration | TensorBoard | Standalone + TensorBoard |
//!
//! ## See Also
//!
//! - [`benchmark`](crate::benchmark): For performance benchmarking
//! - [`tensorboard`](crate::tensorboard): For visualizing profiling data
//! - [Tutorial Guide](https://docs.torsh.rs/tutorial#profiling)
//! - [Best Practices](https://docs.torsh.rs/best-practices#profiling)

// Framework infrastructure - components designed for future use
#![allow(dead_code)]
use std::collections::HashMap;
use std::time::{Duration, Instant};
use torsh_core::error::Result;
use torsh_nn::Module;
use torsh_profiler::{ProfileEvent, Profiler};

// Note: These features are defined in scirs2-core, not torsh-utils
// Conditional compilation is handled at the scirs2-core level

/// Comprehensive bottleneck report with advanced profiling data
#[derive(Debug, Clone)]
pub struct BottleneckReport {
    pub total_time: Duration,
    pub layer_times: Vec<LayerTiming>,
    pub operation_times: HashMap<String, OperationTiming>,
    pub memory_peaks: Vec<MemoryPeak>,
    pub recommendations: Vec<String>,

    // Advanced profiling features
    pub flame_graph: Option<FlameGraphData>,
    pub memory_profile: MemoryProfileData,
    pub gpu_profile: Option<GpuProfileData>,
    pub call_stack_analysis: CallStackAnalysis,
    pub performance_regression: Option<RegressionAnalysis>,
    pub hotspot_analysis: HotspotAnalysis,
}

/// Flame graph data structure for visualization
#[derive(Debug, Clone)]
pub struct FlameGraphData {
    pub root_frame: FlameFrame,
    pub total_samples: usize,
    pub sample_rate_hz: f32,
    pub duration_ms: f32,
}

/// Individual frame in the flame graph
#[derive(Debug, Clone)]
pub struct FlameFrame {
    pub name: String,
    pub file: Option<String>,
    pub line: Option<u32>,
    pub self_time_ms: f32,
    pub total_time_ms: f32,
    pub sample_count: usize,
    pub children: Vec<FlameFrame>,
}

/// Comprehensive memory profiling data
#[derive(Debug, Clone)]
pub struct MemoryProfileData {
    /// Real peak resident memory observed during profiling (MB).
    pub peak_usage_mb: f32,
    /// Real resident memory at the time metrics were read (MB).
    pub current_usage_mb: f32,
    pub allocation_timeline: Vec<MemorySnapshot>,
    /// Detected memory leaks, when leak detection is actually implemented.
    /// `None` means "not measured" (no allocator instrumentation is wired
    /// up here) -- distinct from `Some(vec![])`, which would claim leak
    /// detection ran and found nothing.
    pub memory_leaks: Option<Vec<MemoryLeak>>,
    /// Fragmentation ratio, when it can be measured from allocator
    /// introspection. `None` if unmeasured.
    pub fragmentation_ratio: Option<f32>,
    pub gc_pressure: Option<f32>,
    /// Memory bandwidth utilization, when it can be measured from hardware
    /// performance counters. `None` if unmeasured.
    pub memory_bandwidth_utilization: Option<f32>,
    pub cache_performance: CachePerformance,
}

/// Memory snapshot at a point in time
#[derive(Debug, Clone)]
pub struct MemorySnapshot {
    pub timestamp_ms: f32,
    pub allocated_mb: f32,
    pub reserved_mb: f32,
    pub active_allocations: usize,
    pub largest_free_block_mb: f32,
}

/// Memory leak information
#[derive(Debug, Clone)]
pub struct MemoryLeak {
    pub allocation_site: String,
    pub size_mb: f32,
    pub age_ms: f32,
    pub stack_trace: Vec<String>,
}

/// Cache performance metrics.
///
/// L1/L2/L3 hit rates, cache misses per instruction, and memory stall
/// percentage all require reading hardware performance counters (e.g. via
/// `perf_event_open` on Linux, typically permission-gated), which this
/// crate does not do. Every field is `None` ("not measured") rather than a
/// plausible-looking invented number -- see [`MemoryMetricsCollector`].
#[derive(Debug, Clone)]
pub struct CachePerformance {
    pub l1_hit_rate: Option<f32>,
    pub l2_hit_rate: Option<f32>,
    pub l3_hit_rate: Option<f32>,
    pub cache_misses_per_instruction: Option<f32>,
    pub memory_stalls_percentage: Option<f32>,
}

/// GPU profiling data
#[derive(Debug, Clone)]
pub struct GpuProfileData {
    pub utilization_percentage: f32,
    pub memory_utilization_percentage: f32,
    pub temperature_celsius: f32,
    pub power_consumption_watts: f32,
    pub kernel_executions: Vec<GpuKernelExecution>,
    pub memory_transfers: Vec<GpuMemoryTransfer>,
    pub compute_capability: String,
    pub occupancy_percentage: f32,
}

/// Individual GPU kernel execution data
#[derive(Debug, Clone)]
pub struct GpuKernelExecution {
    pub kernel_name: String,
    pub duration_ms: f32,
    pub grid_size: (u32, u32, u32),
    pub block_size: (u32, u32, u32),
    pub registers_per_thread: u32,
    pub shared_memory_kb: f32,
    pub occupancy: f32,
}

/// GPU memory transfer data
#[derive(Debug, Clone)]
pub struct GpuMemoryTransfer {
    pub direction: MemoryTransferDirection,
    pub size_mb: f32,
    pub duration_ms: f32,
    pub bandwidth_gb_s: f32,
}

/// Memory transfer direction
#[derive(Debug, Clone)]
pub enum MemoryTransferDirection {
    HostToDevice,
    DeviceToHost,
    DeviceToDevice,
    Unified,
}

/// Call stack analysis results
#[derive(Debug, Clone)]
pub struct CallStackAnalysis {
    pub hottest_paths: Vec<CallPath>,
    pub recursive_calls: Vec<RecursiveCall>,
    pub call_frequency: HashMap<String, usize>,
    pub average_stack_depth: f32,
    pub max_stack_depth: usize,
}

/// Call path with timing information
#[derive(Debug, Clone)]
pub struct CallPath {
    pub path: Vec<String>,
    pub total_time_ms: f32,
    pub call_count: usize,
    pub average_time_ms: f32,
}

/// Recursive call detection
#[derive(Debug, Clone)]
pub struct RecursiveCall {
    pub function_name: String,
    pub max_depth: usize,
    pub total_recursive_time_ms: f32,
}

/// Performance regression analysis
#[derive(Debug, Clone)]
pub struct RegressionAnalysis {
    pub baseline_performance: PerformanceMetrics,
    pub current_performance: PerformanceMetrics,
    pub regression_percentage: f32,
    pub regressed_operations: Vec<String>,
    pub improvements: Vec<String>,
}

/// Performance metrics for comparison
#[derive(Debug, Clone)]
pub struct PerformanceMetrics {
    pub total_time_ms: f32,
    pub memory_usage_mb: f32,
    pub throughput_ops_per_sec: f32,
    pub energy_consumption_mj: Option<f32>,
}

/// Hotspot analysis results
#[derive(Debug, Clone)]
pub struct HotspotAnalysis {
    pub cpu_hotspots: Vec<Hotspot>,
    pub memory_hotspots: Vec<MemoryHotspot>,
    pub io_hotspots: Vec<IoHotspot>,
    pub synchronization_hotspots: Vec<SyncHotspot>,
}

/// CPU computation hotspot
#[derive(Debug, Clone)]
pub struct Hotspot {
    pub function_name: String,
    pub time_percentage: f32,
    pub instruction_count: Option<u64>,
    pub cache_misses: Option<u64>,
    pub branch_mispredictions: Option<u64>,
}

/// Memory access hotspot
#[derive(Debug, Clone)]
pub struct MemoryHotspot {
    pub operation: String,
    pub access_pattern: MemoryAccessPattern,
    pub bandwidth_utilization: f32,
    pub latency_ms: f32,
}

/// Memory access pattern
#[derive(Debug, Clone)]
pub enum MemoryAccessPattern {
    Sequential,
    Random,
    Strided { stride: usize },
    Clustered,
}

/// I/O operation hotspot
#[derive(Debug, Clone)]
pub struct IoHotspot {
    pub operation_type: String,
    pub wait_time_ms: f32,
    pub throughput_mb_s: f32,
    pub queue_depth: usize,
}

/// Synchronization hotspot
#[derive(Debug, Clone)]
pub struct SyncHotspot {
    pub synchronization_type: String,
    pub wait_time_ms: f32,
    pub contention_count: usize,
    pub affected_threads: usize,
}

/// Layer timing information
#[derive(Debug, Clone)]
pub struct LayerTiming {
    pub name: String,
    pub module_type: String,
    pub forward_time: Duration,
    pub backward_time: Option<Duration>,
    pub percentage: f32,
    pub num_params: usize,
}

/// Operation timing information
#[derive(Debug, Clone)]
pub struct OperationTiming {
    pub count: usize,
    pub total_time: Duration,
    pub avg_time: Duration,
    pub min_time: Duration,
    pub max_time: Duration,
}

/// Memory peak information
#[derive(Debug, Clone)]
pub struct MemoryPeak {
    pub operation: String,
    pub allocated_mb: f32,
    pub reserved_mb: f32,
}

/// Advanced profiling configuration
#[derive(Debug, Clone)]
pub struct AdvancedProfilingConfig {
    pub enable_flame_graph: bool,
    pub enable_memory_profiling: bool,
    pub enable_gpu_profiling: bool,
    pub enable_call_stack_analysis: bool,
    pub enable_regression_detection: bool,
    pub enable_hotspot_analysis: bool,
    pub sample_rate_hz: f32,
    pub memory_snapshot_interval_ms: f32,
}

impl Default for AdvancedProfilingConfig {
    fn default() -> Self {
        Self {
            enable_flame_graph: true,
            enable_memory_profiling: true,
            enable_gpu_profiling: false, // Only enable if GPU available
            enable_call_stack_analysis: true,
            enable_regression_detection: false,
            enable_hotspot_analysis: true,
            sample_rate_hz: 1000.0,
            memory_snapshot_interval_ms: 10.0,
        }
    }
}

/// Profile bottlenecks with basic profiling
pub fn profile_bottlenecks<M: Module>(
    model: &M,
    input_shape: &[usize],
    num_iterations: usize,
    profile_backward: bool,
) -> Result<BottleneckReport> {
    let config = AdvancedProfilingConfig {
        enable_flame_graph: false,
        enable_memory_profiling: true,
        enable_gpu_profiling: false,
        enable_call_stack_analysis: false,
        enable_regression_detection: false,
        enable_hotspot_analysis: false,
        ..Default::default()
    };

    profile_bottlenecks_advanced(model, input_shape, num_iterations, profile_backward, config)
}

/// Profile bottlenecks with comprehensive advanced profiling
pub fn profile_bottlenecks_advanced<M: Module>(
    model: &M,
    input_shape: &[usize],
    num_iterations: usize,
    profile_backward: bool,
    config: AdvancedProfilingConfig,
) -> Result<BottleneckReport> {
    // Initialize profilers
    let mut profiler = Profiler::new();
    let mut memory_collector = MemoryMetricsCollector::new();
    let mut leak_detector = LeakDetector::new();

    // Start profiling
    profiler.start();

    if config.enable_memory_profiling {
        memory_collector.start_collection();
        leak_detector.enable();
    }

    // Initialize data collection structures
    let layer_times = Vec::new();
    let mut operation_times: HashMap<String, Vec<Duration>> = HashMap::new();
    let mut memory_peaks = Vec::new();
    let mut memory_snapshots = Vec::new();
    // Each entry pairs a real captured call stack with the real duration of
    // the iteration it was captured in, so `analyze_call_stacks` can report
    // genuine per-path timing instead of a fabricated constant.
    let mut call_stacks: Vec<(Vec<String>, Duration)> = Vec::new();

    // GPU profiling setup
    let gpu_profiler = if config.enable_gpu_profiling {
        setup_gpu_profiling()
    } else {
        None
    };

    // Warmup runs
    for _ in 0..3 {
        let input = torsh_tensor::creation::randn(input_shape)?;
        let _ = model.forward(&input)?;
    }

    // Main profiling loop
    let start_time = Instant::now();
    let snapshot_interval = Duration::from_millis(config.memory_snapshot_interval_ms as u64);
    let mut last_snapshot = Instant::now();

    for i in 0..num_iterations {
        let input = torsh_tensor::creation::randn(input_shape)?;
        let iteration_start = Instant::now();

        // Capture the real call stack at the point this iteration's
        // compute begins; paired with the iteration's real duration once
        // that's known below.
        let call_stack = if config.enable_call_stack_analysis {
            Some(capture_call_stack())
        } else {
            None
        };

        // Profile forward pass
        let forward_start = Instant::now();
        let output = model.forward(&input)?;
        let forward_time = forward_start.elapsed();

        operation_times
            .entry("forward".to_string())
            .or_default()
            .push(forward_time);

        // Profile backward pass if requested
        if profile_backward && output.requires_grad() {
            let backward_start = Instant::now();
            output.sum()?.backward()?;
            let backward_time = backward_start.elapsed();

            operation_times
                .entry("backward".to_string())
                .or_default()
                .push(backward_time);
        }

        if let Some(call_stack) = call_stack {
            call_stacks.push((call_stack, iteration_start.elapsed()));
        }

        // Memory snapshots
        if config.enable_memory_profiling && last_snapshot.elapsed() >= snapshot_interval {
            if let Ok(memory_info) = get_detailed_memory_info() {
                memory_snapshots.push(MemorySnapshot {
                    timestamp_ms: start_time.elapsed().as_millis() as f32,
                    allocated_mb: memory_info.0,
                    reserved_mb: memory_info.1,
                    active_allocations: memory_info.2,
                    largest_free_block_mb: memory_info.3,
                });
            }
            // Update the real peak-memory reading alongside the existing
            // snapshot cadence (see MemoryMetricsCollector).
            memory_collector.sample();
            last_snapshot = Instant::now();
        }

        // Periodic memory peaks collection
        if i % 10 == 0 {
            if let Ok(memory_info) = get_memory_info() {
                memory_peaks.push(MemoryPeak {
                    operation: format!("iteration_{}", i),
                    allocated_mb: memory_info.0,
                    reserved_mb: memory_info.1,
                });
            }
        }
    }

    let total_time = start_time.elapsed();

    // Stop all profilers
    profiler.stop();

    if config.enable_memory_profiling {
        memory_collector.stop_collection();
    }

    // Collect profiling results. The flame graph and hotspot analysis are
    // built from the real per-iteration forward/backward `Duration`s
    // collected in the loop above, not from a fabricated sample set.
    let flame_graph = if config.enable_flame_graph {
        Some(generate_flame_graph(&operation_times, total_time))
    } else {
        None
    };

    let memory_profile = if config.enable_memory_profiling {
        generate_memory_profile(&memory_collector, &leak_detector, memory_snapshots)?
    } else {
        MemoryProfileData::default()
    };

    let gpu_profile = if let Some(gpu_prof) = gpu_profiler {
        Some(collect_gpu_profile_data(gpu_prof)?)
    } else {
        None
    };

    let call_stack_analysis = if config.enable_call_stack_analysis {
        analyze_call_stacks(call_stacks)?
    } else {
        CallStackAnalysis::default()
    };

    let hotspot_analysis = if config.enable_hotspot_analysis {
        analyze_hotspots(&operation_times, total_time)
    } else {
        HotspotAnalysis::default()
    };

    // Process operation timings
    let processed_op_times = process_operation_times(operation_times);

    // Generate recommendations
    let recommendations = generate_advanced_recommendations(
        &layer_times,
        &processed_op_times,
        &memory_peaks,
        &memory_profile,
        &hotspot_analysis,
    );

    Ok(BottleneckReport {
        total_time,
        layer_times,
        operation_times: processed_op_times,
        memory_peaks,
        recommendations,
        flame_graph,
        memory_profile,
        gpu_profile,
        call_stack_analysis,
        performance_regression: None,
        hotspot_analysis,
    })
}

/// Generate a flame graph from the real per-iteration operation timings
/// collected during profiling (`operation_times`: e.g. "forward"/"backward"
/// -> one real `Duration` per iteration), instead of a fixed, fabricated
/// sample set describing functions that were never actually executed.
///
/// This is coarser than a true sampling profiler (it can only see the
/// operations this crate explicitly times -- forward/backward passes as a
/// whole, not individual layers inside them, since `Module::forward` is
/// opaque here), but every number in it was genuinely measured.
fn generate_flame_graph(
    operation_times: &HashMap<String, Vec<Duration>>,
    total_time: Duration,
) -> FlameGraphData {
    let samples = real_profile_samples(operation_times);
    let total_samples = samples.len();
    // Real average sampling rate: how many real per-operation
    // measurements were taken per second of wall-clock profiling time.
    let sample_rate_hz = if total_time.as_secs_f32() > 0.0 {
        total_samples as f32 / total_time.as_secs_f32()
    } else {
        0.0
    };

    let root_frame = build_flame_graph_tree(samples);

    FlameGraphData {
        root_frame,
        total_samples,
        sample_rate_hz,
        duration_ms: total_time.as_millis() as f32,
    }
}

/// Turn real per-iteration operation `Duration`s into one [`ProfileSample`]
/// per iteration per operation, each carrying that operation's own name as
/// its (single-frame) stack trace -- the only call-site information
/// available without deeper instrumentation into the profiled model.
fn real_profile_samples(operation_times: &HashMap<String, Vec<Duration>>) -> Vec<ProfileSample> {
    let mut samples = Vec::new();
    for (name, durations) in operation_times {
        for duration in durations {
            samples.push(ProfileSample {
                function_name: name.clone(),
                duration_ms: duration.as_secs_f32() * 1000.0,
                stack_trace: vec![name.clone()],
            });
        }
    }
    samples
}

/// Build flame graph tree structure from real samples (see
/// [`real_profile_samples`]).
fn build_flame_graph_tree(samples: Vec<ProfileSample>) -> FlameFrame {
    let mut root = FlameFrame {
        name: "root".to_string(),
        file: None,
        line: None,
        self_time_ms: 0.0,
        total_time_ms: 0.0,
        sample_count: samples.len(),
        children: Vec::new(),
    };

    // Aggregate samples by function name, also tracking real sample counts
    // (previously hardcoded to 1 regardless of how many samples an
    // operation actually had).
    let mut function_stats: HashMap<String, (f32, usize)> = HashMap::new();
    for sample in &samples {
        let entry = function_stats
            .entry(sample.function_name.clone())
            .or_insert((0.0, 0));
        entry.0 += sample.duration_ms;
        entry.1 += 1;
    }

    // Create child frames
    for (function_name, (total_time, sample_count)) in function_stats {
        let child_frame = FlameFrame {
            name: function_name,
            file: None,
            line: None,
            self_time_ms: total_time,
            total_time_ms: total_time,
            sample_count,
            children: Vec::new(),
        };
        root.children.push(child_frame);
        root.total_time_ms += total_time;
    }

    root
}

/// Profile sample structure
#[derive(Debug, Clone)]
struct ProfileSample {
    function_name: String,
    duration_ms: f32,
    stack_trace: Vec<String>,
}

/// Generate comprehensive memory profile
fn generate_memory_profile(
    collector: &MemoryMetricsCollector,
    leak_detector: &LeakDetector,
    snapshots: Vec<MemorySnapshot>,
) -> Result<MemoryProfileData> {
    let metrics = collector.get_metrics();

    // `None` means leak detection is not implemented (no allocator
    // instrumentation is wired up); `Some(vec)` (even if empty) would
    // falsely claim detection ran and found nothing.
    let memory_leaks = leak_detector.get_detected_leaks().map(|leaks| {
        leaks
            .into_iter()
            .map(|leak| MemoryLeak {
                allocation_site: leak.location,
                size_mb: leak.size_bytes as f32 / 1024.0 / 1024.0,
                age_ms: leak.age_ms,
                stack_trace: leak.stack_trace,
            })
            .collect()
    });

    Ok(MemoryProfileData {
        peak_usage_mb: metrics.peak_usage_mb,
        current_usage_mb: metrics.current_usage_mb,
        allocation_timeline: snapshots,
        memory_leaks,
        fragmentation_ratio: metrics.fragmentation_ratio,
        gc_pressure: None,
        memory_bandwidth_utilization: metrics.bandwidth_utilization,
        cache_performance: CachePerformance {
            l1_hit_rate: metrics.l1_hit_rate,
            l2_hit_rate: metrics.l2_hit_rate,
            l3_hit_rate: metrics.l3_hit_rate,
            cache_misses_per_instruction: metrics.cache_misses_per_instruction,
            memory_stalls_percentage: metrics.memory_stalls_percentage,
        },
    })
}

/// Memory metrics: real process memory readings plus (unmeasured) cache
/// counters. See [`MemoryMetricsCollector`].
#[derive(Debug)]
struct MemoryMetrics {
    peak_usage_mb: f32,
    current_usage_mb: f32,
    fragmentation_ratio: Option<f32>,
    bandwidth_utilization: Option<f32>,
    l1_hit_rate: Option<f32>,
    l2_hit_rate: Option<f32>,
    l3_hit_rate: Option<f32>,
    cache_misses_per_instruction: Option<f32>,
    memory_stalls_percentage: Option<f32>,
}

/// A leak detected by [`LeakDetector`] (currently never constructed --
/// see that type's doc comment).
#[derive(Debug)]
struct DetectedLeak {
    location: String,
    size_bytes: usize,
    age_ms: f32,
    stack_trace: Vec<String>,
}

/// Analyze layer timings from profile events
#[allow(dead_code)]
fn analyze_layer_timings(_events: &[ProfileEvent], _total_time: Duration) -> Vec<LayerTiming> {
    // Simplified implementation - profiler integration not yet complete
    Vec::new()
}

/// Process operation timings
fn process_operation_times(
    raw_times: HashMap<String, Vec<Duration>>,
) -> HashMap<String, OperationTiming> {
    raw_times
        .into_iter()
        .map(|(name, times)| {
            let count = times.len();
            let total_time: Duration = times.iter().sum();
            let avg_time = total_time / count as u32;
            let min_time = times.iter().min().copied().unwrap_or(Duration::ZERO);
            let max_time = times.iter().max().copied().unwrap_or(Duration::ZERO);

            (
                name,
                OperationTiming {
                    count,
                    total_time,
                    avg_time,
                    min_time,
                    max_time,
                },
            )
        })
        .collect()
}

/// Get current memory information from /proc/self/status.
/// Returns (rss_mb, vmsize_mb). On non-Linux platforms returns (0.0, 0.0).
fn get_memory_info() -> Result<(f32, f32)> {
    #[cfg(target_os = "linux")]
    {
        let status = std::fs::read_to_string("/proc/self/status").map_err(|e| {
            torsh_core::TorshError::IoError(format!("Failed to read /proc/self/status: {}", e))
        })?;
        let mut rss_kb: Option<u64> = None;
        let mut vmsize_kb: Option<u64> = None;
        for line in status.lines() {
            if let Some(rest) = line.strip_prefix("VmRSS:") {
                rss_kb = rest.split_whitespace().next().and_then(|s| s.parse().ok());
            } else if let Some(rest) = line.strip_prefix("VmSize:") {
                vmsize_kb = rest.split_whitespace().next().and_then(|s| s.parse().ok());
            }
            if rss_kb.is_some() && vmsize_kb.is_some() {
                break;
            }
        }
        let rss_mb = rss_kb.unwrap_or(0) as f32 / 1024.0;
        let vmsize_mb = vmsize_kb.unwrap_or(0) as f32 / 1024.0;
        return Ok((rss_mb, vmsize_mb));
    }
    #[cfg(not(target_os = "linux"))]
    {
        // Memory measurement via /proc/self/status not available on this platform
        Ok((0.0, 0.0))
    }
}

/// Get detailed memory information for profiling from /proc/self/status and /proc/self/maps.
/// Returns (allocated_mb, reserved_mb, active_allocations, free_mb).
/// On non-Linux platforms returns (0.0, 0.0, 0, 0.0).
fn get_detailed_memory_info() -> Result<(f32, f32, usize, f32)> {
    #[cfg(target_os = "linux")]
    {
        // Read VmRSS (allocated = resident) and VmSize (reserved = virtual) from status
        let status = std::fs::read_to_string("/proc/self/status").map_err(|e| {
            torsh_core::TorshError::IoError(format!("Failed to read /proc/self/status: {}", e))
        })?;
        let mut rss_kb: Option<u64> = None;
        let mut vmsize_kb: Option<u64> = None;
        for line in status.lines() {
            if let Some(rest) = line.strip_prefix("VmRSS:") {
                rss_kb = rest.split_whitespace().next().and_then(|s| s.parse().ok());
            } else if let Some(rest) = line.strip_prefix("VmSize:") {
                vmsize_kb = rest.split_whitespace().next().and_then(|s| s.parse().ok());
            }
            if rss_kb.is_some() && vmsize_kb.is_some() {
                break;
            }
        }
        let allocated_mb = rss_kb.unwrap_or(0) as f32 / 1024.0;
        let reserved_mb = vmsize_kb.unwrap_or(0) as f32 / 1024.0;

        // Approximate active allocations from /proc/self/maps line count
        let active_allocations = std::fs::read_to_string("/proc/self/maps")
            .map(|s| s.lines().count())
            .unwrap_or(0);

        // Approximate free memory from /proc/meminfo MemFree
        let meminfo = std::fs::read_to_string("/proc/meminfo").map_err(|e| {
            torsh_core::TorshError::IoError(format!("Failed to read /proc/meminfo: {}", e))
        })?;
        let mut memfree_kb: Option<u64> = None;
        for line in meminfo.lines() {
            if let Some(rest) = line.strip_prefix("MemFree:") {
                memfree_kb = rest.split_whitespace().next().and_then(|s| s.parse().ok());
                break;
            }
        }
        let free_mb = memfree_kb.unwrap_or(0) as f32 / 1024.0;

        return Ok((allocated_mb, reserved_mb, active_allocations, free_mb));
    }
    #[cfg(not(target_os = "linux"))]
    {
        // Detailed memory measurement via /proc not available on this platform
        Ok((0.0, 0.0, 0, 0.0))
    }
}

/// Setup GPU profiling if available
fn setup_gpu_profiling() -> Option<GpuProfiler> {
    // Check if GPU is available and setup profiling
    // For now, return None (no GPU profiling)
    None
}

/// Placeholder GPU profiler
struct GpuProfiler {
    _context: String,
}

/// Collect GPU profiling data.
///
/// GPU profiling requires NVML or CUDA runtime linkage which is not currently
/// linked into this crate. All metrics are reported as 0.0 / empty to clearly
/// communicate that no measurement was taken. Callers should treat `gpu_profile`
/// as informational only when no `cuda` feature is active.
fn collect_gpu_profile_data(_profiler: GpuProfiler) -> Result<GpuProfileData> {
    Ok(GpuProfileData {
        utilization_percentage: 0.0,
        memory_utilization_percentage: 0.0,
        temperature_celsius: 0.0,
        power_consumption_watts: 0.0,
        kernel_executions: vec![],
        memory_transfers: vec![],
        compute_capability: "unknown".to_string(),
        occupancy_percentage: 0.0,
    })
}

/// Capture the real current call stack via `std::backtrace` (stable since
/// Rust 1.65; no extra dependency needed).
///
/// `force_capture` resolves symbols unconditionally, so behavior does not
/// depend on the `RUST_BACKTRACE` environment variable. The standard
/// library's `Backtrace` exposes only a formatted `Display`/`Debug`
/// rendering (no structured per-frame API), so individual frames are
/// recovered by splitting that rendering into non-empty lines -- real,
/// call-site-specific data, replacing the previous fixed 3-entry
/// placeholder that was returned for every call regardless of where it was
/// actually made from.
fn capture_call_stack() -> Vec<String> {
    let backtrace = std::backtrace::Backtrace::force_capture();
    format!("{backtrace}")
        .lines()
        .map(|line| line.trim())
        .filter(|line| !line.is_empty())
        .map(|line| line.to_string())
        .collect()
}

/// Analyze call stacks for patterns.
///
/// `call_stacks` pairs each real captured stack with the real duration of
/// the iteration it was captured in (see `profile_bottlenecks_advanced`),
/// so per-path timing below is computed from genuine measurements instead
/// of a fixed placeholder.
fn analyze_call_stacks(call_stacks: Vec<(Vec<String>, Duration)>) -> Result<CallStackAnalysis> {
    let mut call_frequency = HashMap::new();
    let mut total_depth = 0;
    let mut max_depth = 0;

    for (stack, _duration) in &call_stacks {
        total_depth += stack.len();
        max_depth = max_depth.max(stack.len());

        for function in stack {
            *call_frequency.entry(function.clone()).or_insert(0) += 1;
        }
    }

    let average_stack_depth = if !call_stacks.is_empty() {
        total_depth as f32 / call_stacks.len() as f32
    } else {
        0.0
    };

    // Group identical stacks together and compute each group's real
    // aggregate timing, rather than stamping every path with a fixed
    // 100.0ms placeholder.
    let mut path_groups: HashMap<Vec<String>, Vec<f32>> = HashMap::new();
    for (stack, duration) in call_stacks {
        path_groups
            .entry(stack)
            .or_default()
            .push(duration.as_secs_f32() * 1000.0);
    }

    let mut hottest_paths: Vec<CallPath> = path_groups
        .into_iter()
        .map(|(path, times_ms)| {
            let call_count = times_ms.len();
            let total_time_ms: f32 = times_ms.iter().sum();
            let average_time_ms = total_time_ms / call_count as f32;
            CallPath {
                path,
                total_time_ms,
                call_count,
                average_time_ms,
            }
        })
        .collect();
    hottest_paths.sort_by(|a, b| {
        b.total_time_ms
            .partial_cmp(&a.total_time_ms)
            .unwrap_or(std::cmp::Ordering::Equal)
    });
    hottest_paths.truncate(5);

    Ok(CallStackAnalysis {
        hottest_paths,
        recursive_calls: vec![], // Would detect recursive patterns
        call_frequency,
        average_stack_depth,
        max_stack_depth: max_depth,
    })
}

/// Analyze performance hotspots from the real per-iteration operation
/// timings collected during profiling.
///
/// CPU hotspots are derived from genuinely measured operation durations
/// (coarse-grained: "forward"/"backward" as a whole, since `Module` does
/// not expose per-layer timing here). `instruction_count`/`cache_misses`/
/// `branch_mispredictions` require hardware performance counters this
/// crate does not read, so they are `None` rather than invented numbers.
///
/// Memory/I/O/synchronization hotspots require access-pattern, I/O, and
/// lock-contention instrumentation that does not exist in this crate;
/// rather than fabricate plausible-looking entries, these are honestly
/// empty until such instrumentation is implemented.
fn analyze_hotspots(
    operation_times: &HashMap<String, Vec<Duration>>,
    total_time: Duration,
) -> HotspotAnalysis {
    let total_secs = total_time.as_secs_f32();
    let mut cpu_hotspots: Vec<Hotspot> = operation_times
        .iter()
        .map(|(name, durations)| {
            let op_total_secs: f32 = durations.iter().map(|d| d.as_secs_f32()).sum();
            let time_percentage = if total_secs > 0.0 {
                (op_total_secs / total_secs) * 100.0
            } else {
                0.0
            };
            Hotspot {
                function_name: name.clone(),
                time_percentage,
                instruction_count: None,
                cache_misses: None,
                branch_mispredictions: None,
            }
        })
        .collect();
    cpu_hotspots.sort_by(|a, b| {
        b.time_percentage
            .partial_cmp(&a.time_percentage)
            .unwrap_or(std::cmp::Ordering::Equal)
    });

    HotspotAnalysis {
        cpu_hotspots,
        // Would require memory access-pattern instrumentation.
        memory_hotspots: vec![],
        // Would require I/O instrumentation.
        io_hotspots: vec![],
        // Would require lock/synchronization instrumentation.
        synchronization_hotspots: vec![],
    }
}

/// Generate advanced recommendations with comprehensive analysis
fn generate_advanced_recommendations(
    layer_times: &[LayerTiming],
    operation_times: &HashMap<String, OperationTiming>,
    memory_peaks: &[MemoryPeak],
    memory_profile: &MemoryProfileData,
    hotspot_analysis: &HotspotAnalysis,
) -> Vec<String> {
    let mut recommendations = Vec::new();

    // Basic recommendations (from original function)
    recommendations.extend(generate_recommendations(
        layer_times,
        operation_times,
        memory_peaks,
    ));

    // Memory-specific recommendations -- only fire when the underlying
    // metric was actually measured.
    if let Some(fragmentation_ratio) = memory_profile.fragmentation_ratio {
        if fragmentation_ratio > 0.3 {
            recommendations.push(format!(
                "High memory fragmentation ({:.1}%). Consider using memory pools or reducing allocation frequency.",
                fragmentation_ratio * 100.0
            ));
        }
    }

    if let Some(leaks) = &memory_profile.memory_leaks {
        if !leaks.is_empty() {
            recommendations.push(format!(
                "Detected {} memory leaks. Review allocation sites: {}",
                leaks.len(),
                leaks
                    .iter()
                    .take(3)
                    .map(|leak| leak.allocation_site.as_str())
                    .collect::<Vec<_>>()
                    .join(", ")
            ));
        }
    }

    if let Some(l1_hit_rate) = memory_profile.cache_performance.l1_hit_rate {
        if l1_hit_rate < 0.9 {
            recommendations.push(format!(
                "Low L1 cache hit rate ({:.1}%). Consider improving data locality and access patterns.",
                l1_hit_rate * 100.0
            ));
        }
    }

    // CPU hotspot recommendations
    for hotspot in &hotspot_analysis.cpu_hotspots {
        if hotspot.time_percentage > 20.0 {
            recommendations.push(format!(
                "Function '{}' consumes {:.1}% of CPU time. Consider optimizing this function.",
                hotspot.function_name, hotspot.time_percentage
            ));

            if let Some(cache_misses) = hotspot.cache_misses {
                if cache_misses > 100_000 {
                    recommendations.push(format!(
                        "High cache miss rate in '{}'. Optimize memory access patterns.",
                        hotspot.function_name
                    ));
                }
            }
        }
    }

    // Memory access pattern recommendations
    for mem_hotspot in &hotspot_analysis.memory_hotspots {
        match mem_hotspot.access_pattern {
            MemoryAccessPattern::Random => {
                recommendations.push(format!(
                    "Random memory access detected in '{}'. Consider restructuring data layout for better locality.",
                    mem_hotspot.operation
                ));
            }
            MemoryAccessPattern::Strided { stride } => {
                if stride > 64 {
                    recommendations.push(format!(
                        "Large stride ({}) in memory access for '{}'. Consider data reorganization.",
                        stride, mem_hotspot.operation
                    ));
                }
            }
            _ => {}
        }

        if mem_hotspot.bandwidth_utilization < 50.0 {
            recommendations.push(format!(
                "Low memory bandwidth utilization ({:.1}%) in '{}'. Consider vectorization or prefetching.",
                mem_hotspot.bandwidth_utilization, mem_hotspot.operation
            ));
        }
    }

    // I/O recommendations
    for io_hotspot in &hotspot_analysis.io_hotspots {
        if io_hotspot.wait_time_ms > 10.0 {
            recommendations.push(format!(
                "High I/O wait time ({:.1}ms) for '{}'. Consider async I/O or data prefetching.",
                io_hotspot.wait_time_ms, io_hotspot.operation_type
            ));
        }
    }

    // Synchronization recommendations
    for sync_hotspot in &hotspot_analysis.synchronization_hotspots {
        if sync_hotspot.wait_time_ms > 5.0 {
            recommendations.push(format!(
                "Synchronization bottleneck in '{}' ({:.1}ms wait time). Consider lock-free algorithms or finer-grained locking.",
                sync_hotspot.synchronization_type, sync_hotspot.wait_time_ms
            ));
        }
    }

    recommendations
}

// Add default implementations for complex structures
impl Default for MemoryProfileData {
    /// Used when memory profiling was not enabled at all
    /// (`enable_memory_profiling: false`). Every field is a genuine zero/
    /// `None` for "not collected", not a fabricated "everything is
    /// perfect" reading (the previous implementation reported 100% cache
    /// hit rates here, which claimed cache performance had been checked
    /// and was flawless -- when in fact nothing had been measured at all).
    fn default() -> Self {
        Self {
            peak_usage_mb: 0.0,
            current_usage_mb: 0.0,
            allocation_timeline: vec![],
            memory_leaks: None,
            fragmentation_ratio: None,
            gc_pressure: None,
            memory_bandwidth_utilization: None,
            cache_performance: CachePerformance {
                l1_hit_rate: None,
                l2_hit_rate: None,
                l3_hit_rate: None,
                cache_misses_per_instruction: None,
                memory_stalls_percentage: None,
            },
        }
    }
}

impl Default for CallStackAnalysis {
    fn default() -> Self {
        Self {
            hottest_paths: vec![],
            recursive_calls: vec![],
            call_frequency: HashMap::new(),
            average_stack_depth: 0.0,
            max_stack_depth: 0,
        }
    }
}

impl Default for HotspotAnalysis {
    fn default() -> Self {
        Self {
            cpu_hotspots: vec![],
            memory_hotspots: vec![],
            io_hotspots: vec![],
            synchronization_hotspots: vec![],
        }
    }
}

trait MemoryCollectorTrait {
    fn new() -> Self;
    fn start_collection(&mut self);
    fn stop_collection(&mut self);
    fn get_metrics(&self) -> MemoryMetrics;
}

impl MemoryCollectorTrait for MemoryMetricsCollector {
    fn new() -> Self {
        MemoryMetricsCollector {
            #[cfg(feature = "collect_env")]
            peak_bytes: 0,
        }
    }

    fn start_collection(&mut self) {
        #[cfg(feature = "collect_env")]
        {
            self.peak_bytes = current_process_memory_bytes().unwrap_or(0);
        }
    }

    fn stop_collection(&mut self) {
        // Take one final real sample so the peak reflects memory right up
        // to the end of the profiled run.
        self.sample();
    }

    fn get_metrics(&self) -> MemoryMetrics {
        #[cfg(feature = "collect_env")]
        {
            let current_bytes = current_process_memory_bytes().unwrap_or(0);
            let peak_bytes = self.peak_bytes.max(current_bytes);
            MemoryMetrics {
                peak_usage_mb: bytes_to_mb(peak_bytes),
                current_usage_mb: bytes_to_mb(current_bytes),
                // None of these require hardware performance counters or
                // allocator introspection this crate does not have.
                fragmentation_ratio: None,
                bandwidth_utilization: None,
                l1_hit_rate: None,
                l2_hit_rate: None,
                l3_hit_rate: None,
                cache_misses_per_instruction: None,
                memory_stalls_percentage: None,
            }
        }
        #[cfg(not(feature = "collect_env"))]
        {
            MemoryMetrics {
                peak_usage_mb: 0.0,
                current_usage_mb: 0.0,
                fragmentation_ratio: None,
                bandwidth_utilization: None,
                l1_hit_rate: None,
                l2_hit_rate: None,
                l3_hit_rate: None,
                cache_misses_per_instruction: None,
                memory_stalls_percentage: None,
            }
        }
    }
}

impl MemoryMetricsCollector {
    /// Take a real process-memory reading now and fold it into the
    /// running peak. A no-op when the `collect_env` feature (which brings
    /// in `sysinfo`) is disabled.
    fn sample(&mut self) {
        #[cfg(feature = "collect_env")]
        {
            if let Some(bytes) = current_process_memory_bytes() {
                self.peak_bytes = self.peak_bytes.max(bytes);
            }
        }
    }
}

/// Real process memory metrics collector.
///
/// Uses `sysinfo` (available via this crate's default-enabled
/// `collect_env` feature) to sample this process's actual resident
/// memory. Cache-level counters (L1/L2/L3 hit rate, cache misses per
/// instruction, memory bandwidth utilization, fragmentation ratio)
/// require hardware performance counters or allocator introspection this
/// crate does not have -- see [`MemoryMetrics`] / [`CachePerformance`],
/// which report those as `None` rather than plausible-looking invented
/// numbers.
struct MemoryMetricsCollector {
    /// Peak resident memory (bytes) observed via [`Self::sample`] /
    /// `start_collection` / `stop_collection` so far.
    #[cfg(feature = "collect_env")]
    peak_bytes: u64,
}

/// Real resident memory (RSS) of the current process, in bytes, via
/// `sysinfo`. Returns `None` if the current process could not be looked up
/// (e.g. an unsupported platform).
#[cfg(feature = "collect_env")]
fn current_process_memory_bytes() -> Option<u64> {
    use sysinfo::{ProcessesToUpdate, System};

    let pid = sysinfo::get_current_pid().ok()?;
    let mut sys = System::new();
    sys.refresh_processes(ProcessesToUpdate::Some(&[pid]), true);
    sys.process(pid).map(|process| process.memory())
}

#[cfg(feature = "collect_env")]
fn bytes_to_mb(bytes: u64) -> f32 {
    bytes as f32 / (1024.0 * 1024.0)
}

trait LeakDetectorTrait {
    fn new() -> Self;
    fn enable(&mut self);
    fn get_detected_leaks(&self) -> Option<Vec<DetectedLeak>>;
}

impl LeakDetectorTrait for LeakDetector {
    fn new() -> Self {
        LeakDetector { _placeholder: () }
    }

    fn enable(&mut self) {
        // Enable leak detection
    }

    /// Real leak detection requires instrumenting the global allocator to
    /// record allocation call sites and ages (or an external tool such as
    /// Valgrind/heaptrack); neither is wired into this crate. Returns
    /// `None` ("not measured") rather than `Some(vec![])`, which would
    /// falsely claim detection ran and cleanly found zero leaks.
    fn get_detected_leaks(&self) -> Option<Vec<DetectedLeak>> {
        None
    }
}

impl LeakDetector {
    fn new() -> Self {
        Self { _placeholder: () }
    }
}

struct LeakDetector {
    _placeholder: (),
}

/// Generate optimization recommendations
fn generate_recommendations(
    layer_times: &[LayerTiming],
    operation_times: &HashMap<String, OperationTiming>,
    memory_peaks: &[MemoryPeak],
) -> Vec<String> {
    let mut recommendations = Vec::new();

    // Check for slow layers
    if let Some(slowest) = layer_times.first() {
        if slowest.percentage > 30.0 {
            recommendations.push(format!(
                "Layer '{}' takes {:.1}% of total time. Consider optimizing or replacing this layer.",
                slowest.name, slowest.percentage
            ));
        }
    }

    // Check forward/backward balance
    if let (Some(forward), Some(backward)) = (
        operation_times.get("forward"),
        operation_times.get("backward"),
    ) {
        let ratio = backward.avg_time.as_secs_f32() / forward.avg_time.as_secs_f32();
        if ratio > 3.0 {
            recommendations.push(format!(
                "Backward pass is {:.1}x slower than forward pass. Consider gradient checkpointing.",
                ratio
            ));
        }
    }

    // Check memory usage
    if !memory_peaks.is_empty() {
        let max_memory = memory_peaks
            .iter()
            .map(|p| p.allocated_mb)
            .fold(0.0f32, |a, b| a.max(b));

        if max_memory > 1000.0 {
            recommendations.push(format!(
                "High memory usage detected ({:.1} MB). Consider using mixed precision training.",
                max_memory
            ));
        }
    }

    // Check for high-parameter layers
    for layer in layer_times.iter().take(5) {
        if layer.module_type.contains("Conv") && layer.percentage > 20.0 {
            recommendations.push(format!(
                "Convolution layer '{}' is slow. Consider using depthwise separable convolutions.",
                layer.name
            ));
        }
    }

    recommendations
}

/// Print bottleneck report
pub fn print_bottleneck_report(report: &BottleneckReport) {
    println!("=== Bottleneck Analysis Report ===");
    println!();
    println!(
        "Total profiling time: {:.3}s",
        report.total_time.as_secs_f32()
    );
    println!();

    println!("Top 10 Slowest Layers:");
    println!(
        "{:<30} {:<15} {:<10} {:<10} {:<10}",
        "Layer", "Type", "Forward", "Backward", "% Time"
    );
    println!("{}", "-".repeat(75));

    for layer in report.layer_times.iter().take(10) {
        let backward_str = layer
            .backward_time
            .map(|t| format!("{:.3}ms", t.as_secs_f32() * 1000.0))
            .unwrap_or_else(|| "N/A".to_string());

        println!(
            "{:<30} {:<15} {:<10.3}ms {:<10} {:<10.1}%",
            layer.name,
            layer.module_type,
            layer.forward_time.as_secs_f32() * 1000.0,
            backward_str,
            layer.percentage
        );
    }
    println!();

    println!("Operation Summary:");
    for (name, timing) in &report.operation_times {
        println!(
            "{}: {} calls, avg {:.3}ms, total {:.3}s",
            name,
            timing.count,
            timing.avg_time.as_secs_f32() * 1000.0,
            timing.total_time.as_secs_f32()
        );
    }
    println!();

    println!("Memory Profile:");
    println!(
        "  Peak usage: {:.1} MB, current usage: {:.1} MB",
        report.memory_profile.peak_usage_mb, report.memory_profile.current_usage_mb
    );
    println!(
        "  Fragmentation ratio: {}",
        format_optional_percent(report.memory_profile.fragmentation_ratio)
    );
    println!(
        "  Memory leaks: {}",
        match &report.memory_profile.memory_leaks {
            Some(leaks) => format!("{}", leaks.len()),
            None => "not measured".to_string(),
        }
    );
    let cache = &report.memory_profile.cache_performance;
    println!(
        "  Cache hit rate: L1 {}, L2 {}, L3 {}",
        format_optional_percent(cache.l1_hit_rate),
        format_optional_percent(cache.l2_hit_rate),
        format_optional_percent(cache.l3_hit_rate)
    );
    println!();

    if !report.recommendations.is_empty() {
        println!("Optimization Recommendations:");
        for (i, rec) in report.recommendations.iter().enumerate() {
            println!("{}. {}", i + 1, rec);
        }
    }
}

/// Render an optional 0.0-1.0 ratio as a percentage, or "not measured"
/// when the underlying metric was never read (rather than silently
/// printing a `0.0%` that would look like a real, if bad, measurement).
fn format_optional_percent(value: Option<f32>) -> String {
    value
        .map(|v| format!("{:.1}%", v * 100.0))
        .unwrap_or_else(|| "not measured".to_string())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_get_memory_info_nonnegative() {
        let (alloc, reserved) = get_memory_info().unwrap_or((0.0, 0.0));
        assert!(
            alloc >= 0.0,
            "allocated MB should be non-negative, got {}",
            alloc
        );
        assert!(
            reserved >= 0.0,
            "reserved MB should be non-negative, got {}",
            reserved
        );
        #[cfg(target_os = "linux")]
        {
            assert!(
                alloc > 0.0,
                "allocated MB should be positive on Linux, got {}",
                alloc
            );
        }
    }

    #[test]
    fn test_get_detailed_memory_info_nonnegative() {
        let (alloc, reserved, active_allocs, free_mb) =
            get_detailed_memory_info().unwrap_or((0.0, 0.0, 0, 0.0));
        assert!(
            alloc >= 0.0,
            "allocated MB should be non-negative, got {}",
            alloc
        );
        assert!(
            reserved >= 0.0,
            "reserved MB should be non-negative, got {}",
            reserved
        );
        assert!(
            free_mb >= 0.0,
            "free MB should be non-negative, got {}",
            free_mb
        );
        #[cfg(target_os = "linux")]
        {
            assert!(
                alloc > 0.0,
                "allocated MB should be positive on Linux, got {}",
                alloc
            );
            assert!(
                active_allocs > 0,
                "active allocations should be positive on Linux, got {}",
                active_allocs
            );
        }
        let _ = active_allocs; // used in cfg(linux) branch above
    }

    #[test]
    fn test_process_operation_times() {
        let mut raw_times = HashMap::new();
        raw_times.insert(
            "test_op".to_string(),
            vec![
                Duration::from_millis(10),
                Duration::from_millis(20),
                Duration::from_millis(15),
            ],
        );

        let processed = process_operation_times(raw_times);
        let timing = processed.get("test_op").unwrap();

        assert_eq!(timing.count, 3);
        assert_eq!(timing.total_time, Duration::from_millis(45));
        assert_eq!(timing.avg_time, Duration::from_millis(15));
        assert_eq!(timing.min_time, Duration::from_millis(10));
        assert_eq!(timing.max_time, Duration::from_millis(20));
    }
}