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
//! ## Intended behaviour
//!
//! In normal operation we try to track as little state as possible, cheaply.
//! We do track total memory use in nominal bytes
//! (but a little approximately).
//!
//! When we exceed the quota, we engage a more expensive algorithm:
//! we build a heap to select oldest victims, and
//! we use the heap to keep reducing memory
//! until we go below a low-water mark (hysteresis).
//!
//! ## Key concepts
//!
//! * **Tracker**:
//! Instance of the memory quota system
//! Each tracker has a notion of how much memory its participants
//! are allowed to use, in aggregate.
//! Tracks memory usage by all the Accounts and Participants.
//! Different Trackers are completely independent.
//!
//! * **Account**:
//! all memory used within the same Account is treated equally,
//! and reclamation also happens on an account-by-account basis.
//! (Each Account is with one Tracker.)
//!
//! See [the `mq_queue` docs](mq_queue/index.html#use-in-arti)
//! for we use Accounts in Arti to track memory for the various queues.
//!
//! * **Participant**:
//! one data structure that uses memory.
//! Each Participant is linked to *one* Account. An account has *one or more* Participants.
//! (An Account can exist with zero Participants, but can't then claim memory.)
//! A Participant provides a `dyn IsParticipant` to the memory system;
//! in turn, the memory system provides the Participant with a `Participation` -
//! a handle for tracking memory alloc/free.
//!
//! Actual memory allocation is handled by the participant itself,
//! using the global heap:
//! for each allocation, the Participant *both*
//! calls [`claim`](mtracker::Participation::claim)
//! *and* allocates the actual object,
//! and later, *both* frees the actual object *and*
//! calls [`release`](mtracker::Participation::release).
//!
//! * **Child Account**/**Parent Account**:
//! An Account may have a Parent.
//! When a tracker requests memory reclamation from a Parent,
//! it will also request it of all that Parent's Children (but not vice versa).
//!
//! The account structure and reclamation strategy for Arti is defined in
//! `tor-proto`, and documented in `tor_proto::memquota`.
//!
//! * **Data age**:
//! Each Participant must be able to say what the oldest data is, that it is storing.
//! The reclamation policy is to try to free the oldest data.
//!
//! * **Reclamation**:
//! When a Tracker decides that too much memory is being used,
//! it will select a victim Account based on the data age.
//! It will then ask *every Participant* in that Account,
//! and every Participant in every Child of that Account,
//! to reclaim memory.
//! A Participant responds by freeing at least some memory,
//! according to the reclamation request, and tells the Tracker when it has done so.
//!
//! * **Reclamation strategy**:
//! To avoid too-frequent reclamation, once reclamation has started,
//! it will continue until a low-water mark is reached, significantly lower than the quota.
//! I.e. the system has a hysteresis.
//!
//! The only currently implemented higher-level Participant is
//! [`mq_queue`], a queue which responds to a reclamation request
//! by completely destroying itself, freeing all its data,
//! and reporting it has been closed.
//!
//! * <div id="is-approximate">
//!
//! **Approximate** (both in time and space):
//! The memory quota system is not completely precise.
//! Participants need not report their use precisely,
//! but the errors should be reasonably small, and bounded.
//! Likewise, the enforcement is not precise:
//! reclamation may start slightly too early, or too late;
//! but the memory use will be bounded below by O(number of participants)
//! and above by O(1) (plus errors from the participants).
//! Reclamation is not immediate, and is dependent on task scheduling;
//! during memory pressure the quota may be exceeded;
//! new allocations are not prevented while attempts at reclamation are ongoing.
//!
//! </div>
//!
// TODO we haven't implemented the queue wrapper yet
// ! * **Queues**:
// ! We provide a higher-level API that wraps an mpsc queue and turns it into a Participant.
// !
//! ## Ownership and Arc keeping-alive
//!
//! * Somewhere, someone must keep an `Account` to keep the account open.
//! Ie, the principal object corresponding to the accountholder should contain an `Account`.
//!
//! * `Arc<MemoryTracker>` holds `Weak<dyn IsParticipant>`.
//! If the tracker finds the `IsParticipant` has vanished,
//! it assumes this means that the Participant is being destroyed and
//! it can treat all of the memory it claimed as freed.
//!
//! * Each participant holds a `Participation`.
//! A `Participation` may be invalidated by collapse of the underlying Account,
//! which may be triggered in any number of ways.
//!
//! * A `Participation` does *not* keep its `Account` alive.
//! Ie, it has only a weak reference to the Account.
//!
//! * A Participant's implementor of `IsParticipant` may hold a `Participation`.
//! If the `impl IsParticipant` is also the principal accountholder object,
//! it must hold an `Account` too.
//!
//! * Child/parent accounts do not imply any keeping-alive relationship.
//! It's just that a reclamation request to a parent (if it still exists)
//! will also be made to its children.
//!
//!
//! ```text
//! accountholder =======================================>* Participant
//! (impl IsParticipant)
//! ||
//! || ^ ||
//! || | ||
//! || global Weak<dyn>| ||
//! || || | ||
//! \/* \/ | ||
//! | ||
//! Account *===========> MemoryTracker ------------------' ||
//! ||
//! ^ ||
//! | \/
//! |
//! `-------------------------------------------------* Participation
//!
//!
//!
//! accountholder which is also directly the Participant ==============\
//! (impl IsParticipant) ||
//! ^ ||
//! || | ||
//! || | ||
//! || global |Weak<dyn> ||
//! || || | ||
//! \/ \/ | ||
//! ||
//! Account *===========> MemoryTracker ||
//! ||
//! ^ ||
//! | \/
//! |
//! `-------------------------------------------------* Participation
//!
//! ```
//!
//! ## Panics and state corruption
//!
//! This library is intended to be entirely panic-free,
//! even in the case of accounting errors, arithmetic overflow, etc.
//!
//! In the case of sufficiently bad account errors,
//! a Participant, or a whole Account, or the whole MemoryQuotaTracker,
//! may become unusable,
//! in which case methods will return errors with kind [`tor_error::ErrorKind::Internal`].
//
// TODO MEMQUOTA: We ought to account for the fixed overhead of each stream, circuit, and
// channel. For example, DataWriterImpl holds a substantial fixed-length buffer. A
// complication is that we want to know the "data age", which is possibly the time this stream
// was last used.
// @@ begin lint list maintained by maint/add_warning @@
// @@REMOVE_WHEN(ci_arti_stable)
// @@REMOVE_WHEN(ci_arti_nightly)
// This can reasonably be done for explicitness
// arti/-/merge_requests/588/#note_2812945
// temporary workaround for arti#587
// complained-about code is fine, often best
// See arti#1765
// temporary workaround for arti#2060
// See arti#2342
// See arti#2571
//! <!-- @@ end lint list maintained by maint/add_warning @@ -->
// TODO #1176
//
// See `Panics` in the crate-level docs, above.
//
// This lint sometimes has bugs, but it seems to DTRT for me as of 1.81.0-beta.6.
// If it breaks, these bug(s) may be relevant:
// https://github.com/rust-lang/rust-clippy/issues/11220
// https://github.com/rust-lang/rust-clippy/issues/11145
// https://github.com/rust-lang/rust-clippy/issues/10209
// Internal supporting modules
// Modules with public items
/// For trait sealing
/// Names exported for testing
//---------- re-exports at the crate root ----------
pub use ;
pub use ;
pub use ;
pub use ;
pub use ArcMemoryQuotaTrackerExt;
pub use derive_deftly;
/// `Result` whose `Err` is [`tor_memtrack::Error`](Error)
pub type Result<T> = Result;
pub use ;