-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhost_zbd.cc
194 lines (166 loc) · 4.23 KB
/
host_zbd.cc
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
#include "host_zbd.h"
Zone::Zone(){
max_capacity_ = SZZONE * SZBLK;
capacity_ = SZZONE * SZBLK;
wp_ = 0;
used_capacity_ = 0;
}
Zone::Zone(int id, BlockManager *blkmgr){
id_ = id;
max_capacity_ = SZZONE * SZBLK;
capacity_ = SZZONE * SZBLK;
wp_ = 0;
used_capacity_ = 0;
blkmgr_ = blkmgr;
}
type_latency Zone::Read(int rd_size){
int64_t latency = ceil((double)rd_size / (double)SZPAGE) * LATENCY_READ;
blkmgr_->AddReadAmount( ceil((double)rd_size / (double)SZPAGE) );
return latency;
}
// Manage the metadata of the Zone
// and transport append command to Device side
type_latency Zone::Write(int data_size){
int64_t s;
int64_t wr_size = data_size;
// padding
if(data_size % SZBLK != 0){
wr_size = (data_size / SZBLK + 1) * SZBLK;
}
if(wr_size % SZBLK != 0 && SHOW_ERR){
std::cout << "Zone Write Failed, Padding size is fault\n";
return -1;
}
// Append data to physical block
// (what I really do is to allocate dynamically physical block to virtual zone)
s = blkmgr_->Append(id_, wp_, wr_size);
if(s == -1){
if(SHOW_ERR)
std::cout << "Zone Write Failed, zoneid:" << id_ << ", wp:" << wp_ << ", data_size:" << data_size << "\n";
return -1;
}
capacity_ -= wr_size;
wp_ += wr_size;
used_capacity_ += data_size;
return s;
}
void Zone::Dummy(){
int s = blkmgr_->Append(id_, wp_, capacity_);
if(s == -1){
if(SHOW_ERR)
std::cout << "Dummy Failed\n";
return;
}
capacity_ = 0;
wp_ = max_capacity_;
}
// Manage the metadata of the Zone
// and transport RESET command to Device side
type_latency Zone::Reset(){
if(SHOW_RESET)
std::cout << "Reset Zone[" << id_
<< "] whose reset hint is " << blkmgr_->GetResetHint(id_)
<< " and migrate " << used_capacity_
<< "bytes(" << used_capacity_ * 100 / (SZZONE * SZBLK) << "\%)\n";
capacity_ = (int64_t)SZZONE * (int64_t)SZBLK;
wp_ = 0;
used_capacity_ = 0;
int64_t latency = blkmgr_->Reset(id_);
return latency;
}
// Manage the metadata of the Zone
int Zone::Delete(int64_t data_size){
if(data_size > used_capacity_){
if(SHOW_ERR){
std::cout << "Zone Deletion Failed, try to delete " << data_size
<< "bytes from zone[" << id_ << "] whose used capacity is " << used_capacity_ << "\n";
}
return -1;
}
if(SHOW_ZONE)
std::cout << "Delete data whose length is " << data_size << " bytes from Zone[" << id_ << "]\n";
used_capacity_ -= data_size;
return 0;
}
bool Zone::IsFull(){
return capacity_ == 0;
}
bool Zone::IsBroken(){
return blkmgr_->GetResetHint(id_) >= EC_LIMIT;
}
int Zone::GetId(){
return id_;
}
int64_t Zone::GetCapacity(){
return capacity_;
}
int64_t Zone::GetWP(){
return wp_;
}
int64_t Zone::GetUsedCapacity(){
return used_capacity_;
}
int64_t Zone::GetMaxCapacity(){
return max_capacity_;
}
ZoneBackend::ZoneBackend(){
if(ENABLE_DYNAMIC_MAPPING)
blkmgr = new BlockManagerDynamic();
else if(WAZONE)
blkmgr = new BlockManagerWazone();
else
blkmgr = new BlockManagerStatic();
zones.resize(NRZONE, nullptr);
for(int i = 0; i < NRZONE; ++i)
zones[i] = new Zone(i, blkmgr);
}
int ZoneBackend::AllocateIOZone(int lifetime, Zone **zone){
for(int i = 0; i < NRZONE; ++i){
if(zones[i]->GetCapacity() != 0){
if(!ENABLE_DYNAMIC_MAPPING && blkmgr->GetResetHint(i) >= EC_LIMIT)
continue;
*zone = zones[i];
return 0;
}
}
*zone = nullptr;
return -1;
}
int64_t ZoneBackend::GetFreeSpace(){
int64_t free = 0;
for(int i = 0; i < NRZONE; ++i)
free += zones[i]->GetCapacity();
return free;
}
int64_t ZoneBackend::GetUsedSpace(){
int64_t used = 0;
for(int i = 0; i < NRZONE; ++i)
used += zones[i]->GetUsedCapacity();
return used;
}
// the size of garbage of full zones
int64_t ZoneBackend::GetReclaimableSpace(){
int64_t reclaimable = 0;
for(int i = 0; i < NRZONE; ++i)
if(zones[i]->IsFull())
reclaimable += (zones[i]->GetMaxCapacity() - zones[i]->GetUsedCapacity());
return reclaimable;
}
int64_t ZoneBackend::GetGarbage(){
int64_t garbage = 0;
for(int i = 0; i < NRZONE; ++i)
garbage += (zones[i]->GetWP() - zones[i]->GetUsedCapacity());
return garbage;
}
int ZoneBackend::GetECMin(){
return blkmgr->GetECMin();
}
int ZoneBackend::GetECMax(){
return blkmgr->GetECMax();
}
int ZoneBackend::GetECMinFree(){
return blkmgr->GetECMinFree();
}
int ZoneBackend::GetResetHint(int zoneid){
return blkmgr->GetResetHint(zoneid);
}