00001 #ifndef __XRDFILECACHE_INFO_HH__
00002 #define __XRDFILECACHE_INFO_HH__
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <stdio.h>
00022 #include <time.h>
00023 #include <assert.h>
00024 #include <vector>
00025
00026 #include "XrdSys/XrdSysPthread.hh"
00027 #include "XrdCl/XrdClConstants.hh"
00028 #include "XrdCl/XrdClDefaultEnv.hh"
00029
00030 class XrdOssDF;
00031 class XrdCksCalc;
00032 class XrdSysTrace;
00033
00034
00035 namespace XrdCl
00036 {
00037 class Log;
00038 }
00039
00040 namespace XrdFileCache
00041 {
00042 class Stats;
00043
00044
00046
00047
00048 class Info
00049 {
00050 public:
00051
00052 struct AStat
00053 {
00054 time_t AttachTime;
00055 time_t DetachTime;
00056 long long BytesDisk;
00057 long long BytesRam;
00058 long long BytesMissed;
00059
00060 AStat() : AttachTime(0), DetachTime(0), BytesDisk(0), BytesRam(0), BytesMissed(0) {}
00061 };
00062
00063 struct Store {
00064 int m_version;
00065 long long m_bufferSize;
00066 long long m_fileSize;
00067 unsigned char *m_buff_synced;
00068 char m_cksum[16];
00069 time_t m_creationTime;
00070 size_t m_accessCnt;
00071 std::vector<AStat> m_astats;
00072
00073 Store () : m_version(1), m_bufferSize(-1), m_fileSize(0), m_buff_synced(0),m_creationTime(0), m_accessCnt(0) {}
00074 };
00075
00076
00077
00079
00080 Info(XrdSysTrace* trace, bool prefetchBuffer = false);
00081
00082
00084
00085 ~Info();
00086
00087
00089
00090 void SetBitWritten(int i);
00091
00092
00094
00095 bool TestBitWritten(int i) const;
00096
00097
00099
00100 bool TestBitPrefetch(int i) const;
00101
00102
00104
00105 void SetBitPrefetch(int i);
00106
00107
00109
00110 void SetBitSynced(int i);
00111
00112
00114
00115 void SetAllBitsSynced();
00116
00117 void SetBufferSize(long long);
00118
00119 void SetFileSize(long long);
00120
00121
00125
00126 void ResizeBits(int n);
00127
00128
00135
00136 bool Read(XrdOssDF* fp, const std::string &fname = "<unknown>");
00137
00138
00141
00142 bool Write(XrdOssDF* fp, const std::string &fname = "<unknown>");
00143
00144
00146
00147 void DisableDownloadStatus();
00148
00149
00151
00152 void ResetAllAccessStats();
00153
00154
00156
00157 void WriteIOStatAttach();
00158
00160
00161 void WriteIOStat(Stats& s);
00162
00163
00165
00166 void WriteIOStatDetach(Stats& s);
00167
00168
00170
00171 void WriteIOStatSingle(long long bytes_disk);
00172
00173
00175
00176 void WriteIOStatSingle(long long bytes_disk, time_t att, time_t dtc);
00177
00178
00180
00181 bool IsAnythingEmptyInRng(int firstIdx, int lastIdx) const;
00182
00183
00185
00186 int GetSizeInBytes() const;
00187
00188
00190
00191 int GetSizeInBits() const;
00192
00193
00195
00196 long long GetFileSize() const;
00197
00198
00200
00201 bool GetLatestDetachTime(time_t& t) const;
00202
00203
00205
00206 long long GetBufferSize() const;
00207
00208
00210
00211 bool IsComplete() const;
00212
00213
00215
00216 int GetNDownloadedBlocks() const;
00217
00218
00220
00221 long long GetNDownloadedBytes() const;
00222
00223
00225
00226 int GetLastDownloadedBlock() const;
00227
00228
00230
00231 long long GetExpectedDataFileSize() const;
00232
00233
00235
00236 void UpdateDownloadCompleteStatus();
00237
00238
00240
00241 size_t GetAccessCnt() { return m_store.m_accessCnt; }
00242
00243
00245
00246 int GetVersion() { return m_store.m_version; }
00247
00248
00250
00251 const Store& RefStoredData() const { return m_store; }
00252
00253
00255
00256 void GetCksum( unsigned char* buff, char* digest);
00257
00258 const static char* m_infoExtension;
00259 const static char* m_traceID;
00260 const static int m_defaultVersion;
00261 const static size_t m_maxNumAccess;
00262
00263 XrdSysTrace* GetTrace() const {return m_trace; }
00264
00265 static size_t GetMaxNumAccess() { return m_maxNumAccess; }
00266
00267 protected:
00268 XrdSysTrace* m_trace;
00269
00270 Store m_store;
00271 bool m_hasPrefetchBuffer;
00272 unsigned char *m_buff_written;
00273 unsigned char *m_buff_prefetch;
00274
00275 int m_sizeInBits;
00276 bool m_complete;
00277
00278 private:
00279 inline unsigned char cfiBIT(int n) const { return 1 << n; }
00280
00281
00282 bool ReadV1(XrdOssDF* fp, const std::string &fname);
00283 XrdCksCalc* m_cksCalc;
00284 };
00285
00286
00287
00288 inline bool Info::TestBitWritten(int i) const
00289 {
00290 const int cn = i/8;
00291 assert(cn < GetSizeInBytes());
00292
00293 const int off = i - cn*8;
00294 return (m_buff_written[cn] & cfiBIT(off)) != 0;
00295 }
00296
00297 inline void Info::SetBitWritten(int i)
00298 {
00299 const int cn = i/8;
00300 assert(cn < GetSizeInBytes());
00301
00302 const int off = i - cn*8;
00303 m_buff_written[cn] |= cfiBIT(off);
00304 }
00305
00306 inline void Info::SetBitPrefetch(int i)
00307 {
00308 if (!m_buff_prefetch) return;
00309
00310 const int cn = i/8;
00311 assert(cn < GetSizeInBytes());
00312
00313 const int off = i - cn*8;
00314 m_buff_prefetch[cn] |= cfiBIT(off);
00315 }
00316
00317 inline bool Info::TestBitPrefetch(int i) const
00318 {
00319 if (!m_buff_prefetch) return false;
00320
00321 const int cn = i/8;
00322 assert(cn < GetSizeInBytes());
00323
00324 const int off = i - cn*8;
00325 return (m_buff_prefetch[cn] & cfiBIT(off)) != 0;
00326 }
00327
00328 inline void Info::SetBitSynced(int i)
00329 {
00330 const int cn = i/8;
00331 assert(cn < GetSizeInBytes());
00332
00333 const int off = i - cn*8;
00334 m_store.m_buff_synced[cn] |= cfiBIT(off);
00335 }
00336
00337
00338
00339 inline int Info::GetNDownloadedBlocks() const
00340 {
00341 int cntd = 0;
00342 for (int i = 0; i < m_sizeInBits; ++i)
00343 if (TestBitWritten(i)) cntd++;
00344
00345 return cntd;
00346 }
00347
00348 inline long long Info::GetNDownloadedBytes() const
00349 {
00350 return m_store.m_bufferSize * GetNDownloadedBlocks();
00351 }
00352
00353 inline int Info::GetLastDownloadedBlock() const
00354 {
00355 for (int i = m_sizeInBits - 1; i >= 0; --i)
00356 if (TestBitWritten(i)) return i;
00357
00358 return -1;
00359 }
00360
00361 inline long long Info::GetExpectedDataFileSize() const
00362 {
00363 int last_block = GetLastDownloadedBlock();
00364 if (last_block == m_sizeInBits - 1)
00365 return m_store.m_fileSize;
00366 else
00367 return (last_block + 1) * m_store.m_bufferSize;
00368 }
00369
00370 inline int Info::GetSizeInBytes() const
00371 {
00372 if (m_sizeInBits)
00373 return ((m_sizeInBits - 1)/8 + 1);
00374 else
00375 return 0;
00376 }
00377
00378 inline int Info::GetSizeInBits() const
00379 {
00380 return m_sizeInBits;
00381 }
00382
00383 inline long long Info::GetFileSize() const
00384 {
00385 return m_store.m_fileSize;
00386 }
00387
00388 inline bool Info::IsComplete() const
00389 {
00390 return m_complete;
00391 }
00392
00393 inline bool Info::IsAnythingEmptyInRng(int firstIdx, int lastIdx) const
00394 {
00395
00396
00397 for (int i = firstIdx; i < lastIdx; ++i)
00398 if (! TestBitWritten(i)) return true;
00399
00400 return false;
00401 }
00402
00403 inline void Info::UpdateDownloadCompleteStatus()
00404 {
00405 m_complete = ! IsAnythingEmptyInRng(0, m_sizeInBits);
00406 }
00407
00408 inline long long Info::GetBufferSize() const
00409 {
00410 return m_store.m_bufferSize;
00411 }
00412
00413 }
00414 #endif