xrootd
XrdOucCacheReal.hh
Go to the documentation of this file.
00001 #ifndef __XRDOUCCACHEREAL_HH__
00002 #define __XRDOUCCACHEREAL_HH__
00003 /******************************************************************************/
00004 /*                                                                            */
00005 /*                    X r d O u c C a c h e R e a l . h h                     */
00006 /*                                                                            */
00007 /* (c) 2011 by the Board of Trustees of the Leland Stanford, Jr., University  */
00008 /*                            All Rights Reserved                             */
00009 /*   Produced by Andrew Hanushevsky for Stanford University under contract    */
00010 /*              DE-AC02-76-SFO0515 with the Department of Energy              */
00011 /*                                                                            */
00012 /* This file is part of the XRootD software suite.                            */
00013 /*                                                                            */
00014 /* XRootD is free software: you can redistribute it and/or modify it under    */
00015 /* the terms of the GNU Lesser General Public License as published by the     */
00016 /* Free Software Foundation, either version 3 of the License, or (at your     */
00017 /* option) any later version.                                                 */
00018 /*                                                                            */
00019 /* XRootD is distributed in the hope that it will be useful, but WITHOUT      */
00020 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or      */
00021 /* FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public       */
00022 /* License for more details.                                                  */
00023 /*                                                                            */
00024 /* You should have received a copy of the GNU Lesser General Public License   */
00025 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file  */
00026 /* COPYING (GPL license).  If not, see <http://www.gnu.org/licenses/>.        */
00027 /*                                                                            */
00028 /* The copyright holder's institutional names and contributor's names may not */
00029 /* be used to endorse or promote products derived from this software without  */
00030 /* specific prior written permission of the institution or contributor.       */
00031 /******************************************************************************/
00032 
00033 #include "XrdOuc/XrdOucCacheDram.hh"
00034 #include "XrdOuc/XrdOucCacheSlot.hh"
00035 #include "XrdSys/XrdSysPthread.hh"
00036 
00037 /* This class defines an actual implementation of an XrdOucCache object. */
00038 
00039 class XrdOucCacheReal : public XrdOucCacheDram
00040 {
00041 friend class XrdOucCacheData;
00042 public:
00043 
00044 XrdOucCacheIO *Attach(XrdOucCacheIO *ioP, int Options=0);
00045 
00046 int            isAttached() {int n;
00047                              CMutex.Lock(); n = Attached; CMutex.UnLock();
00048                              return n;
00049                             }
00050 
00051                XrdOucCacheReal(int     &rc,     // Success = 0; o/w !0
00052                                Parms   &Parms,  // Parameters
00053                XrdOucCacheIO::aprParms *aprP=0);
00054 
00055               ~XrdOucCacheReal();
00056 
00057 void           PreRead();
00058 
00059 private:
00060 
00061 void      eMsg(const char *Path, const char *What, long long xOff,
00062                int xLen, int ec);
00063 int       Detach(XrdOucCacheIO *ioP);
00064 char     *Get(XrdOucCacheIO *ioP, long long lAddr, int &rGot, int &bIO);
00065 
00066 int       ioAdd(XrdOucCacheIO *KeyVal, int &iNum);
00067 int       ioDel(XrdOucCacheIO *KeyVal, int &iNum);
00068 
00069 inline
00070 int       ioEnt(XrdOucCacheIO *kVal)
00071                {union {short sV[4]; XrdOucCacheIO *pV;} Key = {{0,0,0,0}};
00072                 Key.pV = kVal;
00073                 return ((Key.sV[0]^Key.sV[1]^Key.sV[2]^Key.sV[3])&0x7fff)%hMax;
00074                }
00075 inline
00076 int       ioLookup(int &pip, int hip, void *kval)
00077                   {pip = 0;
00078                    while(hip && kval != Slots[hip].Key)
00079                         {pip = hip; hip = Slots[hip].HLink;}
00080                    return hip;
00081                   }
00082 
00083 int       Ref(char *Addr, int rAmt, int sFlags=0);
00084 void      Trunc(XrdOucCacheIO *ioP, long long lAddr);
00085 void      Upd(char *Addr, int wAmt, int wOff);
00086 
00087 static const long long Shift = 48;
00088 static const long long Strip = 0x00000000ffffffffLL;  //
00089 static const long long MaxFO = 0x000007ffffffffffLL;  // Min 4K page -> 8TB-1
00090 
00091 XrdOucCacheIO::aprParms aprDefault; // Default automatic preread
00092 
00093 XrdSysMutex      CMutex;
00094 XrdOucCacheSlot *Slots;       // 1-to-1 slot to memory map
00095 int             *Slash;       // Slot hash table
00096 char            *Base;        // Base of memory cache
00097 long long        HNum;
00098 long long        SegCnt;
00099 long long        SegSize;
00100 long long        OffMask;     // SegSize - 1
00101 long long        SegShft;     // log2(SegSize)
00102 int              SegFull;     // SegSize to mark
00103 int              maxCache;    // Maximum read to cache
00104 int              maxFiles;    // Maximum number of files to support
00105 int              Options;
00106 
00107 // The following supports CacheIO object tracking
00108 //
00109 int             *hTab;        // -> Hash Table
00110 int              hMax;        // Number of entries in table
00111 int              sFree;       // Index of free file slot
00112 int              sBeg;        // Index of file slot array in slot table
00113 int              sEnd;        // Last index + 1
00114 
00115 // Various options
00116 //
00117 char             Dbg;         // Debug setting
00118 char             Lgs;         // Log statistics
00119 
00120 // This is the attach/detach control area
00121 //
00122 XrdSysSemaphore *AZero;
00123 int              Attached;
00124 
00125 // This is the pre-read control area
00126 //
00127 struct prTask
00128       {prTask          *Next;
00129        XrdOucCacheData *Data;
00130       };
00131 void             PreRead(XrdOucCacheReal::prTask *prReq);
00132 prTask          *prFirst;
00133 prTask          *prLast;
00134 XrdSysMutex      prMutex;
00135 XrdSysSemaphore  prReady;
00136 XrdSysSemaphore *prStop;
00137 int              prNum;
00138 };
00139 #endif