-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathblkcache.c
More file actions
383 lines (326 loc) · 10 KB
/
Copy pathblkcache.c
File metadata and controls
383 lines (326 loc) · 10 KB
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#include <blkcache.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <pthread.h>
/**
* 全局缓存实例
*/
static struct blk_cache cache;
/**
* 预读队列
*/
#define PREFETCH_QUEUE_SIZE 16
static struct prefetch_entry {
struct blkdev *bdev;
unsigned long block_num;
int priority;
} prefetch_queue[PREFETCH_QUEUE_SIZE];
static int prefetch_queue_head = 0;
static int prefetch_queue_tail = 0;
static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
static struct blk_cache_entry *allocate_cache_entry(void);
/**
* 查找缓存项(优化版)
*/
static struct blk_cache_entry *find_cache_entry(struct blkdev *bdev, unsigned long block_num)
{
struct blk_cache_entry *entry = cache.head;
while (entry) {
if (entry->valid && entry->bdev == bdev && entry->block_num == block_num) {
// 更新访问时间
entry->access_time = ++cache.timestamp;
return entry;
}
entry = entry->next;
}
return NULL;
}
/**
* 添加预读请求
*/
static void add_prefetch_request(struct blkdev *bdev, unsigned long block_num, int priority)
{
int next = (prefetch_queue_tail + 1) % PREFETCH_QUEUE_SIZE;
pthread_mutex_lock(&cache_lock);
if (next != prefetch_queue_head) {
prefetch_queue[prefetch_queue_tail].bdev = bdev;
prefetch_queue[prefetch_queue_tail].block_num = block_num;
prefetch_queue[prefetch_queue_tail].priority = priority;
prefetch_queue_tail = next;
}
pthread_mutex_unlock(&cache_lock);
}
/**
* 处理预读请求
*/
static void process_prefetch_requests(void)
{
while (prefetch_queue_head != prefetch_queue_tail) {
struct prefetch_entry *entry = &prefetch_queue[prefetch_queue_head];
if (!find_cache_entry(entry->bdev, entry->block_num)) {
char *prefetch_buf = malloc(BLOCK_SIZE);
if (prefetch_buf) {
read_block(entry->bdev, entry->block_num, 0, prefetch_buf, BLOCK_SIZE);
pthread_mutex_lock(&cache_lock);
struct blk_cache_entry *cache_entry = allocate_cache_entry();
if (cache_entry) {
memcpy(cache_entry->data, prefetch_buf, BLOCK_SIZE);
cache_entry->bdev = entry->bdev;
cache_entry->block_num = entry->block_num;
cache_entry->valid = 1;
cache_entry->dirty = 0;
cache_entry->access_time = ++cache.timestamp;
}
pthread_mutex_unlock(&cache_lock);
free(prefetch_buf);
}
}
prefetch_queue_head = (prefetch_queue_head + 1) % PREFETCH_QUEUE_SIZE;
}
}
/**
* 将缓存项移到LRU链表头部
*/
static void move_to_head(struct blk_cache_entry *entry)
{
if (entry == cache.head) {
return; // 已经在头部
}
// 从当前位置移除
struct blk_cache_entry *prev = cache.head;
while (prev && prev->next != entry) {
prev = prev->next;
}
if (prev) {
prev->next = entry->next;
}
// 添加到头部
entry->next = cache.head;
cache.head = entry;
}
/**
* 分配新的缓存项
*/
static struct blk_cache_entry *allocate_cache_entry(void)
{
struct blk_cache_entry *entry = NULL;
// 如果有未使用的项,直接分配
if (cache.entry_count < BLK_CACHE_SIZE) {
entry = &cache.entries[cache.entry_count];
entry->data = malloc(BLOCK_SIZE);
if (!entry->data) {
return NULL;
}
cache.entry_count++;
memset(entry->data, 0, BLOCK_SIZE);
} else {
// 查找最久未访问的项(LRU)
struct blk_cache_entry *lru_entry = cache.head;
struct blk_cache_entry *prev_lru = NULL;
struct blk_cache_entry *prev = NULL;
struct blk_cache_entry *curr = cache.head;
while (curr) {
if (curr->valid && (!lru_entry || curr->access_time < lru_entry->access_time)) {
lru_entry = curr;
prev_lru = prev;
}
prev = curr;
curr = curr->next;
}
if (!lru_entry) {
return NULL;
}
// 如果是脏的,写回磁盘
if (lru_entry->dirty) {
write_block(lru_entry->bdev, lru_entry->block_num, 0, lru_entry->data, BLOCK_SIZE);
lru_entry->dirty = 0;
}
// 重置该缓存项
lru_entry->valid = 0;
lru_entry->dirty = 0;
entry = lru_entry;
// 从链表中移除
if (prev_lru) {
prev_lru->next = lru_entry->next;
} else {
cache.head = lru_entry->next;
}
}
// 初始化新项
entry->valid = 1;
entry->dirty = 0;
entry->access_time = ++cache.timestamp;
entry->bdev = NULL;
entry->block_num = 0;
entry->next = cache.head;
cache.head = entry;
return entry;
}
struct blk_cache_stats cache_stats = {0};
/**
* 初始化块缓存
*/
int blk_cache_init(void)
{
cache.entries = malloc(sizeof(struct blk_cache_entry) * BLK_CACHE_SIZE);
if (!cache.entries) {
return -1;
}
memset(cache.entries, 0, sizeof(struct blk_cache_entry) * BLK_CACHE_SIZE);
cache.head = NULL;
cache.entry_count = 0;
cache.timestamp = 0;
memset(&cache_stats, 0, sizeof(cache_stats));
memset(prefetch_queue, 0, sizeof(prefetch_queue));
return 0;
}
/**
* 获取缓存统计信息
*/
void blk_cache_get_stats(struct blk_cache_stats *stats)
{
if (stats) {
memcpy(stats, &cache_stats, sizeof(cache_stats));
}
}
/**
* 打印缓存统计信息
*/
void blk_cache_print_stats(void)
{
double hit_rate = 0.0;
if (cache_stats.total_reads > 0) {
hit_rate = (double)cache_stats.cache_hits / cache_stats.total_reads * 100.0;
}
printf("\n=== 块缓存统计 ===\n");
printf("总读取次数: %lu\n", cache_stats.total_reads);
printf("缓存命中: %lu (%.2f%%)\n", cache_stats.cache_hits, hit_rate);
printf("缓存未命中: %lu\n", cache_stats.cache_misses);
printf("总写入次数: %lu\n", cache_stats.total_writes);
printf("脏块写入: %lu\n", cache_stats.dirty_writes);
printf("预读命中: %lu\n", cache_stats.prefetch_hits);
printf("缓存使用: %d/%d\n", cache.entry_count, BLK_CACHE_SIZE);
printf("==================\n");
}
/**
* 清理块缓存
*/
void blk_cache_exit(void)
{
// 同步所有脏块
blk_cache_sync(NULL);
// 释放所有内存
for (int i = 0; i < cache.entry_count; i++) {
if (cache.entries[i].data) {
free(cache.entries[i].data);
}
}
free(cache.entries);
memset(&cache, 0, sizeof(cache));
}
/**
* 使用缓存读取块(优化版,支持预读)
*/
int blk_cache_read(struct blkdev *bdev, unsigned long block_num, void *buf, long len)
{
if (!bdev || !buf || len <= 0 || len > BLOCK_SIZE) {
return -1;
}
cache_stats.total_reads++;
// 查找缓存项
struct blk_cache_entry *entry = find_cache_entry(bdev, block_num);
if (entry) {
// 缓存命中
memcpy(buf, entry->data, len);
cache_stats.cache_hits++;
move_to_head(entry);
// 顺序访问预读:预读接下来的块
add_prefetch_request(bdev, block_num + 1, 1);
add_prefetch_request(bdev, block_num + 2, 0);
process_prefetch_requests();
return len;
}
cache_stats.cache_misses++;
// 缓存未命中,分配新项
entry = allocate_cache_entry();
if (!entry) {
// 缓存分配失败,直接从磁盘读取
return read_block(bdev, block_num, 0, buf, len);
}
// 从磁盘读取数据到缓存
if (read_block(bdev, block_num, 0, entry->data, BLOCK_SIZE) != BLOCK_SIZE) {
// 读取失败,标记为无效
entry->valid = 0;
return -1;
}
// 设置缓存项
entry->bdev = bdev;
entry->block_num = block_num;
// 复制数据到用户缓冲区
memcpy(buf, entry->data, len);
// 触发预读
process_prefetch_requests();
return len;
}
/**
* 使用缓存写入块(优化版)
*/
int blk_cache_write(struct blkdev *bdev, unsigned long block_num, void *buf, long len)
{
if (!bdev || !buf || len <= 0 || len > BLOCK_SIZE) {
return -1;
}
cache_stats.total_writes++;
// 查找缓存项
struct blk_cache_entry *entry = find_cache_entry(bdev, block_num);
if (entry) {
// 缓存命中
memcpy(entry->data, buf, len);
entry->dirty = 1;
move_to_head(entry);
return len;
}
// 缓存未命中,分配新项
entry = allocate_cache_entry();
if (!entry) {
// 缓存分配失败,直接写入磁盘
return write_block(bdev, block_num, 0, buf, len);
}
// 设置缓存项
entry->bdev = bdev;
entry->block_num = block_num;
// 先读取整个块(保持一致性)
if (read_block(bdev, block_num, 0, entry->data, BLOCK_SIZE) != BLOCK_SIZE) {
entry->valid = 0;
return -1;
}
// 写入新数据
memcpy(entry->data, buf, len);
entry->dirty = 1;
return len;
}
/**
* 同步所有脏块(优化版)
*/
int blk_cache_sync(struct blkdev *bdev)
{
int sync_count = 0;
for (int i = 0; i < cache.entry_count; i++) {
struct blk_cache_entry *entry = &cache.entries[i];
if (entry->valid && entry->dirty) {
// 如果指定了设备,只同步该设备的块
if (bdev && entry->bdev != bdev) {
continue;
}
// 写入磁盘
if (write_block(entry->bdev, entry->block_num, 0, entry->data, BLOCK_SIZE) == BLOCK_SIZE) {
entry->dirty = 0;
cache_stats.dirty_writes++;
sync_count++;
}
}
}
return sync_count;
}