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
00091
00092 void SetBitWritten(int i);
00093
00094
00098
00099 void SetBitSynced(int i);
00100
00101
00103
00104 void SetAllBitsSynced();
00105
00106
00110
00111 void SetBitPrefetch(int i);
00112
00113 void SetBufferSize(long long);
00114
00115 void SetFileSize(long long);
00116
00117
00121
00122 void ResizeBits(int n);
00123
00124
00131
00132 bool Read(XrdOssDF* fp, const std::string &fname = "<unknown>");
00133
00134
00137
00138 bool Write(XrdOssDF* fp, const std::string &fname = "<unknown>");
00139
00140
00142
00143 void DisableDownloadStatus();
00144
00145
00147
00148 void ResetAllAccessStats();
00149
00150
00152
00153 void WriteIOStatAttach();
00154
00156
00157 void WriteIOStat(Stats& s);
00158
00159
00161
00162 void WriteIOStatDetach(Stats& s);
00163
00164
00166
00167 void WriteIOStatSingle(long long bytes_disk);
00168
00169
00171
00172 void WriteIOStatSingle(long long bytes_disk, time_t att, time_t dtc);
00173
00174
00176
00177 bool IsAnythingEmptyInRng(int firstIdx, int lastIdx) const;
00178
00179
00181
00182 int GetSizeInBytes() const;
00183
00184
00186
00187 int GetSizeInBits() const;
00188
00189
00191
00192 long long GetFileSize() const;
00193
00194
00196
00197 bool GetLatestDetachTime(time_t& t) const;
00198
00199
00201
00202 long long GetBufferSize() const;
00203
00204
00206
00207 bool TestBit(int i) const;
00208
00209
00211
00212 bool TestPrefetchBit(int i) const;
00213
00214
00216
00217 bool IsComplete() const;
00218
00219
00221
00222 int GetNDownloadedBlocks() const;
00223
00224
00226
00227 long long GetNDownloadedBytes() const;
00228
00229
00231
00232 int GetLastDownloadedBlock() const;
00233
00234
00236
00237 long long GetExpectedDataFileSize() const;
00238
00239
00241
00242 void UpdateDownloadCompleteStatus();
00243
00244
00246
00247 size_t GetAccessCnt() { return m_store.m_accessCnt; }
00248
00249
00251
00252 int GetVersion() { return m_store.m_version; }
00253
00254
00256
00257 const Store& RefStoredData() const { return m_store; }
00258
00259
00261
00262 void GetCksum( unsigned char* buff, char* digest);
00263
00264 const static char* m_infoExtension;
00265 const static char* m_traceID;
00266 const static int m_defaultVersion;
00267 const static size_t m_maxNumAccess;
00268
00269 XrdSysTrace* GetTrace() const {return m_trace; }
00270
00271 static size_t GetMaxNumAccess() { return m_maxNumAccess; }
00272
00273 protected:
00274 XrdSysTrace* m_trace;
00275
00276 Store m_store;
00277 bool m_hasPrefetchBuffer;
00278 unsigned char *m_buff_written;
00279 unsigned char *m_buff_prefetch;
00280
00281 int m_sizeInBits;
00282 bool m_complete;
00283
00284 private:
00285 inline unsigned char cfiBIT(int n) const { return 1 << n; }
00286
00287
00288 bool ReadV1(XrdOssDF* fp, const std::string &fname);
00289 XrdCksCalc* m_cksCalc;
00290 };
00291
00292
00293
00294 inline bool Info::TestBit(int i) const
00295 {
00296 const int cn = i/8;
00297 assert(cn < GetSizeInBytes());
00298
00299 const int off = i - cn*8;
00300 return (m_buff_written[cn] & cfiBIT(off)) == cfiBIT(off);
00301 }
00302
00303 inline bool Info::TestPrefetchBit(int i) const
00304 {
00305 if (!m_buff_prefetch) return false;
00306
00307 const int cn = i/8;
00308 assert(cn < GetSizeInBytes());
00309
00310 const int off = i - cn*8;
00311 return (m_buff_prefetch[cn] & cfiBIT(off)) == cfiBIT(off);
00312 }
00313
00314 inline int Info::GetNDownloadedBlocks() const
00315 {
00316 int cntd = 0;
00317 for (int i = 0; i < m_sizeInBits; ++i)
00318 if (TestBit(i)) cntd++;
00319
00320 return cntd;
00321 }
00322
00323 inline long long Info::GetNDownloadedBytes() const
00324 {
00325 return m_store.m_bufferSize * GetNDownloadedBlocks();
00326 }
00327
00328 inline int Info::GetLastDownloadedBlock() const
00329 {
00330 for (int i = m_sizeInBits - 1; i >= 0; --i)
00331 if (TestBit(i)) return i;
00332
00333 return -1;
00334 }
00335
00336 inline long long Info::GetExpectedDataFileSize() const
00337 {
00338 int last_block = GetLastDownloadedBlock();
00339 if (last_block == m_sizeInBits - 1)
00340 return m_store.m_fileSize;
00341 else
00342 return (last_block + 1) * m_store.m_bufferSize;
00343 }
00344
00345 inline int Info::GetSizeInBytes() const
00346 {
00347 if (m_sizeInBits)
00348 return ((m_sizeInBits - 1)/8 + 1);
00349 else
00350 return 0;
00351 }
00352
00353 inline int Info::GetSizeInBits() const
00354 {
00355 return m_sizeInBits;
00356 }
00357
00358 inline long long Info::GetFileSize() const
00359 {
00360 return m_store.m_fileSize;
00361 }
00362
00363 inline bool Info::IsComplete() const
00364 {
00365 return m_complete;
00366 }
00367
00368 inline bool Info::IsAnythingEmptyInRng(int firstIdx, int lastIdx) const
00369 {
00370
00371
00372 for (int i = firstIdx; i < lastIdx; ++i)
00373 if (! TestBit(i)) return true;
00374
00375 return false;
00376 }
00377
00378 inline void Info::UpdateDownloadCompleteStatus()
00379 {
00380 m_complete = ! IsAnythingEmptyInRng(0, m_sizeInBits);
00381 }
00382
00383 inline void Info::SetBitSynced(int i)
00384 {
00385 const int cn = i/8;
00386 assert(cn < GetSizeInBytes());
00387
00388 const int off = i - cn*8;
00389 m_store.m_buff_synced[cn] |= cfiBIT(off);
00390 }
00391
00392 inline void Info::SetBitWritten(int i)
00393 {
00394 const int cn = i/8;
00395 assert(cn < GetSizeInBytes());
00396
00397 const int off = i - cn*8;
00398 m_buff_written[cn] |= cfiBIT(off);
00399 }
00400
00401 inline void Info::SetBitPrefetch(int i)
00402 {
00403 if (!m_buff_prefetch) return;
00404
00405 const int cn = i/8;
00406 assert(cn < GetSizeInBytes());
00407
00408 const int off = i - cn*8;
00409 m_buff_prefetch[cn] |= cfiBIT(off);
00410 }
00411
00412 inline long long Info::GetBufferSize() const
00413 {
00414 return m_store.m_bufferSize;
00415 }
00416
00417 }
00418 #endif