xrootd
XrdClientReadCache.hh
Go to the documentation of this file.
00001 #ifndef XRD_READCACHE_H
00002 #define XRD_READCACHE_H
00003 /******************************************************************************/
00004 /*                                                                            */
00005 /*               X r d C l i e n t R e a d C a c h e . h h                    */
00006 /*                                                                            */
00007 /* Author: Fabrizio Furano (INFN Padova, 2006)                                */
00008 /*                                                                            */
00009 /* This file is part of the XRootD software suite.                            */
00010 /*                                                                            */
00011 /* XRootD is free software: you can redistribute it and/or modify it under    */
00012 /* the terms of the GNU Lesser General Public License as published by the     */
00013 /* Free Software Foundation, either version 3 of the License, or (at your     */
00014 /* option) any later version.                                                 */
00015 /*                                                                            */
00016 /* XRootD is distributed in the hope that it will be useful, but WITHOUT      */
00017 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or      */
00018 /* FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public       */
00019 /* License for more details.                                                  */
00020 /*                                                                            */
00021 /* You should have received a copy of the GNU Lesser General Public License   */
00022 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file  */
00023 /* COPYING (GPL license).  If not, see <http://www.gnu.org/licenses/>.        */
00024 /*                                                                            */
00025 /* The copyright holder's institutional names and contributor's names may not */
00026 /* be used to endorse or promote products derived from this software without  */
00027 /* specific prior written permission of the institution or contributor.       */
00028 /******************************************************************************/
00029 
00031 //                                                                      //
00032 // Classes to handle cache reading and cache placeholders               //
00033 //                                                                      //
00035 
00036 #include "XrdSys/XrdSysHeaders.hh"
00037 #include "XrdClient/XrdClientInputBuffer.hh"
00038 #include "XrdClient/XrdClientMessage.hh"
00039 #include "XrdClient/XrdClientVector.hh"
00040 #include "XrdClient/XrdClientConst.hh"
00041 
00042 //
00043 // XrdClientReadCacheItem
00044 //
00045 // An item is nothing more than an interval of bytes taken from a file.
00046 // Extremes are included.
00047 // Since a cache object is to be associated to a single instance
00048 // of TXNetFile, we do not have to keep here any filehandle
00049 //
00050 
00051 class XrdClientReadCacheItem {
00052 private:
00053     // A placeholder block is a "fake block" used to mark outstanding data
00054     bool             fIsPlaceholder;  
00055 
00056     long long        fBeginOffset;    // Offset of the first byte of data
00057     void             *fData;
00058     long long        fEndOffset;      // Offset of the last byte of data
00059     long             fTimestampTicks; // timestamp updated each time it's referenced
00060 
00061 public:
00062     XrdClientReadCacheItem(const void *buffer, long long begin_offs, 
00063                            long long end_offs, long long ticksnow,
00064                            bool placeholder=false);
00065     ~XrdClientReadCacheItem();
00066 
00067     inline long long BeginOffset() { return fBeginOffset; }
00068     inline long long EndOffset() { return fEndOffset; }
00069 
00070     // Is this obj contained in the given interval (which is going to be inserted) ?
00071     inline bool   ContainedInInterval(long long begin_offs, long long end_offs) {
00072         return ( (end_offs >= begin_offs) &&
00073                  (fBeginOffset >= begin_offs) &&
00074                  (fEndOffset <= end_offs) );
00075     }
00076 
00077     // Does this obj contain the given interval (which is going to be requested) ?
00078     inline bool   ContainsInterval(long long begin_offs, long long end_offs) {
00079         return ( (end_offs > begin_offs) &&
00080                  (fBeginOffset <= begin_offs) && (fEndOffset >= end_offs) );
00081     }
00082 
00083     // Are the two intervals intersecting in some way?
00084     inline bool  IntersectInterval(long long begin_offs, long long end_offs) {
00085       if ( ContainsOffset( begin_offs ) || ContainsOffset( end_offs ) ) return true;
00086       if ( (fBeginOffset >= begin_offs) && (fBeginOffset <= end_offs) ) return true;
00087       return false;
00088     }
00089 
00090 
00091     inline bool ContainsOffset(long long offs) {
00092         return (fBeginOffset <= offs) && (fEndOffset >= offs);
00093     }
00094 
00095     void *GetData() { return fData; }
00096     
00097     // Get the requested interval, if possible
00098     inline bool   GetInterval(const void *buffer, long long begin_offs, 
00099                               long long end_offs) {
00100         if (!ContainsInterval(begin_offs, end_offs))
00101             return FALSE;
00102         memcpy((void *)buffer, ((char *)fData)+(begin_offs - fBeginOffset),
00103                end_offs - begin_offs + 1);
00104         return TRUE;
00105     }
00106 
00107     // Get as many bytes as possible, starting from the beginning of the given
00108     // interval
00109     inline long   GetPartialInterval(const void *buffer, long long begin_offs,
00110                                      long long end_offs) {
00111 
00112         long long b = -1, e, l;
00113 
00114         if (begin_offs > end_offs) return 0;
00115 
00116         // Try to set the starting point, if contained in the given interval
00117         if ( (begin_offs >= fBeginOffset) &&
00118              (begin_offs <= fEndOffset) )
00119             b = begin_offs;
00120 
00121         if (b < 0) return 0;
00122 
00123         // The starting point is in the interval. Let's get the minimum endpoint
00124         e = xrdmin(end_offs, fEndOffset);
00125 
00126         l = e - b + 1;
00127 
00128         if (buffer && fData)
00129             memcpy((void *)buffer, ((char *)fData)+(b - fBeginOffset), l);
00130 
00131         return l;
00132     }
00133 
00134     inline long long GetTimestampTicks() { return(fTimestampTicks); }
00135 
00136     inline bool IsPlaceholder() { return fIsPlaceholder; }
00137 
00138     long Size() { return (fEndOffset - fBeginOffset + 1); }
00139 
00140     inline void     Touch(long long ticksnow) { fTimestampTicks = ticksnow; }
00141 
00142     bool Pinned;
00143 };
00144 
00145 //
00146 // XrdClientReadCache
00147 //
00148 // The content of the cache. Not cache blocks, but
00149 // variable length Items
00150 //
00151 typedef XrdClientVector<XrdClientReadCacheItem *> ItemVect;
00152 
00153 // A cache interval, extremes included
00154 struct XrdClientCacheInterval {
00155     long long beginoffs;
00156     long long endoffs;
00157 };
00158 
00159 typedef XrdClientVector<XrdClientCacheInterval> XrdClientIntvList;
00160 
00161 class XrdClientReadCache {
00162 private:
00163 
00164     long long       fBytesHit;         // Total number of bytes read with a cache hit
00165     long long       fBytesSubmitted;   // Total number of bytes inserted
00166     float           fBytesUsefulness;
00167     ItemVect        fItems;
00168     long long       fMaxCacheSize;
00169     long long       fMissCount;        // Counter of the cache misses
00170     float           fMissRate;            // Miss rate
00171     XrdSysRecMutex  fMutex;
00172     long long       fReadsCounter;     // Counter of all the attempted reads (hit or miss)
00173     int             fBlkRemPolicy;     // The algorithm used to remove "old" chunks
00174     long long       fTimestampTickCounter;        // Aging mechanism yuk!
00175     long long       fTotalByteCount;
00176 
00177     long long       GetTimestampTick();
00178     bool            MakeFreeSpace(long long bytes);
00179 
00180     bool            RemoveItem();
00181     bool            RemoveLRUItem();
00182     bool            RemoveFirstItem();
00183 
00184     inline void     UpdatePerfCounters() {
00185         if (fReadsCounter > 0)
00186             fMissRate = (float)fMissCount / fReadsCounter;
00187         if (fBytesSubmitted > 0)
00188             fBytesUsefulness = (float)fBytesHit / fBytesSubmitted;
00189     }
00190 
00191     int             FindInsertionApprox(long long begin_offs);
00192     int             FindInsertionApprox_rec(int startidx, int endidx,
00193                                         long long begin_offs);
00194 public:
00195 
00196     // The algos available for the removal of "old" blocks
00197     enum {
00198       kRmBlk_LRU = 0,
00199       kRmBlk_LeastOffs,
00200       kRmBlk_FIFO
00201     };
00202 
00203     XrdClientReadCache();
00204     ~XrdClientReadCache();
00205   
00206     long          GetDataIfPresent(const void *buffer, long long begin_offs,
00207                                    long long end_offs, bool PerfCalc,
00208                                    XrdClientIntvList &missingblks, long &outstandingblks);
00209 
00210   void                       GetInfo(
00211                                           // The actual cache size
00212                                           int &size,
00213 
00214                                           // The number of bytes submitted since the beginning
00215                                           long long &bytessubmitted,
00216 
00217                                           // The number of bytes found in the cache (estimate)
00218                                           long long &byteshit,
00219 
00220                                           // The number of reads which did not find their data
00221                                           // (estimate)
00222                                           long long &misscount,
00223 
00224                                           // miss/totalreads ratio (estimate)
00225                                           float &missrate,
00226 
00227                                           // number of read requests towards the cache
00228                                           long long &readreqcnt,
00229 
00230                                           // ratio between bytes found / bytes submitted
00231                                           float &bytesusefulness
00232                                      );
00233 
00234     inline long long GetTotalByteCount() {
00235         XrdSysMutexHelper m(fMutex);
00236         return fTotalByteCount;
00237     }
00238 
00239     void PutPlaceholder(long long begin_offs, long long end_offs);
00240 
00241     inline void     PrintPerfCounters() {
00242         XrdSysMutexHelper m(fMutex);
00243 
00244         cout << "Low level caching info:" << endl;
00245         cout << " StallsRate=" << fMissRate << endl;
00246         cout << " StallsCount=" << fMissCount << endl;
00247         cout << " ReadsCounter=" << fReadsCounter << endl;
00248         cout << " BytesUsefulness=" << fBytesUsefulness << endl;
00249         cout << " BytesSubmitted=" << fBytesSubmitted << " BytesHit=" << 
00250            fBytesHit << endl << endl;
00251     }
00252 
00253 
00254     void            PrintCache();
00255 
00256     void            SubmitXMessage(XrdClientMessage *xmsg, long long begin_offs,
00257                                    long long end_offs);
00258 
00259     bool            SubmitRawData(const void *buffer, long long begin_offs,
00260                                   long long end_offs, bool pinned=false);
00261 
00262     void            RemoveItems(bool leavepinned=true);
00263     void            RemoveItems(long long begin_offs, long long end_offs, bool remove_overlapped = false);
00264     void            RemovePlaceholders();
00265 
00266 
00267     void            SetSize(int sz) {
00268       fMaxCacheSize = sz;
00269     }
00270 
00271     void            SetBlkRemovalPolicy(int p) {
00272       fBlkRemPolicy = p;
00273     }
00274 
00275     void UnPinCacheBlk(long long begin_offs, long long end_offs);
00276     void *FindBlk(long long begin_offs, long long end_offs);
00277 
00278     // To check if a block dimension will fit into the cache
00279     inline bool   WillFit(long long bc) {
00280         XrdSysMutexHelper m(fMutex);
00281         return (bc < fMaxCacheSize);
00282     }
00283 
00284 };
00285 #endif