-
Notifications
You must be signed in to change notification settings - Fork 2
/
blocks.c
225 lines (180 loc) · 5.91 KB
/
blocks.c
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
// Memory is statically allocated at startup as N blocks
// When full we just drop the oldest block
//
// When using disk we write the dropped blocks to disk and truncate the oldest when out of space
//
#include "mrcache.h"
#include "blocks.h"
#include "hashtable.h"
#include <fcntl.h>
#include <unistd.h>
//#include <sys/types.h>
#include <sys/stat.h>
static int num_blocks;
static int min_block;
static uint32_t blocks_bitlen;
static uint32_t blocks_bytelen;
static void *base;
static void *cur;
static uint64_t cur_block;
static uint32_t cur_block_size;
static bool full;
static int *items_in_block;
static int *fsblock_fds;
static int fsblock_index;
static int fsblock_size;
static int fsblock_min_block;
static int num_fs_blocks;
void blocks_init(config_t *cfg) {
blocks_bitlen = 24;
blocks_bytelen = 0x1ull << blocks_bitlen;
num_blocks = cfg->max_memory / cfg->block_size;
base = malloc( cfg->max_memory * 1024 * 1024 );
min_block = 1;
cur = base;
cur_block = 1;
cur_block_size = 0;
items_in_block = calloc( num_blocks, sizeof(int) );
if ( !base ) {
fprintf(stderr, "Failed to allocate memory of %d mb\n", num_blocks);
exit(EXIT_FAILURE);
}
/*
if ( config.disk_size ) {
num_fs_blocks = (config.disk_size*1024) / FSBLOCK_SIZE;
fsblock_fds = calloc( num_fs_blocks, sizeof(int) );
mkdir( "fsblocks", 0700 );
for ( int i = 0; i < num_fs_blocks; i++ ) {
char fn[128];
sprintf( fn, "fsblocks/fsblock.%d", i );
int fd = open(fn, O_RDWR | O_CREAT | O_TRUNC | O_APPEND, 0600);
if (fd < 0) { exit(EXIT_FAILURE); }
fsblock_fds[i] = fd;
}
fsblock_index = 0;
fsblock_size = 0;
fsblock_min_block = min_block;
}
*/
}
// Allocate memory of sz bytes
uint64_t blocks_alloc( int sz ) {
// If the current block cannot hold the new item
if ( (cur_block_size+sz) > ((blocks_bytelen)-1) ) { // TODO make this a constant. No variable block size
if ( cur_block >= num_blocks ) {
blocks_lru();
full = true; // Full is set once ever
}
cur_block += 1;
//items_in_block[ cur_block%num_blocks ] = 0;
cur = base + ((cur_block%num_blocks)<<blocks_bitlen);
cur_block_size = 0;
}
cur += sz;
cur_block_size += sz;
items_in_block[ cur_block%num_blocks ] += 1; // So we can tell the hashtable how many items were dropped on LRU
return (cur_block << BLOCK_SHIFT) | (cur_block_size-sz);
}
void blocks_lru() {
int i = min_block%num_blocks;
int n = items_in_block[ i ];
if ( config.disk_size ) {
blocks_fs_write(i);
} else {
fsblock_min_block = min_block+1;
}
ht_decrement(mrq_ht, n); // Tell the hashtable how many items were dropped TODO what about items on disk?
items_in_block[ i ] = 0;
min_block += 1; // Items dropped are still in the hashtable. Min block tells us if they are still valid in memory or not
}
void *blocks_translate( uint64_t blockAddr ) {
uint64_t blk = GET_BLOCK(blockAddr);
if ( blk < min_block ) return NULL;
uint32_t off = blockAddr & BLOCK_BITMASK;
return (base + ((blk%num_blocks)<<blocks_bitlen)) + off;
}
bool blocks_isvalid( uint64_t blockAddr ) {
uint64_t blk = GET_BLOCK(blockAddr);
if ( blk < min_block ) return false;
return true;
}
bool blocks_is_near_lru( uint64_t blockAddr ) {
if ( !full ) return false;
uint64_t blk = GET_BLOCK(blockAddr);
if ( blk < (min_block + 4) ) return true;
return false;
}
void blocks_decrement( uint64_t blockAddr ) {
uint64_t blk = GET_BLOCK(blockAddr);
items_in_block[ blk%num_blocks ] -= 1;
}
uint32_t blocks_num( uint64_t blk ) {
return items_in_block[ blk%num_blocks ];
}
bool blocks_is_invalid( uint64_t blockAddr ) {
uint64_t blk = GET_BLOCK(blockAddr);
if ( blk < min_block ) return true;
return false;
}
// Only call this if you know its not mem
bool blocks_is_disk( uint64_t blockAddr ) {
uint64_t blk = GET_BLOCK(blockAddr);
//if ( blk >= fsblock_min_block ) return true;
if ( blk < min_block && blk >= fsblock_min_block ) return true;
return false;
}
bool blocks_is_mem( uint64_t blockAddr ) {
uint64_t blk = GET_BLOCK(blockAddr);
if ( blk >= min_block ) return true;
return false;
}
bool blocks_is_lru( uint64_t blockAddr ) {
uint64_t blk = GET_BLOCK(blockAddr);
//if ( blk < min_block && blk > ) return true; // TODO rollover?
if ( blk < fsblock_min_block ) return true; // When not using disk fsblock min needs to be equal to min_block
return false;
}
void blocks_on_write_done( void *iov, int res ) {
free(((struct iovec*)iov)->iov_base);
free(iov);
}
void blocks_fs_write( int blk ) {
char *p = malloc(blocks_bytelen);
memcpy(p, base + ((blk%num_blocks)<<blocks_bitlen), blocks_bytelen);
struct iovec *iov = malloc(sizeof(struct iovec));
iov->iov_base = p;
iov->iov_len = config.block_size*1024*1024;
if ( fsblock_min_block == -1 ) {
fsblock_min_block = blk;
}
fsblock_size += 1;
int fd = fsblock_fds[fsblock_index];
//mr_writevcb( config.loop, fd, iov, 1, (void*)iov, blocks_on_write_done );
//mr_flush(config.loop);
if ( fsblock_size == 64 ) {
fsblock_index = (fsblock_index+1)%num_fs_blocks;
fsblock_size = 0;
fsblock_min_block += 64;
int rc = ftruncate( fsblock_fds[fsblock_index], 0 );// TODO test return code?
}
}
void blocks_on_read_done( void *ptr, int res ) {
//disk_item_t *di = (disk_item_t*)ptr;
//getq_item_t *qi = di->qi;
//qi->reads_done += 1;
//conn_process_queue( di->conn );
//free(((struct iovec*)iov)->iov_base);
//free(iov);
}
void blocks_fs_read( uint64_t blockAddr, disk_item_t *di ) {
uint64_t blk = GET_BLOCK(blockAddr);
int fsblk = (blk/64)%num_fs_blocks;
int fd = fsblock_fds[fsblk];
int sz = 32 * 1024;
di->iov.iov_base = malloc( sz );
di->iov.iov_len = sz;
off_t off = ((blk-1)*blocks_bytelen) + (blockAddr & BLOCK_BITMASK);
// Passing di works as the iov is the first part of the struct
//mr_readvcb( config.loop, fd, (struct iovec*)di, 1, off, di, blocks_on_read_done );
//mr_flush(config.loop);
}