-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssd_block.cpp
215 lines (180 loc) · 5.1 KB
/
ssd_block.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
/* Copyright 2009, 2010 Brendan Tauras */
/* ssd_block.cpp is part of FlashSim. */
/* FlashSim is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version. */
/* FlashSim is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. */
/* You should have received a copy of the GNU General Public License
* along with FlashSim. If not, see <http://www.gnu.org/licenses/>. */
/****************************************************************************/
/* Block class
* Brendan Tauras 2009-10-26
*
* The block is the data storage hardware unit where erases are implemented.
* Blocks maintain wear statistics for the FTL. */
#include <new>
#include <assert.h>
#include <stdio.h>
#include "ssd.h"
using namespace ssd;
Block::Block(const Plane &parent, uint block_size, ulong erases_remaining, double erase_delay):
size(block_size),
/* use a const pointer (Page * const data) to use as an array
* but like a reference, we cannot reseat the pointer */
data((Page *) malloc(block_size * sizeof(Page))),
parent(parent),
pages_valid(0),
pages_invalid(0),
state(FREE),
/* set erases remaining to BLOCK_ERASES to match Block constructor args
* in Plane class
* this is the cheap implementation but can change to pass through classes */
/* erases_remaining(BLOCK_ERASES), */
erases_remaining(erases_remaining),
/* assume hardware created at time 0 and had an implied free erasure */
last_erase_time(0.0),
erase_delay(erase_delay)
{
uint i;
if(erase_delay < 0.0)
{
fprintf(stderr, "Block warning: %s: constructor received negative erase delay value\n\tsetting erase delay to 0.0\n", __func__);
erase_delay = 0.0;
}
/* new cannot initialize an array with constructor args so
* malloc the array
* then use placement new to call the constructor for each element
* chose an array over container class so we don't have to rely on anything
* i.e. STL's std::vector */
/* array allocated in initializer list:
* data = (Page *) malloc(size * sizeof(Page)); */
if(data == NULL){
fprintf(stderr, "Block error: %s: constructor unable to allocate Page data\n", __func__);
exit(MEM_ERR);
}
for(i = 0; i < size; i++)
(void) new (&data[i]) Page(*this, PAGE_READ_DELAY, PAGE_WRITE_DELAY);
return;
}
Block::~Block(void)
{
assert(data != NULL);
uint i;
/* call destructor for each Page array element
* since we used malloc and placement new */
for(i = 0; i < size; i++)
data[i].~Page();
free(data);
return;
}
enum status Block::read(Event &event)
{
assert(data != NULL);
return data[event.get_address().page]._read(event);
}
enum status Block::write(Event &event)
{
assert(data != NULL);
enum status ret = data[event.get_address().page]._write(event);
if(ret == SUCCESS)
{
pages_valid++;
state = ACTIVE;
}
return ret;
}
/* updates Event time_taken
* sets Page statuses to EMPTY
* updates last_erase_time and erases_remaining
* returns 1 for success, 0 for failure */
enum status Block::_erase(Event &event)
{
assert(data != NULL && erase_delay >= 0.0);
uint i;
if(erases_remaining < 1)
{
fprintf(stderr, "Block error: %s: No erases remaining when attempting to erase\n", __func__);
return FAILURE;
}
for(i = 0; i < size; i++)
data[i].set_state(EMPTY);
event.incr_time_taken(erase_delay);
last_erase_time = event.get_start_time() + event.get_time_taken();
erases_remaining--;
pages_valid = 0;
pages_invalid = 0;
state = FREE;
return SUCCESS;
}
const Plane &Block::get_parent(void) const
{
return parent;
}
ssd::uint Block::get_pages_valid(void) const
{
return pages_valid;
}
ssd::uint Block::get_pages_invalid(void) const
{
return pages_invalid;
}
enum block_state Block::get_state(void) const
{
return state;
}
enum page_state Block::get_state(uint page) const
{
assert(data != NULL && page < size);
return data[page].get_state();
}
enum page_state Block::get_state(const Address &address) const
{
assert(data != NULL && address.page < size && address.valid >= BLOCK);
return data[address.page].get_state();
}
double Block::get_last_erase_time(void) const
{
return last_erase_time;
}
ssd::ulong Block::get_erases_remaining(void) const
{
return erases_remaining;
}
ssd::uint Block::get_size(void) const
{
return size;
}
void Block::invalidate_page(uint page)
{
assert(page < size);
data[page].set_state(INVALID);
pages_invalid++;
/* update block state */
if(pages_invalid >= size)
state = INACTIVE;
else if(pages_valid > 0 || pages_invalid > 0)
state = ACTIVE;
else
state = FREE;
return;
}
/* method to find the next usable (empty) page in this block
* method is called by write and erase methods and in Plane::get_next_page() */
enum status Block::get_next_page(Address &address) const
{
uint i;
for(i = 0; i < size; i++)
{
if(data[i].get_state() == EMPTY)
{
address.page = i;
address.valid = PAGE;
return SUCCESS;
}
}
return FAILURE;
}