Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

315 lines
10 KiB

  1. /*-----------------------------------------------------------------------------
  2. Microsoft Denali
  3. Microsoft Confidential
  4. Copyright 1996 Microsoft Corporation. All Rights Reserved.
  5. Component: Template Cache Manager
  6. File: CacheMgr.h
  7. Owner: DGottner
  8. Template cache manager definition
  9. -----------------------------------------------------------------------------*/
  10. #ifndef _CACHEMGR_H
  11. #define _CACHEMGR_H
  12. // Includes -------------------------------------------------------------------
  13. #include "Template.h"
  14. #include "lkrhash.h"
  15. #include "aspdmon.h"
  16. class CHitObj;
  17. #define MAX_CLEANUP_THREADS 32
  18. // Types and Constants --------------------------------------------------------
  19. #define CTEMPLATEBUCKETS 1021 // size of CTemplate hash table
  20. #define CINCFILEBUCKETS 89 // size of CIncFile hash table
  21. /* ****************************************************************************
  22. Class: CTemplateCacheManager
  23. Synopsis: A CCacheManager that manages a cache of Denali templates
  24. */
  25. class CTemplateCacheManager
  26. {
  27. private:
  28. class CTemplateHashTable;
  29. friend class CTemplateHashTable;
  30. // since there is only one CTemplateCacheManager object ever available, namely
  31. // g_TemplateCache, this is safe to call these two members static.
  32. static BOOL m_fFailedToInitPersistCache;
  33. static char m_szPersistCacheDir[MAX_PATH];
  34. HANDLE m_hOnInitCleanupThread;
  35. HANDLE m_hCleanupThreads[MAX_CLEANUP_THREADS];
  36. DWORD m_cCleanupThreads;
  37. // The type for a hash table of CTemplates keyed on instance id + name
  38. //
  39. // since we provide new methods, make parent methods uncallable
  40. class CTemplateHashTable : private CTypedHashTable<CTemplateHashTable, CTemplate, const CTemplateKey *>
  41. {
  42. private:
  43. CDblLink m_listMemoryTemplates;
  44. CDblLink m_listPersistTemplates;
  45. DWORD m_dwInMemoryTemplates;
  46. DWORD m_dwPersistedTemplates;
  47. public:
  48. // export some methods
  49. DWORD InMemoryTemplates() { return m_dwInMemoryTemplates; };
  50. //CTypedHashTable<CTemplateHashTable, CTemplate, const CTemplateKey *>::Size;
  51. // test to see if the template can be persisted...
  52. BOOL CanPersistTemplate(CTemplate *pTemplate);
  53. // trim some number of templates from the persist cache...
  54. BOOL TrimPersistCache(DWORD dwTrimCount);
  55. VOID ScavengePersistCache();
  56. // new methods
  57. CTemplateHashTable()
  58. : CTypedHashTable<CTemplateHashTable, CTemplate, const CTemplateKey *>("ASP Template Cache") {
  59. m_dwInMemoryTemplates = 0;
  60. m_dwPersistedTemplates = 0;
  61. }
  62. static const CTemplateKey *ExtractKey(const CTemplate *pTemplate)
  63. {
  64. return pTemplate->ExtractHashKey();
  65. }
  66. // NOTE: We don't hash the pTemplateKey->nInstanceID because it can be wildcarded.
  67. // if we were to include in the hash, the wildcard won't hash to the same key
  68. //
  69. static DWORD CalcKeyHash(const CTemplateKey *pTemplateKey)
  70. {
  71. return HashString(pTemplateKey->szPathTranslated, 0);
  72. }
  73. static bool EqualKeys(const CTemplateKey *pKey1, const CTemplateKey *pKey2) {
  74. return (_tcscmp(pKey1->szPathTranslated, pKey2->szPathTranslated) == 0)
  75. && (pKey1->dwInstanceID == pKey2->dwInstanceID
  76. || pKey1->dwInstanceID == MATCH_ALL_INSTANCE_IDS
  77. || pKey2->dwInstanceID == MATCH_ALL_INSTANCE_IDS);
  78. }
  79. // NOTE: In theory, the LKHash can help solve our ref. counting problems, by
  80. // automatic addref/release. However, since prior code uses non-refcounting
  81. // data structure, it's safer to leave old code alaone in this respect, and
  82. // no-op the AddRefRecord method.
  83. //
  84. static void AddRefRecord(CTemplate *pTemplate, int nIncr)
  85. {
  86. }
  87. // Provide new methods to automatically manage the LRU ordering.
  88. // NOTE: We used to override the methods but ran into inconsistencies (bugs?)
  89. // in VC compiler. Sometimes it would call derived & sometimes the base class
  90. // given the same arguemt datatypes.
  91. //
  92. LK_RETCODE InsertTemplate(CTemplate *pTemplate);
  93. LK_RETCODE RemoveTemplate(CTemplate *pTemplate, BOOL fPersist = FALSE, BOOL fScavengePersistCache = TRUE);
  94. // NOTE: Template signature also requires const ptr to const data
  95. LK_RETCODE FindTemplate(const CTemplateKey &rTemplateKey, CTemplate **ppTemplate, BOOL* pfNeedsCheck = NULL);
  96. // accessor methods for hidden LRU cache
  97. bool FMemoryTemplatesIsEmpty() const
  98. {
  99. return m_listMemoryTemplates.FIsEmpty();
  100. }
  101. // you CANNOT compare LRU nodes to NULL to know if you are at the end
  102. // of the list! Instead use this member.
  103. //
  104. BOOL FMemoryTemplatesDblLinkAtEnd(CDblLink *pElem)
  105. {
  106. pElem->AssertValid();
  107. return pElem == &m_listMemoryTemplates;
  108. }
  109. CDblLink *MemoryTemplatesBegin() // return pointer to last referenced item
  110. {
  111. return m_listMemoryTemplates.PNext();
  112. }
  113. CDblLink *MemoryTemplatesEnd() // return pointer to least recently accessed item
  114. {
  115. return m_listMemoryTemplates.PPrev();
  116. }
  117. // accessor methods for hidden LRU cache
  118. bool FPersistTemplatesIsEmpty() const
  119. {
  120. return m_listPersistTemplates.FIsEmpty();
  121. }
  122. // you CANNOT compare LRU nodes to NULL to know if you are at the end
  123. // of the list! Instead use this member.
  124. //
  125. BOOL FPersistTemplatesDblLinkAtEnd(CDblLink *pElem)
  126. {
  127. pElem->AssertValid();
  128. return pElem == &m_listPersistTemplates;
  129. }
  130. CDblLink *PersistTemplatesBegin() // return pointer to last referenced item
  131. {
  132. return m_listPersistTemplates.PNext();
  133. }
  134. CDblLink *PersistTemplatesEnd() // return pointer to least recently accessed item
  135. {
  136. return m_listPersistTemplates.PPrev();
  137. }
  138. };
  139. CRITICAL_SECTION m_csUpdate; // CS for updating the data structures
  140. CTemplateHashTable *m_pHashTemplates; // the cache data structure
  141. DWORD m_dwTemplateCacheTag; // Cache Tag to for cache consistency verification
  142. // Initialize the persistant template cache
  143. BOOL InitPersistCache(CIsapiReqInfo *pIReq);
  144. // static methods primarily used from a seperate thread to flush
  145. // the template cache out of band from the FCN thread notification.
  146. static void FlushHashTable(CTemplateHashTable *pTable);
  147. static DWORD __stdcall FlushHashTableThread(VOID *pArg);
  148. // Spawned at FirstInit to cleanup leftover old cache directories
  149. static DWORD OnInitCleanup(VOID *p);
  150. public:
  151. CTemplateCacheManager();
  152. ~CTemplateCacheManager();
  153. inline void LockTemplateCache() { EnterCriticalSection(&m_csUpdate); }
  154. inline void UnLockTemplateCache() { LeaveCriticalSection(&m_csUpdate); }
  155. HRESULT Init();
  156. HRESULT UnInit();
  157. DWORD GetCacheTag() { return m_dwTemplateCacheTag;}
  158. HRESULT FirstHitInit(CIsapiReqInfo *pIReq)
  159. { InitPersistCache(pIReq); return S_OK; }
  160. // Find in cache (don't load) -- for look-aheads
  161. /////
  162. HRESULT FindCached(const TCHAR *szFile, DWORD dwInstanceID, CTemplate **ppTemplate);
  163. // Get a template from the cache, or load it into cache
  164. /////
  165. HRESULT Load(BOOL fRunGlobalAsp, const TCHAR *szFile, DWORD dwInstanceID, CHitObj *pHitObj, CTemplate **ppTemplate, BOOL *pfTemplateInCache);
  166. // Remove a template from the cache
  167. // for backward compatibility, "nInstanceID" can be omitted, in which case all instance ID
  168. // templates are flushed.
  169. /////
  170. void Flush(const TCHAR *szFile, DWORD dwInstanceID);
  171. // Remove templates from the cache that have a common prefix
  172. // Instance ID is ignored.
  173. /////
  174. void FlushFiles(const TCHAR *szFilePrefix);
  175. // Remove all templates from the cache
  176. /////
  177. //void FlushAll(VOID);
  178. void FlushAll(BOOL fDoLazyFlush = FALSE);
  179. // Add all templates that form an application to the debugger's list of
  180. // running documents
  181. /////
  182. void AddApplicationToDebuggerUI(CAppln *pAppln);
  183. // Remove all templates that form an application from the debugger's list of
  184. // running documents
  185. /////
  186. void RemoveApplicationFromDebuggerUI(CAppln *pAppln);
  187. // Get directory change notification on directories used by template
  188. BOOL RegisterTemplateForChangeNotification(CTemplate *pTemplate, CAppln *pApplication);
  189. // Get directory change notification for applications
  190. BOOL RegisterApplicationForChangeNotification(CTemplate *pTemplate, CAppln *pApplication);
  191. // Stop getting change notification for changes to templates in the cache.
  192. BOOL ShutdownCacheChangeNotification();
  193. };
  194. /* ****************************************************************************
  195. Class: CIncFileMap
  196. Synopsis: A database mapping template include files to a list of their users
  197. */
  198. class CIncFileMap
  199. {
  200. CRITICAL_SECTION m_csUpdate; // CS for updating the data structures
  201. CHashTable m_mpszIncFile; // the cache data structure
  202. public:
  203. CIncFileMap();
  204. ~CIncFileMap();
  205. inline void LockIncFileCache() { EnterCriticalSection(&m_csUpdate); }
  206. inline void UnLockIncFileCache() { LeaveCriticalSection(&m_csUpdate); }
  207. HRESULT Init();
  208. HRESULT UnInit();
  209. HRESULT GetIncFile(const TCHAR *szIncFile, CIncFile **ppIncFile);
  210. void Flush(const TCHAR *szIncFile);
  211. void FlushFiles(const TCHAR *szIncFilePrefix);
  212. };
  213. /* ****************************************************************************
  214. Non-class support functions
  215. */
  216. BOOL FFileChangedSinceCached(const TCHAR *szFile, HANDLE hFile, FILETIME& ftPrevWriteTime);
  217. // Globals --------------------------------------------------------------------
  218. extern CTemplateCacheManager g_TemplateCache;
  219. extern CIncFileMap g_IncFileMap;
  220. inline void LockTemplateAndIncFileCaches()
  221. {
  222. g_TemplateCache.LockTemplateCache();
  223. g_IncFileMap.LockIncFileCache();
  224. }
  225. inline void UnLockTemplateAndIncFileCaches()
  226. {
  227. g_TemplateCache.UnLockTemplateCache();
  228. g_IncFileMap.UnLockIncFileCache();
  229. }
  230. // Prototypes -----------------------------------------------------------------
  231. #endif // _CACHEMGR_H