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
use super::address::*;
use super::frame_allocator::*;
use super::page_table::*;
use super::KERNEL_SPACE;
use crate::config::{PAGE_SIZE, PAGE_SIZE_BITS};
use crate::drivers::BLOCK_DEVICE;
use alloc::sync::Arc;
use alloc::sync::Weak;
use alloc::vec::Vec;
use easy_fs::{BlockDevice, Cache, CacheManager};
use lazy_static::*;
use log::warn;
use spin::Mutex;
const BUFFER_SIZE: usize = 512;
const PAGE_BUFFERS: usize = 8;
const PRIORITY_UPPERBOUND: usize = 3;
pub struct PageCache {
priority: usize,
block_ids: [usize; PAGE_BUFFERS],
page_ptr: &'static mut [u8; PAGE_SIZE],
tracker: Arc<FrameTracker>,
}
impl Cache for PageCache {
fn read<T, V>(&self, offset: usize, f: impl FnOnce(&T) -> V) -> V {
assert!(offset.saturating_add(core::mem::size_of::<T>()) <= PAGE_SIZE);
f(unsafe {
self.page_ptr
.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>()) <= PAGE_SIZE);
f(unsafe {
self.page_ptr
.as_mut_ptr()
.add(offset)
.cast::<T>()
.as_mut()
.unwrap()
})
}
}
impl PageCache {
pub fn new() -> Self {
let tracker = frame_alloc().unwrap();
let page_ptr = (tracker.ppn.0 << PAGE_SIZE_BITS) as *mut [u8; PAGE_SIZE];
let page_ptr = unsafe { page_ptr.as_mut().unwrap() };
Self {
priority: 0,
block_ids: [usize::MAX; PAGE_BUFFERS],
page_ptr,
tracker,
}
}
pub fn get_ppn(&self) -> PhysPageNum {
self.tracker.ppn
}
pub fn get_pte(&self) -> PageTableEntry {
KERNEL_SPACE
.lock()
.translate(self.get_ppn().0.into())
.unwrap()
}
pub fn read_in(&mut self, block_ids: Vec<usize>, block_device: &Arc<dyn BlockDevice>) {
assert!(block_ids.len() <= PAGE_BUFFERS);
for (i, block_id) in block_ids.iter().enumerate() {
self.block_ids[i] = *block_id;
let buf = unsafe {
self.page_ptr
.as_mut_ptr()
.add(i * BUFFER_SIZE)
.cast::<[u8; BUFFER_SIZE]>()
.as_mut()
.unwrap()
};
block_device.read_block(*block_id, buf);
}
}
pub fn write_back(&mut self) {
for (i, block_id) in self.block_ids.iter_mut().enumerate() {
if *block_id == usize::MAX {
continue;
}
let buf = unsafe {
self.page_ptr
.as_ptr()
.add(i * BUFFER_SIZE)
.cast::<[u8; BUFFER_SIZE]>()
.as_ref()
.unwrap()
};
BLOCK_DEVICE.write_block(*block_id, buf);
*block_id = usize::MAX;
}
}
}
impl Drop for PageCache {
fn drop(&mut self) {
if self.get_pte().is_dirty() == false {
return;
}
self.write_back();
}
}
lazy_static! {
pub static ref PAGECACHE_MANAGER: Arc<Mutex<Vec<Arc<Mutex<PageCache>>>>> =
Arc::new(Mutex::new(Vec::new()));
}
pub fn oom() {
let mut page_caches = PAGECACHE_MANAGER.lock();
let mut lose_times = 0;
loop {
let mut dropped: usize = 0;
let mut undropped: Vec<Arc<Mutex<PageCache>>> = Vec::new();
for page_cache in page_caches.to_vec() {
let mut locked = page_cache.lock();
if Arc::strong_count(&page_cache) > 1 {
drop(locked);
undropped.push(page_cache);
} else if locked.priority > 0 {
locked.priority -= 1;
drop(locked);
undropped.push(page_cache);
} else {
dropped += 1;
}
}
if dropped > 0 {
page_caches.clear();
page_caches.append(&mut undropped);
warn!("oom: add info here");
break;
} else {
lose_times = lose_times + 1;
if lose_times == PRIORITY_UPPERBOUND {
panic!("No free cache spaces!");
}
}
}
}
pub struct PageCacheManager {
cache_pool: Vec<Weak<Mutex<PageCache>>>,
}
impl CacheManager for PageCacheManager {
const CACHE_SZ: usize = PAGE_SIZE;
type CacheType = PageCache;
fn new() -> Self {
Self {
cache_pool: Vec::new(),
}
}
fn try_get_block_cache(
&mut self,
block_id: usize,
inner_cache_id: usize,
) -> Option<Arc<Mutex<PageCache>>> {
if inner_cache_id >= self.cache_pool.len() {
return None;
}
let page_caches = PAGECACHE_MANAGER.lock();
let page_cache = self.cache_pool[inner_cache_id].upgrade();
if page_cache.is_some() {
let mut locked = page_cache.as_ref().unwrap().lock();
if locked.priority < PRIORITY_UPPERBOUND {
locked.priority += 1;
}
}
page_cache
}
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>,
{
while inner_cache_id >= self.cache_pool.len() {
self.cache_pool.push(Weak::new());
}
let page_caches = PAGECACHE_MANAGER.lock();
let mut page_cache = self.cache_pool[inner_cache_id].upgrade();
if page_cache.is_none() {
let mut new_page_cache = PageCache::new();
new_page_cache.read_in(neighbor(), &block_device);
let new_page_cache = Arc::new(Mutex::new(new_page_cache));
self.cache_pool[inner_cache_id] = Arc::downgrade(&new_page_cache);
page_cache = Some(new_page_cache.clone());
PAGECACHE_MANAGER.lock().push(new_page_cache);
}
let page_cache = page_cache.unwrap();
let mut locked = page_cache.lock();
if locked.priority < PRIORITY_UPPERBOUND {
locked.priority += 1;
}
drop(locked);
page_cache
}
}