-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLRUCache_PrefetchIntra.h
More file actions
249 lines (230 loc) · 6.07 KB
/
LRUCache_PrefetchIntra.h
File metadata and controls
249 lines (230 loc) · 6.07 KB
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
#include <iostream>
#include<stdint.h>
#include <unordered_map>
#include <vector>
using namespace std;
struct SplitInfo
{
unsigned split_blockid;
SplitInfo(){ split_blockid = 0; }
};
const unsigned constShardcount = 8;
vector<int64_t>List_offset;
vector<vector<SplitInfo>>splitInfo(constShardcount);
vector<vector<unsigned>>blockEndpoint;
struct AIOReadInfo
{
int64_t readlength;
int64_t readoffset;
int64_t listlength;
int64_t offsetForenums;
int64_t memoffset;
int64_t curSendpos;
int64_t curReadpos;
uint8_t usedfreq;
uint8_t *list_data;
uint32_t termid;
uint32_t shardid;
uint32_t readblocksize;
};
vector<int64_t>READ_BLOCK_options = { 4 * 1024, 8 * 1024, 16 * 1024, 32 * 1024, 64 * 1024 };
const uint64_t DISK_BLOCK = 4096;
struct Node{
AIOReadInfo aiodata;
Node*prev, *next;
};
int64_t CACHE_SIZE = 1024 * 1024;
class LRUCache{
public:
LRUCache();
~LRUCache();
Node* Put(unsigned shard, unsigned key);
Node* Get(unsigned shard, unsigned key, bool& flag);
Node* Put_Prefetch(unsigned shard, unsigned key);
Node* Get_Prefetch(unsigned shard, unsigned key, bool& flag);
void print(unsigned shardid);
uint64_t hit_size;
uint64_t miss_size;
uint64_t hit_count;
uint64_t miss_count;
void attach(Node *node);
void detach(Node *node);
AIOReadInfo calAioreadinfo(unsigned shard, unsigned term);
vector<unordered_map<unsigned, Node*>>hashmap_;
Node*head_, *tail_;
int64_t sumBytes;
};
LRUCache::LRUCache()
{
hashmap_.resize(constShardcount);
miss_size = 0; hit_size = 0;
miss_count = 0; hit_count = 0;
head_ = new Node;
tail_ = new Node;
head_->prev = NULL;
head_->next = tail_;
tail_->prev = head_;
tail_->next = NULL;
sumBytes = 0;
}
LRUCache::~LRUCache()
{
delete head_;
delete tail_;
}
int64_t cal_properReadBlockSize(int64_t length)
{
for (unsigned i = 0; i < READ_BLOCK_options.size(); i++)
{
if (length <= READ_BLOCK_options[i])
return READ_BLOCK_options[i];
}
return READ_BLOCK_options[READ_BLOCK_options.size() - 1];
}
AIOReadInfo LRUCache::calAioreadinfo(unsigned shard, unsigned term)
{
AIOReadInfo tmpaio;
tmpaio.termid = term;
tmpaio.shardid = shard;
int64_t startpos = splitInfo[shard][term].split_blockid == 0 ? 0 : blockEndpoint[term][splitInfo[shard][term].split_blockid - 1];
int64_t listlength = shard == constShardcount - 1 ? List_offset[term + 1] - List_offset[term] - startpos : blockEndpoint[term][splitInfo[shard + 1][term].split_blockid] - startpos;
tmpaio.listlength = listlength;
tmpaio.memoffset = 0;
int64_t offset = startpos + List_offset[term];
tmpaio.readoffset = ((int64_t)(offset / DISK_BLOCK))*DISK_BLOCK;
tmpaio.offsetForenums = offset - tmpaio.readoffset;
tmpaio.readblocksize = cal_properReadBlockSize(tmpaio.listlength);
int64_t readlength = ((int64_t)(ceil((double)(listlength + tmpaio.offsetForenums) / tmpaio.readblocksize)))*tmpaio.readblocksize;
tmpaio.readlength = readlength;
tmpaio.curSendpos = -tmpaio.offsetForenums;
tmpaio.usedfreq = 0;
tmpaio.curReadpos = -tmpaio.offsetForenums;
miss_size += tmpaio.listlength;
return tmpaio;
}
Node* LRUCache::Put(unsigned shard, unsigned key)
{
AIOReadInfo tmpaio = calAioreadinfo(shard, key);
if (tmpaio.readlength> CACHE_SIZE)
{
cout << tmpaio.readlength << " " << CACHE_SIZE << " listlength=" << List_offset[key + 1] - List_offset[key] << endl;
cout << "That block overflow!!" << endl;
return NULL;
}
node = tail_->prev;
while (sumBytes + tmpaio.readlength>CACHE_SIZE)
{
if (node == head_){ node = tail_->prev; }
if (node->aiodata.usedfreq > 0){ node = node->prev; continue; }
detach(node);
free(node->aiodata.list_data);
node->aiodata.curReadpos = -node->aiodata.offsetForenums;
sumBytes -= node->aiodata.readlength;
hashmap_[node->aiodata.shardid].erase(node->aiodata.termid);
Node *tmp = node->prev;
delete node;
node = tmp;
}
node = new Node();
posix_memalign((void**)&tmpaio.list_data, DISK_BLOCK, tmpaio.readlength);
node->aiodata = tmpaio;
sumBytes += tmpaio.readlength;
attach(node);
hashmap_[shard][key] = node;
return node;
}
Node* LRUCache::Put_Prefetch(unsigned shard, unsigned key)
{
AIOReadInfo tmpaio = calAioreadinfo(shard, key);
Node *node;
if (tmpaio.readlength> CACHE_SIZE)
{
cout << "That block overflow!!" << endl;
return NULL;
}
node = tail_->prev;
while (sumBytes + tmpaio.readlength>CACHE_SIZE&&node != head_)
{
if (node->aiodata.usedfreq > 0){ node = node->prev; continue; }
detach(node);
free(node->aiodata.list_data);
node->aiodata.curReadpos = -node->aiodata.offsetForenums;
sumBytes -= node->aiodata.readlength;
hashmap_[node->aiodata.shardid].erase(node->aiodata.termid);
Node *tmp = node->prev;
delete node;
node = tmp;
}
if (node == head_)
{
return NULL;
}
node = new Node();
posix_memalign((void**)&tmpaio.list_data, DISK_BLOCK, tmpaio.readlength);
node->aiodata = tmpaio;
sumBytes += tmpaio.readlength;
attach(node);
hashmap_[shard][key] = node;
return node;
}
Node* LRUCache::Get(unsigned shard, unsigned key, bool &flag)
{
Node *node;
unordered_map<unsigned, Node* >::iterator it = hashmap_[shard].find(key);
if (it != hashmap_[shard].end())
{
node = it->second;
flag = true;
hit_count++;
detach(node);
attach(node);
}
else
{
flag = false;
miss_count++;
node = Put(shard, key);
}
return node;
}
Node* LRUCache::Get_Prefetch(unsigned shard, unsigned key, bool &flag)
{
Node *node;
unordered_map<unsigned, Node* >::iterator it = hashmap_[shard].find(key);
if (it != hashmap_[shard].end())
{
node = it->second;
flag = true;
detach(node);
attach(node);
}
else
{
flag = false;
miss_count++;
node = Put_Prefetch(shard, key);
}
return node;
}
void LRUCache::attach(Node *node)
{
node->next = head_->next;
head_->next = node;
node->next->prev = node;
node->prev = head_;
}
void LRUCache::detach(Node *node)
{
node->prev->next = node->next;
node->next->prev = node->prev;
}
void LRUCache::print(unsigned shard)
{
unordered_map<unsigned, Node* >::iterator iter;
int64_t mysumsize = 0;
for (iter = hashmap_[shard].begin(); iter != hashmap_[shard].end(); iter++)
{
mysumsize += iter->second->aiodata.listlength;
}
cout << "sumsize=" << mysumsize << endl;
}