xrootd
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;
70  int m_refcnt;
71  int m_errno; // stores negative errno
73  bool m_prefetch;
76 
77  Block(File *f, IO *io, char *buf, long long off, int size, int rsize, bool m_prefetch, bool cks_net) :
78  m_file(f), m_io(io), m_buff(buf), m_offset(off), m_size(size), m_req_size(rsize),
80  m_req_cksum_net(cks_net)
81  {}
82 
83  char* get_buff() { return m_buff; }
84  int get_size() { return m_size; }
85  int get_req_size() { return m_req_size; }
86  long long get_offset() { return m_offset; }
87 
88  IO* get_io() const { return m_io; }
89 
90  bool is_finished() { return m_downloaded || m_errno != 0; }
91  bool is_ok() { return m_downloaded; }
92  bool is_failed() { return m_errno != 0; }
93 
94  void set_downloaded() { m_downloaded = true; }
95  void set_error(int err) { m_errno = err; }
96 
98  {
99  m_errno = 0;
100  m_io = io;
101  }
102 
103  bool req_cksum_net() const { return m_req_cksum_net; }
104  bool has_cksums() const { return ! m_cksum_vec.empty(); }
106 };
107 
108 // ================================================================
109 
111 {
112 public:
114 
116 
117  virtual void Done(int result);
118 };
119 
120 // ================================================================
121 
123 {
124 public:
127  int m_errno;
128 
129  DirectResponseHandler(int to_wait) : m_cond(0), m_to_wait(to_wait), m_errno(0) {}
130 
131  bool is_finished() { XrdSysCondVarHelper _lck(m_cond); return m_to_wait == 0; }
132  bool is_ok() { XrdSysCondVarHelper _lck(m_cond); return m_to_wait == 0 && m_errno == 0; }
133  bool is_failed() { XrdSysCondVarHelper _lck(m_cond); return m_errno != 0; }
134 
135  virtual void Done(int result);
136 };
137 
138 // ================================================================
139 
140 class File
141 {
142 public:
143  //------------------------------------------------------------------------
145  //------------------------------------------------------------------------
146  File(const std::string &path, long long offset, long long fileSize);
147 
148  //------------------------------------------------------------------------
150  //------------------------------------------------------------------------
151  static File* FileOpen(const std::string &path, long long offset, long long fileSize);
152 
153  //------------------------------------------------------------------------
155  //------------------------------------------------------------------------
156  ~File();
157 
160 
162  void BlocksRemovedFromWriteQ(std::list<Block*>&);
163 
165  bool Open();
166 
168  int ReadV(IO *io, const XrdOucIOVec *readV, int n);
169 
171  int Read (IO *io, char* buff, long long offset, int size);
172 
173  //----------------------------------------------------------------------
175  //----------------------------------------------------------------------
176  bool isOpen() const { return m_is_open; }
177 
178  //----------------------------------------------------------------------
180  //----------------------------------------------------------------------
181  void ioUpdated(IO *io);
182 
183  //----------------------------------------------------------------------
186  //----------------------------------------------------------------------
187  bool ioActive(IO *io);
188 
189  //----------------------------------------------------------------------
192  //----------------------------------------------------------------------
194 
195  //----------------------------------------------------------------------
198  //----------------------------------------------------------------------
199  bool FinalizeSyncBeforeExit();
200 
201  //----------------------------------------------------------------------
203  //----------------------------------------------------------------------
204  void Sync();
205 
206 
207  void ProcessBlockResponse(BlockResponseHandler* brh, int res);
208  void WriteBlockToDisk(Block* b);
209 
210  void Prefetch();
211 
212  float GetPrefetchScore() const;
213 
215  const char* lPath() const;
216 
217  std::string& GetLocalPath() { return m_filename; }
218 
219  XrdSysError* GetLog();
221 
222  long long GetFileSize() { return m_file_size; }
223 
224  void AddIO(IO *io);
225  int GetPrefetchCountOnIO(IO *io);
226  void StopPrefetchingOnIO(IO *io);
227  void RemoveIO(IO *io);
228 
230 
231  std::string GetRemoteLocations() const;
233  size_t GetAccessCnt() const { return m_cfi.GetAccessCnt(); }
234  int GetBlockSize() const { return m_cfi.GetBufferSize(); }
235  int GetNBlocks() const { return m_cfi.GetNBlocks(); }
237 
238  // These three methods are called under Cache's m_active lock
239  int get_ref_cnt() { return m_ref_cnt; }
240  int inc_ref_cnt() { return ++m_ref_cnt; }
241  int dec_ref_cnt() { return --m_ref_cnt; }
242 
245 
246 private:
248 
249  int m_ref_cnt;
250 
251  bool m_is_open;
253 
257 
258  std::string m_filename;
259  long long m_offset;
260  long long m_file_size;
261 
262  // IO objects attached to this file.
263 
264  struct IODetails
265  {
269 
270  IODetails(time_t at) :
271  m_attach_time (at),
273  m_allow_prefetching (true)
274  {}
275  };
276 
277  typedef std::map<IO*, IODetails> IoMap_t;
278  typedef IoMap_t::iterator IoMap_i;
279 
283 
284  // fsync
285  std::vector<int> m_writes_during_sync;
287  bool m_in_sync;
288 
289  typedef std::list<int> IntList_t;
290  typedef IntList_t::iterator IntList_i;
291 
292  typedef std::list<Block*> BlockList_t;
293  typedef BlockList_t::iterator BlockList_i;
294 
295  typedef std::map<int, Block*> BlockMap_t;
296  typedef BlockMap_t::iterator BlockMap_i;
297 
298  typedef std::set<Block*> BlockSet_t;
299  typedef BlockSet_t::iterator BlockSet_i;
300 
301 
303 
305 
308 
309  std::set<std::string> m_remote_locations;
310  void insert_remote_location(const std::string &loc);
311 
313 
316  float m_prefetch_score; // cached
317 
319 
320  static const char *m_traceID;
321 
322  bool overlap(int blk, // block to query
323  long long blk_size, //
324  long long req_off, // offset of user request
325  int req_size, // size of user request
326  // output:
327  long long &off, // offset in user buffer
328  long long &blk_off, // offset in block
329  long long &size);
330 
331  // Read
332  Block* PrepareBlockRequest(int i, IO *io, bool prefetch);
333 
334  void ProcessBlockRequest (Block *b);
335  void ProcessBlockRequests(BlockList_t& blks);
336 
337  int RequestBlocksDirect(IO *io, DirectResponseHandler *handler, IntList_t& blocks,
338  char* buff, long long req_off, long long req_size);
339 
340  int ReadBlocksFromDisk(IntList_t& blocks,
341  char* req_buf, long long req_off, long long req_size);
342 
343  // VRead
344  bool VReadValidate (const XrdOucIOVec *readV, int n);
345  void VReadPreProcess (IO *io, const XrdOucIOVec *readV, int n,
346  BlockList_t& blks_to_request,
347  ReadVBlockListRAM& blks_to_process,
348  ReadVBlockListDisk& blks_on_disk,
349  std::vector<XrdOucIOVec>& chunkVec);
350  int VReadFromDisk (const XrdOucIOVec *readV, int n,
351  ReadVBlockListDisk& blks_on_disk);
352  int VReadProcessBlocks(IO *io, const XrdOucIOVec *readV, int n,
353  std::vector<ReadVChunkListRAM>& blks_to_process,
354  std::vector<ReadVChunkListRAM>& blks_processed,
355  long long& bytes_hit,
356  long long& bytes_missed);
357 
358  long long BufferSize();
359 
360  void inc_ref_count(Block*);
361  void dec_ref_count(Block*);
362  void free_block(Block*);
363 
364  bool select_current_io_or_disable_prefetching(bool skip_current);
365 
366  int offsetIdx(int idx);
367 };
368 
369 }
370 
371 #endif
void initiate_emergency_shutdown()
bool is_ok()
Definition: XrdPfcFile.hh:132
int get_req_size()
Definition: XrdPfcFile.hh:85
time_t m_attach_time
Definition: XrdPfcFile.hh:266
DirectResponseHandler(int to_wait)
Definition: XrdPfcFile.hh:129
void BlockRemovedFromWriteQ(Block *)
Handle removal of a block from Cache&#39;s write queue.
int get_size()
Definition: XrdPfcFile.hh:84
int GetBlockSize() const
Definition: XrdPfcFile.hh:234
bool m_req_cksum_net
Definition: XrdPfcFile.hh:74
PrefetchState_e
Definition: XrdPfcFile.hh:247
std::vector< int > m_writes_during_sync
Definition: XrdPfcFile.hh:285
std::list< Block * > BlockList_t
Definition: XrdPfcFile.hh:292
bool is_finished()
Definition: XrdPfcFile.hh:131
void insert_remote_location(const std::string &loc)
Definition: XrdPfcFile.hh:60
size_t GetAccessCnt() const
Get number of accesses.
Definition: XrdPfcInfo.hh:267
void ProcessBlockResponse(BlockResponseHandler *brh, int res)
bool select_current_io_or_disable_prefetching(bool skip_current)
void BlocksRemovedFromWriteQ(std::list< Block *> &)
Handle removal of a set of blocks from Cache&#39;s write queue.
int m_size
Definition: XrdPfcFile.hh:68
void inc_ref_count(Block *)
std::map< int, Block * > BlockMap_t
Definition: XrdPfcFile.hh:295
bool is_finished()
Definition: XrdPfcFile.hh:90
IODetails(time_t at)
Definition: XrdPfcFile.hh:270
bool has_cksums() const
Definition: XrdPfcFile.hh:104
void free_block(Block *)
Stats m_stats
cache statistics for this instance
Definition: XrdPfcFile.hh:306
bool m_is_open
open state (presumably not needed anymore)
Definition: XrdPfcFile.hh:251
int GetNBlocks() const
Definition: XrdPfcFile.hh:235
IoMap_t m_io_map
Definition: XrdPfcFile.hh:280
void Sync()
Sync file cache inf o and output data with disk.
long long GetFileSize()
Definition: XrdPfcFile.hh:222
int m_to_wait
Definition: XrdPfcFile.hh:126
Definition: XrdPfcFile.hh:264
bool m_detach_time_logged
Definition: XrdPfcFile.hh:318
int GetNBlocks() const
Get number of blocks represented in download-state bit-vector.
Definition: XrdPfcInfo.hh:437
std::map< IO *, IODetails > IoMap_t
Definition: XrdPfcFile.hh:277
Base cache-io class that implements XrdOucCacheIO abstract methods.
Definition: XrdPfcIO.hh:16
IntList_t::iterator IntList_i
Definition: XrdPfcFile.hh:290
Block(File *f, IO *io, char *buf, long long off, int size, int rsize, bool m_prefetch, bool cks_net)
Definition: XrdPfcFile.hh:77
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:127
Info m_cfi
download status of file blocks and access statistics
Definition: XrdPfcFile.hh:256
Status of cached file. Can be read from and written into a binary file.
Definition: XrdPfcInfo.hh:44
void Prefetch()
Access statistics.
Definition: XrdPfcInfo.hh:60
void set_error(int err)
Definition: XrdPfcFile.hh:95
int m_refcnt
Definition: XrdPfcFile.hh:70
Definition: XrdPfcFile.hh:110
bool ioActive(IO *io)
Initiate close. Return true if still IO active. Used in XrdPosixXrootd::Close()
float m_prefetch_score
Definition: XrdPfcFile.hh:316
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:94
XrdSysCondVar m_cond
Definition: XrdPfcFile.hh:125
bool FinalizeSyncBeforeExit()
Returns true if any of blocks need sync. Called from Cache::dec_ref_cnt on zero ref cnt...
const Info::AStat * GetLastAccessStats() const
Definition: XrdPfcFile.hh:232
const char * lPath() const
Log path.
const AStat * GetLastAccessStats() const
Get latest access stats.
std::string GetRemoteLocations() const
int inc_ref_cnt()
Definition: XrdPfcFile.hh:240
vCkSum_t & ref_cksum_vec()
Definition: XrdPfcFile.hh:105
std::vector< uint32_t > vCkSum_t
Definition: XrdPfcTypes.hh:27
bool is_failed()
Definition: XrdPfcFile.hh:92
int GetNDownloadedBlocks() const
Get number of downloaded blocks.
Definition: XrdPfcInfo.hh:398
int m_errno
Definition: XrdPfcFile.hh:71
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:282
XrdOssDF * m_data_file
file handle for data file on disk
Definition: XrdPfcFile.hh:254
int GetPrefetchCountOnIO(IO *io)
IO * get_io() const
Definition: XrdPfcFile.hh:88
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:86
Definition: XrdPfc.hh:40
BlockList_t::iterator BlockList_i
Definition: XrdPfcFile.hh:293
IoMap_t::iterator IoMap_i
Definition: XrdPfcFile.hh:278
int VReadFromDisk(const XrdOucIOVec *readV, int n, ReadVBlockListDisk &blks_on_disk)
long long BufferSize()
long long GetBufferSize() const
Get prefetch buffer size.
Definition: XrdPfcInfo.hh:467
BlockSet_t::iterator BlockSet_i
Definition: XrdPfcFile.hh:299
static const char * m_traceID
Definition: XrdPfcFile.hh:320
int m_prefetch_hit_cnt
Definition: XrdPfcFile.hh:315
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:302
XrdOssDF * m_info_file
file handle for data-info file on disk
Definition: XrdPfcFile.hh:255
void RequestSyncOfDetachStats()
Flags that detach stats should be written out in final sync. Called from CacheIO upon Detach...
Definition: XrdPfcFile.hh:247
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)
Stats m_last_stats
copy of cache stats during last purge cycle, used for per directory stat reporting ...
Definition: XrdPfcFile.hh:307
Definition: XrdSysPthread.hh:78
std::string & GetLocalPath()
Definition: XrdPfcFile.hh:217
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 req_cksum_net() const
Definition: XrdPfcFile.hh:103
int m_req_size
Definition: XrdPfcFile.hh:69
IoMap_i m_current_io
IO object to be used for prefetching.
Definition: XrdPfcFile.hh:281
virtual void Done(int result)
Definition: XrdPfcFile.hh:247
File(const std::string &path, long long offset, long long fileSize)
Constructor.
Definition: XrdOucIOVec.hh:40
Definition: XrdClAnyObject.hh:25
char * get_buff()
Definition: XrdPfcFile.hh:83
bool m_downloaded
Definition: XrdPfcFile.hh:72
void reset_error_and_set_io(IO *io)
Definition: XrdPfcFile.hh:97
BlockResponseHandler(Block *b)
Definition: XrdPfcFile.hh:115
void ProcessBlockRequest(Block *b)
long long m_offset
Definition: XrdPfcFile.hh:67
long long m_offset
offset of cached file for block-based / hdfs operation
Definition: XrdPfcFile.hh:259
std::list< int > IntList_t
Definition: XrdPfcFile.hh:289
File * m_file
Definition: XrdPfcFile.hh:63
int get_ref_cnt()
Definition: XrdPfcFile.hh:239
Statistics of cache utilisation by a File object.
Definition: XrdPfcStats.hh:30
Definition: XrdSysPthread.hh:129
Definition: XrdOucCache.hh:52
XrdSysCondVar m_state_cond
Definition: XrdPfcFile.hh:304
int m_non_flushed_cnt
Definition: XrdPfcFile.hh:286
void WriteBlockToDisk(Block *b)
bool m_in_sync
Definition: XrdPfcFile.hh:287
Definition: XrdPfcFile.hh:122
long long m_file_size
size of cached disk file for block-based operation
Definition: XrdPfcFile.hh:260
Definition: XrdPfcFile.hh:140
vCkSum_t m_cksum_vec
Definition: XrdPfcFile.hh:75
void ioUpdated(IO *io)
Notification from IO that it has been updated (remote open).
XrdSysError * GetLog()
Block * m_block
Definition: XrdPfcFile.hh:113
int GetNDownloadedBlocks() const
Definition: XrdPfcFile.hh:236
Definition: XrdPfcFile.hh:247
bool VReadValidate(const XrdOucIOVec *readV, int n)
Definition: XrdPfcFile.hh:247
Definition: XrdOss.hh:62
int m_prefetch_read_cnt
Definition: XrdPfcFile.hh:314
std::set< std::string > m_remote_locations
Gathered in AddIO / ioUpdate / ioActive.
Definition: XrdPfcFile.hh:309
char * m_buff
Definition: XrdPfcFile.hh:66
BlockMap_t::iterator BlockMap_i
Definition: XrdPfcFile.hh:296
void dec_ref_count(Block *)
bool m_allow_prefetching
Definition: XrdPfcFile.hh:268
bool m_prefetch
Definition: XrdPfcFile.hh:73
size_t GetAccessCnt() const
Definition: XrdPfcFile.hh:233
bool is_in_emergency_shutdown()
Definition: XrdPfcFile.hh:244
void StopPrefetchingOnIO(IO *io)
~File()
Destructor.
bool is_ok()
Definition: XrdPfcFile.hh:91
void ProcessBlockRequests(BlockList_t &blks)
std::string m_filename
filename of data file on disk
Definition: XrdPfcFile.hh:258
int m_ref_cnt
number of references from IO or sync
Definition: XrdPfcFile.hh:249
bool m_in_shutdown
file is in emergency shutdown due to irrecoverable error or unlink request
Definition: XrdPfcFile.hh:252
Definition: XrdPfcFile.hh:247
bool Open()
Open file handle for data file and info file on local disk.
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:133
int dec_ref_cnt()
Definition: XrdPfcFile.hh:241
float GetPrefetchScore() const
PrefetchState_e m_prefetch_state
Definition: XrdPfcFile.hh:312
bool isOpen() const
Data and cinfo files are open.
Definition: XrdPfcFile.hh:176
std::set< Block * > BlockSet_t
Definition: XrdPfcFile.hh:298
int Read(IO *io, char *buff, long long offset, int size)
Normal read.
Definition: XrdJob.hh:42
void RemoveIO(IO *io)
int m_active_prefetches
Definition: XrdPfcFile.hh:267