00001 #ifndef __XRDPFC_INFO_HH__
00002 #define __XRDPFC_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 XrdPfc
00041 {
00042 class Stats;
00043
00044
00046
00047
00048 class Info
00049 {
00050 public:
00052 struct AStat
00053 {
00054 time_t AttachTime;
00055 time_t DetachTime;
00056 int NumIos;
00057 int Duration;
00058 int NumMerged;
00059 long long BytesHit;
00060 long long BytesMissed;
00061 long long BytesBypassed;
00062
00063 AStat() :
00064 AttachTime(0), DetachTime(0), NumIos(0), Duration(0), NumMerged(0),
00065 BytesHit(0), BytesMissed(0), BytesBypassed(0)
00066 {}
00067
00068 void MergeWith(const AStat &a);
00069 };
00070
00071 struct Store
00072 {
00073 int m_version;
00074 long long m_buffer_size;
00075 long long m_file_size;
00076 unsigned char *m_buff_synced;
00077 char m_cksum[16];
00078 time_t m_creationTime;
00079 size_t m_accessCnt;
00080 std::vector<AStat> m_astats;
00081
00082 Store () : m_version(1), m_buffer_size(-1), m_file_size(0), m_buff_synced(0), m_creationTime(0), m_accessCnt(0) {}
00083 };
00084
00085
00086
00088
00089 Info(XrdSysTrace* trace, bool prefetchBuffer = false);
00090
00091
00093
00094 ~Info();
00095
00096
00098
00099 void SetBitWritten(int i);
00100
00101
00103
00104 bool TestBitWritten(int i) const;
00105
00106
00108
00109 bool TestBitPrefetch(int i) const;
00110
00111
00113
00114 void SetBitPrefetch(int i);
00115
00116
00118
00119 void SetBitSynced(int i);
00120
00121
00123
00124 void SetAllBitsSynced();
00125
00126 void SetBufferSize(long long);
00127
00128 void SetFileSize(long long);
00129
00130
00134
00135 void ResizeBits(int n);
00136
00137
00144
00145 bool Read(XrdOssDF* fp, const std::string &fname = "<unknown>");
00146
00147
00150
00151 bool Write(XrdOssDF* fp, const std::string &fname = "<unknown>");
00152
00153
00155
00156 void CompactifyAccessRecords();
00157
00158
00160
00161 void DisableDownloadStatus();
00162
00163
00165
00166 void ResetAllAccessStats();
00167
00168
00170
00171 void WriteIOStatAttach();
00172
00173
00175
00176 void WriteIOStat(Stats& s);
00177
00178
00180
00181 void WriteIOStatDetach(Stats& s);
00182
00183
00185
00186 void WriteIOStatSingle(long long bytes_disk);
00187
00188
00190
00191 void WriteIOStatSingle(long long bytes_disk, time_t att, time_t dtc);
00192
00193
00195
00196 bool IsAnythingEmptyInRng(int firstIdx, int lastIdx) const;
00197
00198
00200
00201 int GetSizeInBytes() const;
00202
00203
00205
00206 int GetSizeInBits() const;
00207
00208
00210
00211 long long GetFileSize() const;
00212
00213
00215
00216 bool GetLatestDetachTime(time_t& t) const;
00217
00218
00220
00221 const AStat* GetLastAccessStats() const;
00222
00223
00225
00226 long long GetBufferSize() const;
00227
00228
00230
00231 bool IsComplete() const;
00232
00233
00235
00236 int GetNDownloadedBlocks() const;
00237
00238
00240
00241 long long GetNDownloadedBytes() const;
00242
00243
00245
00246 int GetLastDownloadedBlock() const;
00247
00248
00250
00251 long long GetExpectedDataFileSize() const;
00252
00253
00255
00256 void UpdateDownloadCompleteStatus();
00257
00258
00260
00261 size_t GetAccessCnt() const { return m_store.m_accessCnt; }
00262
00263
00265
00266 int GetVersion() { return m_store.m_version; }
00267
00268
00270
00271 const Store& RefStoredData() const { return m_store; }
00272
00273
00275
00276 void GetCksum( unsigned char* buff, char* digest);
00277
00278 static const char* m_traceID;
00279 static const char* s_infoExtension;
00280 static const int s_defaultVersion;
00281 static size_t s_maxNumAccess;
00282
00283 XrdSysTrace* GetTrace() const {return m_trace; }
00284
00285 protected:
00286 XrdSysTrace* m_trace;
00287
00288 Store m_store;
00289 bool m_hasPrefetchBuffer;
00290 unsigned char *m_buff_written;
00291 unsigned char *m_buff_prefetch;
00292
00293 int m_sizeInBits;
00294 bool m_complete;
00295
00296 private:
00297 inline unsigned char cfiBIT(int n) const { return 1 << n; }
00298
00299
00300 bool ReadV1(XrdOssDF* fp, const std::string &fname);
00301 bool ReadV2(XrdOssDF* fp, const std::string &fname);
00302
00303 XrdCksCalc* m_cksCalc;
00304 };
00305
00306
00307
00308 inline bool Info::TestBitWritten(int i) const
00309 {
00310 const int cn = i/8;
00311 assert(cn < GetSizeInBytes());
00312
00313 const int off = i - cn*8;
00314 return (m_buff_written[cn] & cfiBIT(off)) != 0;
00315 }
00316
00317 inline void Info::SetBitWritten(int i)
00318 {
00319 const int cn = i/8;
00320 assert(cn < GetSizeInBytes());
00321
00322 const int off = i - cn*8;
00323 m_buff_written[cn] |= cfiBIT(off);
00324 }
00325
00326 inline void Info::SetBitPrefetch(int i)
00327 {
00328 if (!m_buff_prefetch) return;
00329
00330 const int cn = i/8;
00331 assert(cn < GetSizeInBytes());
00332
00333 const int off = i - cn*8;
00334 m_buff_prefetch[cn] |= cfiBIT(off);
00335 }
00336
00337 inline bool Info::TestBitPrefetch(int i) const
00338 {
00339 if (!m_buff_prefetch) return false;
00340
00341 const int cn = i/8;
00342 assert(cn < GetSizeInBytes());
00343
00344 const int off = i - cn*8;
00345 return (m_buff_prefetch[cn] & cfiBIT(off)) != 0;
00346 }
00347
00348 inline void Info::SetBitSynced(int i)
00349 {
00350 const int cn = i/8;
00351 assert(cn < GetSizeInBytes());
00352
00353 const int off = i - cn*8;
00354 m_store.m_buff_synced[cn] |= cfiBIT(off);
00355 }
00356
00357
00358
00359 inline int Info::GetNDownloadedBlocks() const
00360 {
00361 int cntd = 0;
00362 for (int i = 0; i < m_sizeInBits; ++i)
00363 if (TestBitWritten(i)) cntd++;
00364
00365 return cntd;
00366 }
00367
00368 inline long long Info::GetNDownloadedBytes() const
00369 {
00370 return m_store.m_buffer_size * GetNDownloadedBlocks();
00371 }
00372
00373 inline int Info::GetLastDownloadedBlock() const
00374 {
00375 for (int i = m_sizeInBits - 1; i >= 0; --i)
00376 if (TestBitWritten(i)) return i;
00377
00378 return -1;
00379 }
00380
00381 inline long long Info::GetExpectedDataFileSize() const
00382 {
00383 int last_block = GetLastDownloadedBlock();
00384 if (last_block == m_sizeInBits - 1)
00385 return m_store.m_file_size;
00386 else
00387 return (last_block + 1) * m_store.m_buffer_size;
00388 }
00389
00390 inline int Info::GetSizeInBytes() const
00391 {
00392 if (m_sizeInBits)
00393 return ((m_sizeInBits - 1)/8 + 1);
00394 else
00395 return 0;
00396 }
00397
00398 inline int Info::GetSizeInBits() const
00399 {
00400 return m_sizeInBits;
00401 }
00402
00403 inline long long Info::GetFileSize() const
00404 {
00405 return m_store.m_file_size;
00406 }
00407
00408 inline bool Info::IsComplete() const
00409 {
00410 return m_complete;
00411 }
00412
00413 inline bool Info::IsAnythingEmptyInRng(int firstIdx, int lastIdx) const
00414 {
00415
00416
00417 for (int i = firstIdx; i < lastIdx; ++i)
00418 if (! TestBitWritten(i)) return true;
00419
00420 return false;
00421 }
00422
00423 inline void Info::UpdateDownloadCompleteStatus()
00424 {
00425 m_complete = ! IsAnythingEmptyInRng(0, m_sizeInBits);
00426 }
00427
00428 inline long long Info::GetBufferSize() const
00429 {
00430 return m_store.m_buffer_size;
00431 }
00432
00433 }
00434 #endif