irontide-session 0.165.0

BitTorrent session management: peers, torrents, and piece selection
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
//! Unified buffer pool — libtorrent 1.x-style combined read cache + write buffer.
//!
//! Replaces separate read cache and write buffer with a single coherent structure
//! that tracks all in-flight piece data under one byte budget. Writing entries
//! accumulate blocks until a piece is complete; Cached entries serve read hits
//! via a purpose-built ARC (Adaptive Replacement Cache) that uses byte-budget
//! accounting rather than entry count.

use std::collections::{BTreeMap, HashMap, HashSet, VecDeque};
#[cfg(unix)]
use std::sync::atomic::{AtomicBool, Ordering};

use bytes::Bytes;
use irontide_core::Id20;

/// Composite key: (info_hash, piece_index).
type PieceKey = (Id20, u32);

/// Default total byte budget: 64 MiB.
#[cfg(test)]
const DEFAULT_CAPACITY: usize = 64 * 1024 * 1024;

/// Maximum number of hot piece indices returned by [`BufferPool::hot_pieces`].
#[allow(dead_code)] // used by Task 4 (suggest_cached_pieces wiring)
const MAX_HOT_PIECES: usize = 16;

// ---------------------------------------------------------------------------
// CachedPiece
// ---------------------------------------------------------------------------

/// State of a piece in the buffer pool.
#[derive(Debug)]
enum CachedPiece {
    /// Blocks are accumulating in memory, not yet flushed to disk.
    Writing {
        blocks: BTreeMap<u32, Bytes>,
        total_bytes: usize,
        piece_size: usize,
    },
    /// Blocks have been flushed to disk; we only track which offsets exist.
    Skeleton {
        flushed: HashSet<u32>,
        total_bytes: usize,
        piece_size: usize,
    },
    /// Full piece held in a single contiguous `Bytes` (read cache).
    Cached { data: Bytes },
}

// ---------------------------------------------------------------------------
// WriteStatus
// ---------------------------------------------------------------------------

/// Result of a [`BufferPool::write_block`] call.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum WriteStatus {
    /// Block buffered; piece is still incomplete.
    Buffered,
    /// All blocks received — piece is ready for hash verification.
    PieceComplete,
    /// Entry is a Skeleton — caller should write directly to storage.
    WriteThrough,
}

// ---------------------------------------------------------------------------
// BufferPoolStats
// ---------------------------------------------------------------------------

/// Snapshot of buffer pool counters.
#[derive(Debug, Clone, Default)]
pub(crate) struct BufferPoolStats {
    /// Bytes consumed by Writing entries.
    pub write_buffer_bytes: usize,
    /// Bytes consumed by Cached entries (managed by ARC).
    pub read_cache_bytes: usize,
    /// Total entries in the pool (Writing + Skeleton + Cached).
    pub total_entries: usize,
    /// Number of read hits served from cache.
    #[allow(dead_code)] // exposed for future session-level stats aggregation
    pub cache_hits: u64,
    /// Number of read misses (fell through to disk or not present).
    #[allow(dead_code)] // exposed for future session-level stats aggregation
    pub cache_misses: u64,
    /// Cumulative bytes served from read cache.
    #[allow(dead_code)] // exposed for future session-level stats aggregation
    pub read_bytes: u64,
    /// Cumulative bytes written into the pool.
    #[allow(dead_code)] // exposed for future session-level stats aggregation
    pub write_bytes: u64,
    /// Number of prefetch insertions.
    pub prefetch_count: u64,
    /// Number of ARC evictions.
    pub eviction_count: u64,
    /// Number of Writing-to-Skeleton demotions.
    pub skeleton_count: u64,
}

// ---------------------------------------------------------------------------
// mlock helpers
// ---------------------------------------------------------------------------

/// Global flag: once we fail an mlock due to `ENOMEM` / `RLIMIT_MEMLOCK`, log
/// once and then suppress further warnings.
#[cfg(unix)]
static MLOCK_WARNED: AtomicBool = AtomicBool::new(false);

/// Best-effort `mlock` of the backing memory for a `Bytes` value.
#[cfg(unix)]
fn mlock_bytes(data: &Bytes) {
    if data.is_empty() {
        return;
    }
    // SAFETY: `as_ptr()` returns a valid pointer into the Bytes' backing
    // allocation and `len()` is the exact length of that slice. mlock is
    // advisory and does not mutate the memory.
    let ret = unsafe { libc::mlock(data.as_ptr().cast::<libc::c_void>(), data.len()) };
    if ret != 0 {
        let errno = std::io::Error::last_os_error();
        if !MLOCK_WARNED.swap(true, Ordering::Relaxed) {
            tracing::warn!("mlock failed (subsequent failures suppressed): {errno}");
        }
    }
}

/// Best-effort `munlock` of the backing memory for a `Bytes` value.
#[cfg(unix)]
fn munlock_bytes(data: &Bytes) {
    if data.is_empty() {
        return;
    }
    // SAFETY: same as mlock_bytes — advisory, read-only pointer.
    unsafe {
        libc::munlock(data.as_ptr().cast::<libc::c_void>(), data.len());
    }
}

#[cfg(not(unix))]
fn mlock_bytes(_data: &Bytes) {}

#[cfg(not(unix))]
fn munlock_bytes(_data: &Bytes) {}

// ---------------------------------------------------------------------------
// PieceArc — purpose-built byte-budget ARC for Cached entries
// ---------------------------------------------------------------------------

/// Byte-budget Adaptive Replacement Cache for piece data.
///
/// Manages only `Cached` entries. Writing and Skeleton entries are tracked
/// separately in the outer `BufferPool::entries` map.
struct PieceArc {
    /// Maximum byte budget for cached entries.
    capacity: usize,
    /// Target T1 size in bytes (adaptive parameter).
    p: usize,
    /// Recently-seen-once entries (recency list).
    t1: VecDeque<PieceKey>,
    /// Seen-at-least-twice entries (frequency list).
    t2: VecDeque<PieceKey>,
    /// Ghost list for entries evicted from T1.
    b1: VecDeque<PieceKey>,
    /// Ghost list for entries evicted from T2.
    b2: VecDeque<PieceKey>,
    /// Byte size of each entry currently in T1 or T2.
    sizes: HashMap<PieceKey, usize>,
    /// Total bytes in T1.
    t1_bytes: usize,
    /// Total bytes in T2.
    t2_bytes: usize,
    /// Byte sizes of ghost entries (for adaptation delta calculation).
    b1_sizes: HashMap<PieceKey, usize>,
    /// Byte sizes of ghost entries (for adaptation delta calculation).
    b2_sizes: HashMap<PieceKey, usize>,
}

impl PieceArc {
    fn new(capacity: usize) -> Self {
        Self {
            capacity,
            p: 0,
            t1: VecDeque::new(),
            t2: VecDeque::new(),
            b1: VecDeque::new(),
            b2: VecDeque::new(),
            sizes: HashMap::new(),
            t1_bytes: 0,
            t2_bytes: 0,
            b1_sizes: HashMap::new(),
            b2_sizes: HashMap::new(),
        }
    }

    /// Total bytes currently tracked in T1 + T2.
    fn used_bytes(&self) -> usize {
        self.t1_bytes.saturating_add(self.t2_bytes)
    }

    /// Insert a new entry. Returns keys evicted from cache (caller must
    /// remove them from `entries` and munlock).
    fn insert(&mut self, key: PieceKey, size: usize) -> Vec<PieceKey> {
        let mut evicted = Vec::new();

        // Already in cache — promote to T2 MRU.
        if self.sizes.contains_key(&key) {
            self.promote_to_t2(&key);
            // Update size if it changed (unlikely but defensive).
            if let Some(old_size) = self.sizes.insert(key, size) {
                // Adjust byte counters — entry is now in T2.
                self.t2_bytes = self.t2_bytes.saturating_sub(old_size).saturating_add(size);
            }
            return evicted;
        }

        // B1 ghost hit — adapt toward recency.
        if let Some(pos) = self.b1.iter().position(|k| *k == key) {
            let ghost_size = self.b1_sizes.remove(&key).unwrap_or(1);
            let b2_total: usize = self.b2_sizes.values().sum();
            let b1_total: usize = self
                .b1_sizes
                .values()
                .sum::<usize>()
                .saturating_add(ghost_size);
            let delta = ghost_size.max(b2_total.checked_div(b1_total.max(1)).unwrap_or(0).max(1));
            self.p = self.p.saturating_add(delta).min(self.capacity);
            self.b1.remove(pos);
            evicted.extend(self.replace(false));
            self.t2.push_back(key);
            self.sizes.insert(key, size);
            self.t2_bytes = self.t2_bytes.saturating_add(size);
            self.evict_overflow(&mut evicted);
            self.cap_ghost_lists();
            return evicted;
        }

        // B2 ghost hit — adapt toward frequency.
        if let Some(pos) = self.b2.iter().position(|k| *k == key) {
            let ghost_size = self.b2_sizes.remove(&key).unwrap_or(1);
            let b1_total: usize = self.b1_sizes.values().sum();
            let b2_total: usize = self
                .b2_sizes
                .values()
                .sum::<usize>()
                .saturating_add(ghost_size);
            let delta = ghost_size.max(b1_total.checked_div(b2_total.max(1)).unwrap_or(0).max(1));
            self.p = self.p.saturating_sub(delta);
            self.b2.remove(pos);
            evicted.extend(self.replace(true));
            self.t2.push_back(key);
            self.sizes.insert(key, size);
            self.t2_bytes = self.t2_bytes.saturating_add(size);
            self.evict_overflow(&mut evicted);
            self.cap_ghost_lists();
            return evicted;
        }

        // Not in cache or ghost lists.
        evicted.extend(self.replace(false));
        self.t1.push_back(key);
        self.sizes.insert(key, size);
        self.t1_bytes = self.t1_bytes.saturating_add(size);
        self.evict_overflow(&mut evicted);
        self.cap_ghost_lists();
        evicted
    }

    /// Re-access an existing entry. Returns `true` if found and promoted.
    fn access(&mut self, key: &PieceKey) -> bool {
        if !self.sizes.contains_key(key) {
            return false;
        }
        self.promote_to_t2(key);
        true
    }

    /// Explicitly remove an entry (not moved to ghost lists).
    fn remove(&mut self, key: &PieceKey) {
        if let Some(size) = self.sizes.remove(key) {
            if let Some(pos) = self.t1.iter().position(|k| k == key) {
                self.t1.remove(pos);
                self.t1_bytes = self.t1_bytes.saturating_sub(size);
            } else if let Some(pos) = self.t2.iter().position(|k| k == key) {
                self.t2.remove(pos);
                self.t2_bytes = self.t2_bytes.saturating_sub(size);
            }
        }
        // Also remove from ghost lists if present (explicit removal).
        if let Some(pos) = self.b1.iter().position(|k| k == key) {
            self.b1.remove(pos);
            self.b1_sizes.remove(key);
        }
        if let Some(pos) = self.b2.iter().position(|k| k == key) {
            self.b2.remove(pos);
            self.b2_sizes.remove(key);
        }
    }

    /// Iterate over T2 keys (proven-popular entries for BEP 6 suggest).
    #[allow(dead_code)] // used by hot_pieces(), wired in Task 4
    fn t2_keys(&self) -> impl Iterator<Item = &PieceKey> {
        self.t2.iter()
    }

    // -- Internal helpers --

    /// Promote a key from T1 to T2 MRU (or move within T2 to MRU).
    fn promote_to_t2(&mut self, key: &PieceKey) {
        let size = self.sizes.get(key).copied().unwrap_or(0);
        if let Some(pos) = self.t1.iter().position(|k| k == key) {
            self.t1.remove(pos);
            self.t1_bytes = self.t1_bytes.saturating_sub(size);
            self.t2.push_back(*key);
            self.t2_bytes = self.t2_bytes.saturating_add(size);
        } else if let Some(pos) = self.t2.iter().position(|k| k == key) {
            self.t2.remove(pos);
            self.t2.push_back(*key);
        }
    }

    /// ARC replacement: evict LRU from T1 or T2 based on p. Returns the
    /// evicted key (if any).
    fn replace(&mut self, b2_hit: bool) -> Option<PieceKey> {
        if self.used_bytes() < self.capacity {
            return None;
        }
        if !self.t1.is_empty() && (self.t1_bytes > self.p || (b2_hit && self.t1_bytes == self.p)) {
            self.evict_t1_lru()
        } else {
            self.evict_t2_lru()
        }
    }

    /// Evict LRU from T1, moving to B1 ghost list.
    fn evict_t1_lru(&mut self) -> Option<PieceKey> {
        let key = self.t1.pop_front()?;
        let size = self.sizes.remove(&key).unwrap_or(0);
        self.t1_bytes = self.t1_bytes.saturating_sub(size);
        self.b1.push_back(key);
        self.b1_sizes.insert(key, size);
        Some(key)
    }

    /// Evict LRU from T2, moving to B2 ghost list.
    fn evict_t2_lru(&mut self) -> Option<PieceKey> {
        let key = self.t2.pop_front()?;
        let size = self.sizes.remove(&key).unwrap_or(0);
        self.t2_bytes = self.t2_bytes.saturating_sub(size);
        self.b2.push_back(key);
        self.b2_sizes.insert(key, size);
        Some(key)
    }

    /// Evict entries until used_bytes <= capacity.
    fn evict_overflow(&mut self, evicted: &mut Vec<PieceKey>) {
        while self.used_bytes() > self.capacity {
            // Prefer evicting from T1 when over p, else T2.
            let victim = if !self.t1.is_empty() && self.t1_bytes > self.p {
                self.evict_t1_lru()
            } else if !self.t2.is_empty() {
                self.evict_t2_lru()
            } else {
                self.evict_t1_lru()
            };
            match victim {
                Some(k) => evicted.push(k),
                None => break,
            }
        }
    }

    /// Cap ghost list total bytes at `capacity`.
    fn cap_ghost_lists(&mut self) {
        let ghost_budget = self.capacity;
        while self.ghost_bytes_b1() > ghost_budget {
            if let Some(k) = self.b1.pop_front() {
                self.b1_sizes.remove(&k);
            } else {
                break;
            }
        }
        while self.ghost_bytes_b2() > ghost_budget {
            if let Some(k) = self.b2.pop_front() {
                self.b2_sizes.remove(&k);
            } else {
                break;
            }
        }
    }

    fn ghost_bytes_b1(&self) -> usize {
        self.b1_sizes.values().sum()
    }

    fn ghost_bytes_b2(&self) -> usize {
        self.b2_sizes.values().sum()
    }
}

// ---------------------------------------------------------------------------
// BufferPool
// ---------------------------------------------------------------------------

/// Unified buffer pool combining write buffering and read caching under a
/// single byte budget.
///
/// Writing entries accumulate blocks in memory. On piece completion the caller
/// takes the assembled data for hash verification. On success the piece is
/// promoted to a Cached entry managed by the ARC eviction policy. Read requests
/// are served from either Writing or Cached entries; Skeleton entries (flushed
/// to disk) return `None` so the caller falls through to disk I/O.
pub(crate) struct BufferPool {
    /// All piece entries regardless of state.
    entries: HashMap<PieceKey, CachedPiece>,
    /// Purpose-built ARC for Cached entries only.
    arc: PieceArc,
    /// Total byte budget (default 64 MiB).
    total_capacity: usize,
    /// Bytes used by Writing entries.
    write_bytes: usize,
    /// Whether to mlock Cached entry memory.
    enable_mlock: bool,
    /// Insertion-order tracker for Writing entries (for eviction ordering).
    write_order: VecDeque<PieceKey>,
    // -- Stats --
    cache_hits: u64,
    cache_misses: u64,
    read_bytes_stat: u64,
    write_bytes_stat: u64,
    prefetch_count: u64,
    eviction_count: u64,
    skeleton_count: u64,
}

impl BufferPool {
    /// Create a new buffer pool with the default 64 MiB capacity.
    #[cfg(test)]
    pub fn new() -> Self {
        Self::with_capacity(DEFAULT_CAPACITY)
    }

    /// Create a new buffer pool with a specific byte capacity.
    pub fn with_capacity(total_capacity: usize) -> Self {
        Self {
            entries: HashMap::new(),
            arc: PieceArc::new(total_capacity),
            total_capacity,
            write_bytes: 0,
            enable_mlock: cfg!(unix),
            write_order: VecDeque::new(),
            cache_hits: 0,
            cache_misses: 0,
            read_bytes_stat: 0,
            write_bytes_stat: 0,
            prefetch_count: 0,
            eviction_count: 0,
            skeleton_count: 0,
        }
    }

    /// Enable or disable mlock for cached entries.
    pub fn set_mlock(&mut self, enable: bool) {
        self.enable_mlock = enable;
    }

    /// Write a block into the pool.
    ///
    /// Returns the write status indicating whether the block was buffered,
    /// the piece is now complete, or the caller should write through to disk.
    pub fn write_block(
        &mut self,
        key: PieceKey,
        begin: u32,
        data: Bytes,
        piece_size: usize,
    ) -> WriteStatus {
        let data_len = data.len();
        self.write_bytes_stat = self.write_bytes_stat.saturating_add(data_len as u64);

        match self.entries.get_mut(&key) {
            Some(CachedPiece::Writing {
                blocks,
                total_bytes,
                piece_size: ps,
            }) => {
                // Dedup: subtract old block size if offset already exists.
                if let Some(old) = blocks.insert(begin, data) {
                    *total_bytes = total_bytes.saturating_sub(old.len());
                    self.write_bytes = self.write_bytes.saturating_sub(old.len());
                }
                *total_bytes = total_bytes.saturating_add(data_len);
                self.write_bytes = self.write_bytes.saturating_add(data_len);

                let complete = *total_bytes >= *ps;
                self.maybe_evict();
                if complete {
                    WriteStatus::PieceComplete
                } else {
                    WriteStatus::Buffered
                }
            }
            Some(CachedPiece::Skeleton {
                flushed,
                total_bytes,
                piece_size: ps,
            }) => {
                // Track that this block was received; caller writes to disk.
                flushed.insert(begin);
                *total_bytes = total_bytes.saturating_add(data_len);

                if *total_bytes >= *ps {
                    WriteStatus::PieceComplete
                } else {
                    WriteStatus::WriteThrough
                }
            }
            Some(CachedPiece::Cached { .. }) => {
                // Already cached as a full piece — treat as duplicate / no-op.
                WriteStatus::Buffered
            }
            None => {
                // New entry.
                let mut blocks = BTreeMap::new();
                blocks.insert(begin, data);
                self.entries.insert(
                    key,
                    CachedPiece::Writing {
                        blocks,
                        total_bytes: data_len,
                        piece_size,
                    },
                );
                self.write_bytes = self.write_bytes.saturating_add(data_len);
                self.write_order.push_back(key);

                let complete = data_len >= piece_size;
                self.maybe_evict();
                if complete {
                    WriteStatus::PieceComplete
                } else {
                    WriteStatus::Buffered
                }
            }
        }
    }

    /// Take the completed piece data for hash verification.
    ///
    /// If the entry is `Writing` and `total_bytes >= piece_size`, concatenates
    /// blocks in offset order and returns them. Removes the entry.
    #[cfg(test)]
    pub fn take_completed_data(&mut self, key: PieceKey) -> Option<Vec<u8>> {
        match self.entries.get(&key) {
            Some(CachedPiece::Writing {
                total_bytes,
                piece_size,
                ..
            }) if *total_bytes >= *piece_size => {}
            _ => return None,
        }

        // Remove the entry to take ownership of blocks.
        let entry = self.entries.remove(&key)?;
        self.remove_from_write_order(&key);

        if let CachedPiece::Writing {
            blocks,
            total_bytes,
            ..
        } = entry
        {
            self.write_bytes = self.write_bytes.saturating_sub(total_bytes);
            let mut assembled = Vec::with_capacity(total_bytes);
            for (_, block) in blocks {
                assembled.extend_from_slice(&block);
            }
            Some(assembled)
        } else {
            None
        }
    }

    /// Take all blocks from a Writing entry, assembling them into a contiguous
    /// `Vec<u8>` regardless of whether `total_bytes >= piece_size`.
    ///
    /// Returns `None` if the entry does not exist or is not in `Writing` state.
    /// Blocks are placed at their byte offset within the piece, so the returned
    /// buffer may contain zero-filled gaps if blocks are non-contiguous.
    ///
    /// The entry is removed on success. This is used by `hash_piece` to hash
    /// from cache when `piece_size` was not known at write time (the existing
    /// `ChunkTracker` handles completion detection).
    pub fn take_all_blocks(&mut self, key: PieceKey) -> Option<Vec<u8>> {
        if !matches!(self.entries.get(&key), Some(CachedPiece::Writing { .. })) {
            return None;
        }

        let entry = self.entries.remove(&key)?;
        self.remove_from_write_order(&key);

        if let CachedPiece::Writing {
            blocks,
            total_bytes,
            ..
        } = entry
        {
            self.write_bytes = self.write_bytes.saturating_sub(total_bytes);

            // Determine the assembled piece size from the furthest block end.
            let size = blocks
                .iter()
                .map(|(begin, data)| (*begin as usize).saturating_add(data.len()))
                .max()
                .unwrap_or(0);

            let mut assembled = vec![0u8; size];
            for (begin, data) in &blocks {
                let start = *begin as usize;
                let end = start.saturating_add(data.len());
                if end <= assembled.len() {
                    assembled[start..end].copy_from_slice(data);
                }
            }
            Some(assembled)
        } else {
            None
        }
    }

    /// Promote a verified piece to the read cache.
    ///
    /// Inserts as a `Cached` entry, managed by the ARC. If the ARC evicts
    /// entries to make room, those are removed from `entries` and munlocked.
    pub fn promote_to_cached(&mut self, key: PieceKey, data: Bytes) {
        let size = data.len();

        // Remove any existing entry first (e.g., stale Skeleton).
        self.remove_entry_internal(&key);

        // Insert into ARC — may evict other Cached entries.
        let evicted = self.arc.insert(key, size);
        self.handle_evictions(&evicted);

        if self.enable_mlock {
            mlock_bytes(&data);
        }

        self.entries.insert(key, CachedPiece::Cached { data });
    }

    /// Read a block from the pool.
    ///
    /// - `Writing`: look up the block in the BTreeMap.
    /// - `Cached`: return a slice and promote in the ARC.
    /// - `Skeleton` or miss: return `None` (fall through to disk).
    pub fn read_block(&mut self, key: PieceKey, begin: u32, length: usize) -> Option<Bytes> {
        match self.entries.get(&key) {
            Some(CachedPiece::Writing { blocks, .. }) => {
                let block = blocks.get(&begin)?;
                if block.len() >= length {
                    self.cache_hits = self.cache_hits.saturating_add(1);
                    self.read_bytes_stat = self.read_bytes_stat.saturating_add(length as u64);
                    Some(block.slice(..length))
                } else {
                    self.cache_misses = self.cache_misses.saturating_add(1);
                    None
                }
            }
            Some(CachedPiece::Cached { data }) => {
                let end = begin as usize + length;
                if end > data.len() {
                    self.cache_misses = self.cache_misses.saturating_add(1);
                    return None;
                }
                self.cache_hits = self.cache_hits.saturating_add(1);
                self.read_bytes_stat = self.read_bytes_stat.saturating_add(length as u64);
                let slice = data.slice(begin as usize..end);
                self.arc.access(&key);
                Some(slice)
            }
            Some(CachedPiece::Skeleton { .. }) | None => {
                self.cache_misses = self.cache_misses.saturating_add(1);
                None
            }
        }
    }

    /// Prefetch a piece into the read cache (called on read cache miss after
    /// loading from disk).
    pub fn prefetch_piece(&mut self, key: PieceKey, data: Bytes) {
        self.prefetch_count = self.prefetch_count.saturating_add(1);
        self.promote_to_cached(key, data);
    }

    /// Remove a piece from the pool regardless of state.
    pub fn clear_piece(&mut self, key: PieceKey) {
        self.remove_entry_internal(&key);
    }

    /// Remove all entries for a torrent.
    pub fn clear_torrent(&mut self, info_hash: Id20) {
        let keys: Vec<PieceKey> = self
            .entries
            .keys()
            .filter(|(ih, _)| *ih == info_hash)
            .copied()
            .collect();
        for key in keys {
            self.remove_entry_internal(&key);
        }
    }

    /// Return up to 16 piece indices from the ARC T2 list (proven-popular)
    /// for the given torrent. Used for BEP 6 suggest messages.
    pub fn hot_pieces(&self, info_hash: Id20) -> Vec<u32> {
        self.arc
            .t2_keys()
            .filter(|(ih, _)| *ih == info_hash)
            .map(|(_, idx)| *idx)
            .take(MAX_HOT_PIECES)
            .collect()
    }

    /// Return all piece indices currently in `Cached` state for the given
    /// torrent. Used by [`DiskIoBackend::cached_pieces`] for peer suggest.
    #[allow(dead_code)] // reserved for future use; hot_pieces() is the primary API
    pub fn cached_pieces(&self, info_hash: Id20) -> Vec<u32> {
        self.entries
            .iter()
            .filter_map(|((ih, idx), entry)| {
                if *ih == info_hash && matches!(entry, CachedPiece::Cached { .. }) {
                    Some(*idx)
                } else {
                    None
                }
            })
            .collect()
    }

    /// Flush a single Writing entry, returning its blocks for the caller to
    /// write to disk. The entry is removed entirely (not converted to Skeleton).
    pub fn flush_piece(&mut self, key: PieceKey) -> Option<Vec<(u32, Bytes)>> {
        match self.entries.get(&key) {
            Some(CachedPiece::Writing { .. }) => {}
            _ => return None,
        }

        let entry = self.entries.remove(&key)?;
        self.remove_from_write_order(&key);

        if let CachedPiece::Writing {
            blocks,
            total_bytes,
            ..
        } = entry
        {
            self.write_bytes = self.write_bytes.saturating_sub(total_bytes);
            Some(blocks.into_iter().collect())
        } else {
            None
        }
    }

    /// Flush all Writing entries, returning their blocks.
    pub fn flush_all(&mut self) -> Vec<(PieceKey, Vec<(u32, Bytes)>)> {
        let writing_keys: Vec<PieceKey> = self
            .entries
            .iter()
            .filter_map(|(k, v)| {
                if matches!(v, CachedPiece::Writing { .. }) {
                    Some(*k)
                } else {
                    None
                }
            })
            .collect();

        let mut result = Vec::with_capacity(writing_keys.len());
        for key in writing_keys {
            if let Some(blocks) = self.flush_piece(key) {
                result.push((key, blocks));
            }
        }
        result
    }

    /// Return a snapshot of all counters.
    pub fn stats(&self) -> BufferPoolStats {
        BufferPoolStats {
            write_buffer_bytes: self.write_bytes,
            read_cache_bytes: self.arc.used_bytes(),
            total_entries: self.entries.len(),
            cache_hits: self.cache_hits,
            cache_misses: self.cache_misses,
            read_bytes: self.read_bytes_stat,
            write_bytes: self.write_bytes_stat,
            prefetch_count: self.prefetch_count,
            eviction_count: self.eviction_count,
            skeleton_count: self.skeleton_count,
        }
    }

    // -- Internal helpers --

    /// Current total bytes used (Writing + Cached).
    fn total_used(&self) -> usize {
        self.write_bytes.saturating_add(self.arc.used_bytes())
    }

    /// If the pool is over capacity, evict Cached entries first, then demote
    /// the oldest Writing entry to Skeleton.
    fn maybe_evict(&mut self) {
        // Phase 1: evict Cached entries via ARC.
        while self.total_used() > self.total_capacity && self.arc.used_bytes() > 0 {
            // Evict LRU from ARC.
            let victim = if !self.arc.t1.is_empty() && self.arc.t1_bytes > self.arc.p {
                self.arc.evict_t1_lru()
            } else if !self.arc.t2.is_empty() {
                self.arc.evict_t2_lru()
            } else {
                self.arc.evict_t1_lru()
            };
            match victim {
                Some(key) => {
                    if let Some(CachedPiece::Cached { data }) = self.entries.remove(&key)
                        && self.enable_mlock
                    {
                        munlock_bytes(&data);
                    }
                    self.eviction_count = self.eviction_count.saturating_add(1);
                }
                None => break,
            }
        }

        // Phase 2: if still over capacity, demote oldest Writing to Skeleton.
        while self.total_used() > self.total_capacity {
            let oldest = self.write_order.pop_front();
            match oldest {
                Some(key) => {
                    if let Some(CachedPiece::Writing {
                        blocks,
                        total_bytes,
                        piece_size,
                    }) = self.entries.remove(&key)
                    {
                        let flushed: HashSet<u32> = blocks.keys().copied().collect();
                        self.write_bytes = self.write_bytes.saturating_sub(total_bytes);
                        self.skeleton_count = self.skeleton_count.saturating_add(1);
                        self.entries.insert(
                            key,
                            CachedPiece::Skeleton {
                                flushed,
                                total_bytes,
                                piece_size,
                            },
                        );
                    }
                }
                None => break,
            }
        }
    }

    /// Remove an entry from the pool, cleaning up all associated state.
    fn remove_entry_internal(&mut self, key: &PieceKey) {
        if let Some(entry) = self.entries.remove(key) {
            match entry {
                CachedPiece::Writing { total_bytes, .. } => {
                    self.write_bytes = self.write_bytes.saturating_sub(total_bytes);
                    self.remove_from_write_order(key);
                }
                CachedPiece::Cached { ref data } => {
                    if self.enable_mlock {
                        munlock_bytes(data);
                    }
                    self.arc.remove(key);
                }
                CachedPiece::Skeleton { .. } => {
                    // No byte counters to adjust (Skeleton doesn't hold memory).
                }
            }
        }
    }

    /// Remove a key from the write-order tracker.
    fn remove_from_write_order(&mut self, key: &PieceKey) {
        if let Some(pos) = self.write_order.iter().position(|k| k == key) {
            self.write_order.remove(pos);
        }
    }

    /// Handle evicted keys from the ARC: remove from entries + munlock.
    fn handle_evictions(&mut self, evicted: &[PieceKey]) {
        for key in evicted {
            if let Some(CachedPiece::Cached { data }) = self.entries.remove(key)
                && self.enable_mlock
            {
                munlock_bytes(&data);
            }
            self.eviction_count = self.eviction_count.saturating_add(1);
        }
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    /// Helper: create a deterministic `Id20` from a single byte.
    fn hash(n: u8) -> Id20 {
        let mut b = [0u8; 20];
        b[0] = n;
        Id20(b)
    }

    /// Helper: create test data of a specific length.
    fn data(len: usize) -> Bytes {
        Bytes::from(vec![0xAB_u8; len])
    }

    /// Helper: create test data with a specific fill byte.
    fn data_fill(len: usize, fill: u8) -> Bytes {
        Bytes::from(vec![fill; len])
    }

    // 1. write_chunk_buffers_until_complete
    #[test]
    fn write_chunk_buffers_until_complete() {
        let mut pool = BufferPool::with_capacity(1024 * 1024);
        pool.set_mlock(false);
        let key = (hash(1), 0);
        let piece_size = 300;

        let s1 = pool.write_block(key, 0, data(100), piece_size);
        assert_eq!(s1, WriteStatus::Buffered);

        let s2 = pool.write_block(key, 100, data(100), piece_size);
        assert_eq!(s2, WriteStatus::Buffered);

        let s3 = pool.write_block(key, 200, data(100), piece_size);
        assert_eq!(s3, WriteStatus::PieceComplete);
    }

    // 2. write_chunk_accepts_bytes_zero_copy
    #[test]
    fn write_chunk_accepts_bytes_zero_copy() {
        let mut pool = BufferPool::with_capacity(1024 * 1024);
        pool.set_mlock(false);
        let key = (hash(1), 0);

        let original = Bytes::from(vec![42u8; 100]);
        // Clone to keep a reference — the refcount should be 2 (original + clone).
        let clone = original.clone();
        pool.write_block(key, 0, clone, 200);

        // original still has a refcount > 1 because the pool holds a Bytes
        // sharing the same backing allocation.
        let attempt = original;
        // try_mut fails when refcount > 1, confirming zero-copy sharing.
        assert!(attempt.try_into_mut().is_err());
    }

    // 3. read_chunk_hits_write_cache
    #[test]
    fn read_chunk_hits_write_cache() {
        let mut pool = BufferPool::with_capacity(1024 * 1024);
        pool.set_mlock(false);
        let key = (hash(1), 0);

        let block = data_fill(100, 0xCC);
        pool.write_block(key, 0, block, 200);

        let read = pool.read_block(key, 0, 100);
        assert!(read.is_some());
        assert_eq!(read.as_ref().unwrap().len(), 100);
        assert_eq!(pool.cache_hits, 1);
    }

    // 4. read_chunk_miss_prefetches_full_piece
    #[test]
    fn read_chunk_miss_prefetches_full_piece() {
        let mut pool = BufferPool::with_capacity(1024 * 1024);
        pool.set_mlock(false);
        let key = (hash(1), 0);

        // Initial read is a miss.
        let miss = pool.read_block(key, 0, 100);
        assert!(miss.is_none());
        assert_eq!(pool.cache_misses, 1);

        // Prefetch the full piece.
        pool.prefetch_piece(key, data_fill(256, 0xDD));

        // Now read should hit.
        let hit = pool.read_block(key, 0, 100);
        assert!(hit.is_some());
        assert_eq!(&hit.unwrap()[..], &[0xDD; 100]);
        assert_eq!(pool.cache_hits, 1);
        assert_eq!(pool.prefetch_count, 1);
    }

    // 5. read_cache_arc_eviction
    #[test]
    fn read_cache_arc_eviction() {
        // Capacity for exactly 2 entries of 100 bytes each.
        let mut pool = BufferPool::with_capacity(200);
        pool.set_mlock(false);
        let k1 = (hash(1), 0);
        let k2 = (hash(1), 1);
        let k3 = (hash(1), 2);

        pool.promote_to_cached(k1, data(100));
        pool.promote_to_cached(k2, data(100));

        // Pool is at capacity. Inserting k3 should evict k1 (LRU).
        pool.promote_to_cached(k3, data(100));

        assert!(pool.read_block(k1, 0, 100).is_none());
        assert!(pool.read_block(k2, 0, 100).is_some());
        assert!(pool.read_block(k3, 0, 100).is_some());
        assert!(pool.eviction_count >= 1);
    }

    // 6. back_pressure_evicts_cached_first
    #[test]
    fn back_pressure_evicts_cached_first() {
        // 300 bytes capacity: 100 Writing + 200 Cached = at capacity.
        let mut pool = BufferPool::with_capacity(300);
        pool.set_mlock(false);
        let wk = (hash(1), 0);
        let ck1 = (hash(1), 1);
        let ck2 = (hash(1), 2);

        // One writing entry.
        pool.write_block(wk, 0, data(100), 200);
        // Two cached entries.
        pool.promote_to_cached(ck1, data(100));
        pool.promote_to_cached(ck2, data(100));

        // Add more writing data — should evict Cached entries first.
        pool.write_block(wk, 100, data(100), 200);

        // Writing entry should still be present.
        assert!(pool.read_block(wk, 0, 100).is_some());
        // At least one cached entry should have been evicted.
        let cached_remaining = [ck1, ck2]
            .iter()
            .filter(|k| {
                pool.entries.contains_key(k)
                    && matches!(pool.entries[k], CachedPiece::Cached { .. })
            })
            .count();
        assert!(
            cached_remaining < 2,
            "expected at least one cached eviction"
        );
    }

    // 7. back_pressure_creates_skeleton
    #[test]
    fn back_pressure_creates_skeleton() {
        // Very small capacity — only room for one Writing entry.
        let mut pool = BufferPool::with_capacity(100);
        pool.set_mlock(false);
        let k1 = (hash(1), 0);
        let k2 = (hash(1), 1);

        pool.write_block(k1, 0, data(80), 200);
        // This should trigger eviction — k1 becomes Skeleton.
        pool.write_block(k2, 0, data(80), 200);

        // One of them should be a Skeleton.
        let skeleton_count = pool
            .entries
            .values()
            .filter(|v| matches!(v, CachedPiece::Skeleton { .. }))
            .count();
        assert!(skeleton_count >= 1, "expected at least one Skeleton entry");
        assert!(pool.skeleton_count >= 1);
    }

    // 8. skeleton_entry_completes
    #[test]
    fn skeleton_entry_completes() {
        let mut pool = BufferPool::with_capacity(1024 * 1024);
        pool.set_mlock(false);
        let key = (hash(1), 0);
        let piece_size = 200;

        // Manually insert a Skeleton entry.
        let mut flushed = HashSet::new();
        flushed.insert(0);
        pool.entries.insert(
            key,
            CachedPiece::Skeleton {
                flushed,
                total_bytes: 100,
                piece_size,
            },
        );

        // Write the remaining block — should complete.
        let status = pool.write_block(key, 100, data(100), piece_size);
        assert_eq!(status, WriteStatus::PieceComplete);

        // Verify the Skeleton state.
        match pool.entries.get(&key) {
            Some(CachedPiece::Skeleton {
                total_bytes,
                piece_size: ps,
                ..
            }) => {
                assert_eq!(*total_bytes, 200);
                assert_eq!(*ps, piece_size);
            }
            other => panic!("expected Skeleton, got {other:?}"),
        }
    }

    // 9. clear_piece_removes_any_state
    #[test]
    fn clear_piece_removes_any_state() {
        let mut pool = BufferPool::with_capacity(1024 * 1024);
        pool.set_mlock(false);

        let k_writing = (hash(1), 0);
        let k_cached = (hash(1), 1);
        let k_skeleton = (hash(1), 2);

        // Writing entry.
        pool.write_block(k_writing, 0, data(100), 200);

        // Cached entry.
        pool.promote_to_cached(k_cached, data(100));

        // Skeleton entry (manually).
        pool.entries.insert(
            k_skeleton,
            CachedPiece::Skeleton {
                flushed: HashSet::new(),
                total_bytes: 0,
                piece_size: 200,
            },
        );

        // Clear all three.
        pool.clear_piece(k_writing);
        pool.clear_piece(k_cached);
        pool.clear_piece(k_skeleton);

        assert!(pool.entries.is_empty());
        assert_eq!(pool.write_bytes, 0);
        assert_eq!(pool.arc.used_bytes(), 0);
    }

    // 10. hash_from_cache_pass_promotes
    #[test]
    fn hash_from_cache_pass_promotes() {
        let mut pool = BufferPool::with_capacity(1024 * 1024);
        pool.set_mlock(false);
        let key = (hash(1), 0);
        let piece_size = 200;

        pool.write_block(key, 0, data(100), piece_size);
        pool.write_block(key, 100, data(100), piece_size);

        let assembled = pool.take_completed_data(key).expect("should be complete");
        assert_eq!(assembled.len(), piece_size);
        assert!(!pool.entries.contains_key(&key));

        // Promote to cached (hash passed).
        pool.promote_to_cached(key, Bytes::from(assembled));
        assert!(matches!(
            pool.entries.get(&key),
            Some(CachedPiece::Cached { .. })
        ));
        assert!(pool.read_block(key, 0, 100).is_some());
    }

    // 11. hash_from_cache_fail_discards
    #[test]
    fn hash_from_cache_fail_discards() {
        let mut pool = BufferPool::with_capacity(1024 * 1024);
        pool.set_mlock(false);
        let key = (hash(1), 0);
        let piece_size = 200;

        pool.write_block(key, 0, data(100), piece_size);
        pool.write_block(key, 100, data(100), piece_size);

        let assembled = pool.take_completed_data(key);
        assert!(assembled.is_some());

        // Hash failed — discard.
        pool.clear_piece(key);
        assert!(!pool.entries.contains_key(&key));
        assert!(pool.read_block(key, 0, 100).is_none());
    }

    // 12. read_from_skeleton_falls_to_disk
    #[test]
    fn read_from_skeleton_falls_to_disk() {
        let mut pool = BufferPool::with_capacity(1024 * 1024);
        pool.set_mlock(false);
        let key = (hash(1), 0);

        pool.entries.insert(
            key,
            CachedPiece::Skeleton {
                flushed: HashSet::new(),
                total_bytes: 0,
                piece_size: 200,
            },
        );

        let read = pool.read_block(key, 0, 100);
        assert!(read.is_none());
        assert_eq!(pool.cache_misses, 1);
    }

    // 13. hot_pieces_returns_t2_entries
    #[test]
    fn hot_pieces_returns_t2_entries() {
        let mut pool = BufferPool::with_capacity(1024 * 1024);
        pool.set_mlock(false);
        let ih = hash(1);

        // Insert pieces and access them twice to promote to T2.
        for i in 0..5_u32 {
            let key = (ih, i);
            pool.promote_to_cached(key, data(100));
            // Access again to promote from T1 to T2.
            pool.read_block(key, 0, 50);
        }

        let hot = pool.hot_pieces(ih);
        assert_eq!(hot.len(), 5);
        for i in 0..5_u32 {
            assert!(hot.contains(&i), "expected piece {i} in hot list");
        }
    }

    // 14. hot_pieces_caps_at_16
    #[test]
    fn hot_pieces_caps_at_16() {
        let mut pool = BufferPool::with_capacity(1024 * 1024);
        pool.set_mlock(false);
        let ih = hash(1);

        for i in 0..30_u32 {
            let key = (ih, i);
            pool.promote_to_cached(key, data(100));
            pool.read_block(key, 0, 50);
        }

        let hot = pool.hot_pieces(ih);
        assert_eq!(hot.len(), MAX_HOT_PIECES);
    }

    // 15. volatile_read_bypasses_cache
    #[test]
    fn volatile_read_bypasses_cache() {
        let mut pool = BufferPool::with_capacity(1024 * 1024);
        pool.set_mlock(false);
        let key = (hash(99), 42);

        let read = pool.read_block(key, 0, 100);
        assert!(read.is_none());
        assert_eq!(pool.cache_misses, 1);
    }

    // 16. mlock_called_on_insert_and_evict
    #[cfg(unix)]
    #[test]
    fn mlock_called_on_insert_and_evict() {
        use std::sync::atomic::AtomicUsize;

        // We cannot easily mock libc::mlock, so we verify the behavior
        // indirectly: mlock is called on Cached insert and munlock on eviction.
        // On most test systems mlock succeeds for small allocations.
        static MLOCK_CALLS: AtomicUsize = AtomicUsize::new(0);
        static MUNLOCK_CALLS: AtomicUsize = AtomicUsize::new(0);
        MLOCK_CALLS.store(0, Ordering::SeqCst);
        MUNLOCK_CALLS.store(0, Ordering::SeqCst);

        // Create a pool with mlock enabled and small capacity.
        let mut pool = BufferPool::with_capacity(200);
        pool.set_mlock(true);

        let k1 = (hash(1), 0);
        let k2 = (hash(1), 1);
        let k3 = (hash(1), 2);

        // Insert two entries (should call mlock twice).
        pool.promote_to_cached(k1, data(100));
        pool.promote_to_cached(k2, data(100));

        // Insert third — should evict one (munlock called).
        pool.promote_to_cached(k3, data(100));

        // We can verify that evictions happened.
        assert!(pool.eviction_count >= 1, "expected at least one eviction");

        // At least verify the entries are in the expected state.
        assert_eq!(pool.entries.len(), 2);
    }

    // 17. eviction_flush_error_reverts_to_writing
    #[test]
    fn eviction_flush_error_reverts_to_writing() {
        // Test that Skeleton -> PieceComplete still works even after blocks
        // were "flushed". Since BufferPool doesn't do I/O, we verify that
        // a Skeleton entry completes properly when all blocks are received.
        let mut pool = BufferPool::with_capacity(1024 * 1024);
        pool.set_mlock(false);
        let key = (hash(1), 0);
        let piece_size = 300;

        // Simulate: blocks 0 and 100 were flushed to disk (Skeleton state).
        let mut flushed = HashSet::new();
        flushed.insert(0);
        flushed.insert(100);
        pool.entries.insert(
            key,
            CachedPiece::Skeleton {
                flushed,
                total_bytes: 200,
                piece_size,
            },
        );

        // Remaining block arrives.
        let status = pool.write_block(key, 200, data(100), piece_size);
        assert_eq!(status, WriteStatus::PieceComplete);
    }

    // 18. arc_ghost_list_adaptation
    #[test]
    fn arc_ghost_list_adaptation() {
        // Test the ARC adaptation mechanics: B1 hits expand T1 target (p),
        // B2 hits shrink T1 target (p), and ghost lists are capped.
        let mut arc = PieceArc::new(200);
        let ih = hash(1);

        // Fill T1 with two entries.
        let k1 = (ih, 0);
        let k2 = (ih, 1);
        arc.insert(k1, 100);
        arc.insert(k2, 100);

        // Insert k3 — evicts k1 to B1 ghost list.
        let k3 = (ih, 2);
        let evicted = arc.insert(k3, 100);
        assert!(evicted.contains(&k1) || !arc.sizes.contains_key(&k1));

        // Record p before B1 hit.
        let p_before = arc.p;

        // Re-insert k1 — B1 ghost hit, should increase p (favor recency).
        arc.insert(k1, 100);
        assert!(
            arc.p >= p_before,
            "B1 hit should increase p: was {p_before}, now {}",
            arc.p
        );

        // Now set up a B2 hit scenario.
        // Access k2 to promote it to T2 (if still in T1).
        arc.access(&k2);

        // Fill up and evict from T2 to B2.
        let k4 = (ih, 3);
        let k5 = (ih, 4);
        arc.insert(k4, 100);
        arc.insert(k5, 100);

        // At this point some entries have been evicted to B2.
        let p_before_b2 = arc.p;

        // Find a key in B2 and re-insert it.
        let b2_key = arc.b2.front().copied();
        if let Some(bk) = b2_key {
            arc.insert(bk, 100);
            assert!(
                arc.p <= p_before_b2,
                "B2 hit should decrease p: was {p_before_b2}, now {}",
                arc.p
            );
        }

        // Verify ghost lists are bounded.
        assert!(
            arc.ghost_bytes_b1() <= arc.capacity,
            "B1 ghost bytes {} exceeds capacity {}",
            arc.ghost_bytes_b1(),
            arc.capacity
        );
        assert!(
            arc.ghost_bytes_b2() <= arc.capacity,
            "B2 ghost bytes {} exceeds capacity {}",
            arc.ghost_bytes_b2(),
            arc.capacity
        );
    }
}