-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlsm_simulation.c
364 lines (315 loc) · 9.06 KB
/
lsm_simulation.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
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
#include <string.h>
#include <stdlib.h>
#include <cmath>
#include "lsm_simulation.h"
#include "iteration.h"
using namespace std;
extern uint64_t write_cnt;
extern uint32_t last_compaction_cnt;
extern monitor lsm_monitor;
LSM *tlsm;
LSM* lsm_init(char t, uint32_t level_num, uint32_t size_factor, uint32_t blocknum, uint64_t range){
LSM *lsm=(LSM*)malloc(sizeof(LSM));
if(level_num && size_factor){
lsm->level=level_num;
lsm->sizefactor=size_factor;
lsm->now=0;
lsm->max=level_num;
}
else{
if(level_num){
lsm->level=level_num;
lsm->sizefactor=get_size_factor(level_num, blocknum);
lsm->now=0;
lsm->max=level_num;
}
else{
lsm->sizefactor=size_factor;
lsm->level=get_level(size_factor, blocknum);
lsm->now=0;
lsm->max=lsm->level;
}
}
lsm->max_block_num=blocknum;
lsm->valid_block_num=blocknum;
lsm->array=(level*)malloc(sizeof(level) * lsm->max);
for(uint32_t i=0; i<lsm->max; i++){
level_init(&lsm->array[i], i+1, lsm);
}
lsm->lba_run_id=(uint64_t*)calloc(sizeof(uint64_t), range+1);
lsm->range=range+1;
lsm->buffer=(block*)malloc(sizeof(block));
block_init(lsm->buffer);
lsm->last_level_valid=0;
tlsm=lsm;
return lsm;
}
run lsm_level_to_run(LSM *lsm, level *lev, uint32_t idx, iter **iter_set, uint32_t iter_num, uint32_t target_run_size){
run new_run;
run_init(&new_run, target_run_size?target_run_size:pow(lsm->sizefactor, idx+1));
bool *empty=(bool*)calloc(iter_num, sizeof(bool));
uint32_t t_run=0, insert_idx=0;
uint32_t t_lba=0, *temp, temp_idx=0;
if((idx==lsm->max-2 &&tlsm->valid_block_num < (new_run.max/lsm->sizefactor)) ||
(idx!=lsm->max-2 && tlsm->valid_block_num<(new_run.max))){
lsm_last_gc(tlsm, NULL, UINT32_MAX, new_run.max-tlsm->valid_block_num, false);
}
while(!check_done(empty, iter_num)){
t_lba=UINT32_MAX;
for(uint32_t i=0; i<iter_num; i++){
if(empty[i]) continue;
temp=(uint32_t*)iter_pick(iter_set[i]);
if(t_lba> *temp){
t_lba= *temp;
t_run=i;
}
}
//remove same lba
for(uint32_t i=0; i<iter_num; i++){
if(empty[i]) continue;
if(i==t_run) continue;
temp=(uint32_t*)iter_pick(iter_set[i]);
if(t_lba == *temp){
iter_move(iter_set[i]);
if(!iter_pick(iter_set[i])){
empty[i]=true;
}
}
}
run_insert_value(&new_run, t_lba, insert_idx++, &write_cnt, lsm->lba_run_id);
iter_move(iter_set[t_run]);
if(!iter_pick(iter_set[t_run])){
empty[t_run]=true;
}
}
new_run.now=insert_idx/LPPB+(insert_idx%LPPB?1:0);
free(empty);
return new_run;
}
void lsm_compaction(LSM *lsm, uint32_t idx){
//static int cnt=0;
//printf("\ncompaction cnt %d %u -> %u (M:V - %u:%u)\n",cnt++, idx, idx+1, tlsm->max_block_num, tlsm->valid_block_num);
lsm->gc_cnt=0;
uint64_t before_write=write_cnt;
lsm_monitor.level_read_num[idx]+=level_data(&lsm->array[idx]);
iter **iter_set=(iter**)malloc(sizeof(iter*) * lsm->sizefactor);
for(uint32_t i=0; i<lsm->sizefactor; i++){
iter_set[i]=iter_init(0, lsm->array[idx].array[i].now, &lsm->array[idx].array[i], run_get_value_from_idx);
}
run new_run=lsm_level_to_run(lsm, NULL, idx, iter_set, lsm->sizefactor, 0);
run *t=&lsm->array[idx+1].array[lsm->array[idx+1].now];
run_free(t);
run *now;
uint32_t i;
//lsm_print_run(lsm, idx);
for_each_target_max(&lsm->array[idx], i, now){
run_free(now);
}
free(lsm->array[idx].array);
level_init(&lsm->array[idx], idx+1, lsm);
for(uint32_t i=0; i<lsm->sizefactor; i++){
free(iter_set[i]);
}
free(iter_set);
level_insert_run(&lsm->array[idx+1], new_run);
lsm_monitor.level_write_num[idx]+=write_cnt-before_write-lsm->gc_cnt;
//lsm_print_run(lsm, idx+1);
}
int lba_comp(const void *_a, const void *_b){
uint32_t a=*(uint32_t*)_a;
uint32_t b=*(uint32_t*)_b;
return a-b;
}
int lsm_insert(LSM* lsm, uint32_t lba){
lsm->buffer->array[lsm->buffer->now++]=lba;
write_cnt++;
lsm_monitor.buffer_write++;
if(!full_check_target(lsm->buffer)) return 1;
qsort(lsm->buffer->array, lsm->buffer->now, sizeof(uint32_t), lba_comp);
z_level_insert_block(&lsm->array[0], *lsm->buffer);
lsm->array[0].now++;
for(uint32_t i=0; i<lsm->max-1; i++){
if(i==lsm->max-2 && full_check_target(&lsm->array[i])
&& full_check_target(&lsm->array[i+1])){
// printf("compaction %d -> %d\n", i, i+1);
last_compaction_cnt++;
lsm_last_compaction(lsm, &lsm->array[i], i);
continue;
}
if(full_check_target(&lsm->array[i])){
// printf("compaction %d -> %d\n", i, i+1);
lsm_compaction(lsm, i);
}
else break;
}
uint64_t temp_sum=lsm_monitor.buffer_write;
for(uint32_t i=0; i<=lsm->max; i++){
temp_sum+=lsm_monitor.level_write_num[i];
}
if(write_cnt!=temp_sum){
printf("buffer write:%lu\n", lsm_monitor.buffer_write);
for(uint32_t i=0; i<=lsm->max; i++){
printf("[lev:%u] %lu\n",i,lsm_monitor.level_write_num[i]);
}
abort();
}
free(lsm->buffer);
lsm->buffer=(block*)malloc(sizeof(block));
block_init(lsm->buffer);
return 1;
}
void lsm_print_level(LSM *lsm, uint32_t target_level){
run *now;
uint32_t idx=0;
static uint32_t idx_t=1;
for_each_target_now(&lsm->array[target_level], idx, now){
block *b;
uint32_t idx2=0;
for_each_target_now(now, idx2, b){
//printf("block #%d\n0 :",idx2);
for(uint32_t j=1; j<=b->max; j++){
printf("%u %u\n",idx_t++, b->array[j-1]);
}
}
}
}
void lsm_print_shape(LSM *lsm){
for(int i=0; i<lsm->level; i++){//level
uint32_t block_sum=0;
for(int j=0; j<lsm->array[i].max; j++){ //run
block_sum+=lsm->array[i].array[j].max;
/*
for(int k=0; k<lsm->array[i].max; k++){//block
}
*/
}
printf("[%d] -> max_block %u, max_page %lu\n",i, block_sum, (uint64_t)block_sum*LPPB);
}
}
void lsm_print_run(LSM *lsm, uint32_t target_level){
run *now;
uint32_t idx=0;
for_each_target_now(&lsm->array[target_level], idx, now){
printf("[%u, %u] now:%u max:%u used_list:", target_level, idx, now->now, now->max);
block *b;
uint32_t idx2=0;
for_each_target_now(now, idx2, b){
//printf("%u,", b->used);
printf("%u-%u,", b->used, b->now);
}
printf("\n");
}
}
void lsm_print_summary(run *t=NULL){
run *now;
uint32_t idx=0;
LSM *lsm=tlsm;
uint32_t total_block=0;
for(uint32_t i=0; i<lsm->max; i++){
printf("level:%u\n",i);
for_each_target_now(&lsm->array[i], idx, now){
printf("[%u, %u]id:%lu now:%u max:%u\n", i, idx, now->id, now->now, now->max);
total_block+=now->now;
}
}
if(t){
now=t;
printf("[temp]id:%lu now:%u max:%u\n", now->id, now->now, now->max);
total_block+=now->now;
}
printf("total block:%u max block:%u valid_block:%u\n", total_block, lsm->max_block_num, lsm->valid_block_num);
if(lsm->max_block_num != total_block+lsm->valid_block_num){
printf("break!\n");
abort();
}
}
void lsm_free(LSM *lsm, uint64_t t){
for(uint32_t idx=0; idx<lsm->max; idx++){
uint32_t i=0;
run *now;
for_each_target_max(&lsm->array[idx], i, now){
free(now->array);
}
free(lsm->array[idx].array);
}
free(lsm->array);
free(lsm->buffer);
printf("lev read write WAF BF+PIN\n");
printf("0 0 %lu(%lu) 1\n", t, lsm_monitor.buffer_write);
for(uint32_t i=0; i<=lsm->max; i++){
printf("%u %lu %lu %.3lf %.3lf\n", i+1,lsm_monitor.level_read_num[i], lsm_monitor.level_write_num[i],
(double) lsm_monitor.level_write_num[i]/t,
(double) lsm_monitor.level_write_num[i]/t/PACK
);
}
printf("invalid ratio %.2lf\n", (double)lsm_monitor.last_run_invalid/(lsm_monitor.last_run_invalid+lsm_monitor.last_run_valid));
printf("last level merge run\n");
for(uint32_t i=0; i<=lsm->sizefactor; i++){
printf("%u %lu\n", i, lsm_monitor.last_level_merg_run_num[i]);
}
}
void run_init(run *r, uint32_t blocknum){
static uint32_t cnt=0;
r->now=0;
r->max=blocknum;
r->id=cnt++;
r->array=(block*)malloc(sizeof(block)* blocknum);
for(uint32_t i=0; i<r->max; i++){
block_init(&r->array[i]);
}
}
void run_free(run *r){
for(uint32_t i=0; i<r->now; i++){
if(r->array[i].used){
printf("should be invalidate before free %s:%d\n", __FILE__, __LINE__);
abort();
}
}
free((r)->array);
}
void level_init(level *lev, uint32_t idx, LSM *lsm){
lev->now=0;
lev->max=lsm->sizefactor;
lev->array=(run*)malloc(sizeof(run)*lev->max);
for(uint32_t i=0; i<lev->max; i++){
run_init(&lev->array[i], pow(lsm->sizefactor, idx-1));
}
}
void run_insert_value(run *r, uint32_t lba, uint32_t idx, uint64_t *write_monitor_cnt, uint64_t* lba_run_id){
uint32_t block_n=idx/LPPB;
uint32_t offset=idx%LPPB;
lba_run_id[lba]=r->id;
if(offset % 64==0){
r->array[block_n].guard[offset/64]=lba;
}
if(!r->array[block_n].used){
r->array[block_n].used=true;
tlsm->valid_block_num--;
if(tlsm->valid_block_num < 0){
lsm_print_summary(NULL);
printf("valid block can't be minus %s:%d\n", __FILE__, __LINE__);
abort();
}
tlsm->now_block_num++;
}
r->array[block_n].array[offset]=lba;
r->array[block_n].now++;
(*write_monitor_cnt)++;
}
void *run_get_value_from_idx(void *r, uint32_t idx){
run *tr=(run*)r;
uint32_t block_n=idx/LPPB;
uint32_t offset=idx%LPPB;
if(idx>=tr->now * LPPB){
return NULL;
}
uint32_t * target=&tr->array[block_n].array[offset];
if(*target==0){
return NULL;
}
return target;
}
void z_level_insert_block(level *l, block b){
tlsm->valid_block_num--;
(run_insert_block(&(l)->array[(l)->now],(b)));
}