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  static File* FileOpen(const std::string &path, long long offset, long long fileSize);
145 
146  //------------------------------------------------------------------------
148  //------------------------------------------------------------------------
149  ~File();
150 
153 
155  bool Open();
156 
158  int ReadV(IO *io, const XrdOucIOVec *readV, int n);
159 
161  int Read (IO *io, char* buff, long long offset, int size);
162 
163  //----------------------------------------------------------------------
165  //----------------------------------------------------------------------
166  bool isOpen() const { return m_is_open; }
167 
168  //----------------------------------------------------------------------
171  //----------------------------------------------------------------------
172  bool ioActive(IO *io);
173 
174  //----------------------------------------------------------------------
177  //----------------------------------------------------------------------
179 
180  //----------------------------------------------------------------------
183  //----------------------------------------------------------------------
184  bool FinalizeSyncBeforeExit();
185 
186  //----------------------------------------------------------------------
188  //----------------------------------------------------------------------
189  void Sync();
190 
191  //----------------------------------------------------------------------
193  //----------------------------------------------------------------------
194  Stats& GetStats() { return m_stats; }
195 
196  void ProcessBlockResponse(BlockResponseHandler* brh, int res);
197  void WriteBlockToDisk(Block* b);
198 
199  void Prefetch();
200 
201  float GetPrefetchScore() const;
202 
204  const char* lPath() const;
205 
206  std::string& GetLocalPath() { return m_filename; }
207 
208  XrdSysError* GetLog();
210 
211  long long GetFileSize() { return m_fileSize; }
212 
213  void AddIO(IO *io);
214  int GetPrefetchCountOnIO(IO *io);
215  void StopPrefetchingOnIO(IO *io);
216  void RemoveIO(IO *io);
217 
218  // These three methods are called under Cache's m_active lock
219  int get_ref_cnt() { return m_ref_cnt; }
220  int inc_ref_cnt() { return ++m_ref_cnt; }
221  int dec_ref_cnt() { return --m_ref_cnt; }
222 
223 private:
225 
226  int m_ref_cnt;
227 
228  bool m_is_open;
229 
233 
234  std::string m_filename;
235  long long m_offset;
236  long long m_fileSize;
237 
238  // IO objects attached to this file.
239 
240  struct IODetails
241  {
244 
246  };
247 
248  typedef std::map<IO*, IODetails> IoMap_t;
249  typedef IoMap_t::iterator IoMap_i;
250 
254 
255  // fsync
256  std::vector<int> m_writes_during_sync;
258  bool m_in_sync;
259 
260  typedef std::list<int> IntList_t;
261  typedef IntList_t::iterator IntList_i;
262 
263  typedef std::list<Block*> BlockList_t;
264  typedef BlockList_t::iterator BlockList_i;
265 
266  typedef std::map<int, Block*> BlockMap_t;
267  typedef BlockMap_t::iterator BlockMap_i;
268 
269 
271 
273 
275 
277 
280  float m_prefetchScore; // cached
281 
283 
284  static const char *m_traceID;
285  bool overlap(int blk, // block to query
286  long long blk_size, //
287  long long req_off, // offset of user request
288  int req_size, // size of user request
289  // output:
290  long long &off, // offset in user buffer
291  long long &blk_off, // offset in block
292  long long &size);
293 
294  // Read
295  Block* PrepareBlockRequest(int i, IO *io, bool prefetch);
296 
297  void ProcessBlockRequest (Block *b, bool prefetch);
298  void ProcessBlockRequests(BlockList_t& blks, bool prefetch);
299 
300  int RequestBlocksDirect(IO *io, DirectResponseHandler *handler, IntList_t& blocks,
301  char* buff, long long req_off, long long req_size);
302 
303  int ReadBlocksFromDisk(IntList_t& blocks,
304  char* req_buf, long long req_off, long long req_size);
305 
306  // VRead
307  bool VReadValidate (const XrdOucIOVec *readV, int n);
308  bool VReadPreProcess (IO *io, const XrdOucIOVec *readV, int n,
309  ReadVBlockListRAM& blks_to_process,
310  ReadVBlockListDisk& blks_on_disk,
311  std::vector<XrdOucIOVec>& chunkVec);
312  int VReadFromDisk (const XrdOucIOVec *readV, int n,
313  ReadVBlockListDisk& blks_on_disk);
314  int VReadProcessBlocks(IO *io, const XrdOucIOVec *readV, int n,
315  std::vector<ReadVChunkListRAM>& blks_to_process,
316  std::vector<ReadVChunkListRAM>& blks_rocessed);
317 
318  long long BufferSize();
319 
320  void inc_ref_count(Block*);
321  void dec_ref_count(Block*);
322  void free_block(Block*);
323 
324  bool select_current_io_or_disable_prefetching(bool skip_current);
325 
326  int offsetIdx(int idx);
327 };
328 
329 }
330 
331 #endif
void dec_ref_count(Block *)
long long BufferSize()
std::string m_filename
filename of data file on disk
Definition: XrdFileCacheFile.hh:234
int VReadFromDisk(const XrdOucIOVec *readV, int n, ReadVBlockListDisk &blks_on_disk)
long long get_offset()
Definition: XrdFileCacheFile.hh:81
PrefetchState_e
Definition: XrdFileCacheFile.hh:224
Definition: XrdFileCacheFile.hh:115
bool m_allow_prefetching
Definition: XrdFileCacheFile.hh:243
File * m_file
Definition: XrdFileCacheFile.hh:64
long long m_fileSize
size of cached disk file for block-based operation
Definition: XrdFileCacheFile.hh:236
Definition: XrdFileCacheFile.hh:224
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:232
int m_to_wait
Definition: XrdFileCacheFile.hh:119
Stats m_stats
cache statistics, used in IO detach
Definition: XrdFileCacheFile.hh:274
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:272
bool ioActive(IO *io)
Initiate close. Return true if still IO active. Used in XrdPosixXrootd::Close()
IoMap_t::iterator IoMap_i
Definition: XrdFileCacheFile.hh:249
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:166
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:230
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)
Definition: XrdSysError.hh:89
File(const std::string &path, long long offset, long long fileSize)
Constructor.
Definition: XrdSysTrace.hh:48
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:257
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:252
BlockMap_t m_block_map
Definition: XrdFileCacheFile.hh:270
bool m_is_open
open state
Definition: XrdFileCacheFile.hh:228
void set_downloaded()
Definition: XrdFileCacheFile.hh:89
Definition: XrdFileCacheFile.hh:240
XrdSysCondVar m_cond
Definition: XrdFileCacheFile.hh:118
void ProcessBlockRequests(BlockList_t &blks, bool prefetch)
int get_ref_cnt()
Definition: XrdFileCacheFile.hh:219
BlockMap_t::iterator BlockMap_i
Definition: XrdFileCacheFile.hh:267
bool m_in_sync
Definition: XrdFileCacheFile.hh:258
IoMap_t m_io_map
Definition: XrdFileCacheFile.hh:251
void Sync()
Sync file cache inf o and output data with disk.
int m_active_prefetches
Definition: XrdFileCacheFile.hh:242
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:276
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:224
int offsetIdx(int idx)
std::list< int > IntList_t
Definition: XrdFileCacheFile.hh:260
Definition: XrdOucIOVec.hh:40
~File()
Destructor.
IODetails()
Definition: XrdFileCacheFile.hh:245
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:226
long long GetFileSize()
Definition: XrdFileCacheFile.hh:211
std::string & GetLocalPath()
Definition: XrdFileCacheFile.hh:206
float m_prefetchScore
Definition: XrdFileCacheFile.hh:280
bool is_failed()
Definition: XrdFileCacheFile.hh:87
void BlockRemovedFromWriteQ(Block *)
Handle removal of a block from Cache&#39;s write queue.
static const char * m_traceID
Definition: XrdFileCacheFile.hh:284
Definition: XrdFileCacheFile.hh:224
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:263
Definition: XrdFileCacheFile.hh:133
Block * m_block
Definition: XrdFileCacheFile.hh:104
void StopPrefetchingOnIO(IO *io)
bool m_downloaded
Definition: XrdFileCacheFile.hh:69
XrdSysError * GetLog()
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:253
void ProcessBlockRequest(Block *b, bool prefetch)
std::vector< int > m_writes_during_sync
Definition: XrdFileCacheFile.hh:256
int m_prefetchHitCnt
Definition: XrdFileCacheFile.hh:279
void set_error(int err)
Definition: XrdFileCacheFile.hh:90
Definition: XrdFileCacheFile.hh:224
int inc_ref_cnt()
Definition: XrdFileCacheFile.hh:220
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:224
static File * FileOpen(const std::string &path, long long offset, long long fileSize)
Static constructor that also does Open. Returns null ptr if Open fails.
Stats & GetStats()
Reference to prefetch statistics.
Definition: XrdFileCacheFile.hh:194
bool m_detachTimeIsLogged
Definition: XrdFileCacheFile.hh:282
XrdOssDF * m_infoFile
file handle for data-info file on disk
Definition: XrdFileCacheFile.hh:231
void reset_error_and_set_io(IO *io)
Definition: XrdFileCacheFile.hh:92
std::map< IO *, IODetails > IoMap_t
Definition: XrdFileCacheFile.hh:248
float GetPrefetchScore() const
int m_prefetchReadCnt
Definition: XrdFileCacheFile.hh:278
int dec_ref_cnt()
Definition: XrdFileCacheFile.hh:221
void inc_ref_count(Block *)
IntList_t::iterator IntList_i
Definition: XrdFileCacheFile.hh:261
bool is_ok()
Definition: XrdFileCacheFile.hh:125
long long m_offset
offset of cached file for block-based / hdfs operation
Definition: XrdFileCacheFile.hh:235
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:264
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:266