forked from MatiasBjorling/flashsim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathssd_bm.cpp
327 lines (261 loc) · 7.64 KB
/
ssd_bm.cpp
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
/* Copyright 2011 Matias Bjørling */
/* Block Management
*
* This class handle allocation of block pools for the FTL
* algorithms.
*/
#include <new>
#include <assert.h>
#include <stdio.h>
#include <vector>
#include <stdexcept>
#include <algorithm>
#include <queue>
#include "ssd.h"
using namespace ssd;
Block_manager::Block_manager(FtlParent *ftl) : ftl(ftl)
{
/*
* Configuration of blocks.
* User-space is the number of blocks minus the
* requirements for map directory.
*/
max_blocks = NUMBER_OF_ADDRESSABLE_BLOCKS;
max_log_blocks = max_blocks;
if (FTL_IMPLEMENTATION == IMPL_FAST)
max_log_blocks = FAST_LOG_PAGE_LIMIT;
// Block-based map lookup simulation
max_map_pages = MAP_DIRECTORY_SIZE * BLOCK_SIZE;
directoryCurrentPage = 0;
num_insert_events = 0;
data_active = 0;
log_active = 0;
current_writing_block = -2;
out_of_blocks = false;
active_cost.reserve(NUMBER_OF_ADDRESSABLE_BLOCKS);
}
Block_manager::~Block_manager(void)
{
return;
}
void Block_manager::cost_insert(Block *b)
{
active_cost.push_back(b);
}
void Block_manager::instance_initialize(FtlParent *ftl)
{
Block_manager::inst = new Block_manager(ftl);
}
Block_manager *Block_manager::instance()
{
return Block_manager::inst;
}
/*
* Retrieves a page using either simple approach (when not all
* pages have been written or the complex that retrieves
* it from a free page list.
*/
void Block_manager::get_page_block(Address &address, Event &event)
{
// We need separate queues for each plane? communication channel? communication channel is at the per die level at the moment. i.e. each LUN is a die.
if (simpleCurrentFree < max_blocks*BLOCK_SIZE)
{
address.set_linear_address(simpleCurrentFree, BLOCK);
current_writing_block = simpleCurrentFree;
simpleCurrentFree += BLOCK_SIZE;
}
else
{
if (free_list.size() <= 1 && !out_of_blocks)
{
out_of_blocks = true;
insert_events(event);
}
assert(free_list.size() != 0);
address.set_linear_address(free_list.front()->get_physical_address(), BLOCK);
current_writing_block = free_list.front()->get_physical_address();
free_list.erase(free_list.begin());
out_of_blocks = false;
}
}
Address Block_manager::get_free_block(Event &event)
{
return get_free_block(DATA, event);
}
/*
* Handles block manager statistics when changing a
* block to a data block from a log block or vice versa.
*/
void Block_manager::promote_block(block_type to_type)
{
if (to_type == DATA)
{
data_active++;
log_active--;
}
else if (to_type == LOG)
{
log_active++;
data_active--;
}
}
/*
* Returns true if there are no space left for additional log pages.
*/
bool Block_manager::is_log_full()
{
return log_active == max_log_blocks;
}
void Block_manager::print_statistics()
{
printf("-----------------\n");
printf("Block Statistics:\n");
printf("-----------------\n");
printf("Log blocks: %lu\n", log_active);
printf("Data blocks: %lu\n", data_active);
printf("Free blocks: %lu\n", (max_blocks - (simpleCurrentFree/BLOCK_SIZE)) + free_list.size());
printf("Invalid blocks: %lu\n", invalid_list.size());
printf("Free2 blocks: %lu\n", (unsigned long int)invalid_list.size() + (unsigned long int)log_active + (unsigned long int)data_active - (unsigned long int)free_list.size());
printf("-----------------\n");
}
void Block_manager::invalidate(Address address, block_type type)
{
invalid_list.push_back(ftl->get_block_pointer(address));
switch (type)
{
case DATA:
data_active--;
break;
case LOG:
log_active--;
break;
case LOG_SEQ:
break;
}
}
/*
* Insert erase events into the event stream.
* The strategy is to clean up all invalid pages instantly.
*/
void Block_manager::insert_events(Event &event)
{
// Calculate if GC should be activated.
float used = (int)invalid_list.size() + (int)log_active + (int)data_active - (int)free_list.size();
float total = NUMBER_OF_ADDRESSABLE_BLOCKS;
float ratio = used/total;
if (ratio < 0.90) // Magic number
return;
uint num_to_erase = 5; // More Magic!
//printf("%i %i %i\n", invalid_list.size(), log_active, data_active);
// First step and least expensive is to go though invalid list. (Only used by FAST)
while (num_to_erase != 0 && invalid_list.size() != 0)
{
Event erase_event = Event(ERASE, event.get_logical_address(), 1, event.get_start_time());
erase_event.set_address(Address(invalid_list.back()->get_physical_address(), BLOCK));
if (ftl->controller.issue(erase_event) == FAILURE) { assert(false);}
event.incr_time_taken(erase_event.get_time_taken());
free_list.push_back(invalid_list.back());
invalid_list.pop_back();
num_to_erase--;
ftl->controller.stats.numFTLErase++;
}
num_insert_events++;
if (FTL_IMPLEMENTATION == IMPL_DFTL || FTL_IMPLEMENTATION == IMPL_BIMODAL)
{
ActiveByCost::iterator it = active_cost.get<1>().end();
--it;
while (num_to_erase != 0 && (*it)->get_pages_invalid() > 0 && (*it)->get_pages_valid() == BLOCK_SIZE)
{
if (current_writing_block != (*it)->physical_address)
{
//printf("erase p: %p phy: %li ratio: %i num: %i\n", (*it), (*it)->physical_address, (*it)->get_pages_invalid(), num_to_erase);
Block *blockErase = (*it);
// Let the FTL handle cleanup of the block.
ftl->cleanup_block(event, blockErase);
// Create erase event and attach to current event queue.
Event erase_event = Event(ERASE, event.get_logical_address(), 1, event.get_start_time());
erase_event.set_address(Address(blockErase->get_physical_address(), BLOCK));
// Execute erase
if (ftl->controller.issue(erase_event) == FAILURE) { assert(false); }
free_list.push_back(blockErase);
event.incr_time_taken(erase_event.get_time_taken());
ftl->controller.stats.numFTLErase++;
}
it = active_cost.get<1>().end();
--it;
if (current_writing_block == (*it)->physical_address)
--it;
num_to_erase--;
}
}
}
Address Block_manager::get_free_block(block_type type, Event &event)
{
Address address;
get_page_block(address, event);
switch (type)
{
case DATA:
ftl->controller.get_block_pointer(address)->set_block_type(DATA);
data_active++;
break;
case LOG:
if (log_active > max_log_blocks)
throw std::bad_alloc();
ftl->controller.get_block_pointer(address)->set_block_type(LOG);
log_active++;
break;
default:
break;
}
return address;
}
void Block_manager::print_cost_status()
{
ActiveByCost::iterator it = active_cost.get<1>().begin();
for (uint i=0;i<10;i++) //SSD_SIZE*PACKAGE_SIZE*DIE_SIZE*PLANE_SIZE
{
printf("%li %i %i\n", (*it)->physical_address, (*it)->get_pages_valid(), (*it)->get_pages_invalid());
++it;
}
printf("end:::\n");
it = active_cost.get<1>().end();
--it;
for (uint i=0;i<10;i++) //SSD_SIZE*PACKAGE_SIZE*DIE_SIZE*PLANE_SIZE
{
printf("%li %i %i\n", (*it)->physical_address, (*it)->get_pages_valid(), (*it)->get_pages_invalid());
--it;
}
}
void Block_manager::erase_and_invalidate(Event &event, Address &address, block_type btype)
{
Event erase_event = Event(ERASE, event.get_logical_address(), 1, event.get_start_time()+event.get_time_taken());
erase_event.set_address(address);
if (ftl->controller.issue(erase_event) == FAILURE) { assert(false);}
free_list.push_back(ftl->get_block_pointer(address));
switch (btype)
{
case DATA:
data_active--;
break;
case LOG:
log_active--;
break;
case LOG_SEQ:
break;
}
event.incr_time_taken(erase_event.get_time_taken());
ftl->controller.stats.numFTLErase++;
}
int Block_manager::get_num_free_blocks()
{
if (simpleCurrentFree < max_blocks*BLOCK_SIZE)
return (simpleCurrentFree / BLOCK_SIZE) + free_list.size();
else
return free_list.size();
}
void Block_manager::update_block(Block * b)
{
std::size_t pos = (b->physical_address / BLOCK_SIZE);
active_cost.replace(active_cost.begin()+pos, b);
}