-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtrees.rs
293 lines (265 loc) · 8.29 KB
/
trees.rs
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
use core::mem::align_of;
use core::ops::RangeBounds;
use core::sync::atomic::AtomicU32;
use core::{fmt, slice};
use bitfield_struct::bitfield;
use crate::atomic::{Atom, Atomic};
use crate::util::{size_of_slice, Align};
use crate::{Error, Flags, Result, HUGE_ORDER, TREE_FRAMES};
#[derive(Default)]
pub struct Trees<'a> {
/// Array of level 3 entries, which are the roots of the trees
pub entries: &'a [Atom<Tree>],
}
impl fmt::Debug for Trees<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let max = self.entries.len();
let mut free = 0;
let mut partial = 0;
for e in self.entries {
let f = e.load().free();
if f == TREE_FRAMES {
free += 1;
} else if f > Self::MIN_FREE {
partial += 1;
}
}
write!(f, "(total: {max}, free: {free}, partial: {partial})")?;
Ok(())
}
}
impl<'a> Trees<'a> {
pub const MIN_FREE: usize = TREE_FRAMES / 16;
pub fn metadata_size(frames: usize) -> usize {
// Event thought the elements are not cache aligned, the whole array should be
size_of_slice::<Atom<Tree>>(frames.div_ceil(TREE_FRAMES))
.next_multiple_of(align_of::<Align>())
}
pub fn metadata(&mut self) -> &'a mut [u8] {
let len = Self::metadata_size(self.len() * TREE_FRAMES);
unsafe { slice::from_raw_parts_mut(self.entries.as_ptr().cast_mut().cast(), len) }
}
/// Initialize the tree array
pub fn new(
frames: usize,
buffer: &'a mut [u8],
tree_init: Option<impl Fn(usize) -> usize>,
) -> Self {
assert!(buffer.len() >= Self::metadata_size(frames));
let len = frames.div_ceil(TREE_FRAMES);
let entries: &mut [Atom<Tree>] =
unsafe { slice::from_raw_parts_mut(buffer.as_mut_ptr().cast(), len) };
if let Some(tree_init) = tree_init {
for (i, e) in entries.iter_mut().enumerate() {
let frames = tree_init(i * TREE_FRAMES);
*e = Atom::new(Tree::with(frames, false, Kind::Fixed));
}
}
Self { entries }
}
pub fn len(&self) -> usize {
self.entries.len()
}
pub fn get(&self, i: usize) -> Tree {
self.entries[i].load()
}
/// Return the number of entirely free trees
pub fn free(&self) -> usize {
self.entries
.iter()
.filter(|e| e.load().free() == TREE_FRAMES)
.count()
}
/// Return the total sum of the tree counters
pub fn free_frames(&self) -> usize {
self.entries.iter().map(|e| e.load().free()).sum()
}
/// Sync with the global tree, stealing its counters
pub fn sync(&self, i: usize, min: usize) -> Option<Tree> {
self.entries[i].fetch_update(|e| e.sync_steal(min)).ok()
}
/// Increment or reserve the tree
pub fn inc_or_reserve(&self, i: usize, free: usize, may_reserve: bool) -> Option<Tree> {
let mut reserved = false;
let tree = self.entries[i]
.fetch_update(|v| {
let v = v.inc(free);
if may_reserve && !v.reserved() && v.free() > Self::MIN_FREE {
// Reserve the tree that was targeted by the last N frees
reserved = true;
Some(v.with_free(0).with_reserved(true))
} else {
reserved = false; // <- This one is very important if CAS fails!
Some(v)
}
})
.unwrap();
if reserved {
Some(tree)
} else {
None
}
}
/// Unreserve an entry, adding the local entry counter to the global one
pub fn unreserve(&self, i: usize, free: usize, kind: Kind) {
self.entries[i]
.fetch_update(|v| v.unreserve_add(free, kind))
.expect("Unreserve failed");
}
/// Iterate through all trees as long `f` returns `Error::Memory`
pub fn search(
&self,
start: usize,
offset: usize,
len: usize,
mut f: impl FnMut(usize) -> Result<usize> + Clone,
) -> Result<usize> {
// There has to be enough space for the current allocation
let start = (start + self.entries.len()) as isize;
for i in offset..len {
// Alternating between before and after this entry
let off = if i % 2 == 0 {
(i / 2) as isize
} else {
-(i.div_ceil(2) as isize)
};
let i = (start + off) as usize % self.entries.len();
match f(i) {
Err(Error::Memory) => {}
r => return r,
}
}
Err(Error::Memory)
}
#[allow(unused)]
pub fn dump(&'a self) -> TreeDbg<'a> {
TreeDbg(self)
}
}
pub struct TreeDbg<'a>(&'a Trees<'a>);
impl fmt::Debug for TreeDbg<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(f, "[")?;
for entry in self.0.entries {
writeln!(f, " {:?}", entry.load())?;
}
write!(f, "]")?;
Ok(())
}
}
/// Tree entry for 4K frames
#[bitfield(u32)]
#[derive(PartialEq, Eq)]
pub struct Tree {
/// Number of free 4K frames.
#[bits(29)]
pub free: usize,
/// If this subtree is reserved by a CPU.
pub reserved: bool,
/// Are the frames movable?
#[bits(2)]
pub kind: Kind,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Kind {
Fixed,
Movable,
Huge,
}
impl Kind {
pub const LEN: usize = 3;
pub fn accepts(self, other: Kind) -> bool {
(other as usize) >= (self as usize)
}
const fn from_bits(bits: u8) -> Self {
match bits {
0 => Self::Fixed,
1 => Self::Movable,
2 => Self::Huge,
_ => Self::Fixed,
}
}
const fn into_bits(self) -> u8 {
match self {
Self::Fixed => 0,
Self::Movable => 1,
Self::Huge => 2,
}
}
}
impl From<Flags> for Kind {
fn from(flags: Flags) -> Self {
if flags.order() >= HUGE_ORDER {
Self::Huge
} else if flags.movable() {
Self::Movable
} else {
Self::Fixed
}
}
}
impl From<usize> for Kind {
fn from(value: usize) -> Self {
Self::from_bits(value as _)
}
}
const _: () = assert!(1 << Tree::FREE_BITS >= TREE_FRAMES);
impl Atomic for Tree {
type I = AtomicU32;
}
impl Tree {
/// Creates a new entry.
pub fn with(free: usize, reserved: bool, kind: Kind) -> Self {
assert!(free <= TREE_FRAMES);
Self::new()
.with_free(free)
.with_reserved(reserved)
.with_kind(kind)
}
/// Increments the free frames counter.
pub fn inc(self, free: usize) -> Self {
let free = self.free() + free;
assert!(free <= TREE_FRAMES);
self.with_free(free)
}
pub fn dec_force(mut self, free: usize, kind: Kind) -> Option<Self> {
if !self.reserved() && self.free() >= free {
if !self.kind().accepts(kind) {
self.set_kind(kind);
}
Some(self.with_free(self.free() - free))
} else {
None
}
}
/// Reserves this entry if its frame count is in `range`.
pub fn reserve(self, free: impl RangeBounds<usize>, kind: Kind) -> Option<Self> {
if !self.reserved()
&& free.contains(&self.free())
&& (kind == self.kind() || self.free() == TREE_FRAMES)
{
Some(Self::with(0, true, kind))
} else {
None
}
}
/// Add the frames from the `other` entry to the reserved `self` entry and unreserve it.
/// `self` is the entry in the global array / table.
pub fn unreserve_add(self, free: usize, kind: Kind) -> Option<Self> {
if self.reserved() {
let free = self.free() + free;
assert!(free <= TREE_FRAMES);
Some(Self::with(free, false, kind))
} else {
None
}
}
/// Set the free counter to zero if it is large enough for synchronization
pub fn sync_steal(self, min: usize) -> Option<Self> {
if self.reserved() && self.free() > min {
Some(self.with_free(0))
} else {
None
}
}
}