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
use super::frame_allocator::*;
use crate::config::PAGE_SIZE_BITS;
use crate::drivers::BLOCK_DEVICE;
use alloc::sync::Arc;
use alloc::vec::Vec;
use easy_fs::{BlockDevice, Cache, CacheManager};
use spin::Mutex;
const PAGE_BUFFERS: usize = 8;
const BUFFER_SIZE: usize = 512;
const CACHEPOOLSIZE: usize = 16;
const CACHEPOOLPAGE: usize = CACHEPOOLSIZE >> 3;
const PRIORITY_UPPERBOUND: usize = 3;
pub struct BufferCache {
priority: usize,
block_id: usize,
buffer: &'static mut [u8; BUFFER_SIZE],
}
impl Cache for BufferCache {
fn read<T, V>(&self, offset: usize, f: impl FnOnce(&T) -> V) -> V {
assert!(offset.saturating_add(core::mem::size_of::<T>()) <= BUFFER_SIZE);
f(unsafe {
self.buffer
.as_ptr()
.add(offset)
.cast::<T>()
.as_ref()
.unwrap()
})
}
fn modify<T, V>(&mut self, offset: usize, f: impl FnOnce(&mut T) -> V) -> V {
assert!(offset.saturating_add(core::mem::size_of::<T>()) <= BUFFER_SIZE);
f(unsafe {
self.buffer
.as_mut_ptr()
.add(offset)
.cast::<T>()
.as_mut()
.unwrap()
})
}
}
impl BufferCache {
pub fn new(buffer_ptr: *mut [u8; BUFFER_SIZE]) -> Self {
let buffer = unsafe { buffer_ptr.as_mut().unwrap() };
Self {
priority: 0,
block_id: usize::MAX,
buffer,
}
}
pub fn read_block(&mut self, block_id: usize, block_device: Arc<dyn BlockDevice>) {
self.block_id = block_id;
let buf = self.buffer.as_mut();
block_device.read_block(block_id, buf);
}
}
pub struct BlockCacheManager {
_hold: Vec<Arc<FrameTracker>>,
cache_pool: Vec<Arc<Mutex<BufferCache>>>,
}
impl BlockCacheManager {
fn oom(&self) {
for buffer_cache in &self.cache_pool {
if Arc::strong_count(buffer_cache) > 1 {
continue;
}
let mut locked = buffer_cache.lock();
if locked.priority > 0 {
locked.priority -= 1;
} else {
let block_id = locked.block_id;
let buf = locked.buffer.as_ref();
BLOCK_DEVICE.write_block(block_id, buf);
locked.block_id = usize::MAX;
}
}
}
fn alloc_buffer_cache(&self) -> Arc<Mutex<BufferCache>> {
loop {
for buffer_cache in &self.cache_pool {
let mut locked = buffer_cache.lock();
if locked.block_id == usize::MAX {
return buffer_cache.clone();
}
}
self.oom();
}
}
fn try_get_block_cache(
&self,
block_id: usize,
inner_cache_id: usize,
) -> Option<Arc<Mutex<BufferCache>>> {
for buffer_cache in &self.cache_pool {
let mut locked = buffer_cache.lock();
if locked.block_id == block_id {
if locked.priority < PRIORITY_UPPERBOUND {
locked.priority += 1;
}
return Some(buffer_cache.clone());
}
}
None
}
}
impl CacheManager for BlockCacheManager {
const CACHE_SZ: usize = BUFFER_SIZE;
type CacheType = BufferCache;
fn new() -> Self {
let mut hold: Vec<Arc<FrameTracker>> = Vec::new();
let mut cache_pool: Vec<Arc<Mutex<BufferCache>>> = Vec::new();
for i in 0..CACHEPOOLPAGE {
hold.push(frame_alloc().unwrap());
let page_ptr = (hold[i].ppn.0 << PAGE_SIZE_BITS) as *mut [u8; BUFFER_SIZE];
for j in 0..PAGE_BUFFERS {
let buffer_ptr = unsafe { page_ptr.add(j) };
cache_pool.push(Arc::new(Mutex::new(BufferCache::new(buffer_ptr))))
}
}
Self {
_hold: hold,
cache_pool,
}
}
fn try_get_block_cache(
&mut self,
block_id: usize,
inner_cache_id: usize,
) -> Option<Arc<Mutex<Self::CacheType>>> {
for buffer_cache in &self.cache_pool {
let mut locked = buffer_cache.lock();
if locked.block_id == block_id {
if locked.priority < PRIORITY_UPPERBOUND {
locked.priority += 1;
}
return Some(buffer_cache.clone());
}
}
None
}
fn get_block_cache<FUNC>(
&mut self,
block_id: usize,
inner_cache_id: usize,
neighbor: FUNC,
block_device: Arc<dyn BlockDevice>,
) -> Arc<Mutex<Self::CacheType>>
where
FUNC: Fn() -> Vec<usize>,
{
let try_get = self.try_get_block_cache(block_id, inner_cache_id);
if try_get.is_some() {
return try_get.unwrap();
}
let buffer_cache = self.alloc_buffer_cache();
let mut locked = buffer_cache.lock();
locked.read_block(block_id, block_device);
if locked.priority < PRIORITY_UPPERBOUND {
locked.priority += 1;
}
buffer_cache.clone()
}
}