00001 #ifndef __XRDFILECACHE_PREFETCH_HH__
00002 #define __XRDFILECACHE_PREFETCH_HH__
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <string>
00022 #include <queue>
00023
00024 #include "XrdCl/XrdClDefaultEnv.hh"
00025
00026 #include "XrdFileCacheInfo.hh"
00027 #include "XrdFileCacheStats.hh"
00028
00029 class XrdOucIOVec;
00030 namespace XrdCl
00031 {
00032 class Log;
00033 }
00034
00035 namespace XrdFileCache
00036 {
00037
00039
00040 class Prefetch
00041 {
00042 friend class IOEntireFile;
00043 friend class IOFileBlock;
00044 enum ReadRamState_t { kReadWait, kReadSuccess, kReadFailed};
00045
00046 struct Task;
00047 public:
00048
00050
00051 Prefetch(XrdOucCacheIO& inputFile, std::string& path,
00052 long long offset, long long fileSize);
00053
00054
00056
00057 ~Prefetch();
00058
00059
00061
00062 void Run();
00063
00064
00066
00067 Stats& GetStats() { return m_stats; }
00068
00069
00071
00072 void WriteBlockToDisk(int ramIdx, size_t size);
00073
00074
00076
00077 void DecRamBlockRefCount(int ramIdx);
00078
00079
00082
00083 bool InitiateClose();
00084
00085 protected:
00087 ssize_t Read(char * buff, off_t offset, size_t size);
00088
00090 int ReadV (const XrdOucIOVec *readV, int n);
00091
00093 void AppendIOStatToFileInfo();
00094
00095 private:
00096
00098
00099 struct Task
00100 {
00101 int ramBlockIdx;
00102 XrdSysCondVar *condVar;
00103
00104 Task(): ramBlockIdx(-1), condVar(0) {}
00105 Task(int r, XrdSysCondVar *cv):
00106 ramBlockIdx(r), condVar(cv) {}
00107 ~Task() {}
00108 };
00109
00110 struct RAMBlock {
00111 int fileBlockIdx;
00112 int refCount;
00113 bool fromRead;
00114 ReadRamState_t status;
00115 int readErrno;
00116
00117 RAMBlock():fileBlockIdx(-1), refCount(0), fromRead(false), status(kReadWait) {}
00118 };
00119
00120 struct RAM
00121 {
00122 int m_numBlocks;
00123 char* m_buffer;
00124 RAMBlock* m_blockStates;
00125 XrdSysCondVar m_writeMutex;
00126
00127 RAM();
00128 ~RAM();
00129 };
00130
00132 void CloseCleanly();
00133
00135 Task* GetNextTask();
00136
00138 bool Open();
00139
00141 void RecordDownloadInfo();
00142
00144 XrdCl::Log* clLog() const { return XrdCl::DefaultEnv::GetLog(); }
00145
00147 ssize_t ReadInBlocks( char* buff, off_t offset, size_t size);
00148
00150 Task* CreateTaskForFirstUndownloadedBlock();
00151
00153 bool ReadFromTask(int bIdx, char* buff, long long off, size_t size);
00154
00156 void DoTask(Task* task);
00157
00159 const char* lPath() const;
00160
00161 RAM m_ram;
00162
00163 XrdOssDF *m_output;
00164 XrdOssDF *m_infoFile;
00165 Info m_cfi;
00166 XrdOucCacheIO &m_input;
00167
00168 std::string m_temp_filename;
00169
00170 long long m_offset;
00171 long long m_fileSize;
00172
00173 bool m_started;
00174 bool m_failed;
00175 bool m_stopping;
00176 bool m_stopped;
00177 XrdSysCondVar m_stateCond;
00178
00179 XrdSysMutex m_downloadStatusMutex;
00180
00181 std::deque<Task*> m_tasks_queue;
00182 XrdSysCondVar m_queueCond;
00183
00184 Stats m_stats;
00185 };
00186 }
00187 #endif