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;
69  int m_refcnt;
70  int m_errno; // stores negative errno
72  bool m_prefetch;
75 
76  Block(File *f, IO *io, char *buf, long long off, int size, bool m_prefetch, bool cks_net) :
77  m_file(f), m_io(io), m_buff(buf), m_offset(off), m_size(size),
79  m_req_cksum_net(cks_net)
80  {}
81 
82  char* get_buff() { return m_buff; }
83  int get_size() { return m_size; }
84  long long get_offset() { return m_offset; }
85 
86  IO* get_io() const { return m_io; }
87 
88  bool is_finished() { return m_downloaded || m_errno != 0; }
89  bool is_ok() { return m_downloaded; }
90  bool is_failed() { return m_errno != 0; }
91 
92  void set_downloaded() { m_downloaded = true; }
93  void set_error(int err) { m_errno = err; }
94 
96  {
97  m_errno = 0;
98  m_io = io;
99  }
100 
101  bool req_cksum_net() const { return m_req_cksum_net; }
102  bool has_cksums() const { return ! m_cksum_vec.empty(); }
104 };
105 
106 // ================================================================
107 
109 {
110 public:
112 
114 
115  virtual void Done(int result);
116 };
117 
118 // ================================================================
119 
121 {
122 public:
125  int m_errno;
126 
127  DirectResponseHandler(int to_wait) : m_cond(0), m_to_wait(to_wait), m_errno(0) {}
128 
129  bool is_finished() { XrdSysCondVarHelper _lck(m_cond); return m_to_wait == 0; }
130  bool is_ok() { XrdSysCondVarHelper _lck(m_cond); return m_to_wait == 0 && m_errno == 0; }
131  bool is_failed() { XrdSysCondVarHelper _lck(m_cond); return m_errno != 0; }
132 
133  virtual void Done(int result);
134 };
135 
136 // ================================================================
137 
138 class File
139 {
140 public:
141  //------------------------------------------------------------------------
143  //------------------------------------------------------------------------
144  File(const std::string &path, long long offset, long long fileSize);
145 
146  //------------------------------------------------------------------------
148  //------------------------------------------------------------------------
149  static File* FileOpen(const std::string &path, long long offset, long long fileSize);
150 
151  //------------------------------------------------------------------------
153  //------------------------------------------------------------------------
154  ~File();
155 
158 
160  void BlocksRemovedFromWriteQ(std::list<Block*>&);
161 
163  bool Open();
164 
166  int ReadV(IO *io, const XrdOucIOVec *readV, int n);
167 
169  int Read (IO *io, char* buff, long long offset, int size);
170 
171  //----------------------------------------------------------------------
173  //----------------------------------------------------------------------
174  bool isOpen() const { return m_is_open; }
175 
176  //----------------------------------------------------------------------
178  //----------------------------------------------------------------------
179  void ioUpdated(IO *io);
180 
181  //----------------------------------------------------------------------
184  //----------------------------------------------------------------------
185  bool ioActive(IO *io);
186 
187  //----------------------------------------------------------------------
190  //----------------------------------------------------------------------
192 
193  //----------------------------------------------------------------------
196  //----------------------------------------------------------------------
198 
199  //----------------------------------------------------------------------
201  //----------------------------------------------------------------------
202  void Sync();
203 
204 
207 
208  void Prefetch();
209 
210  float GetPrefetchScore() const;
211 
213  const char* lPath() const;
214 
215  std::string& GetLocalPath() { return m_filename; }
216 
219 
220  long long GetFileSize() { return m_file_size; }
221 
222  void AddIO(IO *io);
225  void RemoveIO(IO *io);
226 
228 
229  std::string GetRemoteLocations() const;
231  size_t GetAccessCnt() const { return m_cfi.GetAccessCnt(); }
232  int GetBlockSize() const { return m_cfi.GetBufferSize(); }
233  int GetNBlocks() const { return m_cfi.GetNBlocks(); }
235 
236  // These three methods are called under Cache's m_active lock
237  int get_ref_cnt() { return m_ref_cnt; }
238  int inc_ref_cnt() { return ++m_ref_cnt; }
239  int dec_ref_cnt() { return --m_ref_cnt; }
240 
243 
244 private:
246 
247  int m_ref_cnt;
248 
249  bool m_is_open;
251 
255 
256  std::string m_filename;
257  long long m_offset;
258  long long m_file_size;
259 
260  // IO objects attached to this file.
261 
262  struct IODetails
263  {
268 
269  IODetails(time_t at) :
270  m_attach_time (at),
272  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 
336 
338  char* buff, long long req_off, long long req_size);
339 
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 
363 
365 
366  int offsetIdx(int idx);
367 };
368 
369 }
370 
371 #endif
Definition: XrdJob.hh:43
Definition: XrdOss.hh:63
Definition: XrdOucCache.hh:53
Definition: XrdPfcFile.hh:109
Block * m_block
Definition: XrdPfcFile.hh:111
virtual void Done(int result)
BlockResponseHandler(Block *b)
Definition: XrdPfcFile.hh:113
Definition: XrdPfcFile.hh:61
IO * m_io
Definition: XrdPfcFile.hh:64
File * m_file
Definition: XrdPfcFile.hh:63
void reset_error_and_set_io(IO *io)
Definition: XrdPfcFile.hh:95
int m_size
Definition: XrdPfcFile.hh:68
void set_error(int err)
Definition: XrdPfcFile.hh:93
vCkSum_t & ref_cksum_vec()
Definition: XrdPfcFile.hh:103
bool m_prefetch
Definition: XrdPfcFile.hh:72
Block(File *f, IO *io, char *buf, long long off, int size, bool m_prefetch, bool cks_net)
Definition: XrdPfcFile.hh:76
IO * get_io() const
Definition: XrdPfcFile.hh:86
long long get_offset()
Definition: XrdPfcFile.hh:84
void set_downloaded()
Definition: XrdPfcFile.hh:92
int m_errno
Definition: XrdPfcFile.hh:70
bool req_cksum_net() const
Definition: XrdPfcFile.hh:101
int m_refcnt
Definition: XrdPfcFile.hh:69
char * m_buff
Definition: XrdPfcFile.hh:66
bool has_cksums() const
Definition: XrdPfcFile.hh:102
long long m_offset
Definition: XrdPfcFile.hh:67
bool m_downloaded
Definition: XrdPfcFile.hh:71
vCkSum_t m_cksum_vec
Definition: XrdPfcFile.hh:74
bool is_failed()
Definition: XrdPfcFile.hh:90
bool is_ok()
Definition: XrdPfcFile.hh:89
bool is_finished()
Definition: XrdPfcFile.hh:88
bool m_req_cksum_net
Definition: XrdPfcFile.hh:73
int get_size()
Definition: XrdPfcFile.hh:83
char * get_buff()
Definition: XrdPfcFile.hh:82
Definition: XrdPfcFile.hh:121
int m_to_wait
Definition: XrdPfcFile.hh:124
virtual void Done(int result)
bool is_ok()
Definition: XrdPfcFile.hh:130
DirectResponseHandler(int to_wait)
Definition: XrdPfcFile.hh:127
bool is_failed()
Definition: XrdPfcFile.hh:131
bool is_finished()
Definition: XrdPfcFile.hh:129
XrdSysCondVar m_cond
Definition: XrdPfcFile.hh:123
int m_errno
Definition: XrdPfcFile.hh:125
Definition: XrdPfcFile.hh:139
XrdOssDF * m_info_file
file handle for data-info file on disk
Definition: XrdPfcFile.hh:253
std::list< Block * > BlockList_t
Definition: XrdPfcFile.hh:292
void ProcessBlockRequests(BlockList_t &blks)
const char * lPath() const
Log path.
long long m_offset
offset of cached file for block-based / hdfs operation
Definition: XrdPfcFile.hh:257
void inc_ref_count(Block *)
long long BufferSize()
bool isOpen() const
Data and cinfo files are open.
Definition: XrdPfcFile.hh:174
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)
bool VReadValidate(const XrdOucIOVec *readV, int n)
void free_block(Block *)
int m_non_flushed_cnt
Definition: XrdPfcFile.hh:286
void RemoveIO(IO *io)
IntList_t::iterator IntList_i
Definition: XrdPfcFile.hh:290
IoMap_t m_io_map
Definition: XrdPfcFile.hh:280
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)
XrdSysTrace * GetTrace()
int GetNBlocks() const
Definition: XrdPfcFile.hh:233
void ioUpdated(IO *io)
Notification from IO that it has been updated (remote open).
BlockMap_t::iterator BlockMap_i
Definition: XrdPfcFile.hh:296
int RequestBlocksDirect(IO *io, DirectResponseHandler *handler, IntList_t &blocks, char *buff, long long req_off, long long req_size)
bool m_in_sync
Definition: XrdPfcFile.hh:287
int offsetIdx(int idx)
File(const std::string &path, long long offset, long long fileSize)
Constructor.
void StopPrefetchingOnIO(IO *io)
void RequestSyncOfDetachStats()
Flags that detach stats should be written out in final sync. Called from CacheIO upon Detach.
BlockSet_t::iterator BlockSet_i
Definition: XrdPfcFile.hh:299
const Info::AStat * GetLastAccessStats() const
Definition: XrdPfcFile.hh:230
Stats m_last_stats
copy of cache stats during last purge cycle, used for per directory stat reporting
Definition: XrdPfcFile.hh:307
size_t GetAccessCnt() const
Definition: XrdPfcFile.hh:231
int m_prefetch_read_cnt
Definition: XrdPfcFile.hh:314
void BlocksRemovedFromWriteQ(std::list< Block * > &)
Handle removal of a set of blocks from Cache's write queue.
bool ioActive(IO *io)
Initiate close. Return true if still IO active. Used in XrdPosixXrootd::Close()
void Sync()
Sync file cache inf o and output data with disk.
Stats m_stats
cache statistics for this instance
Definition: XrdPfcFile.hh:306
XrdSysError * GetLog()
static const char * m_traceID
Definition: XrdPfcFile.hh:320
XrdSysCondVar m_state_cond
Definition: XrdPfcFile.hh:304
float GetPrefetchScore() const
IoMap_i m_current_io
IO object to be used for prefetching.
Definition: XrdPfcFile.hh:281
int GetBlockSize() const
Definition: XrdPfcFile.hh:232
int GetNDownloadedBlocks() const
Definition: XrdPfcFile.hh:234
long long GetFileSize()
Definition: XrdPfcFile.hh:220
int m_prefetch_hit_cnt
Definition: XrdPfcFile.hh:315
void AddIO(IO *io)
bool m_detach_time_logged
Definition: XrdPfcFile.hh:318
std::set< Block * > BlockSet_t
Definition: XrdPfcFile.hh:298
void ProcessBlockResponse(BlockResponseHandler *brh, int res)
BlockList_t::iterator BlockList_i
Definition: XrdPfcFile.hh:293
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
std::list< int > IntList_t
Definition: XrdPfcFile.hh:289
bool Open()
Open file handle for data file and info file on local disk.
std::map< IO *, IODetails > IoMap_t
Definition: XrdPfcFile.hh:277
PrefetchState_e
Definition: XrdPfcFile.hh:245
@ kStopped
Definition: XrdPfcFile.hh:245
@ kOff
Definition: XrdPfcFile.hh:245
@ kHold
Definition: XrdPfcFile.hh:245
@ kComplete
Definition: XrdPfcFile.hh:245
@ kOn
Definition: XrdPfcFile.hh:245
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.
int inc_ref_cnt()
Definition: XrdPfcFile.hh:238
void dec_ref_count(Block *)
int GetPrefetchCountOnIO(IO *io)
void ProcessBlockRequest(Block *b)
int dec_ref_cnt()
Definition: XrdPfcFile.hh:239
void Prefetch()
int ReadV(IO *io, const XrdOucIOVec *readV, int n)
Vector read from disk if block is already downloaded, else ReadV from client.
IoMap_t::iterator IoMap_i
Definition: XrdPfcFile.hh:278
int get_ref_cnt()
Definition: XrdPfcFile.hh:237
bool select_current_io_or_disable_prefetching(bool skip_current)
void WriteBlockToDisk(Block *b)
Info m_cfi
download status of file blocks and access statistics
Definition: XrdPfcFile.hh:254
std::string m_filename
filename of data file on disk
Definition: XrdPfcFile.hh:256
std::string GetRemoteLocations() const
void insert_remote_location(const std::string &loc)
bool m_is_open
open state (presumably not needed anymore)
Definition: XrdPfcFile.hh:249
Block * PrepareBlockRequest(int i, IO *io, bool prefetch)
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)
bool m_in_shutdown
file is in emergency shutdown due to irrecoverable error or unlink request
Definition: XrdPfcFile.hh:250
std::set< std::string > m_remote_locations
Gathered in AddIO / ioUpdate / ioActive.
Definition: XrdPfcFile.hh:309
int m_ref_cnt
number of references from IO or sync
Definition: XrdPfcFile.hh:247
std::string & GetLocalPath()
Definition: XrdPfcFile.hh:215
bool FinalizeSyncBeforeExit()
Returns true if any of blocks need sync. Called from Cache::dec_ref_cnt on zero ref cnt.
Stats DeltaStatsFromLastCall()
void BlockRemovedFromWriteQ(Block *)
Handle removal of a block from Cache's write queue.
int ReadBlocksFromDisk(IntList_t &blocks, char *req_buf, long long req_off, long long req_size)
long long m_file_size
size of cached disk file for block-based operation
Definition: XrdPfcFile.hh:258
float m_prefetch_score
Definition: XrdPfcFile.hh:316
int Read(IO *io, char *buff, long long offset, int size)
Normal read.
PrefetchState_e m_prefetch_state
Definition: XrdPfcFile.hh:312
XrdOssDF * m_data_file
file handle for data file on disk
Definition: XrdPfcFile.hh:252
~File()
Destructor.
int VReadFromDisk(const XrdOucIOVec *readV, int n, ReadVBlockListDisk &blks_on_disk)
bool is_in_emergency_shutdown()
Definition: XrdPfcFile.hh:242
void initiate_emergency_shutdown()
std::vector< int > m_writes_during_sync
Definition: XrdPfcFile.hh:285
BlockMap_t m_block_map
Definition: XrdPfcFile.hh:302
std::map< int, Block * > BlockMap_t
Definition: XrdPfcFile.hh:295
Base cache-io class that implements XrdOucCacheIO abstract methods.
Definition: XrdPfcIO.hh:17
Status of cached file. Can be read from and written into a binary file.
Definition: XrdPfcInfo.hh:45
long long GetBufferSize() const
Get prefetch buffer size.
Definition: XrdPfcInfo.hh:467
const AStat * GetLastAccessStats() const
Get latest access stats.
int GetNDownloadedBlocks() const
Get number of downloaded blocks.
Definition: XrdPfcInfo.hh:398
size_t GetAccessCnt() const
Get number of accesses.
Definition: XrdPfcInfo.hh:267
int GetNBlocks() const
Get number of blocks represented in download-state bit-vector.
Definition: XrdPfcInfo.hh:437
Statistics of cache utilisation by a File object.
Definition: XrdPfcStats.hh:31
Definition: XrdSysPthread.hh:130
Definition: XrdSysPthread.hh:79
Definition: XrdSysError.hh:90
Definition: XrdSysTrace.hh:49
Definition: XrdClAnyObject.hh:26
Definition: XrdPfc.hh:41
std::vector< uint32_t > vCkSum_t
Definition: XrdPfcTypes.hh:27
Definition: XrdOucIOVec.hh:41
Definition: XrdPfcFile.hh:263
int m_active_prefetches
Definition: XrdPfcFile.hh:265
time_t m_attach_time
Definition: XrdPfcFile.hh:264
bool m_ioactive_false_reported
Definition: XrdPfcFile.hh:267
bool m_allow_prefetching
Definition: XrdPfcFile.hh:266
IODetails(time_t at)
Definition: XrdPfcFile.hh:269
Access statistics.
Definition: XrdPfcInfo.hh:61