]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Windows: merge alternative getrusage() into squid_getrusage() wrapper
authorAmos Jeffries <squid3@treenet.co.nz>
Mon, 8 Oct 2012 08:00:06 +0000 (02:00 -0600)
committerAmos Jeffries <squid3@treenet.co.nz>
Mon, 8 Oct 2012 08:00:06 +0000 (02:00 -0600)
Since we are already providing a wrapper layer for getrusage() we can
merge the Windows alternative implementation into our wrapper directly.
No need to perform multiple layers of abstraction.

src/tools.cc
src/win32.cc
src/win32.h

index 440d7652c06e1400e7dab3f69fc69ca43cd2b5d9..d8443d6b5826bd3a2bdbd347842e928dafd7d3cf 100644 (file)
@@ -55,6 +55,9 @@
 #if HAVE_SYS_PRCTL_H
 #include <sys/prctl.h>
 #endif
+#if HAVE_WIN32_PSAPI
+#include <psapi.h>
+#endif
 #if HAVE_SYS_STAT_H
 #include <sys/stat.h>
 #endif
@@ -244,22 +247,59 @@ dumpMallocStats(void)
 }
 
 void
-
 squid_getrusage(struct rusage *r)
 {
-
     memset(r, '\0', sizeof(struct rusage));
-#if HAVE_GETRUSAGE && defined(RUSAGE_SELF)
+#if HAVE_GETRUSAGE && defined(RUSAGE_SELF) && !_SQUID_WINDOWS_
 #if _SQUID_SOLARIS_
     /* Solaris 2.5 has getrusage() permission bug -- Arjan de Vet */
     enter_suid();
 #endif
 
     getrusage(RUSAGE_SELF, r);
-#if _SQUID_SOLARIS_
 
+#if _SQUID_SOLARIS_
     leave_suid();
 #endif
+
+#elif _SQUID_WINDOWS_ && HAVE_WIN32_PSAPI
+    // Windows has an alternative method if there is no POSIX getrusage defined.
+    if (WIN32_OS_version >= _WIN_OS_WINNT) {
+        /* On Windows NT and later call PSAPI.DLL for process Memory */
+        /* informations -- Guido Serassio                       */
+        HANDLE hProcess;
+        PROCESS_MEMORY_COUNTERS pmc;
+        hProcess = OpenProcess(PROCESS_QUERY_INFORMATION |
+                               PROCESS_VM_READ,
+                               FALSE, GetCurrentProcessId());
+        {
+            /* Microsoft CRT doesn't have getrusage function,  */
+            /* so we get process CPU time information from PSAPI.DLL. */
+            FILETIME ftCreate, ftExit, ftKernel, ftUser;
+            if (GetProcessTimes(hProcess, &ftCreate, &ftExit, &ftKernel, &ftUser)) {
+                int64_t *ptUser = (int64_t *)&ftUser;
+                int64_t tUser64 = *ptUser / 10;
+                int64_t *ptKernel = (int64_t *)&ftKernel;
+                int64_t tKernel64 = *ptKernel / 10;
+                r->ru_utime.tv_sec =(long)(tUser64 / 1000000);
+                r->ru_stime.tv_sec =(long)(tKernel64 / 1000000);
+                r->ru_utime.tv_usec =(long)(tUser64 % 1000000);
+                r->ru_stime.tv_usec =(long)(tKernel64 % 1000000);
+            } else {
+                CloseHandle( hProcess );
+                return;
+            }
+        }
+        if (GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc))) {
+            r->ru_maxrss=(DWORD)(pmc.WorkingSetSize / getpagesize());
+            r->ru_majflt=pmc.PageFaultCount;
+        } else {
+            CloseHandle( hProcess );
+            return;
+        }
+
+        CloseHandle( hProcess );
+    }
 #endif
 }
 
index ba039a5c073b931374e6ee9c82b33eab774d637c..4ee568e76b59aabf4132df6733190c27d1c25a3f 100644 (file)
@@ -110,53 +110,6 @@ int WIN32_pipe(int handles[2])
     return 0;
 }
 
-int WIN32_getrusage(int who, struct rusage *usage)
-{
-#if HAVE_WIN32_PSAPI
-
-    if (WIN32_OS_version >= _WIN_OS_WINNT) {
-        /* On Windows NT and later call PSAPI.DLL for process Memory */
-        /* informations -- Guido Serassio                       */
-        HANDLE hProcess;
-        PROCESS_MEMORY_COUNTERS pmc;
-        hProcess = OpenProcess(PROCESS_QUERY_INFORMATION |
-                               PROCESS_VM_READ,
-                               FALSE, GetCurrentProcessId());
-        {
-            /* Microsoft CRT doesn't have getrusage function,  */
-            /* so we get process CPU time information from PSAPI.DLL. */
-            FILETIME ftCreate, ftExit, ftKernel, ftUser;
-
-            if (GetProcessTimes(hProcess, &ftCreate, &ftExit, &ftKernel, &ftUser)) {
-                int64_t *ptUser = (int64_t *)&ftUser;
-                int64_t tUser64 = *ptUser / 10;
-                int64_t *ptKernel = (int64_t *)&ftKernel;
-                int64_t tKernel64 = *ptKernel / 10;
-                usage->ru_utime.tv_sec =(long)(tUser64 / 1000000);
-                usage->ru_stime.tv_sec =(long)(tKernel64 / 1000000);
-                usage->ru_utime.tv_usec =(long)(tUser64 % 1000000);
-                usage->ru_stime.tv_usec =(long)(tKernel64 % 1000000);
-            } else {
-                CloseHandle( hProcess );
-                return -1;
-            }
-        }
-
-        if (GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc))) {
-            usage->ru_maxrss=(DWORD)(pmc.WorkingSetSize / getpagesize());
-            usage->ru_majflt=pmc.PageFaultCount;
-        } else {
-            CloseHandle( hProcess );
-            return -1;
-        }
-
-        CloseHandle( hProcess );
-    }
-
-#endif
-    return 0;
-}
-
 int
 Win32__WSAFDIsSet(int fd, fd_set FAR * set)
 {
index d5719a62dce4f307a13bd65b1cfadae99ffc22a9..292fb891e067fba8fba84d6ed12a531995286b9b 100644 (file)
@@ -50,7 +50,6 @@
 
 int WIN32_pipe(int[2]);
 
-int WIN32_getrusage(int, struct rusage *);
 void WIN32_ExceptionHandlerInit(void);
 
 int Win32__WSAFDIsSet(int fd, fd_set* set);