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
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
//! Per-transfer read-ahead window: the occupancy bound on speculative issuance.
//!
//! Issuance of a speculative part requires `issued - released < window()`, where
//! `issued` counts parts claimed for issuance and `released` counts parts freed by
//! either delivery surface (stream pull or disk drain; see `recv_buffer`). The
//! quantity `issued - released` is the transfer's resident occupancy in parts, so
//! the gate bounds how far issuance runs ahead of consumption and nothing more.
//! It is one of three independent upper bounds on issuance; the others — total
//! in-flight concurrency and the client's memory budget — live elsewhere, and
//! issuance takes their min.
//!
//! The window is free buffer space, `window - occupancy`, an exact subtraction
//! recomputed as the buffer fills and drains. It is not estimated from a rate and
//! it does not decay, so a gap in delivery does not shrink it: a consumer paused
//! between bursts, or blocked behind a not-yet-filled part, still leaves a full
//! window of slack to make progress.
//!
//! # Pacing
//!
//! The window is a fixed per-transfer cap
//! ([`DEFAULT_WINDOW_PARTS`](crate::operation::download::read_ahead::DEFAULT_WINDOW_PARTS)).
//! The pacing behaviors follow from the occupancy gate directly:
//!
//! - Fast consumer: `released` keeps pace, `issued - released` stays small, the
//! gate does not bind, and the transfer runs at the concurrency limit.
//! - Slow consumer: `released` lags, `issued - released` fills to the window, the
//! gate closes, and issuance pauses until the consumer drains. This transfer's
//! resident occupancy is then bounded at `window` parts. The client's shared
//! memory budget bounds aggregate demand across transfers and typically binds
//! a transfer before its read-ahead window fills.
//! - Blocked consumer (a stalled or missing part): `released` cannot advance past
//! the hole, occupancy pins at the window, and issuance stops at `window`, leaving
//! a full window of slack to make progress around the hole rather than collapsing
//! to one part.
//!
//! The window is stored in an [`std::sync::atomic::AtomicU64`] so it can be lowered
//! with a lock-free store while a transfer runs (see
//! [`ReadAhead::set_window`](crate::operation::download::read_ahead::ReadAhead::set_window)).
use ;
/// Fixed read-ahead window, in parts. A per-transfer cap on how far speculative
/// issuance runs ahead of consumption, set well above the bandwidth-delay product of
/// a 100 Gbps link so the gate does not bind a consumer that keeps pace. It is a
/// per-transfer prefetch-depth ceiling, not the memory bound. The client's
/// memory budget applies across transfers and usually binds concurrent work
/// before one transfer reaches this window.
///
/// The default for the public [`ReadAhead::Auto`](crate::config::ReadAhead) mode;
/// [`window_parts_for`] resolves the knob to a window in parts.
pub const DEFAULT_WINDOW_PARTS: u64 = 1024;
/// Resolve the effective read-ahead window in parts for a download, in precedence
/// order: the per-request [`ReadAhead`](crate::config::ReadAhead) override if set,
/// else the client default. See [`window_parts_for`] for the per-mode mapping.
pub
/// Map the public [`ReadAhead`](crate::config::ReadAhead) knob to a window in parts.
/// `Auto` is the fixed per-transfer cap; `Parts(n)` is `n + 1` — `n` parts of
/// speculation beyond the part the consumer is waiting on, which is always admitted,
/// so `Parts(0)` is demand paging. The single definition of the knob's meaning, used
/// at transfer construction and by the dynamic control surface.
pub
/// Per-transfer read-ahead window: the occupancy bound on speculative issuance.
///
/// `window()` is the hot read (the issuance gate, per claim) and is lock-free. The
/// value is fixed at construction; the [`AtomicU64`] allows it to be lowered with a
/// lock-free store while the transfer runs. Shared `&self` across the issuer and
/// consumer tasks.
pub