xrootd
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
XrdPfcFile.hh
Go to the documentation of this file.
1 #ifndef __XRDPFC_FILE_HH__
2 #define __XRDPFC_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/XrdOucCache.hh"
25 #include "XrdOuc/XrdOucIOVec.hh"
26 
27 #include "XrdPfcInfo.hh"
28 #include "XrdPfcStats.hh"
29 
30 #include <string>
31 #include <map>
32 #include <set>
33 
34 class XrdJob;
35 class XrdOucIOVec;
36 
37 namespace XrdCl
38 {
39 class Log;
40 }
41 
42 namespace XrdPfc
43 {
44 class BlockResponseHandler;
45 class DirectResponseHandler;
46 class IO;
47 
48 struct ReadVBlockListRAM;
49 struct ReadVChunkListRAM;
50 struct ReadVBlockListDisk;
51 struct ReadVChunkListDisk;
52 }
53 
54 
55 namespace XrdPfc
56 {
57 
58 class File;
59 
60 class Block
61 {
62 public:
64  IO *m_io; // IO that handled current request, used for == / != comparisons only
65 
66  char *m_buff;
67  long long m_offset;
68  int m_size;
69  int m_refcnt;
70  int m_errno; // stores negative errno
72  bool m_prefetch;
73 
74  Block(File *f, IO *io, char *buf, long long off, int size, bool m_prefetch) :
75  m_file(f), m_io(io), m_buff(buf), m_offset(off), m_size(size),
76  m_refcnt(0), m_errno(0), m_downloaded(false), m_prefetch(m_prefetch)
77  {}
78 
79  char* get_buff() { return m_buff; }
80  int get_size() { return m_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  void BlocksRemovedFromWriteQ(std::list<Block*>&);
156 
158  bool Open();
159 
161  int ReadV(IO *io, const XrdOucIOVec *readV, int n);
162 
164  int Read (IO *io, char* buff, long long offset, int size);
165 
166  //----------------------------------------------------------------------
168  //----------------------------------------------------------------------
169  bool isOpen() const { return m_is_open; }
170 
171  //----------------------------------------------------------------------
174  //----------------------------------------------------------------------
175  bool ioActive(IO *io);
176 
177  //----------------------------------------------------------------------
180  //----------------------------------------------------------------------
182 
183  //----------------------------------------------------------------------
186  //----------------------------------------------------------------------
187  bool FinalizeSyncBeforeExit();
188 
189  //----------------------------------------------------------------------
191  //----------------------------------------------------------------------
192  void Sync();
193 
194 
195  void ProcessBlockResponse(BlockResponseHandler* brh, int res);
196  void WriteBlockToDisk(Block* b);
197 
198  void Prefetch();
199 
200  float GetPrefetchScore() const;
201 
203  const char* lPath() const;
204 
205  std::string& GetLocalPath() { return m_filename; }
206 
207  XrdSysError* GetLog();
209 
210  long long GetFileSize() { return m_file_size; }
211 
212  void AddIO(IO *io);
213  int GetPrefetchCountOnIO(IO *io);
214  void StopPrefetchingOnIO(IO *io);
215  void RemoveIO(IO *io);
216 
218 
220  size_t GetAccessCnt() const { return m_cfi.GetAccessCnt(); }
221  int GetBlockSize() const { return m_cfi.GetBufferSize(); }
222  int GetNBlocks() const { return m_cfi.GetSizeInBits(); }
224 
225  // These three methods are called under Cache's m_active lock
226  int get_ref_cnt() { return m_ref_cnt; }
227  int inc_ref_cnt() { return ++m_ref_cnt; }
228  int dec_ref_cnt() { return --m_ref_cnt; }
229 
232 
233 private:
235 
236  int m_ref_cnt;
237 
238  bool m_is_open;
240 
244 
245  std::string m_filename;
246  long long m_offset;
247  long long m_file_size;
248 
249  // IO objects attached to this file.
250 
251  struct IODetails
252  {
257 
258  IODetails(time_t at) :
259  m_attach_time (at),
261  m_allow_prefetching (true),
263  {}
264  };
265 
266  typedef std::map<IO*, IODetails> IoMap_t;
267  typedef IoMap_t::iterator IoMap_i;
268 
272 
273  // fsync
274  std::vector<int> m_writes_during_sync;
276  bool m_in_sync;
277 
278  typedef std::list<int> IntList_t;
279  typedef IntList_t::iterator IntList_i;
280 
281  typedef std::list<Block*> BlockList_t;
282  typedef BlockList_t::iterator BlockList_i;
283 
284  typedef std::map<int, Block*> BlockMap_t;
285  typedef BlockMap_t::iterator BlockMap_i;
286 
287  typedef std::set<Block*> BlockSet_t;
288  typedef BlockSet_t::iterator BlockSet_i;
289 
290 
292 
294 
297 
299 
302  float m_prefetch_score; // cached
303 
305 
306  static const char *m_traceID;
307 
308  bool overlap(int blk, // block to query
309  long long blk_size, //
310  long long req_off, // offset of user request
311  int req_size, // size of user request
312  // output:
313  long long &off, // offset in user buffer
314  long long &blk_off, // offset in block
315  long long &size);
316 
317  // Read
318  Block* PrepareBlockRequest(int i, IO *io, bool prefetch);
319 
320  void ProcessBlockRequest (Block *b, bool prefetch);
321  void ProcessBlockRequests(BlockList_t& blks, bool prefetch);
322 
323  int RequestBlocksDirect(IO *io, DirectResponseHandler *handler, IntList_t& blocks,
324  char* buff, long long req_off, long long req_size);
325 
326  int ReadBlocksFromDisk(IntList_t& blocks,
327  char* req_buf, long long req_off, long long req_size);
328 
329  // VRead
330  bool VReadValidate (const XrdOucIOVec *readV, int n);
331  void VReadPreProcess (IO *io, const XrdOucIOVec *readV, int n,
332  BlockList_t& blks_to_request,
333  ReadVBlockListRAM& blks_to_process,
334  ReadVBlockListDisk& blks_on_disk,
335  std::vector<XrdOucIOVec>& chunkVec);
336  int VReadFromDisk (const XrdOucIOVec *readV, int n,
337  ReadVBlockListDisk& blks_on_disk);
338  int VReadProcessBlocks(IO *io, const XrdOucIOVec *readV, int n,
339  std::vector<ReadVChunkListRAM>& blks_to_process,
340  std::vector<ReadVChunkListRAM>& blks_processed,
341  long long& bytes_hit,
342  long long& bytes_missed);
343 
344  long long BufferSize();
345 
346  void inc_ref_count(Block*);
347  void dec_ref_count(Block*);
348  void free_block(Block*);
349 
350  bool select_current_io_or_disable_prefetching(bool skip_current);
351 
352  int offsetIdx(int idx);
353 };
354 
355 }
356 
357 #endif
void initiate_emergency_shutdown()
bool is_ok()
Definition: XrdPfcFile.hh:125
time_t m_attach_time
Definition: XrdPfcFile.hh:253
DirectResponseHandler(int to_wait)
Definition: XrdPfcFile.hh:122
void BlockRemovedFromWriteQ(Block *)
Handle removal of a block from Cache&#39;s write queue.
int get_size()
Definition: XrdPfcFile.hh:80
PrefetchState_e
Definition: XrdPfcFile.hh:234
std::vector< int > m_writes_during_sync
Definition: XrdPfcFile.hh:274
std::list< Block * > BlockList_t
Definition: XrdPfcFile.hh:281
size_t GetAccessCnt() const
Get number of accesses.
Definition: XrdPfcInfo.hh:261
bool is_finished()
Definition: XrdPfcFile.hh:124
Definition: XrdPfcFile.hh:60
void ProcessBlockResponse(BlockResponseHandler *brh, int res)
bool select_current_io_or_disable_prefetching(bool skip_current)
int m_size
Definition: XrdPfcFile.hh:68
void inc_ref_count(Block *)
std::map< int, Block * > BlockMap_t
Definition: XrdPfcFile.hh:284
bool is_finished()
Definition: XrdPfcFile.hh:85
IODetails(time_t at)
Definition: XrdPfcFile.hh:258
int GetBlockSize() const
Definition: XrdPfcFile.hh:221
void free_block(Block *)
Stats m_stats
cache statistics for this instance
Definition: XrdPfcFile.hh:295
bool m_is_open
open state (presumably not needed anymore)
Definition: XrdPfcFile.hh:238
IoMap_t m_io_map
Definition: XrdPfcFile.hh:269
void Sync()
Sync file cache inf o and output data with disk.
long long GetFileSize()
Definition: XrdPfcFile.hh:210
int m_to_wait
Definition: XrdPfcFile.hh:119
Definition: XrdPfcFile.hh:251
bool m_detach_time_logged
Definition: XrdPfcFile.hh:304
std::map< IO *, IODetails > IoMap_t
Definition: XrdPfcFile.hh:266
Base cache-io class that implements XrdOucCacheIO abstract methods.
Definition: XrdPfcIO.hh:16
IntList_t::iterator IntList_i
Definition: XrdPfcFile.hh:279
IO * m_io
Definition: XrdPfcFile.hh:64
int RequestBlocksDirect(IO *io, DirectResponseHandler *handler, IntList_t &blocks, char *buff, long long req_off, long long req_size)
int m_errno
Definition: XrdPfcFile.hh:120
Info m_cfi
download status of file blocks and access statistics
Definition: XrdPfcFile.hh:243
Status of cached file. Can be read from and written into a binary file.
Definition: XrdPfcInfo.hh:48
void Prefetch()
Access statistics.
Definition: XrdPfcInfo.hh:52
void set_error(int err)
Definition: XrdPfcFile.hh:90
int m_refcnt
Definition: XrdPfcFile.hh:69
const char * lPath() const
Log path.
Definition: XrdPfcFile.hh:101
bool ioActive(IO *io)
Initiate close. Return true if still IO active. Used in XrdPosixXrootd::Close()
float m_prefetch_score
Definition: XrdPfcFile.hh:302
Block * PrepareBlockRequest(int i, IO *io, bool prefetch)
void VReadPreProcess(IO *io, const XrdOucIOVec *readV, int n, BlockList_t &blks_to_request, ReadVBlockListRAM &blks_to_process, ReadVBlockListDisk &blks_on_disk, std::vector< XrdOucIOVec > &chunkVec)
virtual void Done(int result)
void set_downloaded()
Definition: XrdPfcFile.hh:89
XrdSysCondVar m_cond
Definition: XrdPfcFile.hh:118
float GetPrefetchScore() const
bool FinalizeSyncBeforeExit()
Returns true if any of blocks need sync. Called from Cache::dec_ref_cnt on zero ref cnt...
int GetNDownloadedBlocks() const
Definition: XrdPfcFile.hh:223
int inc_ref_cnt()
Definition: XrdPfcFile.hh:227
void ProcessBlockRequest(Block *b, bool prefetch)
bool is_failed()
Definition: XrdPfcFile.hh:87
int m_errno
Definition: XrdPfcFile.hh:70
Definition: XrdSysError.hh:89
int m_ios_in_detach
Number of IO objects to which we replied false to ioActive() and will be removed soon.
Definition: XrdPfcFile.hh:271
XrdOssDF * m_data_file
file handle for data file on disk
Definition: XrdPfcFile.hh:241
int GetPrefetchCountOnIO(IO *io)
Definition: XrdSysTrace.hh:48
int ReadBlocksFromDisk(IntList_t &blocks, char *req_buf, long long req_off, long long req_size)
long long get_offset()
Definition: XrdPfcFile.hh:81
bool m_ioactive_false_reported
Definition: XrdPfcFile.hh:256
BlockList_t::iterator BlockList_i
Definition: XrdPfcFile.hh:282
IoMap_t::iterator IoMap_i
Definition: XrdPfcFile.hh:267
int VReadFromDisk(const XrdOucIOVec *readV, int n, ReadVBlockListDisk &blks_on_disk)
long long BufferSize()
BlockSet_t::iterator BlockSet_i
Definition: XrdPfcFile.hh:288
static const char * m_traceID
Definition: XrdPfcFile.hh:306
int m_prefetch_hit_cnt
Definition: XrdPfcFile.hh:301
int ReadV(IO *io, const XrdOucIOVec *readV, int n)
Vector read from disk if block is already downloaded, else ReadV from client.
void AddIO(IO *io)
BlockMap_t m_block_map
Definition: XrdPfcFile.hh:291
XrdOssDF * m_info_file
file handle for data-info file on disk
Definition: XrdPfcFile.hh:242
void RequestSyncOfDetachStats()
Flags that detach stats should be written out in final sync. Called from CacheIO upon Detach...
Definition: XrdPfcFile.hh:234
int offsetIdx(int idx)
int VReadProcessBlocks(IO *io, const XrdOucIOVec *readV, int n, std::vector< ReadVChunkListRAM > &blks_to_process, std::vector< ReadVChunkListRAM > &blks_processed, long long &bytes_hit, long long &bytes_missed)
const Info::AStat * GetLastAccessStats() const
Definition: XrdPfcFile.hh:219
Stats m_last_stats
copy of cache stats during last purge cycle, used for per directory stat reporting ...
Definition: XrdPfcFile.hh:296
Definition: XrdSysPthread.hh:78
std::string & GetLocalPath()
Definition: XrdPfcFile.hh:205
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)
bool isOpen() const
Data and cinfo files are open.
Definition: XrdPfcFile.hh:169
int GetNBlocks() const
Definition: XrdPfcFile.hh:222
Block(File *f, IO *io, char *buf, long long off, int size, bool m_prefetch)
Definition: XrdPfcFile.hh:74
IoMap_i m_current_io
IO object to be used for prefetching.
Definition: XrdPfcFile.hh:270
virtual void Done(int result)
Definition: XrdPfcFile.hh:234
File(const std::string &path, long long offset, long long fileSize)
Constructor.
Definition: XrdOucIOVec.hh:40
char * get_buff()
Definition: XrdPfcFile.hh:79
bool m_downloaded
Definition: XrdPfcFile.hh:71
void reset_error_and_set_io(IO *io)
Definition: XrdPfcFile.hh:92
const AStat * GetLastAccessStats() const
Get latest access stats.
long long m_offset
Definition: XrdPfcFile.hh:67
long long m_offset
offset of cached file for block-based / hdfs operation
Definition: XrdPfcFile.hh:246
std::list< int > IntList_t
Definition: XrdPfcFile.hh:278
IO * get_io() const
Definition: XrdPfcFile.hh:83
File * m_file
Definition: XrdPfcFile.hh:63
int get_ref_cnt()
Definition: XrdPfcFile.hh:226
Statistics of cache utilisation by a File object.
Definition: XrdPfcStats.hh:30
Definition: XrdSysPthread.hh:129
Definition: XrdOucCache.hh:50
XrdSysCondVar m_state_cond
Definition: XrdPfcFile.hh:293
int m_non_flushed_cnt
Definition: XrdPfcFile.hh:275
void WriteBlockToDisk(Block *b)
bool m_in_sync
Definition: XrdPfcFile.hh:276
Definition: XrdPfcFile.hh:115
long long m_file_size
size of cached disk file for block-based operation
Definition: XrdPfcFile.hh:247
Definition: XrdPfcFile.hh:133
XrdSysError * GetLog()
Block * m_block
Definition: XrdPfcFile.hh:104
Definition: XrdPfcFile.hh:234
bool VReadValidate(const XrdOucIOVec *readV, int n)
Definition: XrdPfcFile.hh:234
Definition: XrdOss.hh:61
int m_prefetch_read_cnt
Definition: XrdPfcFile.hh:300
char * m_buff
Definition: XrdPfcFile.hh:66
BlockMap_t::iterator BlockMap_i
Definition: XrdPfcFile.hh:285
void dec_ref_count(Block *)
bool m_allow_prefetching
Definition: XrdPfcFile.hh:255
int GetSizeInBits() const
Get number of blocks represented in download-state bit-vector.
Definition: XrdPfcInfo.hh:398
bool m_prefetch
Definition: XrdPfcFile.hh:72
bool is_in_emergency_shutdown()
Definition: XrdPfcFile.hh:231
void StopPrefetchingOnIO(IO *io)
~File()
Destructor.
bool is_ok()
Definition: XrdPfcFile.hh:86
std::string m_filename
filename of data file on disk
Definition: XrdPfcFile.hh:245
int m_ref_cnt
number of references from IO or sync
Definition: XrdPfcFile.hh:236
bool m_in_shutdown
file is in emergency shutdown due to irrecoverable error or unlink request
Definition: XrdPfcFile.hh:239
Definition: XrdPfcFile.hh:234
void ProcessBlockRequests(BlockList_t &blks, bool prefetch)
bool m_for_prefetch
Definition: XrdPfcFile.hh:105
bool Open()
Open file handle for data file and info file on local disk.
void BlocksRemovedFromWriteQ(std::list< Block * > &)
Handle removal of a set of blocks from Cache&#39;s write queue.
XrdSysTrace * GetTrace()
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 DeltaStatsFromLastCall()
bool is_failed()
Definition: XrdPfcFile.hh:126
int dec_ref_cnt()
Definition: XrdPfcFile.hh:228
PrefetchState_e m_prefetch_state
Definition: XrdPfcFile.hh:298
std::set< Block * > BlockSet_t
Definition: XrdPfcFile.hh:287
BlockResponseHandler(Block *b, bool prefetch)
Definition: XrdPfcFile.hh:107
int Read(IO *io, char *buff, long long offset, int size)
Normal read.
long long GetBufferSize() const
Get prefetch buffer size.
Definition: XrdPfcInfo.hh:428
Definition: XrdJob.hh:42
void RemoveIO(IO *io)
int m_active_prefetches
Definition: XrdPfcFile.hh:254
int GetNDownloadedBlocks() const
Get number of downloaded blocks.
Definition: XrdPfcInfo.hh:359
size_t GetAccessCnt() const
Definition: XrdPfcFile.hh:220