windows-virtual-memory-20041224
[openafs.git] / src / WINNT / afsd / cm_buf.c
index 9fef57d..ed31f37 100644 (file)
 #include <malloc.h>
 #include <stdio.h>
 #include <assert.h>
+#include <strsafe.h>
 
 #include "afsd.h"
 
+#ifdef DEBUG
+#define TRACE_BUFFER 1
+#endif
+
 extern void afsi_log(char *pattern, ...);
 
 /* This module implements the buffer package used by the local transaction
@@ -81,6 +86,9 @@ int buf_cacheType = CM_BUF_CACHETYPE_FILE;
 static
 HANDLE CacheHandle;
 
+static 
+VOID * ViewOfFile;
+
 static
 SYSTEM_INFO sysInfo;
 #endif /* !DJGPP */
@@ -306,12 +314,12 @@ long buf_Init(cm_buf_ops_t *opsp)
                              OPEN_ALWAYS,
                              FILE_ATTRIBUTE_NORMAL,
                              NULL);
+            FreeCacheFileSA(psa);
             if (hf == INVALID_HANDLE_VALUE) {
                 afsi_log("Error creating cache file \"%s\" error %d", 
                           cm_CachePath, GetLastError());
                 return CM_ERROR_INVAL;
             }
-            FreeCacheFileSA(psa);
         } else { /* buf_cacheType == CM_BUF_CACHETYPE_VIRTUAL */
             hf = INVALID_HANDLE_VALUE;
         }
@@ -329,17 +337,20 @@ long buf_Init(cm_buf_ops_t *opsp)
             }
             return CM_ERROR_INVAL;
         }
-        data = MapViewOfFile(hm,
-                              FILE_MAP_ALL_ACCESS,
-                              0, 0,   
-                              buf_nbuffers * buf_bufferSize);
-        if (data == NULL) {
+        ViewOfFile = MapViewOfFile(hm,
+                                   FILE_MAP_ALL_ACCESS,
+                                   0, 0,   
+                                   buf_nbuffers * buf_bufferSize);
+        if (ViewOfFile == NULL) {
+            afsi_log("Error mapping view of file: 0x%X", GetLastError());
             if (hf != INVALID_HANDLE_VALUE)
                 CloseHandle(hf);
             CloseHandle(hm);
             return CM_ERROR_INVAL;
         }
         CloseHandle(hm);
+
+        data = ViewOfFile;
 #else   
         /* djgpp doesn't support memory mapped files */
         data = malloc(buf_nbuffers * buf_bufferSize);
@@ -378,8 +389,11 @@ long buf_Init(cm_buf_ops_t *opsp)
         /* just for safety's sake */
         buf_maxReservedBufs = buf_nbuffers - 3;
 
+#ifdef TRACE_BUFFER
         /* init the buffer trace log */
-        buf_logp = osi_LogCreate("buffer", 10);
+        buf_logp = osi_LogCreate("buffer", 1000);
+        osi_LogEnable(buf_logp);
+#endif
 
         osi_EndOnce(&once);
 
@@ -397,6 +411,13 @@ long buf_Init(cm_buf_ops_t *opsp)
     return 0;
 }
 
+void
+buf_Shutdown(void)
+{
+    UnmapViewOfFile(ViewOfFile);
+    CloseHandle(CacheHandle);
+}
+
 /* add nbuffers to the buffer pool, if possible.
  * Called with no locks held.
  */
@@ -532,7 +553,7 @@ void buf_WaitIO(cm_buf_t *bp)
         bp->flags |= CM_BUF_WAITING;
         osi_SleepM((long) bp, &bp->mx);
         lock_ObtainMutex(&bp->mx);
-               osi_Log1(buf_logp, "buf_WaitIO conflict wait done for 0x%x", bp);
+        osi_Log1(buf_logp, "buf_WaitIO conflict wait done for 0x%x", bp);
     }
         
     /* if we get here, the IO is done, but we may have to wakeup people waiting for
@@ -1456,17 +1477,16 @@ int cm_DumpBufHashTable(FILE *outputFile, char *cookie)
 
     lock_ObtainRead(&buf_globalLock);
   
-    sprintf(output, "%s - dumping buf_HashTable - buf_hashSize=%d\n", cookie, buf_hashSize);
+    StringCbPrintfA(output, sizeof(output), "%s - dumping buf_HashTable - buf_hashSize=%d\n", cookie, buf_hashSize);
     WriteFile(outputFile, output, strlen(output), &zilch, NULL);
   
     for (i = 0; i < buf_hashSize; i++)
     {
-        for(bp = buf_hashTablepp[i]; bp; bp=bp->hashp) 
+        for (bp = buf_hashTablepp[i]; bp; bp=bp->hashp) 
         {
             if (bp->refCount)
             {
-                sprintf(output, "%s bp=0x%08X, hash=%d, fid (cell=%d, volume=%d,"
-                        "vnode=%d, unique=%d), size=%d refCount=%d\n", 
+                StringCbPrintfA(output, sizeof(output), "vnode=%d, unique=%d), size=%d refCount=%d\n", 
                         cookie, (void *)bp, i, bp->fid.cell, bp->fid.volume, 
                         bp->fid.vnode, bp->fid.unique, bp->size, bp->refCount);
                 WriteFile(outputFile, output, strlen(output), &zilch, NULL);
@@ -1474,10 +1494,31 @@ int cm_DumpBufHashTable(FILE *outputFile, char *cookie)
         }
     }
   
-    sprintf(output, "%s - Done dumping buf_HashTable.\n", cookie);
+    StringCbPrintfA(output, sizeof(output), "%s - Done dumping buf_HashTable.\n", cookie);
     WriteFile(outputFile, output, strlen(output), &zilch, NULL);
 
     lock_ReleaseRead(&buf_globalLock);
     return 0;
 }
 
+void buf_ForceTrace(BOOL flush)
+{
+    HANDLE handle;
+    int len;
+    char buf[256];
+
+    if (!buf_logp) 
+        return;
+
+    len = GetTempPath(sizeof(buf)-10, buf);
+    StringCbCopyA(&buf[len], sizeof(buf)-len, "/afs-buffer.log");
+    handle = CreateFile(buf, GENERIC_WRITE, FILE_SHARE_READ,
+                           NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
+    if (handle == INVALID_HANDLE_VALUE) {
+        osi_panic("Cannot create log file", __FILE__, __LINE__);
+    }
+    osi_LogPrint(buf_logp, handle);
+    if (flush)
+        FlushFileBuffers(handle);
+    CloseHandle(handle);
+}