-
Notifications
You must be signed in to change notification settings - Fork 149
/
XrdFileCacheVRead.cc
384 lines (322 loc) · 11.6 KB
/
XrdFileCacheVRead.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
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#include "XrdFileCacheFile.hh"
#include "XrdFileCache.hh"
#include "XrdFileCacheTrace.hh"
#include "XrdFileCacheInfo.hh"
#include "XrdFileCacheStats.hh"
#include "XrdFileCacheIO.hh"
#include "XrdOss/XrdOss.hh"
#include "XrdCl/XrdClDefaultEnv.hh"
#include "XrdCl/XrdClFile.hh"
#include "XrdCl/XrdClXRootDResponses.hh"
#include "XrdPosix/XrdPosixFile.hh"
namespace XrdFileCache
{
// a list of IOVec chuncks that match a given block index
// first element is block index, the following vector elements are chunk readv indicies
struct ReadVChunkListDisk
{
ReadVChunkListDisk(int i) : block_idx(i) {}
int block_idx;
std::vector<int> arr;
};
struct ReadVChunkListRAM
{
ReadVChunkListRAM(Block*b, std::vector <int>* iarr) : block(b), arr(iarr) {}
Block *block;
std::vector<int> *arr;
};
// RAM
struct ReadVBlockListRAM
{
std::vector<XrdFileCache::ReadVChunkListRAM> bv;
bool AddEntry(Block* block, int chunkIdx)
{
for (std::vector<ReadVChunkListRAM>::iterator i = bv.begin(); i != bv.end(); ++i)
{
if (i->block == block)
{
i->arr->push_back(chunkIdx);
return false;
}
}
bv.push_back(ReadVChunkListRAM(block, new std::vector<int>));
bv.back().arr->push_back(chunkIdx);
return true;
}
};
// Disk
struct ReadVBlockListDisk
{
std::vector<ReadVChunkListDisk> bv;
void AddEntry(int blockIdx, int chunkIdx)
{
for (std::vector<ReadVChunkListDisk>::iterator i = bv.begin(); i != bv.end(); ++i)
{
if (i->block_idx == blockIdx)
{
i->arr.push_back(chunkIdx);
return;
}
}
bv.push_back(XrdFileCache::ReadVChunkListDisk(blockIdx));
bv.back().arr.push_back(chunkIdx);
}
};
}
using namespace XrdFileCache;
//------------------------------------------------------------------------------
int File::ReadV(const XrdOucIOVec *readV, int n)
{
if ( ! isOpen())
{
return m_io->GetInput()->ReadV(readV, n);
}
TRACEF(Dump, "ReadV for " << n << " chunks.");
if ( ! VReadValidate(readV, n))
{
errno = EINVAL;
return -1;
}
int bytesRead = 0;
ReadVBlockListRAM blocks_to_process;
std::vector<ReadVChunkListRAM> blks_processed;
ReadVBlockListDisk blocks_on_disk;
std::vector<XrdOucIOVec> chunkVec;
DirectResponseHandler *direct_handler = 0;
// XXXX The following call never fails (other than with out of mem exception).
// XXXX This should be implemented in PrepareBlockRequest().
if ( ! VReadPreProcess(readV, n, blocks_to_process, blocks_on_disk, chunkVec))
{
bytesRead = -1;
errno = ENOMEM;
}
// issue a client read
if (bytesRead >= 0)
{
if ( ! chunkVec.empty())
{
direct_handler = new DirectResponseHandler(1);
m_io->GetInput()->ReadV(*direct_handler, &chunkVec[0], chunkVec.size());
}
}
// disk read
if (bytesRead >= 0)
{
int dr = VReadFromDisk(readV, n, blocks_on_disk);
if (dr < 0)
bytesRead = dr;
else
bytesRead += dr;
}
// read from cached blocks
if (bytesRead >= 0)
{
int br = VReadProcessBlocks(readV, n, blocks_to_process.bv, blks_processed);
if (br < 0)
bytesRead = br;
else
bytesRead += br;
}
// check direct requests have arrived, get bytes read from read handle
if (bytesRead >= 0 && direct_handler != 0)
{
XrdSysCondVarHelper _lck(direct_handler->m_cond);
while (direct_handler->m_to_wait > 0)
{
direct_handler->m_cond.Wait();
}
if (direct_handler->m_errno == 0)
{
for (std::vector<XrdOucIOVec>::iterator i = chunkVec.begin(); i != chunkVec.end(); ++i)
{
bytesRead += i->size;
m_stats.m_BytesMissed += i->size;
}
}
else
{
errno = - direct_handler->m_errno;
bytesRead = -1;
}
}
{
XrdSysCondVarHelper _lck(m_downloadCond);
// decrease ref count on the remaining blocks
// this happens in case read process has been broke due to previous errors
for (std::vector<ReadVChunkListRAM>::iterator i = blocks_to_process.bv.begin(); i != blocks_to_process.bv.end(); ++i)
dec_ref_count(i->block);
for (std::vector<ReadVChunkListRAM>::iterator i = blks_processed.begin(); i != blks_processed.end(); ++i)
dec_ref_count(i->block);
}
// remove objects on heap
delete direct_handler;
for (std::vector<ReadVChunkListRAM>::iterator i = blocks_to_process.bv.begin(); i != blocks_to_process.bv.end(); ++i)
delete i->arr;
for (std::vector<ReadVChunkListRAM>::iterator i = blks_processed.begin(); i != blks_processed.end(); ++i)
delete i->arr;
TRACEF(Dump, "VRead exit, total = " << bytesRead);
return bytesRead;
}
//------------------------------------------------------------------------------
bool File::VReadValidate(const XrdOucIOVec *vr, int n)
{
for (int i = 0; i < n; ++i)
{
if (vr[i].offset < 0 || vr[i].offset >= m_fileSize ||
vr[i].offset + vr[i].size > m_fileSize)
{
return false;
}
}
return true;
}
//------------------------------------------------------------------------------
bool File::VReadPreProcess(const XrdOucIOVec *readV, int n,
ReadVBlockListRAM &blocks_to_process,
ReadVBlockListDisk &blocks_on_disk,
std::vector<XrdOucIOVec> &chunkVec)
{
BlockList_t blks_to_request;
m_downloadCond.Lock();
for (int iov_idx = 0; iov_idx < n; iov_idx++)
{
const int blck_idx_first = readV[iov_idx].offset / m_cfi.GetBufferSize();
const int blck_idx_last = (readV[iov_idx].offset + readV[iov_idx].size - 1) / m_cfi.GetBufferSize();
for (int block_idx = blck_idx_first; block_idx <= blck_idx_last; ++block_idx)
{
TRACEF(Dump, "VReadPreProcess chunk "<< readV[iov_idx].size << "@"<< readV[iov_idx].offset);
BlockMap_i bi = m_block_map.find(block_idx);
if (bi != m_block_map.end())
{
if (blocks_to_process.AddEntry(bi->second, iov_idx))
inc_ref_count(bi->second);
TRACEF(Dump, "VReadPreProcess block "<< block_idx <<" in map");
}
else if (m_cfi.TestBit(offsetIdx(block_idx)))
{
blocks_on_disk.AddEntry(block_idx, iov_idx);
TRACEF(Dump, "VReadPreProcess block "<< block_idx <<" , chunk idx = " << iov_idx << " on disk");
}
else
{
if (Cache::GetInstance().RequestRAMBlock())
{
Block *b = PrepareBlockRequest(block_idx, false);
// MT XXX this can not fail (other than out of memory which we don't handle).
if (!b) return false;
inc_ref_count(b);
blocks_to_process.AddEntry(b, iov_idx);
blks_to_request.push_back(b);
TRACEF(Dump, "VReadPreProcess request block " << block_idx);
}
else
{
long long off; // offset in user buffer
long long blk_off; // offset in block
long long size; // size to copy
const long long BS = m_cfi.GetBufferSize();
overlap(block_idx, BS, readV[iov_idx].offset, readV[iov_idx].size, off, blk_off, size);
chunkVec.push_back(XrdOucIOVec2(readV[iov_idx].data+off, BS*block_idx + blk_off,size));
TRACEF(Dump, "VReadPreProcess direct read " << block_idx);
}
}
}
}
m_downloadCond.UnLock();
ProcessBlockRequests(blks_to_request);
return true;
}
//------------------------------------------------------------------------------
int File::VReadFromDisk(const XrdOucIOVec *readV, int n, ReadVBlockListDisk& blocks_on_disk)
{
int bytes_read = 0;
for (std::vector<ReadVChunkListDisk>::iterator bit = blocks_on_disk.bv.begin(); bit != blocks_on_disk.bv.end(); ++bit )
{
int blockIdx = bit->block_idx;
for (std::vector<int>::iterator chunkIt = bit->arr.begin(); chunkIt != bit->arr.end(); ++chunkIt)
{
int chunkIdx = *chunkIt;
long long off; // offset in user buffer
long long blk_off; // offset in block
long long size; // size to copy
TRACEF(Dump, "VReadFromDisk block= " << blockIdx <<" chunk=" << chunkIdx);
overlap(blockIdx, m_cfi.GetBufferSize(), readV[chunkIdx].offset, readV[chunkIdx].size, off, blk_off, size);
int rs = m_output->Read(readV[chunkIdx].data + off, blockIdx*m_cfi.GetBufferSize() + blk_off - m_offset, size);
if (rs >=0)
{
bytes_read += rs;
m_stats.m_BytesDisk += rs;
}
else
{
// ofs read should set the errno
TRACEF(Error, "VReadFromDisk FAILED block=" << blockIdx << " chunk=" << chunkIdx << " off= " << off << " blk_off=" << blk_off << " size = " << size << "chunOff " << readV[chunkIdx].offset);
return -1;
}
}
}
return bytes_read;
}
//------------------------------------------------------------------------------
int File::VReadProcessBlocks(const XrdOucIOVec *readV, int n,
std::vector<ReadVChunkListRAM>& blocks_to_process,
std::vector<ReadVChunkListRAM>& blocks_processed)
{
int bytes_read = 0;
while ((! blocks_to_process.empty()) && (bytes_read >= 0))
{
std::vector<ReadVChunkListRAM> finished;
{
XrdSysCondVarHelper _lck(m_downloadCond);
std::vector<ReadVChunkListRAM>::iterator bi = blocks_to_process.begin();
while (bi != blocks_to_process.end())
{
if (bi->block->is_finished())
{
finished.push_back(ReadVChunkListRAM(bi->block, bi->arr));
// Here we rely on the fact that std::vector does not reallocate on erase!
blocks_to_process.erase(bi);
}
else
{
++bi;
}
}
if (finished.empty())
{
m_downloadCond.Wait();
continue;
}
}
std::vector<ReadVChunkListRAM>::iterator bi = finished.begin();
while (bi != finished.end())
{
if (bi->block->is_ok())
{
for (std::vector<int>::iterator chunkIt = bi->arr->begin(); chunkIt < bi->arr->end(); ++chunkIt)
{
long long off; // offset in user buffer
long long blk_off; // offset in block
long long size; // size to copy
int block_idx =bi->block->m_offset/m_cfi.GetBufferSize();
overlap(block_idx, m_cfi.GetBufferSize(), readV[*chunkIt].offset, readV[*chunkIt].size, off, blk_off, size);
memcpy(readV[*chunkIt].data + off, &(bi->block->m_buff[blk_off]), size);
bytes_read += size;
m_stats.m_BytesRam += size;
}
}
else
{
bytes_read = -1;
errno = - bi->block->m_errno;
break;
}
++bi;
}
// add finished to processed list
std::copy(finished.begin(), finished.end(), std::back_inserter(blocks_processed));
finished.clear();
}
TRACEF(Dump, "VReadProcessBlocks total read " << bytes_read);
return bytes_read;
}