xrootd
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
XrdFileCacheFile.hh
Go to the documentation of this file.
1 #ifndef __XRDFILECACHE_FILE_HH__
2 #define __XRDFILECACHE_FILE_HH__
3 //----------------------------------------------------------------------------------
4 // Copyright (c) 2014 by Board of Trustees of the Leland Stanford, Jr., University
5 // Author: Alja Mrak-Tadel, Matevz Tadel
6 //----------------------------------------------------------------------------------
7 // XRootD is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Lesser General Public License as published by
9 // the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // XRootD is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU Lesser General Public License
18 // along with XRootD. If not, see <http://www.gnu.org/licenses/>.
19 //----------------------------------------------------------------------------------
20 
22 #include "XrdCl/XrdClDefaultEnv.hh"
23 
24 #include "XrdOuc/XrdOucCache2.hh"
25 #include "XrdOuc/XrdOucIOVec.hh"
26 
27 #include "XrdFileCacheInfo.hh"
28 #include "XrdFileCacheStats.hh"
29 
30 #include <string>
31 #include <map>
32 
33 class XrdJob;
34 class XrdOucIOVec;
35 
36 namespace XrdCl
37 {
38 class Log;
39 }
40 
41 namespace XrdFileCache
42 {
43 class BlockResponseHandler;
44 class DirectResponseHandler;
45 class IO;
46 
47 struct ReadVBlockListRAM;
48 struct ReadVChunkListRAM;
49 struct ReadVBlockListDisk;
50 struct ReadVChunkListDisk;
51 }
52 
53 
54 namespace XrdFileCache
55 {
56 
57 class File;
58 
59 class Block
60 {
61 public:
62  std::vector<char> m_buff;
63  long long m_offset;
65  IO *m_io; // IO that handled current request, used for == / != comparisons only
66 
67  int m_refcnt;
68  int m_errno; // stores negative errno
70  bool m_prefetch;
71 
72  Block(File *f, IO *io, long long off, int size, bool m_prefetch) :
73  m_offset(off), m_file(f), m_io(io), m_refcnt(0),
74  m_errno(0), m_downloaded(false), m_prefetch(m_prefetch)
75  {
76  m_buff.resize(size);
77  }
78 
79  char* get_buff(long long pos = 0) { return &m_buff[pos]; }
80  int get_size() { return (int) m_buff.size(); }
81  long long get_offset() { return m_offset; }
82 
83  IO* get_io() const { return m_io; }
84 
85  bool is_finished() { return m_downloaded || m_errno != 0; }
86  bool is_ok() { return m_downloaded; }
87  bool is_failed() { return m_errno != 0; }
88 
89  void set_downloaded() { m_downloaded = true; }
90  void set_error(int err) { m_errno = err; }
91 
93  {
94  m_errno = 0;
95  m_io = io;
96  }
97 };
98 
99 // ================================================================
100 
102 {
103 public:
106 
107  BlockResponseHandler(Block *b, bool prefetch) :
108  m_block(b), m_for_prefetch(prefetch) {}
109 
110  virtual void Done(int result);
111 };
112 
113 // ================================================================
114 
116 {
117 public:
120  int m_errno;
121 
122  DirectResponseHandler(int to_wait) : m_cond(0), m_to_wait(to_wait), m_errno(0) {}
123 
124  bool is_finished() { XrdSysCondVarHelper _lck(m_cond); return m_to_wait == 0; }
125  bool is_ok() { XrdSysCondVarHelper _lck(m_cond); return m_to_wait == 0 && m_errno == 0; }
126  bool is_failed() { XrdSysCondVarHelper _lck(m_cond); return m_errno != 0; }
127 
128  virtual void Done(int result);
129 };
130 
131 // ================================================================
132 
133 class File
134 {
135 public:
136  //------------------------------------------------------------------------
138  //------------------------------------------------------------------------
139  File(const std::string &path, long long offset, long long fileSize);
140 
141  //------------------------------------------------------------------------
143  //------------------------------------------------------------------------
144  ~File();
145 
148  bool Open();
149 
151  int ReadV(IO *io, const XrdOucIOVec *readV, int n);
152 
154  int Read (IO *io, char* buff, long long offset, int size);
155 
156  //----------------------------------------------------------------------
158  //----------------------------------------------------------------------
159  bool isOpen() const { return m_is_open; }
160 
161  //----------------------------------------------------------------------
164  //----------------------------------------------------------------------
165  bool ioActive(IO *io);
166 
167  //----------------------------------------------------------------------
170  //----------------------------------------------------------------------
172 
173  //----------------------------------------------------------------------
176  //----------------------------------------------------------------------
177  bool FinalizeSyncBeforeExit();
178 
179  //----------------------------------------------------------------------
181  //----------------------------------------------------------------------
182  void Sync();
183 
184  //----------------------------------------------------------------------
186  //----------------------------------------------------------------------
187  Stats& GetStats() { return m_stats; }
188 
189  void ProcessBlockResponse(BlockResponseHandler* brh, int res);
190  void WriteBlockToDisk(Block* b);
191 
192  void Prefetch();
193 
194  float GetPrefetchScore() const;
195 
197  const char* lPath() const;
198 
199  std::string& GetLocalPath() { return m_filename; }
200 
202 
203  long long GetFileSize() { return m_fileSize; }
204 
205  void AddIO(IO *io);
206  int GetPrefetchCountOnIO(IO *io);
207  void StopPrefetchingOnIO(IO *io);
208  void RemoveIO(IO *io);
209 
210  // These three methods are called under Cache's m_active lock
211  int get_ref_cnt() { return m_ref_cnt; }
212  int inc_ref_cnt() { return ++m_ref_cnt; }
213  int dec_ref_cnt() { return --m_ref_cnt; }
214 
215 private:
217 
218  int m_ref_cnt;
219 
220  bool m_is_open;
221 
225 
226  std::string m_filename;
227  long long m_offset;
228  long long m_fileSize;
229 
230  // IO objects attached to this file.
231 
232  struct IODetails
233  {
236 
238  };
239 
240  typedef std::map<IO*, IODetails> IoMap_t;
241  typedef IoMap_t::iterator IoMap_i;
242 
246 
247  // fsync
248  std::vector<int> m_writes_during_sync;
250  bool m_in_sync;
251 
252  typedef std::list<int> IntList_t;
253  typedef IntList_t::iterator IntList_i;
254 
255  typedef std::list<Block*> BlockList_t;
256  typedef BlockList_t::iterator BlockList_i;
257 
258  typedef std::map<int, Block*> BlockMap_t;
259  typedef BlockMap_t::iterator BlockMap_i;
260 
261 
263 
265 
267 
269 
272  float m_prefetchScore; // cached
273 
275 
276  static const char *m_traceID;
277  bool overlap(int blk, // block to query
278  long long blk_size, //
279  long long req_off, // offset of user request
280  int req_size, // size of user request
281  // output:
282  long long &off, // offset in user buffer
283  long long &blk_off, // offset in block
284  long long &size);
285 
286  // Read
287  Block* PrepareBlockRequest(int i, IO *io, bool prefetch);
288 
289  void ProcessBlockRequest (Block *b, bool prefetch);
290  void ProcessBlockRequests(BlockList_t& blks, bool prefetch);
291 
292  int RequestBlocksDirect(IO *io, DirectResponseHandler *handler, IntList_t& blocks,
293  char* buff, long long req_off, long long req_size);
294 
295  int ReadBlocksFromDisk(IntList_t& blocks,
296  char* req_buf, long long req_off, long long req_size);
297 
298  // VRead
299  bool VReadValidate (const XrdOucIOVec *readV, int n);
300  bool VReadPreProcess (IO *io, const XrdOucIOVec *readV, int n,
301  ReadVBlockListRAM& blks_to_process,
302  ReadVBlockListDisk& blks_on_disk,
303  std::vector<XrdOucIOVec>& chunkVec);
304  int VReadFromDisk (const XrdOucIOVec *readV, int n,
305  ReadVBlockListDisk& blks_on_disk);
306  int VReadProcessBlocks(IO *io, const XrdOucIOVec *readV, int n,
307  std::vector<ReadVChunkListRAM>& blks_to_process,
308  std::vector<ReadVChunkListRAM>& blks_rocessed);
309 
310  long long BufferSize();
311 
312  void inc_ref_count(Block*);
313  void dec_ref_count(Block*);
314  void free_block(Block*);
315 
316  bool select_current_io_or_disable_prefetching(bool skip_current);
317 
318  int offsetIdx(int idx);
319 };
320 
321 }
322 
323 #endif
void dec_ref_count(Block *)
long long BufferSize()
std::string m_filename
filename of data file on disk
Definition: XrdFileCacheFile.hh:226
int VReadFromDisk(const XrdOucIOVec *readV, int n, ReadVBlockListDisk &blks_on_disk)
long long get_offset()
Definition: XrdFileCacheFile.hh:81
PrefetchState_e
Definition: XrdFileCacheFile.hh:216
Definition: XrdFileCacheFile.hh:115
bool m_allow_prefetching
Definition: XrdFileCacheFile.hh:235
File * m_file
Definition: XrdFileCacheFile.hh:64
long long m_fileSize
size of cached disk file for block-based operation
Definition: XrdFileCacheFile.hh:228
Definition: XrdFileCacheFile.hh:216
XrdSysTrace * GetTrace()
void ProcessBlockResponse(BlockResponseHandler *brh, int res)
Statistics of disk cache utilisation.
Definition: XrdFileCacheStats.hh:30
Info m_cfi
download status of file blocks and access statistics
Definition: XrdFileCacheFile.hh:224
int m_to_wait
Definition: XrdFileCacheFile.hh:119
Stats m_stats
cache statistics, used in IO detach
Definition: XrdFileCacheFile.hh:266
BlockResponseHandler(Block *b, bool prefetch)
Definition: XrdFileCacheFile.hh:107
bool is_finished()
Definition: XrdFileCacheFile.hh:124
void RemoveIO(IO *io)
int RequestBlocksDirect(IO *io, DirectResponseHandler *handler, IntList_t &blocks, char *buff, long long req_off, long long req_size)
Definition: XrdFileCacheFile.hh:59
XrdSysCondVar m_downloadCond
Definition: XrdFileCacheFile.hh:264
bool ioActive(IO *io)
Initiate close. Return true if still IO active. Used in XrdPosixXrootd::Close()
IoMap_t::iterator IoMap_i
Definition: XrdFileCacheFile.hh:241
char * get_buff(long long pos=0)
Definition: XrdFileCacheFile.hh:79
DirectResponseHandler(int to_wait)
Definition: XrdFileCacheFile.hh:122
bool m_prefetch
Definition: XrdFileCacheFile.hh:70
bool select_current_io_or_disable_prefetching(bool skip_current)
bool is_failed()
Definition: XrdFileCacheFile.hh:126
bool isOpen() const
Data and cinfo files are open.
Definition: XrdFileCacheFile.hh:159
int get_size()
Definition: XrdFileCacheFile.hh:80
int Read(IO *io, char *buff, long long offset, int size)
Normal read.
Status of cached file. Can be read from and written into a binary file.
Definition: XrdFileCacheInfo.hh:48
bool VReadValidate(const XrdOucIOVec *readV, int n)
XrdOssDF * m_output
file handle for data file on disk
Definition: XrdFileCacheFile.hh:222
int GetPrefetchCountOnIO(IO *io)
bool VReadPreProcess(IO *io, const XrdOucIOVec *readV, int n, ReadVBlockListRAM &blks_to_process, ReadVBlockListDisk &blks_on_disk, std::vector< XrdOucIOVec > &chunkVec)
Block * PrepareBlockRequest(int i, IO *io, bool prefetch)
File(const std::string &path, long long offset, long long fileSize)
Constructor.
Definition: XrdSysTrace.hh:45
bool overlap(int blk, long long blk_size, long long req_off, int req_size, long long &off, long long &blk_off, long long &size)
int m_non_flushed_cnt
Definition: XrdFileCacheFile.hh:249
bool Open()
Open file handle for data file and info file on local disk.
Base cache-io class that implements XrdOucCacheIO abstract methods.
Definition: XrdFileCacheIO.hh:16
void free_block(Block *)
IoMap_i m_current_io
IO object to be used for prefetching.
Definition: XrdFileCacheFile.hh:244
BlockMap_t m_block_map
Definition: XrdFileCacheFile.hh:262
bool m_is_open
open state
Definition: XrdFileCacheFile.hh:220
void set_downloaded()
Definition: XrdFileCacheFile.hh:89
Definition: XrdFileCacheFile.hh:232
XrdSysCondVar m_cond
Definition: XrdFileCacheFile.hh:118
void ProcessBlockRequests(BlockList_t &blks, bool prefetch)
int get_ref_cnt()
Definition: XrdFileCacheFile.hh:211
BlockMap_t::iterator BlockMap_i
Definition: XrdFileCacheFile.hh:259
bool m_in_sync
Definition: XrdFileCacheFile.hh:250
IoMap_t m_io_map
Definition: XrdFileCacheFile.hh:243
void Sync()
Sync file cache inf o and output data with disk.
int m_active_prefetches
Definition: XrdFileCacheFile.hh:234
Definition: XrdSysPthread.hh:78
int m_refcnt
Definition: XrdFileCacheFile.hh:67
void RequestSyncOfDetachStats()
Flags that detach stats should be written out in final sync. Called from CacheIO upon Detach...
PrefetchState_e m_prefetchState
Definition: XrdFileCacheFile.hh:268
void AddIO(IO *io)
bool m_for_prefetch
Definition: XrdFileCacheFile.hh:105
Block(File *f, IO *io, long long off, int size, bool m_prefetch)
Definition: XrdFileCacheFile.hh:72
Definition: XrdFileCacheFile.hh:216
int offsetIdx(int idx)
std::list< int > IntList_t
Definition: XrdFileCacheFile.hh:252
Definition: XrdOucIOVec.hh:40
~File()
Destructor.
IODetails()
Definition: XrdFileCacheFile.hh:237
bool FinalizeSyncBeforeExit()
Returns true if any of blocks need sync. Called from Cache::dec_ref_cnt on zero ref cnt...
int m_errno
Definition: XrdFileCacheFile.hh:68
int m_ref_cnt
number of references from IO or sync
Definition: XrdFileCacheFile.hh:218
long long GetFileSize()
Definition: XrdFileCacheFile.hh:203
std::string & GetLocalPath()
Definition: XrdFileCacheFile.hh:199
float m_prefetchScore
Definition: XrdFileCacheFile.hh:272
bool is_failed()
Definition: XrdFileCacheFile.hh:87
void BlockRemovedFromWriteQ(Block *)
static const char * m_traceID
Definition: XrdFileCacheFile.hh:276
Definition: XrdFileCacheFile.hh:216
Definition: XrdSysPthread.hh:129
const char * lPath() const
Log path.
bool is_finished()
Definition: XrdFileCacheFile.hh:85
Definition: XrdOucCache.hh:93
Definition: XrdFileCacheFile.hh:101
std::list< Block * > BlockList_t
Definition: XrdFileCacheFile.hh:255
Definition: XrdFileCacheFile.hh:133
Block * m_block
Definition: XrdFileCacheFile.hh:104
void StopPrefetchingOnIO(IO *io)
bool m_downloaded
Definition: XrdFileCacheFile.hh:69
virtual void Done(int result)
int m_ios_in_detach
Number of IO objects to which we replied false to ioActive() and will be removed soon.
Definition: XrdFileCacheFile.hh:245
void ProcessBlockRequest(Block *b, bool prefetch)
std::vector< int > m_writes_during_sync
Definition: XrdFileCacheFile.hh:248
int m_prefetchHitCnt
Definition: XrdFileCacheFile.hh:271
void set_error(int err)
Definition: XrdFileCacheFile.hh:90
Definition: XrdFileCacheFile.hh:216
int inc_ref_cnt()
Definition: XrdFileCacheFile.hh:212
int m_errno
Definition: XrdFileCacheFile.hh:120
int ReadBlocksFromDisk(IntList_t &blocks, char *req_buf, long long req_off, long long req_size)
std::vector< char > m_buff
Definition: XrdFileCacheFile.hh:62
Definition: XrdOss.hh:59
long long m_offset
Definition: XrdFileCacheFile.hh:63
void WriteBlockToDisk(Block *b)
Definition: XrdFileCacheFile.hh:216
Stats & GetStats()
Reference to prefetch statistics.
Definition: XrdFileCacheFile.hh:187
bool m_detachTimeIsLogged
Definition: XrdFileCacheFile.hh:274
XrdOssDF * m_infoFile
file handle for data-info file on disk
Definition: XrdFileCacheFile.hh:223
void reset_error_and_set_io(IO *io)
Definition: XrdFileCacheFile.hh:92
std::map< IO *, IODetails > IoMap_t
Definition: XrdFileCacheFile.hh:240
float GetPrefetchScore() const
int m_prefetchReadCnt
Definition: XrdFileCacheFile.hh:270
int dec_ref_cnt()
Definition: XrdFileCacheFile.hh:213
void inc_ref_count(Block *)
IntList_t::iterator IntList_i
Definition: XrdFileCacheFile.hh:253
bool is_ok()
Definition: XrdFileCacheFile.hh:125
long long m_offset
offset of cached file for block-based / hdfs operation
Definition: XrdFileCacheFile.hh:227
bool is_ok()
Definition: XrdFileCacheFile.hh:86
int VReadProcessBlocks(IO *io, const XrdOucIOVec *readV, int n, std::vector< ReadVChunkListRAM > &blks_to_process, std::vector< ReadVChunkListRAM > &blks_rocessed)
IO * m_io
Definition: XrdFileCacheFile.hh:65
IO * get_io() const
Definition: XrdFileCacheFile.hh:83
virtual void Done(int result)
BlockList_t::iterator BlockList_i
Definition: XrdFileCacheFile.hh:256
int ReadV(IO *io, const XrdOucIOVec *readV, int n)
Vector read from disk if block is already downloaded, else ReadV from client.
Definition: XrdJob.hh:42
std::map< int, Block * > BlockMap_t
Definition: XrdFileCacheFile.hh:258