-
Notifications
You must be signed in to change notification settings - Fork 149
/
XrdFileCacheFile.hh
293 lines (221 loc) · 9.41 KB
/
XrdFileCacheFile.hh
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
#ifndef __XRDFILECACHE_FILE_HH__
#define __XRDFILECACHE_FILE_HH__
//----------------------------------------------------------------------------------
// Copyright (c) 2014 by Board of Trustees of the Leland Stanford, Jr., University
// Author: Alja Mrak-Tadel, Matevz Tadel
//----------------------------------------------------------------------------------
// XRootD is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// XRootD 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 Lesser General Public License
// along with XRootD. If not, see <http://www.gnu.org/licenses/>.
//----------------------------------------------------------------------------------
#include "XrdCl/XrdClXRootDResponses.hh"
#include "XrdCl/XrdClDefaultEnv.hh"
#include "XrdOuc/XrdOucCache2.hh"
#include "XrdOuc/XrdOucIOVec.hh"
#include "XrdFileCacheInfo.hh"
#include "XrdFileCacheStats.hh"
#include <string>
#include <map>
class XrdJob;
class XrdOucIOVec;
namespace XrdCl
{
class Log;
}
namespace XrdFileCache
{
class BlockResponseHandler;
class DirectResponseHandler;
class IO;
struct ReadVBlockListRAM;
struct ReadVChunkListRAM;
struct ReadVBlockListDisk;
struct ReadVChunkListDisk;
}
namespace XrdFileCache
{
class File;
class Block
{
public:
std::vector<char> m_buff;
long long m_offset;
File *m_file;
bool m_prefetch;
int m_refcnt;
int m_errno; // stores negative errno
bool m_downloaded;
Block(File *f, long long off, int size, bool m_prefetch) :
m_offset(off), m_file(f), m_prefetch(m_prefetch), m_refcnt(0),
m_errno(0), m_downloaded(false)
{
m_buff.resize(size);
}
char* get_buff(long long pos = 0) { return &m_buff[pos]; }
int get_size() { return (int) m_buff.size(); }
long long get_offset() { return m_offset; }
bool is_finished() { return m_downloaded || m_errno != 0; }
bool is_ok() { return m_downloaded; }
bool is_failed() { return m_errno != 0; }
void set_error_and_free(int err)
{
m_errno = err;
std::vector<char> x;
m_buff.swap(x);
}
};
// ================================================================
class BlockResponseHandler : public XrdOucCacheIOCB
{
public:
Block *m_block;
BlockResponseHandler(Block *b) : m_block(b) {}
virtual void Done(int result);
};
// ================================================================
class DirectResponseHandler : public XrdOucCacheIOCB
{
public:
XrdSysCondVar m_cond;
int m_to_wait;
int m_errno;
DirectResponseHandler(int to_wait) : m_cond(0), m_to_wait(to_wait), m_errno(0) {}
bool is_finished() { XrdSysCondVarHelper _lck(m_cond); return m_to_wait == 0; }
bool is_ok() { XrdSysCondVarHelper _lck(m_cond); return m_to_wait == 0 && m_errno == 0; }
bool is_failed() { XrdSysCondVarHelper _lck(m_cond); return m_errno != 0; }
virtual void Done(int result);
};
// ================================================================
class File
{
public:
//------------------------------------------------------------------------
//! Constructor.
//------------------------------------------------------------------------
File(IO *io, const std::string &path, long long offset, long long fileSize);
//------------------------------------------------------------------------
//! Destructor.
//------------------------------------------------------------------------
~File();
void BlockRemovedFromWriteQ(Block*);
//! Open file handle for data file and info file on local disk.
bool Open();
//! Vector read from disk if block is already downloaded, else ReadV from client.
int ReadV (const XrdOucIOVec *readV, int n);
int Read(char* buff, long long offset, int size);
//----------------------------------------------------------------------
//! \brief Data and cinfo files are open.
//----------------------------------------------------------------------
bool isOpen() const { return m_is_open; }
//----------------------------------------------------------------------
//! \brief Initiate close. Return true if still IO active.
//! Used in XrdPosixXrootd::Close()
//----------------------------------------------------------------------
bool ioActive();
//----------------------------------------------------------------------
//! \brief Flags that detach stats should be written out in final sync.
//! Called from CacheIO upon Detach.
//----------------------------------------------------------------------
void RequestSyncOfDetachStats();
//----------------------------------------------------------------------
//! \brief Returns true if any of blocks need sync.
//! Called from Cache::dec_ref_cnt on zero ref cnt
//----------------------------------------------------------------------
bool FinalizeSyncBeforeExit();
//----------------------------------------------------------------------
//! Sync file cache inf o and output data with disk
//----------------------------------------------------------------------
void Sync();
//----------------------------------------------------------------------
//! Reference to prefetch statistics.
//----------------------------------------------------------------------
Stats& GetStats() { return m_stats; }
void ProcessBlockResponse(Block* b, int res);
void WriteBlockToDisk(Block* b);
void Prefetch();
float GetPrefetchScore() const;
//! Log path
const char* lPath() const;
std::string& GetLocalPath() { return m_filename; }
XrdSysTrace* GetTrace();
long long GetFileSize() { return m_fileSize; }
IO* SetIO(IO* io);
void ReleaseIO();
// These three methods are called under Cache's m_active lock
int get_ref_cnt() { return m_ref_cnt; }
int inc_ref_cnt() { return ++m_ref_cnt; }
int dec_ref_cnt() { return --m_ref_cnt; }
private:
enum PrefetchState_e { kOff=-1, kOn, kHold, kStopped, kComplete };
int m_ref_cnt; //!< number of references from IO or sync
bool m_is_open; //!< open state
IO *m_io; //!< original data source
XrdOssDF *m_output; //!< file handle for data file on disk
XrdOssDF *m_infoFile; //!< file handle for data-info file on disk
Info m_cfi; //!< download status of file blocks and access statistics
std::string m_filename; //!< filename of data file on disk
long long m_offset; //!< offset of cached file for block-based operation
long long m_fileSize; //!< size of cached disk file for block-based operation
// fsync
std::vector<int> m_writes_during_sync;
int m_non_flushed_cnt;
bool m_in_sync;
typedef std::list<int> IntList_t;
typedef IntList_t::iterator IntList_i;
typedef std::list<Block*> BlockList_t;
typedef BlockList_t::iterator BlockList_i;
typedef std::map<int, Block*> BlockMap_t;
typedef BlockMap_t::iterator BlockMap_i;
BlockMap_t m_block_map;
XrdSysCondVar m_downloadCond;
Stats m_stats; //!< cache statistics, used in IO detach
PrefetchState_e m_prefetchState;
int m_prefetchReadCnt;
int m_prefetchHitCnt;
float m_prefetchScore; //cached
bool m_detachTimeIsLogged;
static const char *m_traceID;
bool overlap(int blk, // block to query
long long blk_size, //
long long req_off, // offset of user request
int req_size, // size of user request
// output:
long long &off, // offset in user buffer
long long &blk_off, // offset in block
long long &size);
// Read
Block* PrepareBlockRequest(int i, bool prefetch);
void ProcessBlockRequests(BlockList_t& blks);
int RequestBlocksDirect(DirectResponseHandler *handler, IntList_t& blocks,
char* buff, long long req_off, long long req_size);
int ReadBlocksFromDisk(IntList_t& blocks,
char* req_buf, long long req_off, long long req_size);
// VRead
bool VReadValidate (const XrdOucIOVec *readV, int n);
bool VReadPreProcess (const XrdOucIOVec *readV, int n,
ReadVBlockListRAM& blks_to_process,
ReadVBlockListDisk& blks_on_disk,
std::vector<XrdOucIOVec>& chunkVec);
int VReadFromDisk (const XrdOucIOVec *readV, int n,
ReadVBlockListDisk& blks_on_disk);
int VReadProcessBlocks(const XrdOucIOVec *readV, int n,
std::vector<ReadVChunkListRAM>& blks_to_process,
std::vector<ReadVChunkListRAM>& blks_rocessed);
long long BufferSize();
void AppendIOStatToFileInfo();
void inc_ref_count(Block*);
void dec_ref_count(Block*);
void free_block(Block*);
int offsetIdx(int idx);
};
}
#endif