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
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
//! Worker-local io_uring submission, completion, and deadlines.
//!
//! [`Waiters`] owns each request's descriptors, buffers, progress, and observer.
//! The driver queues only IDs. Durability requests first acquire their open's
//! permit. Eligible requests and partial completions join the ready queue,
//! waiting for both SQ space and an available operation slot.
//! Completion releases that slot even if the local result remains unconsumed
//! in its waiter. Detached sync results leave through [`Deferred`].
//!
//! # Submission and Cancellation
//!
//! Staging keeps every SQE-referenced resource alive until the operation CQE,
//! including when submission returns a transient error. Cancellation CQEs only
//! acknowledge the cancellation attempt. They cannot release those resources.
//!
//! Dropping a local or forwarded observer cancels reads and network operations.
//! Queued requests can finish immediately, while in-flight requests need a cancellation
//! SQE and must await their operation CQE. Writes and syncs continue through all
//! follow-up SQEs after their observers are dropped.
//!
//! Cancellations stage first, followed by operations in FIFO order and wake-poll
//! rearming. The operation limit excludes cancellation and wake SQEs. A full SQ
//! needs a flush, but reaching the operation limit requires a CQE to free capacity.
//!
//! # Service and Parking
//!
//! Each service turn reaps completions, advances the timeout wheel, registers new
//! deadlines, and stages queued work. Reaping first lets a terminal completion
//! win a race with expiry. Partial progress after expiry cannot issue another SQE.
//! Queued requests expire even while all operation slots are occupied.
//!
//! The ring uses SINGLE_ISSUER and DEFER_TASKRUN, requiring Linux 6.1 or newer.
//! Unfinished requests need a GETEVENTS enter even when tasks keep the worker
//! busy. An idle turn may defer that enter to [`Driver::park`]. Control SQEs carry
//! no request resources and may remain until later I/O service or ring teardown.
//!
//! Parking installs the wake poll if needed, then arms the mailbox waker and
//! checks for pending publication or a latched signal before blocking. A wake
//! CQE without MORE requires rearm before the next blocking wait. See
//! [`super::idle`] for how pool worker selection fits with this wait.
//!
//! # Cleanup
//!
//! Observer callbacks and request-resource destruction go through [`Deferred`],
//! outside the worker borrow. Closure detaches local and forwarded observers and
//! cancels eligible requests. The worker continues servicing until all requests
//! retire, keeping the driver in place throughout the drain.
use super::{
registration::Observation,
request::{Request, RequestOutput},
runtime::{Deferred, RingConfig},
timeout::TimeoutWheel,
waiter::{CompletionOutcome, Observer, UserData, WaiterId, Waiters},
waker::{WAKE_USER_DATA, Waker},
};
use crate::Error;
use commonware_utils::{channel::oneshot, sync::OwnedAsyncMutexGuard};
use futures::{Stream, future::BoxFuture, stream::FuturesUnordered};
use io_uring::{
EnterFlags, IoUring,
opcode::AsyncCancel,
squeue::SubmissionQueue,
types::{SubmitArgs, Timespec},
};
use std::{
collections::VecDeque,
io,
pin::Pin,
sync::Arc,
task::{Context, Poll, Waker as TaskWaker},
time::{Duration, Instant},
};
use tracing::warn;
/// Shared acknowledgement token for cancellation SQEs.
///
/// Like the wake token, this uses the slot index reserved by [`Waiters::insert`].
const CANCEL_USER_DATA: UserData = u32::MAX as UserData;
/// Ring and request state accessed exclusively by the owning worker.
pub struct Driver {
/// Declared first so ring destruction precedes descriptor and buffer release.
ring: IoUring,
/// Kept separately to allow batching SQ access while updating request state.
state: State,
}
/// Request state borrowed independently of the ring's SQ and CQ mappings.
struct State {
/// Queued requests, in-flight requests, and unconsumed local results.
waiters: Waiters,
/// Maximum number of outstanding operation SQEs.
in_flight_limit: usize,
/// FIFO of initial and follow-up operation SQEs, with lazily removed stale IDs.
ready_queue: VecDeque<WaiterId>,
/// Accepted durability requests awaiting their open's permit, absent from the FIFO.
acquiring: FuturesUnordered<BoxFuture<'static, (WaiterId, OwnedAsyncMutexGuard<()>)>>,
/// Permit grants wake ring service even after the ordinary mailbox closes.
acquisition_waker: TaskWaker,
/// New requests whose deadlines must be registered after advancing the wheel.
pending_deadlines: VecDeque<WaiterId>,
/// Requests awaiting a cancellation SQE, skipped if their operation finishes first.
pending_cancels: VecDeque<WaiterId>,
/// Deadlines for queued and in-flight requests, independent of sleepers.
timeout_wheel: TimeoutWheel,
/// Shared mailbox wake source retained through ring destruction.
waker: Waker,
/// Whether the multishot wake poll needs to be installed again.
wake_rearm_needed: bool,
}
/// What [`Driver::enter`] asks of the kernel after submitting queued SQEs.
#[derive(Clone, Copy)]
enum Enter {
/// Nothing more. Used to make room in a full SQ.
Flush,
/// Run deferred task work, which posts finished requests to the CQ, and
/// return without waiting.
TaskWork,
/// Run deferred task work and sleep until a completion arrives.
Wait {
/// When to stop waiting, or `None` to wait without a bound. A
/// deadline that already passed does not sleep.
deadline: Option<Instant>,
},
}
impl Driver {
/// Create a ring on its permanent owner thread after configuration validation.
///
/// Uses the CQ sizing and overflow policy described by [`RingConfig::size`].
pub fn new(
cfg: &RingConfig,
max_timeout: Duration,
waker: Waker,
now: Instant,
) -> io::Result<Self> {
assert!(cfg.size > 0 && cfg.size.is_power_of_two() && cfg.size <= 32_768);
TimeoutWheel::validate_layout(max_timeout, cfg.timeout_wheel_tick)
.expect("validated timeout wheel configuration");
// Deferred task work runs during GETEVENTS, supplied by service or park.
let ring = IoUring::builder()
.setup_single_issuer()
.setup_defer_taskrun()
.build(cfg.size)?;
let size = cfg.size as usize;
Ok(Self {
ring,
state: State {
waiters: Waiters::new(size),
in_flight_limit: size,
ready_queue: VecDeque::with_capacity(size),
acquiring: FuturesUnordered::new(),
acquisition_waker: Arc::new(waker.clone()).into(),
pending_deadlines: VecDeque::with_capacity(size),
pending_cancels: VecDeque::with_capacity(size),
timeout_wheel: TimeoutWheel::new(max_timeout, cfg.timeout_wheel_tick, now),
waker,
wake_rearm_needed: true,
},
})
}
/// Accept an owned request without staging kernel work.
///
/// Cached time can reject expired requests immediately. Service validates
/// remaining deadlines after the wheel has advanced.
pub fn admit(
&mut self,
request: Request,
observer: Observer,
now: Instant,
deferred: &mut Deferred,
) -> WaiterId {
let deadline = request.deadline();
let durability = request.durability().cloned();
// Transfer ownership before any wait for staging capacity or durability.
let id = self.state.waiters.insert(request, observer);
if let Some(deadline) = deadline {
if deadline <= now {
self.state.complete(id, Err(Error::Timeout), deferred);
return id;
}
// A task poll may have taken long enough to leave the wheel behind.
// Service refreshes it before assigning this deadline a tick.
self.state.pending_deadlines.push_back(id);
}
// A durability request holds the open's permit through terminal accounting so a later
// barrier observes any earlier failure. Contended requests wait in `acquiring` outside
// the ready queue. They have no deadline and finish even if their caller is dropped.
if let Some(durability) = durability {
match durability.clone().try_lock_owned() {
Ok(permit) => self.state.acquired(id, permit, deferred),
Err(_) => {
// The native driver owns this wait, so no cooperative budget
// of a task polling the driver may delay it. Admission may
// follow the last service turn before parking, so poll now
// to register the foreign release's wake target.
self.state.acquiring.push(Box::pin(async move {
(
id,
tokio::task::unconstrained(durability.lock_owned()).await,
)
}));
self.state.poll_acquisitions(deferred);
}
}
} else {
self.state.ready_queue.push_back(id);
}
id
}
/// Number of active logical requests, excluding retained completed outputs.
pub const fn len(&self) -> usize {
self.state.waiters.len()
}
/// Whether all logical requests retired, excluding retained completed outputs.
pub const fn is_empty(&self) -> bool {
self.state.waiters.is_empty()
}
/// Whether actionable staging or deadline registration must run before parking.
pub fn has_pending_submissions(&self) -> bool {
(!self.state.ready_queue.is_empty()
&& self.state.waiters.in_flight() < self.state.in_flight_limit)
|| !self.state.pending_deadlines.is_empty()
|| !self.state.pending_cancels.is_empty()
}
/// Earliest active deadline registered on the operation wheel.
pub fn next_deadline(&self) -> Option<Instant> {
self.state.timeout_wheel.next_deadline()
}
/// Consume a retained result or check whether its waker needs refreshing.
pub fn observe(&mut self, id: WaiterId, waker: &TaskWaker) -> Observation<RequestOutput> {
self.state.waiters.observe(id, waker)
}
/// Install a waker cloned outside the worker borrow, returning the displaced one.
pub fn set_waker(&mut self, id: WaiterId, waker: TaskWaker) -> Option<TaskWaker> {
self.state.waiters.set_waker(id, waker)
}
/// Forward local observation while retaining all kernel resources locally.
pub fn forward(
&mut self,
id: WaiterId,
sender: oneshot::Sender<Result<RequestOutput, Error>>,
deferred: &mut Deferred,
) {
self.state.waiters.forward(id, sender, deferred);
}
/// Detach local or forwarded observation and request eligible cancellation.
pub fn orphan(&mut self, id: WaiterId, deferred: &mut Deferred) {
if self.state.waiters.orphan(id, deferred) {
self.state.cancel(id, deferred);
}
}
/// Clear local and forwarded observation before draining retained writes and syncs.
pub fn close(&mut self, deferred: &mut Deferred) {
for id in self.state.waiters.close(deferred) {
self.state.cancel(id, deferred);
}
}
/// Process completions, deadlines, and queued submissions without callbacks.
///
/// Returns whether this turn deferred GETEVENTS for unfinished requests. The
/// `defer_kernel_service` argument permits the following idle ring wait to
/// supply GETEVENTS. It is ignored when callbacks or unfinished staging
/// already require another busy turn.
pub fn service(
&mut self,
now: Instant,
defer_kernel_service: bool,
deferred: &mut Deferred,
) -> io::Result<bool> {
// Finish posted work before expiring requests, then register deadlines
// against the refreshed wheel so an idle interval cannot shorten them.
self.state.reap(&mut self.ring, deferred);
self.state.advance_timeouts(now, deferred);
self.state.register_deadlines(now, deferred);
self.state.poll_acquisitions(deferred);
self.state.compact_ready_queue();
while self.state.fill_submission_queue(&mut self.ring) {
let before = self.ring.submission().len();
Self::enter(&mut self.ring, Enter::Flush)?;
if self.ring.submission().len() >= before {
// A transient enter may leave the SQ full. Preserve queued
// identities and give the kernel a completion-service point
// before another staging attempt.
break;
}
}
// Busy turns must run deferred kernel work. An idle turn can leave this
// enter to park, unless callbacks or more submissions need attention.
let service_kernel = !self.is_empty()
&& (!defer_kernel_service || !deferred.is_empty() || self.has_pending_submissions());
if service_kernel {
Self::enter(&mut self.ring, Enter::TaskWork)?;
}
// New CQEs can complete requests or terminate the multishot wake poll.
// Park rechecks rearm before its next blocking enter.
self.state.reap(&mut self.ring, deferred);
#[cfg(test)]
tests::after_service(&self.ring, deferred)?;
Ok(!service_kernel && !self.is_empty())
}
/// Wait for ring activity or the deadline while retaining worker ownership.
///
/// Returns `false` when rearm needs another service turn or the publication
/// handshake rejects sleeping. A skipped wait does not satisfy a deferred
/// GETEVENTS requirement, so the worker must service nonblocking before its
/// next task-polling turn when kernel work remains.
pub fn park(&mut self, processed_seq: u32, deadline: Option<Instant>) -> io::Result<bool> {
if self.has_pending_submissions() {
return Ok(false);
}
// Rearm before publishing the intention to sleep. A transient flush can
// leave the SQ full, in which case service must run before trying again.
if self.state.wake_rearm_needed {
if !self.state.waker.reinstall(&mut self.ring.submission()) {
Self::enter(&mut self.ring, Enter::Flush)?;
if !self.state.waker.reinstall(&mut self.ring.submission()) {
return Ok(false);
}
}
self.state.wake_rearm_needed = false;
}
// Keep the arm guard alive through the wait. A publication racing this
// check either prevents sleeping or signals the installed wake poll.
let arm = self.state.waker.arm(processed_seq);
if !arm.still_idle() {
return Ok(false);
}
Self::enter(&mut self.ring, Enter::Wait { deadline })?;
Ok(true)
}
/// Submit queued SQEs, then do what `mode` asks of the kernel.
///
/// The caller reaps the CQ afterwards. A timeout or a transient error
/// (`EINTR`, `EAGAIN`, `EBUSY`) returns `Ok` because the next service
/// turn retries, so `Ok` does not mean the kernel consumed the SQ or posted
/// a completion. Other errors are permanent, and once the kernel has
/// dropped a completion every enter fails with `EBADR`.
fn enter(ring: &mut IoUring, mut mode: Enter) -> io::Result<()> {
#[cfg(test)]
if matches!(mode, Enter::Flush) && tests::stall_flush(ring) {
return Ok(());
}
// Read the clock once, right before the syscall. A wait for one
// completion with a zero timeout arms an expired timer and sleeps until
// it fires whenever no completion is ready, so a deadline that already
// passed runs only the task work.
let mut timeout = Duration::ZERO;
if let Enter::Wait {
deadline: Some(deadline),
} = mode
{
timeout = deadline.saturating_duration_since(Instant::now());
if timeout.is_zero() {
mode = Enter::TaskWork;
}
}
let result = match mode {
// The crate adds GETEVENTS when the CQ has overflowed, so the kernel
// also moves its overflow backlog into the CQ.
Enter::Flush => ring.submit(),
// GETEVENTS with no minimum completion count runs a batch of
// deferred task work and returns, as liburing's
// `io_uring_submit_and_get_events` does. Newer kernels bound the
// batch and leave the rest for a later enter.
Enter::TaskWork => {
let to_submit =
u32::try_from(ring.submission().len()).expect("SQ length must fit u32");
// SAFETY: `to_submit` counts only entries already published to
// the SQ, whose resources stay alive until their CQEs, and no
// argument pointer is passed.
unsafe {
ring.submitter().enter::<libc::sigset_t>(
to_submit,
0,
EnterFlags::GETEVENTS.bits(),
None,
)
}
}
Enter::Wait { deadline: None } => ring.submit_and_wait(1),
Enter::Wait { deadline: Some(_) } => {
// EXT_ARG bounds the wait without reserving an SQE for a timeout.
let ts = Timespec::new()
.sec(timeout.as_secs())
.nsec(timeout.subsec_nanos());
let args = SubmitArgs::new().timespec(&ts);
match ring.submitter().submit_with_args(1, &args) {
// This timeout bounds the enter. Request expiry belongs to
// the wheel.
Err(err) if err.raw_os_error() == Some(libc::ETIME) => Ok(0),
result => result,
}
}
};
// A transient error falls through and returns `Ok`, so the caller
// reaps before retrying. An immediate retry here could spin while the
// kernel needs CQ space to make further progress.
if let Err(err) = result
&& !matches!(
err.raw_os_error(),
Some(libc::EINTR | libc::EAGAIN | libc::EBUSY)
)
{
return Err(err);
}
// The kernel reports a dropped completion only once, and only from an
// enter that submitted no SQEs and then waited with no completion
// ready. An enter that submits discards the wait's result, and a flush,
// a task-work enter or a wait that finds completions queued never
// reaches the check. The ring's overflow counter grows only when the
// kernel drops a CQE and never decreases, so check it after every
// enter. Every later enter fails too, since the driver cannot tell
// which request lost its completion and a drain would otherwise wait
// for it forever.
let dropped = ring.completion().overflow() != 0;
#[cfg(test)]
let dropped = dropped || tests::drop_completion(ring);
if dropped {
return Err(io::Error::from_raw_os_error(libc::EBADR));
}
Ok(())
}
}
impl State {
/// Make a granted request eligible only after checking the open's retained outcome.
fn acquired(
&mut self,
id: WaiterId,
permit: OwnedAsyncMutexGuard<()>,
deferred: &mut Deferred,
) {
if let Some(result) = self.waiters.acquire_durability(id, permit) {
self.complete(id, result, deferred);
} else {
self.ready_queue.push_back(id);
}
}
/// Advance only notified permit waits, using a wake path that survives worker closure.
fn poll_acquisitions(&mut self, deferred: &mut Deferred) {
while let Poll::Ready(Some((id, permit))) = Pin::new(&mut self.acquiring)
.poll_next(&mut Context::from_waker(&self.acquisition_waker))
{
self.acquired(id, permit, deferred);
}
}
/// Store the terminal result and defer resource destruction and callbacks.
fn complete(&mut self, id: WaiterId, result: Result<(), Error>, deferred: &mut Deferred) {
// Finish splits the request into its result and deferred resources.
// A previously cancelled request has already released its wheel tick.
if let Some(tick) = self.waiters.finish(id, result, deferred) {
self.timeout_wheel.remove(tick);
}
}
/// Request cancellation once, finishing locally if no SQE is in flight.
fn cancel(&mut self, id: WaiterId, deferred: &mut Deferred) {
// Cancellation clears the waiter's tick. Save it before the transition
// so the wheel loses its active count exactly once.
let tick = self.waiters.target_tick(id);
if !self.waiters.cancel(id) {
return;
}
if let Some(tick) = tick {
self.timeout_wheel.remove(tick);
}
if self.waiters.is_in_flight(id) {
self.pending_cancels.push_back(id);
} else {
// No kernel access remains, including between partial completions.
self.complete(id, Err(Error::Timeout), deferred);
}
}
/// Advance the wheel even when no active operation deadline remains.
fn advance_timeouts(&mut self, now: Instant, deferred: &mut Deferred) {
let Some(expired) = self.timeout_wheel.advance(now) else {
return;
};
for id in expired {
self.cancel(id, deferred);
}
}
/// Register deadlines for new requests against this turn's refreshed wheel.
fn register_deadlines(&mut self, now: Instant, deferred: &mut Deferred) {
while let Some(id) = self.pending_deadlines.pop_front() {
// Cancellation may have retired the request before registration.
let Some(deadline) = self.waiters.deadline(id) else {
continue;
};
match self.timeout_wheel.checked_target_tick(deadline, now) {
Ok(Some(tick)) => {
// Remember the same tick in the waiter so completion or
// cancellation can remove its active count from the wheel.
self.timeout_wheel.schedule(id, tick);
self.waiters.set_deadline(id, tick);
}
// Time spent waiting for service can exhaust the deadline.
// Complete before staging, leaving the ready ID for lazy removal.
Ok(None) => self.complete(id, Err(Error::Timeout), deferred),
Err(message) => {
// An unsupported deadline fails through the request result
// path before the kernel can reference this request.
let error =
Error::Io(io::Error::new(io::ErrorKind::InvalidInput, message).into());
self.complete(id, Err(error), deferred);
}
}
}
}
/// Remove stale queue IDs once they exceed both 64 and the live queued count.
fn compact_ready_queue(&mut self) {
// Requests awaiting a permit have no ready ID. Every other pending
// request without an in-flight SQE has exactly one. Extra IDs are stale.
let queued = self.waiters.len() - self.waiters.in_flight() - self.acquiring.len();
let stale = self
.ready_queue
.len()
.checked_sub(queued)
.expect("queued request missing from ready queue");
if stale <= 64 || stale <= queued {
return;
}
// Queued cancellations and expirations leave IDs behind even when no
// staging capacity opens. Compact in place without disturbing FIFO order.
self.ready_queue.retain(|id| self.waiters.is_pending(*id));
}
/// Stage operation SQEs in FIFO order, returning whether an SQ flush is needed.
fn stage_ready_requests(&mut self, submission_queue: &mut SubmissionQueue<'_>) -> bool {
while self.waiters.in_flight() < self.in_flight_limit {
let Some(id) = self.ready_queue.front().copied() else {
return false;
};
// Cancelled and expired requests leave their IDs behind. Drop them
// without consuming SQ capacity or reporting a flush.
if !self.waiters.is_pending(id) {
self.ready_queue.pop_front();
continue;
}
if submission_queue.is_full() {
return true;
}
self.ready_queue.pop_front();
let sqe = self.waiters.stage(id);
// SAFETY: The waiter retains every referenced descriptor and buffer
// until its operation CQE. The SQ has capacity, and no callback can
// retire the waiter between constructing and pushing this SQE.
unsafe {
submission_queue
.push(&sqe)
.expect("checked operation SQ capacity");
}
}
// Only a completion can release this limit. A flush cannot, and
// cancellation and mailbox wake SQEs must still be allowed to stage.
false
}
/// Stage cancellation SQEs, returning whether an SQ flush is needed.
fn stage_cancellations(&mut self, submission_queue: &mut SubmissionQueue<'_>) -> bool {
while !submission_queue.is_full() {
let Some(id) = self.pending_cancels.pop_front() else {
return false;
};
// The operation may have finished before its cancellation staged.
if !self.waiters.is_in_flight(id) {
continue;
}
let cancel = AsyncCancel::new(id.user_data())
.build()
.user_data(CANCEL_USER_DATA);
// SAFETY: AsyncCancel carries only the stable user_data identity.
// Its target's waiter retains every kernel-visible owner, and the
// loop checked SQ capacity before taking this queued identity.
unsafe {
submission_queue
.push(&cancel)
.expect("checked cancellation SQ capacity");
}
}
!self.pending_cancels.is_empty()
}
/// Fill available SQ slots and report whether more work requires a flush.
fn fill_submission_queue(&mut self, ring: &mut IoUring) -> bool {
// One SQ view batches tail publication until it is dropped on return.
let mut submission_queue = ring.submission();
// Cancellation must progress even when all operation slots are occupied.
if self.stage_cancellations(&mut submission_queue) {
return true;
}
if self.stage_ready_requests(&mut submission_queue) {
return true;
}
// Reaching the operation limit still leaves wake polling eligible.
// If the SQ itself is full, retain the rearm flag across the flush.
if self.wake_rearm_needed {
if !self.waker.reinstall(&mut submission_queue) {
return true;
}
self.wake_rearm_needed = false;
}
false
}
/// Reap every posted CQE without invoking observer callbacks.
fn reap(&mut self, ring: &mut IoUring, deferred: &mut Deferred) {
// Dropping this CQ view returns the consumed slots to the kernel.
// Observer callbacks remain deferred until the worker borrow ends.
for cqe in ring.completion() {
self.handle_cqe(cqe.user_data(), cqe.result(), cqe.flags(), deferred);
}
}
/// Apply a CQE without invoking observer callbacks.
fn handle_cqe(
&mut self,
user_data: UserData,
result: i32,
flags: u32,
deferred: &mut Deferred,
) {
// The reserved mailbox token is outside the waiter's ID space.
if user_data == WAKE_USER_DATA {
assert!(
result >= 0,
"wake poll CQE failed: requires Linux 6.1+ multishot polling"
);
// Clear eventfd readiness. The worker drains the actual messages
// after this CQ batch and the local borrow have ended.
self.waker.acknowledge();
if !io_uring::cqueue::more(flags) {
// The kernel removed the multishot poll. Reinstall it before
// the next blocking wait, even if this was service's final reap.
self.wake_rearm_needed = true;
}
return;
}
// Only the target operation's CQE can retire its resource owners.
if user_data == CANCEL_USER_DATA {
if result == 0 {
// Cancellation successful.
} else if result == -libc::EALREADY {
// Cancellation is no longer possible at this stage. The target
// operation CQE should follow shortly.
} else if result == -libc::ENOENT {
// The target may already have completed and vacated its slot.
} else if result == -libc::EINVAL {
panic!("async cancel SQE rejected by kernel: EINVAL");
} else {
warn!(result, "unexpected async cancel CQE result");
}
return;
}
// Waiters releases an operation's in-flight count before returning its outcome.
match self.waiters.on_completion(user_data, result) {
// Partial progress rejoins the tail behind already queued work.
CompletionOutcome::Requeue(id) => self.ready_queue.push_back(id),
CompletionOutcome::Complete(id, result) => self.complete(id, result, deferred),
}
}
}
#[cfg(test)]
pub mod tests {
use super::*;
use crate::{
IoBuf, IoBufMut, IoBufs,
iouring::{
mailbox::Mailbox,
request::{
Cache, DIRECT_ALIGN, IOVEC_BATCH_SIZE, ReadAtRequest, RecvRequest, RequestOutput,
SyncRequest, WriteAtRequest, WriteAtState,
},
waker::tests::wait_until_eventfd_armed,
},
storage::{hold::Hold, iouring::Shared},
};
use commonware_utils::channel::oneshot;
use std::{
cell::Cell,
fs::{self, OpenOptions},
io::Write,
os::{
fd::{AsRawFd, RawFd},
unix::{fs::OpenOptionsExt as _, net::UnixStream},
},
panic::{AssertUnwindSafe, catch_unwind},
path::PathBuf,
sync::Arc,
task::Wake,
thread,
};
/// Bound of a timed wait that times out. It leaves the deadline in the
/// future when `Driver::enter` samples the clock on a loaded runner, so
/// the wait takes the timed path rather than the elapsed one.
const TIMED_WAIT: Duration = Duration::from_millis(10);
/// Kernel outcomes that cannot be scheduled reliably with a real ring.
#[derive(Clone, Copy, PartialEq, Eq)]
enum Fault {
/// A transient flush leaves every SQ entry untouched.
StallFlush,
/// Service fails with retired resources awaiting deferred cleanup.
AfterCompletion,
/// The kernel drops a CQE because it cannot allocate an overflow entry.
DropCompletion,
}
thread_local! {
/// One failure scoped to the calling thread and the selected ring.
static FAULT: Cell<Option<(RawFd, Fault)>> = const { Cell::new(None) };
}
/// Clear an unused injection when its test scope exits or unwinds.
pub struct FaultGuard;
impl Drop for FaultGuard {
fn drop(&mut self) {
FAULT.set(None);
}
}
/// Install one failure without changing the driver's production state.
fn inject(driver: &Driver, fault: Fault) -> FaultGuard {
assert!(FAULT.get().is_none(), "driver fault already installed");
FAULT.set(Some((driver.ring.as_raw_fd(), fault)));
FaultGuard
}
/// Fail service after completion ownership has moved to deferred cleanup.
pub fn fail_after_completion(driver: &Driver) -> FaultGuard {
inject(driver, Fault::AfterCompletion)
}
/// Consume the selected fault once, leaving later cleanup turns unaffected.
fn take_fault(ring: &IoUring, fault: Fault) -> bool {
if FAULT.get() != Some((ring.as_raw_fd(), fault)) {
return false;
}
FAULT.set(None);
true
}
/// Model a transient flush before the kernel consumes any SQ entries.
pub fn stall_flush(ring: &IoUring) -> bool {
take_fault(ring, Fault::StallFlush)
}
/// Model a CQE the kernel dropped, which it counts in the ring's overflow
/// counter.
pub fn drop_completion(ring: &IoUring) -> bool {
take_fault(ring, Fault::DropCompletion)
}
/// Fail service when retired resources are awaiting deferred cleanup.
pub fn after_service(ring: &IoUring, deferred: &Deferred) -> io::Result<()> {
if !deferred.resources.is_empty() && take_fault(ring, Fault::AfterCompletion) {
return Err(io::Error::other(
"injected service failure after completion",
));
}
Ok(())
}
/// Destination of a result collected by the harness.
enum TestObserver {
/// Local result identified by a test-assigned tag.
Local(u64),
/// Detached sync awaiting explicit publication by the test.
DetachedSync(oneshot::Sender<Result<(), Error>>),
/// Discarded result from an orphaned request.
Orphaned,
}
/// Completed request and the observer that would receive its output.
struct Completed {
/// Destination retained for assertions or detached publication.
observer: TestObserver,
/// Request result removed from its waiter or deferred callbacks.
output: RequestOutput,
}
/// Distinct waker allocation used to identify a local completion.
struct Notify;
// Each observer needs a distinct identity for matching its deferred wake.
#[allow(clippy::manual_noop_waker)]
impl Wake for Notify {
fn wake(self: Arc<Self>) {}
}
/// Collect results while driving a real ring or injecting operation CQEs.
struct Harness {
/// Ring and request state under test.
driver: Driver,
/// Callbacks and resources detached by driver transitions.
deferred: Deferred,
/// Local observers matched by waker identity, with test-assigned tags.
tracked: Vec<(WaiterId, u64, TaskWaker)>,
/// Collected results retained for assertions.
completed: Vec<Completed>,
/// Initial wheel time, also used as the origin for simulated deadlines.
start: Instant,
}
impl Harness {
/// Create a real ring whose SQ size also bounds in-flight operations.
fn new(size: u32) -> Self {
let start = Instant::now();
Self {
driver: Driver::new(
&RingConfig {
size,
timeout_wheel_tick: Duration::from_millis(5),
},
Duration::from_secs(60),
Waker::new().unwrap(),
start,
)
.unwrap(),
deferred: Deferred::default(),
tracked: Vec::new(),
completed: Vec::new(),
start,
}
}
/// Queue a local request whose result `collect` will consume automatically.
fn admit(&mut self, request: Request, tag: u64) -> WaiterId {
let waker = TaskWaker::from(Arc::new(Notify));
let id = self.driver.admit(
request,
Observer::Local(Some(waker.clone())),
self.start,
&mut self.deferred,
);
self.tracked.push((id, tag, waker));
id
}
/// Stage a request without submitting its SQE, for simulated completions.
fn stage(&mut self, request: Request, tick: Option<u64>, tag: u64) -> WaiterId {
let waker = TaskWaker::from(Arc::new(Notify));
let id = self
.driver
.state
.waiters
.insert(request, Observer::Local(Some(waker.clone())));
if let Some(tick) = tick {
self.driver.state.timeout_wheel.schedule(id, tick);
self.driver.state.waiters.set_deadline(id, tick);
}
// Keep the fixture out of the ready queue and the kernel. Only
// simulated_completion may release this operation's in-flight count.
self.driver.state.waiters.stage(id);
self.tracked.push((id, tag, waker));
id
}
/// Collect deferred outputs and consume local results in wake order.
fn collect(&mut self) {
for output in self.deferred.outputs.drain(..) {
self.completed.push(Completed {
observer: TestObserver::Orphaned,
output,
});
}
for waker in self.deferred.wakes.drain(..) {
// Match by observer identity to preserve completion order even
// when earlier requests have already vacated and reused slots.
let index = self
.tracked
.iter()
.position(|(_, _, current)| current.will_wake(&waker))
.unwrap();
let (id, tag, _) = self.tracked.swap_remove(index);
let Observation::Ready(output) = self.driver.observe(id, &waker) else {
panic!("completion wake without retained output");
};
self.completed.push(Completed {
observer: TestObserver::Local(tag),
output,
});
waker.wake();
}
// Orphaned observers leave a waker to discard without consuming a result.
for waker in self.deferred.drops.drain(..) {
if let Some(index) = self
.tracked
.iter()
.position(|(_, _, current)| current.will_wake(&waker))
{
self.tracked.swap_remove(index);
}
}
self.deferred.resources.clear();
// Let tests delay detached publication until after the driver drains.
for (sender, output) in self.deferred.completions.drain(..) {
self.completed.push(Completed {
observer: TestObserver::DetachedSync(sender),
output: RequestOutput::Sync(output),
});
}
}
/// Drop a local or forwarded observer and collect any immediately discarded output.
fn orphan(&mut self, id: WaiterId) {
self.driver.orphan(id, &mut self.deferred);
self.collect();
}
/// Service at a chosen time and collect results.
fn service_at(&mut self, now: Instant, defer: bool) {
self.driver.service(now, defer, &mut self.deferred).unwrap();
self.collect();
}
/// Service using wall-clock time and run deferred kernel work immediately.
fn service(&mut self) {
self.service_at(Instant::now(), false);
}
/// Require a real wake CQE before any further submission can mask its absence.
fn reap_wake(&mut self) {
let cqe = self
.driver
.ring
.completion()
.next()
.expect("missing wake CQE");
assert_eq!(cqe.user_data(), WAKE_USER_DATA);
self.driver.state.handle_cqe(
cqe.user_data(),
cqe.result(),
cqe.flags(),
&mut self.deferred,
);
}
/// Drive until `count` results have been collected, failing if progress stalls.
fn until(&mut self, count: usize) {
let limit = Instant::now() + Duration::from_secs(10);
while self.completed.len() < count {
assert!(Instant::now() < limit, "driver completion stalled");
self.service();
thread::yield_now();
}
}
/// Notify observers of closure without trying to consume retained results.
fn close(&mut self) {
self.driver.close(&mut self.deferred);
for waker in self.deferred.wakes.drain(..) {
waker.wake();
}
self.tracked.clear();
self.collect();
}
/// Close local and forwarded observation and wait for request retirement.
fn drain(&mut self) {
self.close();
let limit = Instant::now() + Duration::from_secs(10);
while !self.driver.is_empty() || self.driver.has_pending_submissions() {
assert!(Instant::now() < limit, "driver retirement stalled");
self.service();
thread::yield_now();
}
}
/// Inject progress only for SQEs deliberately kept out of the real ring.
///
/// This models the completion status and byte count without writing data.
fn simulated_completion(&mut self, id: WaiterId, result: i32) {
match self
.driver
.state
.waiters
.on_completion(id.user_data(), result)
{
CompletionOutcome::Complete(id, result) => {
self.driver.state.complete(id, result, &mut self.deferred)
}
CompletionOutcome::Requeue(id) => self.driver.state.ready_queue.push_back(id),
}
self.collect();
}
}
/// Build an exact receive with initialized storage for payload assertions.
fn recv(fd: UnixStream, len: usize, deadline: Option<Instant>) -> Request {
Request::Recv(RecvRequest {
fd: Arc::new(fd.into()),
buf: IoBufMut::zeroed(len),
offset: 0,
len,
exact: true,
deadline,
})
}
/// Borrow the initialized prefix reported by a successful receive.
fn received(output: &Completed) -> &[u8] {
match &output.output {
RequestOutput::Recv(Ok((buf, len))) => &buf.as_ref()[..*len],
_ => panic!("expected successful recv"),
}
}
/// Create an isolated open for tests that control durability completion.
fn make_file(name: &str) -> (PathBuf, Arc<Shared>) {
let directory =
std::env::temp_dir().join(format!("commonware_driver_{name}_{}", std::process::id()));
let hold = Hold::acquire(&directory).unwrap();
let file = OpenOptions::new()
.read(true)
.write(true)
.create(true)
.truncate(true)
.open(directory.join("blob"))
.unwrap();
(directory, Shared::detached(file, hold))
}
#[test]
fn test_unconsumed_results_leave_staging_and_deadline_tracking() {
let mut harness = Harness::new(1);
// Occupy the only operation slot so stale IDs cannot drain by staging.
let (active, mut peer) = UnixStream::pair().unwrap();
harness.admit(recv(active, 1, None), 0);
harness.service();
assert_eq!(harness.driver.state.waiters.in_flight(), 1);
// Keep completed results in their slots while the sole operation slot
// stays occupied. Their queue IDs must still count as stale.
let mut results = Vec::new();
let deadline = harness.start + Duration::from_nanos(1);
for _ in 0..65 {
let (socket, _peer) = UnixStream::pair().unwrap();
results.push(harness.driver.admit(
recv(socket, 1, Some(deadline)),
Observer::Local(None),
harness.start,
&mut harness.deferred,
));
}
harness.service();
assert_eq!(harness.driver.len(), 1);
assert!(harness.driver.state.ready_queue.is_empty());
assert!(harness.driver.state.pending_deadlines.is_empty());
assert!(harness.driver.next_deadline().is_none());
assert!(!harness.driver.has_pending_submissions());
// Below the compaction threshold, staging itself must skip the ID
// even though its generation still matches a retained result.
let (socket, _peer) = UnixStream::pair().unwrap();
results.push(harness.driver.admit(
recv(socket, 1, Some(deadline)),
Observer::Local(None),
harness.start,
&mut harness.deferred,
));
harness.service();
assert_eq!(harness.driver.state.ready_queue.len(), 1);
// Releasing capacity lets staging discard the one remaining stale ID.
peer.write_all(b"x").unwrap();
harness.until(1);
harness.service();
assert!(harness.driver.is_empty());
assert!(!harness.driver.has_pending_submissions());
// Neither compaction nor skipped staging may consume the results.
for id in results {
assert!(matches!(
harness.driver.observe(id, TaskWaker::noop()),
Observation::Ready(RequestOutput::Recv(Err((_, Error::Timeout))))
));
}
harness.drain();
}
#[test]
fn test_cancelled_backlog_compacts_at_staging_limit() {
let mut harness = Harness::new(1);
let (active, mut active_peer) = UnixStream::pair().unwrap();
harness.admit(recv(active, 1, None), 0);
harness.service();
// Cancellation must bound stale storage even while a live request at
// the front prevents normal FIFO removal.
let mut peak_queued = 0;
for tag in 1..=2 {
let (queued, mut peer) = UnixStream::pair().unwrap();
peer.write_all(b"x").unwrap();
harness.admit(recv(queued, 1, None), tag);
// Stale IDs sit behind live requests. The second live request also
// reuses a slot still named by earlier cancelled queue entries.
for _ in 0..256 {
let (socket, _peer) = UnixStream::pair().unwrap();
let id = harness.admit(recv(socket, 1, None), 3);
harness.orphan(id);
harness.service();
harness.completed.clear();
peak_queued = peak_queued.max(harness.driver.state.ready_queue.len());
}
}
assert_eq!(harness.driver.len(), 3);
assert_eq!(harness.driver.state.waiters.in_flight(), 1);
assert!(!harness.driver.has_pending_submissions());
// Compaction must preserve both live identities and their FIFO order.
active_peer.write_all(b"x").unwrap();
harness.until(3);
for (tag, completed) in harness.completed.iter().enumerate() {
assert!(matches!(completed.observer, TestObserver::Local(id) if id == tag as u64));
assert_eq!(received(completed), b"x");
}
assert!(peak_queued <= 128, "retained {peak_queued} queue entries");
harness.drain();
}
#[test]
fn test_expired_backlog_compacts_at_staging_limit() {
let mut harness = Harness::new(1);
let (active, _active_peer) = UnixStream::pair().unwrap();
harness.admit(recv(active, 1, None), 0);
harness.service();
let (queued, _queued_peer) = UnixStream::pair().unwrap();
harness.admit(recv(queued, 1, None), 1);
let mut now = harness.start;
let mut peak_queued = 0;
for register_first in [false, true] {
for _ in 0..256 {
let deadline = now + Duration::from_millis(10);
let (socket, _peer) = UnixStream::pair().unwrap();
let id = harness.admit(recv(socket, 1, Some(deadline)), 2);
// Exercise expiry during initial registration and through the
// timeout wheel, while the active request prevents staging.
if register_first {
harness.service_at(now, false);
}
now = deadline;
harness.service_at(now, false);
assert!(!harness.driver.state.waiters.is_pending(id));
assert_eq!(harness.completed.len(), 1);
assert!(matches!(
harness.completed[0].output,
RequestOutput::Recv(Err((_, Error::Timeout)))
));
harness.completed.clear();
peak_queued = peak_queued.max(harness.driver.state.ready_queue.len());
}
}
assert_eq!(harness.driver.len(), 2);
assert_eq!(harness.driver.state.waiters.in_flight(), 1);
assert!(!harness.driver.has_pending_submissions());
assert!(peak_queued <= 128, "retained {peak_queued} queue entries");
harness.drain();
}
#[test]
fn test_staging_limit_parks_and_preserves_fifo_across_growth() {
let mut harness = Harness::new(1);
let (first, mut first_peer) = UnixStream::pair().unwrap();
let first = harness.admit(recv(first, 1, None), 0);
harness.service();
assert!(harness.driver.state.waiters.is_in_flight(first));
// Grow the waiter slab while the kernel still holds the first buffer.
for (tag, bytes) in [(1, b"b"), (2, b"c")] {
let (socket, mut peer) = UnixStream::pair().unwrap();
peer.write_all(bytes).unwrap();
harness.admit(recv(socket, 1, None), tag);
}
harness.service();
assert_eq!(harness.driver.len(), 3);
assert_eq!(harness.driver.state.waiters.in_flight(), 1);
assert!(!harness.driver.has_pending_submissions());
// The backlog waits for a CQE. It must not prevent entering the idle wait.
assert!(harness.driver.park(0, Some(Instant::now())).unwrap());
first_peer.write_all(b"a").unwrap();
harness.until(3);
for (tag, expected) in [b"a", b"b", b"c"].into_iter().enumerate() {
let completed = &harness.completed[tag];
assert!(matches!(completed.observer, TestObserver::Local(id) if id == tag as u64));
assert_eq!(received(completed), expected);
}
assert_eq!(harness.driver.state.waiters.in_flight(), 0);
harness.drain();
}
#[test]
fn test_queued_expiry_and_cancellation_progress_at_staging_limit() {
let mut harness = Harness::new(1);
let (first, _first_peer) = UnixStream::pair().unwrap();
let first = harness.admit(recv(first, 1, None), 0);
harness.service();
let (timed, _timed_peer) = UnixStream::pair().unwrap();
let timed = harness.admit(
recv(timed, 1, Some(harness.start + Duration::from_secs(1))),
1,
);
let (cancelled, _cancelled_peer) = UnixStream::pair().unwrap();
let cancelled = harness.admit(recv(cancelled, 1, None), 2);
harness.orphan(cancelled);
// Neither queued request needs an SQE to retire while the first is blocked.
harness.service_at(harness.start + Duration::from_secs(2), false);
assert!(!harness.driver.state.waiters.is_pending(cancelled));
assert!(!harness.driver.state.waiters.is_pending(timed));
assert_eq!(harness.completed.len(), 2);
assert!(matches!(
harness.completed[0].observer,
TestObserver::Orphaned
));
assert!(matches!(
harness.completed[1].output,
RequestOutput::Recv(Err((_, Error::Timeout)))
));
assert!(harness.driver.state.waiters.is_in_flight(first));
assert_eq!(harness.driver.state.waiters.in_flight(), 1);
assert!(!harness.driver.has_pending_submissions());
// Cancelling the active request must still stage its cancellation at the limit.
harness.drain();
assert_eq!(harness.driver.state.waiters.in_flight(), 0);
}
#[test]
fn test_unconsumed_result_releases_staging_capacity() {
let mut harness = Harness::new(1);
let (first, mut first_peer) = UnixStream::pair().unwrap();
first_peer.write_all(b"a").unwrap();
// Omit the observer waker so the harness leaves this result in Waiters.
let first = harness.driver.admit(
recv(first, 1, None),
Observer::Local(None),
harness.start,
&mut harness.deferred,
);
let (second, mut second_peer) = UnixStream::pair().unwrap();
second_peer.write_all(b"b").unwrap();
harness.admit(recv(second, 1, None), 1);
// The second request needs the sole operation slot released by the first.
harness.until(1);
assert!(harness.driver.is_empty());
assert_eq!(harness.driver.state.waiters.in_flight(), 0);
assert_eq!(received(&harness.completed[0]), b"b");
// Only now consume the first result, after the second has finished.
assert!(matches!(
harness.driver.observe(first, TaskWaker::noop()),
Observation::Ready(RequestOutput::Recv(Ok((_, 1))))
));
harness.drain();
}
#[test]
fn test_expired_registration_never_stages() {
let mut harness = Harness::new(1);
let (left, _right) = UnixStream::pair().unwrap();
let deadline = harness.start + Duration::from_nanos(1);
harness.admit(recv(left, 8, Some(deadline)), 0);
// A deadline equal to this turn's time has already expired.
harness.service_at(deadline, true);
assert!(matches!(
harness.completed[0].output,
RequestOutput::Recv(Err((_, Error::Timeout)))
));
assert!(harness.driver.is_empty());
assert!(harness.driver.next_deadline().is_none());
// Only the mailbox poll reached the SQ. The expired receive did not.
assert_eq!(harness.driver.ring.submission().len(), 1);
harness.drain();
}
#[test]
fn test_staging_discards_stale_ids_with_full_sq() {
for with_pending in [false, true] {
let mut harness = Harness::new(1);
// The mailbox poll fills the SQ without using an operation slot.
harness.service();
assert!(harness.driver.ring.submission().is_full());
let (socket, _peer) = UnixStream::pair().unwrap();
let deadline = harness.start + Duration::from_secs(10);
let stale = harness.admit(recv(socket, 1, Some(deadline)), 0);
harness.orphan(stale);
let pending = with_pending.then(|| {
let (socket, mut peer) = UnixStream::pair().unwrap();
peer.write_all(b"x").unwrap();
harness.admit(recv(socket, 1, None), 1)
});
if let Some(pending) = pending {
assert_eq!(stale.0.index, pending.0.index);
assert_ne!(stale, pending);
}
let flush = harness
.driver
.state
.stage_ready_requests(&mut harness.driver.ring.submission());
// Only a live request justifies a flush. A stale ID must disappear
// even if there is no SQ space and its slot has already been reused.
assert_eq!(flush, with_pending);
assert_eq!(
harness.driver.state.ready_queue.len(),
usize::from(with_pending)
);
assert_eq!(harness.driver.state.ready_queue.front().copied(), pending);
assert_eq!(harness.driver.state.waiters.in_flight(), 0);
assert!(harness.driver.ring.submission().is_full());
if with_pending {
harness.until(2);
assert_eq!(received(&harness.completed[1]), b"x");
}
harness.drain();
// The old deadline registration must also be skipped after reuse.
assert!(harness.driver.next_deadline().is_none());
assert!(harness.driver.state.pending_deadlines.is_empty());
}
}
#[test]
fn test_advance_timeouts_ignores_stale_entry_after_slot_reuse() {
let mut harness = Harness::new(1);
let early = harness.start + Duration::from_millis(5);
let late = harness.start + Duration::from_millis(15);
let (old, _old_peer) = UnixStream::pair().unwrap();
let old = harness.admit(recv(old, 1, Some(early)), 0);
let (survivor, _survivor_peer) = UnixStream::pair().unwrap();
let survivor = harness.admit(recv(survivor, 1, Some(early)), 1);
harness
.driver
.state
.register_deadlines(harness.start, &mut harness.deferred);
// Keep another live deadline in the old bucket. Otherwise the wheel
// can skip it entirely, without passing its stale record to the driver.
harness.orphan(old);
harness.completed.clear();
let (current, _current_peer) = UnixStream::pair().unwrap();
let current = harness.admit(recv(current, 1, Some(late)), 2);
assert_eq!(old.0.index, current.0.index);
assert_ne!(old, current);
harness
.driver
.state
.register_deadlines(harness.start, &mut harness.deferred);
// The shared bucket expires, but its stale ID must not cancel the
// replacement or remove the replacement's later deadline.
harness
.driver
.state
.advance_timeouts(early, &mut harness.deferred);
harness.collect();
assert!(!harness.driver.state.waiters.is_pending(survivor));
assert!(harness.driver.state.waiters.is_pending(current));
assert_eq!(harness.completed.len(), 1);
assert_eq!(harness.driver.next_deadline(), Some(late));
// The replacement must still expire at its own deadline.
harness
.driver
.state
.advance_timeouts(late, &mut harness.deferred);
harness.collect();
assert!(harness.driver.is_empty());
assert!(harness.driver.next_deadline().is_none());
assert_eq!(harness.completed.len(), 2);
for (tag, completed) in (1..=2).zip(&harness.completed) {
assert!(matches!(completed.observer, TestObserver::Local(id) if id == tag));
assert!(matches!(
completed.output,
RequestOutput::Recv(Err((_, Error::Timeout)))
));
}
harness.drain();
}
#[test]
fn test_deadline_registration_uses_refreshed_wheel() {
let mut harness = Harness::new(1);
let now = harness.start + Duration::from_secs(3600) + Duration::from_millis(1);
let deadline = now + Duration::from_secs(60);
let (left, _right) = UnixStream::pair().unwrap();
let id = harness.admit(recv(left, 1, Some(deadline)), 0);
// The wheel is an hour behind, but the deadline fits its horizon when
// measured from this service turn. Refresh before validating it.
harness.service_at(now, true);
assert!(harness.completed.is_empty());
let scheduled = harness.driver.next_deadline().unwrap();
assert!(scheduled >= deadline);
assert!(scheduled < deadline + Duration::from_millis(5));
harness.orphan(id);
harness.drain();
}
#[test]
fn test_unsupported_deadline_is_rejected_without_submission() {
let mut harness = Harness::new(1);
let (left, _right) = UnixStream::pair().unwrap();
// This future deadline exceeds the harness's 60-second horizon.
harness.admit(
recv(left, 1, Some(harness.start + Duration::from_secs(61))),
0,
);
harness.service_at(harness.start, true);
assert!(matches!(
harness.completed[0].output,
RequestOutput::Recv(Err((_, Error::Io(_))))
));
assert!(harness.driver.is_empty());
assert_eq!(harness.driver.ring.submission().len(), 1);
harness.drain();
}
#[test]
fn test_completion_wins_cancellation() {
let mut harness = Harness::new(1);
let (left, _right) = UnixStream::pair().unwrap();
let id = harness.stage(recv(left, 5, None), None, 0);
harness.driver.state.cancel(id, &mut harness.deferred);
// A cancellation request cannot undo a successful terminal CQE.
harness.simulated_completion(id, 5);
assert_eq!(received(&harness.completed[0]).len(), 5);
harness.drain();
}
#[test]
fn test_cancel_acknowledgements_leave_requests_and_results_untouched() {
let mut harness = Harness::new(1);
let (left, _right) = UnixStream::pair().unwrap();
let id = harness.stage(recv(left, 5, None), None, 0);
harness.driver.state.cancel(id, &mut harness.deferred);
// Acknowledgements cannot release the operation's kernel resources.
for result in [0, -libc::EALREADY, -libc::ENOENT, -libc::EPERM] {
harness
.driver
.state
.handle_cqe(CANCEL_USER_DATA, result, 0, &mut harness.deferred);
assert_eq!(harness.driver.state.waiters.in_flight(), 1);
assert!(harness.driver.state.waiters.is_pending(id));
}
// Complete without collecting the result, leaving it in its waiter slot.
let CompletionOutcome::Complete(current, result) = harness
.driver
.state
.waiters
.on_completion(id.user_data(), 5)
else {
panic!("expected terminal completion");
};
assert_eq!(current, id);
harness
.driver
.state
.complete(id, result, &mut harness.deferred);
for result in [0, -libc::ENOENT] {
harness
.driver
.state
.handle_cqe(CANCEL_USER_DATA, result, 0, &mut harness.deferred);
assert_eq!(harness.driver.state.waiters.in_flight(), 0);
}
harness.collect();
assert_eq!(harness.completed.len(), 1);
assert_eq!(received(&harness.completed[0]).len(), 5);
harness.drain();
}
#[test]
fn test_cancel_acknowledgement_invariants() {
// Both control tokens use the index that Waiters refuses to allocate.
assert_ne!(CANCEL_USER_DATA, WAKE_USER_DATA);
for token in [CANCEL_USER_DATA, WAKE_USER_DATA] {
assert_eq!(token as u32, u32::MAX);
}
let mut harness = Harness::new(1);
// EINVAL rejects the cancellation SQE itself, regardless of slot reuse.
assert!(
catch_unwind(AssertUnwindSafe(|| {
harness.driver.state.handle_cqe(
CANCEL_USER_DATA,
-libc::EINVAL,
0,
&mut harness.deferred,
);
}))
.is_err()
);
}
#[test]
fn test_request_retirement_releases_hold_before_cancel_acknowledgement() {
for consume_cancel in [false, true] {
for reuse in [false, true] {
let mut harness = Harness::new(1);
let directory = std::env::temp_dir().join(format!(
"commonware_driver_retirement_{}_{}_{}",
std::process::id(),
consume_cancel,
reuse,
));
let hold = Hold::acquire(&directory).unwrap();
let path = directory.join("read");
fs::write(&path, b"hello").unwrap();
let file = OpenOptions::new().read(true).open(path).unwrap();
let probe = OpenOptions::new()
.read(true)
.write(true)
.open(directory.join(".hold"))
.unwrap();
let id = harness.admit(
Request::ReadAt(ReadAtRequest::new(
Shared::detached(file, hold),
0,
IoBufMut::zeroed(5),
Cache::Enabled,
)),
0,
);
// Complete the real read in the kernel, retaining its CQE and
// userspace owner while a later cancellation enters the SQ.
assert!(
!harness
.driver
.state
.stage_ready_requests(&mut harness.driver.ring.submission())
);
harness.driver.ring.submit_and_wait(1).unwrap();
assert!(matches!(
probe.try_lock(),
Err(fs::TryLockError::WouldBlock)
));
harness.driver.orphan(id, &mut harness.deferred);
assert!(
!harness
.driver
.state
.stage_cancellations(&mut harness.driver.ring.submission())
);
assert_eq!(harness.driver.ring.submission().len(), 1);
// Only processing the target CQE permits resource retirement.
// The deferred owner still protects the directory until disposal.
harness
.driver
.state
.reap(&mut harness.driver.ring, &mut harness.deferred);
assert!(matches!(
probe.try_lock(),
Err(fs::TryLockError::WouldBlock)
));
harness.collect();
let RequestOutput::ReadAt(Ok(buf)) = &harness.completed[0].output else {
panic!("real read did not complete successfully");
};
assert_eq!(buf.as_ref(), b"hello");
probe.try_lock().unwrap();
probe.unlock().unwrap();
if consume_cancel {
// Leave the real cancellation acknowledgement unobserved.
harness.driver.ring.submit_and_wait(1).unwrap();
assert!(harness.driver.ring.submission().is_empty());
assert!(!harness.driver.ring.completion().is_empty());
} else {
assert_eq!(harness.driver.ring.submission().len(), 1);
}
if reuse {
let (left, mut right) = UnixStream::pair().unwrap();
right.write_all(b"x").unwrap();
let next = harness.admit(recv(left, 1, None), 1);
assert_eq!(next.0.index, id.0.index);
assert_ne!(next, id);
harness.until(2);
assert_eq!(received(&harness.completed[1]), b"x");
}
harness.drain();
drop(harness);
probe.try_lock().unwrap();
drop(probe);
fs::remove_dir_all(directory).unwrap();
}
}
}
#[test]
fn test_timeout_between_partial_completions() {
let mut harness = Harness::new(1);
let (left, _right) = UnixStream::pair().unwrap();
let id = harness.stage(recv(left, 8, None), Some(1), 0);
// Partial progress leaves the request queued, with no SQE in flight.
harness.simulated_completion(id, 4);
assert_eq!(harness.driver.state.ready_queue.len(), 1);
// Expiring between SQEs must finish locally, without an async cancel.
harness.driver.state.advance_timeouts(
harness.start + Duration::from_millis(5),
&mut harness.deferred,
);
assert!(harness.driver.state.pending_cancels.is_empty());
assert!(harness.driver.is_empty());
harness.collect();
assert!(matches!(
harness.completed[0].output,
RequestOutput::Recv(Err((_, Error::Timeout)))
));
harness.drain();
}
#[test]
fn test_orphaned_follow_up_cannot_stage_reused_slot() {
let mut harness = Harness::new(1);
let (left, _right) = UnixStream::pair().unwrap();
let id = harness.stage(recv(left, 8, None), None, 0);
harness.simulated_completion(id, 4);
harness.orphan(id);
// Reuse the slot while the abandoned follow-up ID is still queued.
let (left, mut right) = UnixStream::pair().unwrap();
right.write_all(b"fresh").unwrap();
let new = harness.admit(recv(left, 5, None), 1);
assert_eq!(new.0.index, id.0.index);
harness.until(2);
assert_eq!(received(&harness.completed[1]), b"fresh");
harness.drain();
}
#[test]
fn test_orphaned_read_retries_do_not_requeue() {
let directory = std::env::temp_dir().join(format!(
"commonware_driver_read_test_{}",
std::process::id()
));
let hold = Hold::acquire(&directory).unwrap();
let file = OpenOptions::new()
.read(true)
.write(true)
.create(true)
.truncate(true)
.open(directory.join("read"))
.unwrap();
let held = Shared::detached(file, hold);
for close in [false, true] {
for result in [-libc::EAGAIN, 2] {
let mut harness = Harness::new(1);
let id = harness.stage(
Request::ReadAt(ReadAtRequest::new(
held.clone(),
0,
IoBufMut::zeroed(5),
Cache::Enabled,
)),
None,
0,
);
if close {
harness.close();
} else {
harness.orphan(id);
}
// ReadAt reports a retry or partial progress even after
// cancellation. CancelRequested prevents a follow-up SQE
// and retires the request at this CQE.
harness.simulated_completion(id, result);
assert!(harness.driver.state.ready_queue.is_empty());
assert!(harness.driver.is_empty());
assert_eq!(harness.completed.len(), 1);
assert!(matches!(
harness.completed[0].observer,
TestObserver::Orphaned
));
harness.drain();
}
}
drop(held);
fs::remove_dir_all(directory).unwrap();
}
/// Cancelling a direct read never stages a successor. When its observer is dropped or its
/// worker closes, a read the kernel holds keeps its open and buffer until its operation CQE
/// decides its outcome, even when the cancellation acknowledgement arrives first. A read the
/// kernel does not hold releases them at once when its observer is dropped, whether it is
/// queued for its first submission or requeued after aligned progress or a switch to a
/// buffered read.
#[test]
fn test_cancelled_direct_reads_retain_their_owners_until_completion() {
let (directory, file) = make_file("direct_cancel");
let fd = file.set_direct(
OpenOptions::new()
.read(true)
.open(directory.join("blob"))
.unwrap(),
);
let probe = OpenOptions::new()
.write(true)
.open(directory.join(".hold"))
.unwrap();
// Each read covers three blocks less 200 bytes at offset 0, so its superset is three
// blocks. An output returns its request's buffer, so the buffer's address ties the two.
let len = 3 * DIRECT_ALIGN - 200;
let request = || {
let mut request = ReadAtRequest::direct(file.clone(), fd, 0, len).unwrap();
let target = request.buf.as_mut_ptr();
(Request::ReadAt(request), target)
};
let returned = |completed: &mut Completed| match &mut completed.output {
RequestOutput::ReadAt(Ok(buf) | Err((buf, _))) => buf.as_mut_ptr(),
_ => unreachable!("a read request must return a read output"),
};
for close in [false, true] {
for acknowledged in [false, true] {
for (result, outcome) in [
(-libc::EAGAIN, Err(Error::Timeout)),
(DIRECT_ALIGN as i32, Err(Error::Timeout)),
(10, Err(Error::Timeout)),
(0, Err(Error::Timeout)),
(-libc::EINVAL, Err(Error::Timeout)),
(-libc::EIO, Err(Error::Timeout)),
(-libc::ECANCELED, Err(Error::ReadFailed)),
(3 * DIRECT_ALIGN as i32, Ok(())),
] {
// Orphan a staged read or close its worker, with or without a simulated
// cancellation acknowledgement. The kernel may write the buffer until the
// operation CQE, so the request keeps its open and buffer.
let mut harness = Harness::new(1);
let (request, target) = request();
let id = harness.stage(request, None, 0);
if close {
harness.close();
} else {
harness.orphan(id);
}
if acknowledged {
harness.driver.state.handle_cqe(
CANCEL_USER_DATA,
0,
0,
&mut harness.deferred,
);
harness.collect();
}
assert_eq!(Arc::strong_count(&file), 2);
assert_eq!(harness.driver.state.waiters.in_flight(), 1);
assert!(harness.completed.is_empty());
// The simulated operation CQE ends the read without a successor. A covering
// completion succeeds, ECANCELED fails, and any other result times out,
// including one that would otherwise continue the read or fall back.
harness.simulated_completion(id, result);
assert!(harness.driver.is_empty());
assert!(harness.driver.state.ready_queue.is_empty());
assert_eq!(harness.driver.state.waiters.in_flight(), 0);
let RequestOutput::ReadAt(output) = &harness.completed[0].output else {
panic!("a read request must return a read output");
};
match (output, outcome) {
(Ok(buf), Ok(())) => assert_eq!(buf.len(), len),
(Err((_, Error::Timeout)), Err(Error::Timeout))
| (Err((_, Error::ReadFailed)), Err(Error::ReadFailed)) => {}
(output, outcome) => panic!("{result}: {output:?} must match {outcome:?}"),
}
// The CQE released the open, and the unobserved output owns the buffer until
// the worker disposes of it.
assert_eq!(Arc::strong_count(&file), 1);
assert_eq!(harness.completed.len(), 1);
assert_eq!(returned(&mut harness.completed[0]), target);
harness.completed.clear();
harness.drain();
}
}
}
// Cancel a read still queued for its first submission, or requeued after simulated
// aligned progress or a rejection that switched it to a buffered read. The kernel holds
// none of them, so cancelling completes the read at once with a timeout and releases its
// open and buffer, and the next service has nothing to submit.
for progress in [None, Some(DIRECT_ALIGN as i32), Some(-libc::EINVAL)] {
let mut harness = Harness::new(1);
let (request, target) = request();
let id = match progress {
None => harness.admit(request, 0),
Some(result) => {
let id = harness.stage(request, None, 0);
harness.simulated_completion(id, result);
assert_eq!(harness.driver.state.ready_queue.len(), 1);
id
}
};
assert_eq!(harness.driver.state.waiters.in_flight(), 0);
assert_eq!(Arc::strong_count(&file), 2);
harness.orphan(id);
assert!(harness.driver.is_empty());
assert_eq!(Arc::strong_count(&file), 1);
assert_eq!(harness.completed.len(), 1);
assert_eq!(returned(&mut harness.completed[0]), target);
assert!(matches!(
harness.completed[0].output,
RequestOutput::ReadAt(Err((_, Error::Timeout)))
));
harness.service();
assert!(harness.driver.state.ready_queue.is_empty());
assert_eq!(harness.driver.state.waiters.in_flight(), 0);
harness.completed.clear();
harness.drain();
}
// Only the open retains the directory hold, so dropping it releases the hold.
assert!(matches!(
probe.try_lock(),
Err(fs::TryLockError::WouldBlock)
));
drop(file);
probe.try_lock().unwrap();
drop(probe);
fs::remove_dir_all(directory).unwrap();
}
/// A direct read whose observer is forwarded before its rejection falls back within the same
/// request and delivers the buffered result through that observer.
#[test]
fn test_forwarded_direct_read_falls_back_through_its_observer() {
let (directory, file) = make_file("direct_forwarded");
let data: Vec<u8> = (0..2 * DIRECT_ALIGN).map(|i| (i % 251) as u8).collect();
fs::write(directory.join("blob"), &data).unwrap();
let fd = file.set_direct(
OpenOptions::new()
.read(true)
.open(directory.join("blob"))
.unwrap(),
);
let request =
Request::ReadAt(ReadAtRequest::direct(file.clone(), fd, 100, DIRECT_ALIGN).unwrap());
// Forward the observer of a staged read, then simulate a rejection of its direct read.
// The request switches to a buffered read and requeues, so it stays in the driver and the
// forwarded observer has no result yet.
let mut harness = Harness::new(1);
let id = harness.stage(request, None, 0);
let (sender, mut receiver) = oneshot::channel();
harness.driver.forward(id, sender, &mut harness.deferred);
harness.collect();
harness.simulated_completion(id, -libc::EINVAL);
assert_eq!(harness.driver.len(), 1);
assert_eq!(harness.driver.state.ready_queue.len(), 1);
// The kernel serves the buffered read through the open's file, and the request retires
// with one result for the forwarded observer.
let limit = Instant::now() + Duration::from_secs(10);
while !harness.driver.is_empty() {
assert!(Instant::now() < limit, "the buffered read must finish");
harness.service();
}
assert_eq!(harness.deferred.results.len(), 1);
// Publishing the result delivers the range's bytes to the forwarded observer.
for (sender, output) in harness.deferred.results.drain(..) {
let _ = sender.send(output);
}
let Ok(Ok(RequestOutput::ReadAt(Ok(buf)))) = receiver.try_recv() else {
panic!("the forwarded observer must receive a successful read");
};
assert_eq!(buf.as_ref(), &data[100..100 + DIRECT_ALIGN]);
harness.drain();
drop(file);
fs::remove_dir_all(directory).unwrap();
}
/// The kernel serves direct reads through a real `O_DIRECT` descriptor, for an unaligned
/// range and for a range ending at an unaligned end of file. The open's own file is empty, so
/// only a read served by the direct descriptor returns the data.
#[test]
fn test_direct_reads_are_served_by_the_direct_descriptor() {
let (directory, file) = make_file("direct_served");
let data: Vec<u8> = (0..3 * DIRECT_ALIGN - 50)
.map(|i| (i % 251) as u8)
.collect();
fs::write(directory.join("data"), &data).unwrap();
let fd = file.set_direct(
OpenOptions::new()
.read(true)
.custom_flags(libc::O_DIRECT)
.open(directory.join("data"))
.expect("temporary directory must support O_DIRECT"),
);
// The direct read of the first range fills its whole superset [0, 8192). The second range
// ends at the end of the file, inside the last superset, so its direct read completes
// short and unaligned.
let mut harness = Harness::new(1);
for (offset, len) in [(100, 5000), (2 * DIRECT_ALIGN + 10, DIRECT_ALIGN - 60)] {
let request = ReadAtRequest::direct(file.clone(), fd, offset as u64, len).unwrap();
harness.admit(Request::ReadAt(request), 0);
harness.until(1);
let RequestOutput::ReadAt(Ok(buf)) = harness.completed.remove(0).output else {
panic!("the direct read of {offset}..{} must succeed", offset + len);
};
assert_eq!(buf.as_ref(), &data[offset..offset + len]);
}
harness.drain();
drop(file);
fs::remove_dir_all(directory).unwrap();
}
#[test]
fn test_orphan_in_flight_cancels_once_and_releases_deadline() {
for expire_bucket in [false, true] {
let mut harness = Harness::new(1);
let deadline = harness.start + Duration::from_secs(10);
let (left, _right) = UnixStream::pair().unwrap();
let id = harness.admit(recv(left, 8, Some(deadline)), 0);
let (survivor, _survivor_peer) = UnixStream::pair().unwrap();
if expire_bucket {
harness.admit(recv(survivor, 1, Some(deadline)), 1);
}
harness.service();
assert!(harness.driver.state.waiters.is_in_flight(id));
// Repeated observer drops must not queue another cancel or remove the
// same deadline from the wheel twice.
harness.orphan(id);
harness.orphan(id);
if expire_bucket {
// A live sibling forces the orphan's stale wheel record through
// expiry before its cancellation or operation CQE arrives.
harness
.driver
.state
.advance_timeouts(deadline, &mut harness.deferred);
assert!(harness.driver.state.waiters.is_in_flight(id));
}
assert_eq!(harness.driver.state.pending_cancels.len(), 1);
assert!(harness.driver.next_deadline().is_none());
harness.collect();
harness.drain();
assert_eq!(harness.completed.len(), 1 + usize::from(expire_bucket));
assert_eq!(
harness
.completed
.iter()
.filter(|completed| matches!(completed.observer, TestObserver::Orphaned))
.count(),
1
);
if expire_bucket {
let survivor = harness
.completed
.iter()
.find(|completed| matches!(completed.observer, TestObserver::Local(1)))
.unwrap();
assert!(matches!(
survivor.output,
RequestOutput::Recv(Err((_, Error::Timeout)))
));
}
}
}
#[test]
fn test_expiring_bucket_preserves_unconsumed_success() {
let mut harness = Harness::new(1);
let deadline = harness.start + Duration::from_secs(30);
let (socket, mut peer) = UnixStream::pair().unwrap();
peer.write_all(b"x").unwrap();
let completed = harness.admit(recv(socket, 1, Some(deadline)), 0);
let (survivor, _peer) = UnixStream::pair().unwrap();
harness.admit(recv(survivor, 1, Some(deadline)), 1);
// Retain the first result while another request keeps its bucket live.
let limit = Instant::now() + Duration::from_secs(10);
while harness.driver.state.waiters.is_pending(completed) {
assert!(Instant::now() < limit, "receive stalled");
harness
.driver
.service(Instant::now(), false, &mut harness.deferred)
.unwrap();
thread::yield_now();
}
harness
.driver
.state
.advance_timeouts(deadline, &mut harness.deferred);
assert!(harness.driver.next_deadline().is_none());
harness.collect();
assert!(matches!(
harness.completed[0].observer,
TestObserver::Local(0)
));
assert_eq!(received(&harness.completed[0]), b"x");
harness.drain();
assert_eq!(harness.completed.len(), 2);
assert!(matches!(
harness.completed[1].observer,
TestObserver::Local(1)
));
assert!(matches!(
harness.completed[1].output,
RequestOutput::Recv(Err((_, Error::Timeout)))
));
}
#[test]
fn test_stale_waiter_identity_cannot_orphan_reused_slot() {
let mut harness = Harness::new(1);
let (old, _old_peer) = UnixStream::pair().unwrap();
let old = harness.admit(recv(old, 1, None), 0);
harness.orphan(old);
harness.completed.clear();
let (socket, mut peer) = UnixStream::pair().unwrap();
let current = harness.admit(recv(socket, 1, None), 1);
assert_eq!(old.0.index, current.0.index);
assert_ne!(old, current);
// A delayed drop from the old observer cannot affect its replacement.
harness.orphan(old);
assert_eq!(harness.driver.len(), 1);
assert!(harness.completed.is_empty());
peer.write_all(b"x").unwrap();
harness.until(1);
assert!(matches!(
harness.completed[0].observer,
TestObserver::Local(1)
));
harness.drain();
}
#[test]
fn test_deferred_taskrun_is_serviced_during_busy_turn() {
// Both SQEs fit, so staging does not itself force a submission syscall.
let mut harness = Harness::new(2);
let (left, mut right) = UnixStream::pair().unwrap();
harness.admit(recv(left, 1, None), 0);
harness.service_at(harness.start, true);
assert!(harness.completed.is_empty());
assert!(!harness.driver.is_empty());
assert_eq!(harness.driver.ring.submission().len(), 2);
// The next busy turn submits the receive, which then waits for data.
harness.service();
assert_eq!(harness.driver.ring.submission().len(), 0);
assert!(harness.completed.is_empty());
// The data's completion waits in deferred task work. Only busy turns
// run from here, so their enter alone must post it.
right.write_all(b"x").unwrap();
harness.until(1);
assert_eq!(received(&harness.completed[0]), b"x");
harness.drain();
}
#[test]
fn test_wake_poll_alone_does_not_require_kernel_service() {
let mut harness = Harness::new(1);
// Staging the mailbox poll must not make a CPU-only turn enter the kernel.
harness.service();
assert!(harness.driver.is_empty());
assert!(!harness.driver.has_pending_submissions());
assert_eq!(harness.driver.ring.submission().len(), 1);
harness.drain();
}
#[test]
fn test_skipped_park_keeps_deferred_service_obligation() {
let mut harness = Harness::new(2);
let (left, mut right) = UnixStream::pair().unwrap();
right.write_all(b"x").unwrap();
harness.admit(recv(left, 1, None), 0);
harness.service_at(harness.start, true);
// Publication prevents parking, so the deferred GETEVENTS enter must
// still happen through service before the next busy turn.
if harness.driver.state.waker.publish() {
harness.driver.state.waker.wake();
}
assert!(!harness.driver.park(0, None).unwrap());
assert!(!harness.driver.is_empty());
harness.until(1);
harness.drain();
}
#[test]
fn test_publish_wakes_eventfd_blocked_loop() {
let mut harness = Harness::new(2);
let (left, _right) = UnixStream::pair().unwrap();
harness.admit(recv(left, 1, None), 0);
harness.service();
// Publish after arming so the eventfd path must carry the notification.
let deadline = Instant::now() + Duration::from_secs(10);
let waker = harness.driver.state.waker.clone();
let producer = thread::spawn(move || {
wait_until_eventfd_armed(&waker, deadline);
if waker.publish() {
waker.wake();
}
});
assert!(harness.driver.park(0, Some(deadline)).unwrap());
producer.join().unwrap();
assert!(harness.driver.state.waker.pending(0));
// A published sequence alone does not prove eventfd reached the ring.
harness.reap_wake();
harness.drain();
}
#[test]
fn test_park_rearms_wake_poll_with_full_sq() {
for stalled in [false, true] {
let mut harness = Harness::new(1);
let (socket, mut peer) = UnixStream::pair().unwrap();
let id = harness.admit(recv(socket, 1, None), 0);
// The receive fills the SQ before wake polling can be installed.
// Keep the SQE unsubmitted so park must flush before rearming.
assert!(
harness
.driver
.state
.fill_submission_queue(&mut harness.driver.ring)
);
assert!(harness.driver.ring.submission().is_full());
assert!(harness.driver.state.waiters.is_in_flight(id));
assert!(harness.driver.state.wake_rearm_needed);
let _fault = stalled.then(|| inject(&harness.driver, Fault::StallFlush));
if stalled {
// An unchanged SQ cannot accept the wake poll. Park must return
// to service with the request and rearm still pending.
assert!(!harness.driver.park(0, Some(Instant::now())).unwrap());
assert!(harness.driver.state.wake_rearm_needed);
assert!(!harness.driver.is_empty());
assert!(harness.driver.ring.submission().is_full());
harness.service();
assert!(!harness.driver.state.wake_rearm_needed);
}
// Keep the receive blocked. Only a wake published after arming can
// produce a CQE during this bounded wait.
let deadline = Instant::now() + Duration::from_secs(10);
let waker = harness.driver.state.waker.clone();
let producer = thread::spawn(move || {
wait_until_eventfd_armed(&waker, deadline);
if waker.publish() {
waker.wake();
}
});
assert!(harness.driver.park(0, Some(deadline)).unwrap());
producer.join().unwrap();
assert!(harness.driver.state.waker.pending(0));
assert!(!harness.driver.state.wake_rearm_needed);
// Reap directly so service cannot hide a missing poll by installing
// one after park returns. The receive must still be in flight.
harness.reap_wake();
assert!(harness.driver.state.waiters.is_in_flight(id));
// Rearming must preserve the receive that occupied the SQ.
peer.write_all(b"x").unwrap();
harness.until(1);
assert_eq!(received(&harness.completed[0]), b"x");
harness.drain();
}
}
#[test]
fn test_transient_flush_without_sq_progress_returns_through_service() {
let mut harness = Harness::new(1);
harness.service();
assert_eq!(harness.driver.ring.submission().len(), 1);
// Stall the flush needed to get past the already staged mailbox poll.
let _fault = inject(&harness.driver, Fault::StallFlush);
let (left, mut right) = UnixStream::pair().unwrap();
right.write_all(b"x").unwrap();
let id = harness.admit(recv(left, 1, None), 0);
harness.service();
// The failed flush must not consume or mark the queued operation as
// in-flight. Its next attempt follows one GETEVENTS service point.
assert!(!harness.driver.state.waiters.is_in_flight(id));
assert_eq!(harness.driver.state.ready_queue.front(), Some(&id));
assert!(FAULT.get().is_none());
harness.until(1);
assert_eq!(received(&harness.completed[0]), b"x");
harness.drain();
}
#[test]
fn test_partial_completion_releases_capacity_to_next_queued_request() {
let mut harness = Harness::new(1);
let (first, mut first_peer) = UnixStream::pair().unwrap();
first_peer.write_all(b"a").unwrap();
harness.admit(recv(first, 2, None), 0);
let (second, mut second_peer) = UnixStream::pair().unwrap();
second_peer.write_all(b"x").unwrap();
harness.admit(recv(second, 1, None), 1);
// The first request needs another SQE. Its partial CQE releases the
// sole slot so the older queued SQE can run before that follow-up.
harness.until(1);
assert!(matches!(
harness.completed[0].observer,
TestObserver::Local(1)
));
assert_eq!(received(&harness.completed[0]), b"x");
assert!(harness.driver.state.waiters.in_flight() <= 1);
// The retry must append to the original buffer and finish second.
first_peer.write_all(b"b").unwrap();
harness.until(2);
assert!(matches!(
harness.completed[1].observer,
TestObserver::Local(0)
));
assert_eq!(received(&harness.completed[1]), b"ab");
assert_eq!(harness.driver.state.waiters.in_flight(), 0);
harness.drain();
}
#[test]
fn test_same_open_sync_waits_for_failure_accounting() {
for state in [
None,
Some(WriteAtState::WritingSync),
Some(WriteAtState::WritingBeforeSync),
] {
let (directory, held) = make_file("sync_order");
held.tracker.write();
held.tracker.complete();
// Both barriers are accepted while staging capacity remains available.
let mut harness = Harness::new(2);
let first = harness.admit(Request::Sync(SyncRequest::new(held.clone())), 0);
let successor = state.map_or_else(
|| Request::Sync(SyncRequest::new(held.clone())),
|state| {
let bufs = if state == WriteAtState::WritingBeforeSync {
(0..IOVEC_BATCH_SIZE + 1)
.map(|_| IoBuf::from(b"x"))
.collect()
} else {
IoBufs::from(IoBuf::from(b"new"))
};
Request::WriteAt(WriteAtRequest::new(
held.clone(),
0,
bufs.into(),
state,
Cache::Enabled,
))
},
);
let second = harness.admit(successor, 1);
assert_eq!(harness.driver.len(), 2);
assert_eq!(harness.driver.state.ready_queue.pop_front(), Some(first));
assert!(
!harness.driver.state.ready_queue.contains(&second),
"durability request became ready before its predecessor was accounted"
);
// Keep the SQE out of the ring so its failure is supplied only by the
// synthetic CQE. This models terminal ordering without a disk fault.
harness.driver.state.waiters.stage(first);
assert_eq!(harness.driver.state.waiters.in_flight(), 1);
assert!(!harness.driver.has_pending_submissions());
harness.driver.state.handle_cqe(
first.user_data(),
-libc::EIO,
0,
&mut harness.deferred,
);
assert!(matches!(
held.tracker.failure(),
Some(Error::BlobSyncFailed(_, _, error))
if error.raw_os_error() == Some(libc::EIO)
));
// The already accepted successor observes the retained failure without
// staging a second kernel barrier or consuming the first result.
harness
.driver
.service(Instant::now(), true, &mut harness.deferred)
.unwrap();
assert!(harness.driver.is_empty());
assert_eq!(harness.driver.state.waiters.in_flight(), 0);
harness.collect();
assert_eq!(harness.completed.len(), 2);
assert!(matches!(
&harness.completed[0].output,
RequestOutput::Sync(Err(Error::Io(error)))
if error.raw_os_error() == Some(libc::EIO)
));
assert!(matches!(
&harness.completed[1].output,
RequestOutput::Sync(Err(Error::BlobSyncFailed(_, _, error)))
| RequestOutput::WriteAt(Err(Error::BlobSyncFailed(_, _, error)))
if error.raw_os_error() == Some(libc::EIO)
));
harness.drain();
drop(harness);
drop(held);
fs::remove_dir_all(directory).unwrap();
}
}
#[test]
fn test_durable_write_keeps_permit_through_follow_up_sqes() {
for state in [WriteAtState::WritingSync, WriteAtState::WritingBeforeSync] {
let multibatch = state == WriteAtState::WritingBeforeSync;
let (directory, file) = make_file("durable_follow_up");
file.tracker.write();
file.tracker.complete();
let mut harness = Harness::new(2);
let (bufs, completions) = if !multibatch {
(
IoBufs::from(IoBuf::from(b"hello")),
vec![-libc::EOPNOTSUPP, -libc::EINTR, 2, 3],
)
} else {
(
(0..IOVEC_BATCH_SIZE + 1)
.map(|_| IoBuf::from(b"x"))
.collect(),
vec![
-libc::EOPNOTSUPP,
-libc::EAGAIN,
IOVEC_BATCH_SIZE as i32,
1,
0,
],
)
};
let first = harness.driver.admit(
Request::WriteAt(WriteAtRequest::new(
file.clone(),
0,
bufs.into(),
state,
Cache::Disabled,
)),
Observer::Local(None),
harness.start,
&mut harness.deferred,
);
let second = harness.admit(Request::Sync(SyncRequest::new(file.clone())), 1);
// Orphaned work and unconsumed local outputs have the same permit lifetime.
if multibatch {
harness.driver.orphan(first, &mut harness.deferred);
}
for (index, result) in completions.iter().enumerate() {
assert_eq!(harness.driver.state.ready_queue.pop_front(), Some(first));
assert!(harness.driver.state.ready_queue.is_empty());
harness.driver.state.waiters.stage(first);
harness.driver.state.handle_cqe(
first.user_data(),
*result,
0,
&mut harness.deferred,
);
harness
.driver
.state
.poll_acquisitions(&mut harness.deferred);
if index + 1 < completions.len() {
assert_eq!(harness.driver.state.acquiring.len(), 1);
assert!(!harness.driver.state.ready_queue.contains(&second));
assert!(file.durability.try_lock().is_err());
}
}
assert!(harness.driver.state.acquiring.is_empty());
assert!(!harness.driver.state.waiters.is_pending(first));
// A trailing sync credits the write completed before it under the permit, so the
// successor resolves without an SQE. A fused write covers only its own range.
if multibatch {
assert!(harness.driver.state.ready_queue.is_empty());
assert!(!harness.driver.state.waiters.is_pending(second));
harness.collect();
} else {
assert_eq!(harness.driver.state.ready_queue.pop_front(), Some(second));
assert!(matches!(
harness.driver.observe(first, TaskWaker::noop()),
Observation::Ready(RequestOutput::WriteAt(Ok(())))
));
harness.driver.state.waiters.stage(second);
harness.simulated_completion(second, 0);
}
assert_eq!(harness.completed.len(), 1 + usize::from(multibatch));
assert!(harness.completed.iter().all(|completion| matches!(
completion.output,
RequestOutput::WriteAt(Ok(())) | RequestOutput::Sync(Ok(()))
)));
assert!(!file.tracker.is_dirty());
harness.drain();
drop(harness);
drop(file);
fs::remove_dir_all(directory).unwrap();
}
}
#[test]
fn test_blocked_durability_does_not_count_as_ready_during_compaction() {
let (directory, file) = make_file("blocked_compaction");
file.tracker.write();
file.tracker.complete();
let permit = file.durability.clone().try_lock_owned().unwrap();
let mut harness = Harness::new(1);
let (active, mut active_peer) = UnixStream::pair().unwrap();
harness.admit(recv(active, 1, None), 0);
harness.service();
let (independent, mut independent_peer) = UnixStream::pair().unwrap();
independent_peer.write_all(b"x").unwrap();
let independent = harness.admit(recv(independent, 1, None), 1);
for tag in 2..4 {
harness.admit(Request::Sync(SyncRequest::new(file.clone())), tag);
}
for _ in 0..65 {
let (socket, _peer) = UnixStream::pair().unwrap();
let stale = harness.admit(recv(socket, 1, None), 4);
harness.orphan(stale);
}
harness.completed.clear();
harness.service();
assert_eq!(harness.driver.state.ready_queue, [independent]);
assert_eq!(harness.driver.state.acquiring.len(), 2);
assert_eq!(harness.driver.len(), 4);
active_peer.write_all(b"x").unwrap();
harness.until(2);
assert_eq!(harness.driver.len(), 2);
assert_eq!(harness.driver.state.waiters.in_flight(), 0);
assert!(!harness.driver.has_pending_submissions());
assert!(harness.driver.state.ready_queue.is_empty());
drop(permit);
harness.until(4);
for (tag, completed) in harness.completed.iter().enumerate() {
assert!(matches!(completed.observer, TestObserver::Local(id) if id == tag as u64));
}
harness.drain();
drop(harness);
drop(file);
fs::remove_dir_all(directory).unwrap();
}
#[tokio::test]
async fn test_durability_admitted_after_service_registers_its_wait() {
let (directory, file) = make_file("admission_poll");
let permit = file.durability.clone().try_lock_owned().unwrap();
let mut harness = Harness::new(2);
harness.service();
// The native driver must progress even once the enclosing task has spent its
// cooperative budget.
while tokio::task::coop::has_budget_remaining() {
tokio::task::consume_budget().await;
}
// A deferred callback can admit work after service, immediately before parking.
harness.admit(Request::Sync(SyncRequest::new(file.clone())), 0);
drop(permit);
assert!(
file.durability.try_lock().is_err(),
"accepted wait did not reserve the released permit"
);
harness.until(1);
assert!(matches!(
harness.completed[0].output,
RequestOutput::Sync(Ok(()))
));
harness.drain();
drop(harness);
drop(file);
fs::remove_dir_all(directory).unwrap();
}
#[test]
fn test_foreign_durability_release_wakes_closed_worker_park() {
let (directory, file) = make_file("closed_permit_wake");
let permit = file.durability.clone().try_lock_owned().unwrap();
let mailbox = Arc::new(Mailbox::new().unwrap());
let mut driver = Driver::new(
&RingConfig {
size: 2,
timeout_wheel_tick: Duration::from_millis(5),
},
Duration::from_secs(60),
mailbox.waker.clone(),
Instant::now(),
)
.unwrap();
let mut deferred = Deferred::default();
let (sender, receiver) = oneshot::channel();
driver.admit(
Request::Sync(SyncRequest::new(file.clone())),
Observer::DetachedSync(sender),
Instant::now(),
&mut deferred,
);
drop(receiver);
assert!(mailbox.close().is_empty());
driver.close(&mut deferred);
driver.service(Instant::now(), true, &mut deferred).unwrap();
// Consume a latched acquisition-poll wake before the measured park, then service
// again as cleanup does after every wait. No operation SQE can produce a CQE.
driver.park(0, Some(Instant::now())).unwrap();
driver.service(Instant::now(), true, &mut deferred).unwrap();
assert_eq!(driver.len(), 1);
assert_eq!(driver.state.waiters.in_flight(), 0);
assert!(!driver.has_pending_submissions());
assert!(driver.ring.completion().is_empty());
let deadline = Instant::now() + Duration::from_secs(10);
let producer = thread::spawn(move || {
wait_until_eventfd_armed(&mailbox.waker, deadline);
assert!(!mailbox.is_open());
drop(permit);
});
assert!(driver.park(0, Some(deadline)).unwrap());
producer.join().unwrap();
let cqe = driver
.ring
.completion()
.next()
.expect("permit release did not wake the ring");
assert_eq!(cqe.user_data(), WAKE_USER_DATA);
assert!(
!driver.state.waker.pending(0),
"permit release published an unmatched mailbox batch"
);
driver
.state
.handle_cqe(cqe.user_data(), cqe.result(), cqe.flags(), &mut deferred);
driver.service(Instant::now(), true, &mut deferred).unwrap();
assert!(driver.is_empty());
assert_eq!(deferred.completions.len(), 1);
let (sender, result) = deferred.completions.pop().unwrap();
assert!(result.is_ok());
assert!(sender.send(result).is_err());
drop(deferred);
drop(driver);
drop(file);
fs::remove_dir_all(directory).unwrap();
}
#[test]
fn test_shutdown_finishes_orphaned_write_and_detached_sync() {
let mut harness = Harness::new(1);
let directory =
std::env::temp_dir().join(format!("commonware_driver_test_{}", std::process::id()));
let hold = Hold::acquire(&directory).unwrap();
let path = directory.join("durable");
let file = OpenOptions::new()
.read(true)
.write(true)
.create(true)
.truncate(true)
.open(&path)
.unwrap();
let held = Shared::detached(file, hold);
// More than one iovec batch forces a follow-up write before the sync.
let bufs = (0..IOVEC_BATCH_SIZE + 1)
.map(|_| IoBuf::from(b"x"))
.collect::<IoBufs>();
let id = harness.admit(
Request::WriteAt(WriteAtRequest::new(
held.clone(),
0,
bufs.into(),
WriteAtState::WritingBeforeSync,
Cache::Enabled,
)),
0,
);
harness.orphan(id);
let (sender, receiver) = oneshot::channel();
harness.driver.admit(
Request::Sync(SyncRequest::new(held)),
Observer::DetachedSync(sender),
harness.start,
&mut harness.deferred,
);
// Retained completion receivers do not participate in drain progress.
harness.drain();
assert_eq!(fs::read(&path).unwrap(), vec![b'x'; IOVEC_BATCH_SIZE + 1]);
assert_eq!(harness.driver.state.waiters.in_flight(), 0);
// Publish only after request ownership has retired. The receiver has
// not participated in the drain.
let mut published = false;
for completed in harness.completed {
if let TestObserver::DetachedSync(sender) = completed.observer {
let RequestOutput::Sync(result) = completed.output else {
panic!("wrong sync result");
};
assert!(sender.send(result).is_ok());
published = true;
}
}
assert!(published);
assert!(futures::executor::block_on(receiver).unwrap().is_ok());
fs::remove_dir_all(directory).unwrap();
}
/// Voluntary context switches of the calling thread so far.
fn voluntary_switches() -> libc::c_long {
// SAFETY: rusage holds only integers and timevals, for which all-zero
// bytes are a valid value.
let mut usage: libc::rusage = unsafe { std::mem::zeroed() };
assert_eq!(
// SAFETY: `usage` is valid, writable storage for one rusage.
unsafe { libc::getrusage(libc::RUSAGE_THREAD, &mut usage) },
0
);
usage.ru_nvcsw
}
/// Busy service turns and parks whose deadline already passed must not
/// sleep while a request is unfinished, and such a park must still run the
/// deferred task work. A zero-timeout GETEVENTS wait arms a timer and
/// schedules whenever no completion is ready, which shows as a voluntary
/// context switch per enter.
#[test]
fn test_busy_service_and_expired_park_do_not_sleep() {
/// Turns of one busy service enter and one expired park each.
const TURNS: usize = 200;
/// Voluntary context switches that unrelated sources may add over the
/// turns. A sleeping enter would cost one for every enter.
const MAX_VOLUNTARY_SWITCHES: libc::c_long = 20;
// Keep a receive in flight with no data, so every turn must enter the
// ring for its deferred task work and no completion is ready.
let mut harness = Harness::new(2);
let (left, mut right) = UnixStream::pair().unwrap();
harness.admit(recv(left, 1, None), 0);
harness.service();
assert_eq!(harness.driver.state.waiters.in_flight(), 1);
// Each turn makes one busy service enter and one park whose deadline
// has already passed.
let before = voluntary_switches();
for _ in 0..TURNS {
harness.service();
assert!(harness.driver.park(0, Some(Instant::now())).unwrap());
}
let switches = voluntary_switches() - before;
assert!(
switches < MAX_VOLUNTARY_SWITCHES,
"{switches} voluntary context switches in {TURNS} turns"
);
// The data's completion now waits in deferred task work, which a park
// whose deadline already passed must still run.
right.write_all(b"x").unwrap();
assert_eq!(harness.driver.ring.completion().len(), 0);
assert!(harness.driver.park(0, Some(Instant::now())).unwrap());
assert_eq!(harness.driver.ring.completion().len(), 1);
harness.until(1);
assert_eq!(received(&harness.completed[0]), b"x");
harness.drain();
}
/// A completion the kernel dropped fails every kind of enter with `EBADR`,
/// as it fails a wait that finds no completion ready. The kernel checks
/// for dropped completions only in that wait. The fault simulates a
/// nonzero overflow counter, so this checks how a drop propagates, not
/// the counter read itself.
#[test]
fn test_every_enter_reports_a_dropped_completion() {
// A flush fails, here the one park needs before it can rearm wake
// polling behind a receive that fills the SQ.
let mut harness = Harness::new(1);
let (socket, _peer) = UnixStream::pair().unwrap();
harness.admit(recv(socket, 1, None), 0);
assert!(
harness
.driver
.state
.fill_submission_queue(&mut harness.driver.ring)
);
assert!(harness.driver.state.wake_rearm_needed);
let fault = inject(&harness.driver, Fault::DropCompletion);
let error = harness.driver.park(0, Some(Instant::now())).unwrap_err();
assert_eq!(error.raw_os_error(), Some(libc::EBADR));
// The flush failed before rearming, not a later enter in this park.
assert!(harness.driver.state.wake_rearm_needed);
drop(fault);
harness.drain();
// Keep a receive in flight, so every turn enters the ring.
let mut harness = Harness::new(2);
let (left, mut right) = UnixStream::pair().unwrap();
harness.admit(recv(left, 1, None), 0);
harness.service();
assert_eq!(harness.driver.state.waiters.in_flight(), 1);
// A task-work enter fails, here the next busy service turn.
let fault = inject(&harness.driver, Fault::DropCompletion);
let error = harness
.driver
.service(Instant::now(), false, &mut harness.deferred)
.unwrap_err();
assert_eq!(error.raw_os_error(), Some(libc::EBADR));
drop(fault);
// So does a park whose deadline already passed.
let fault = inject(&harness.driver, Fault::DropCompletion);
let error = harness.driver.park(0, Some(Instant::now())).unwrap_err();
assert_eq!(error.raw_os_error(), Some(libc::EBADR));
drop(fault);
// A timed wait fails too, here one that times out.
let fault = inject(&harness.driver, Fault::DropCompletion);
let deadline = Instant::now() + TIMED_WAIT;
let error = harness.driver.park(0, Some(deadline)).unwrap_err();
assert_eq!(error.raw_os_error(), Some(libc::EBADR));
drop(fault);
// So does a wait that returns a completion, here an unbounded one
// whose receive has data.
right.write_all(b"x").unwrap();
let fault = inject(&harness.driver, Fault::DropCompletion);
let error = harness.driver.park(0, None).unwrap_err();
assert_eq!(error.raw_os_error(), Some(libc::EBADR));
drop(fault);
// With no completion dropped, the receive completes and retires.
harness.until(1);
assert_eq!(received(&harness.completed[0]), b"x");
harness.drain();
}
#[test]
fn test_enter_distinguishes_timeout_from_failure() {
let mut harness = Harness::new(1);
// The empty ring cannot produce a CQE. A wait that times out, a wait
// whose deadline already passed and the enters that never wait still
// succeed.
for enter in [
Enter::Flush,
Enter::TaskWork,
Enter::Wait {
deadline: Some(Instant::now() + TIMED_WAIT),
},
Enter::Wait {
deadline: Some(Instant::now()),
},
] {
Driver::enter(&mut harness.driver.ring, enter).unwrap();
}
// SAFETY: This isolated test closes the unused ring descriptor once,
// then forgets the ring mapping owner before another FD can reuse it.
assert_eq!(unsafe { libc::close(harness.driver.ring.as_raw_fd()) }, 0);
let results = [
Enter::Flush,
Enter::TaskWork,
Enter::Wait { deadline: None },
Enter::Wait {
deadline: Some(Instant::now() + Duration::from_secs(1)),
},
Enter::Wait {
deadline: Some(Instant::now()),
},
]
.map(|enter| Driver::enter(&mut harness.driver.ring, enter));
std::mem::forget(harness.driver.ring);
// Every enter path must preserve a permanent error. Forget the closed
// ring before asserting so a failure cannot try to close its FD again.
for result in results {
assert_eq!(result.unwrap_err().raw_os_error(), Some(libc::EBADF));
}
}
}