]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Fix getrusage emulation on windows.
authorFrancesco Chemolli <kinkie@squid-cache.org>
Sun, 29 Jan 2012 13:53:57 +0000 (14:53 +0100)
committerFrancesco Chemolli <kinkie@squid-cache.org>
Sun, 29 Jan 2012 13:53:57 +0000 (14:53 +0100)
compat/mswin.cc
compat/os/mswin.h
src/protos.h
src/tools.cc

index 5aeff1f6cce29d273ef33e6c008ff7109059c799..33925f7f4199277ada46d1b31394f14c42d4e68d 100644 (file)
@@ -77,6 +77,55 @@ getpagesize()
 }
 #endif /* HAVE_GETPAGESIZE > 1 */
 
+#if 0
+#if HAVE_GETRUSAGE > 1 && defined(_SQUID_MINGW_)
+#define DELTA_EPOCHS_MSEC 11644473600000000ULL
+void
+winFileTimeToStructTimeval(FILETIME *ft, struct timeval *tv)
+{
+       int64_t tmp;
+       // extract high- and low- bytes
+       tmp |= ft->dwHighDateTime;
+       tmp <<= 32;
+       tmp |= ft->dwLowDateTime;
+       // convert to microseconds
+       tmp /= 10;
+       // adapt to different epochs (Windows' is Jan 1, 1601)
+       tmp-=DELTA_EPOCHS_MSEC;
+       // fill in timeval;
+       tv->tv_sec=(long)(tmp/1000000UL); // MSFT can't even get timeval right..
+       tv->tv_usec=(long)(tmp%1000000UL);
+}
+int
+getrusage(int who, struct rusage *usage)
+{
+       memset(usage,0,sizeof(usage));
+       if (who!=RUSAGE_SELF)
+               return 0;
+       HANDLE hProc;
+       PROCESS_MEMORY_COUNTERS pc;
+       hProc=OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
+                       FALSE, GetCurrentProcessId());
+       if (hProc==NULL) {
+               errno=EINVAL;
+               return -1
+       }
+       if (GetProcessMemoryInfo(hProc,&pc,sizeof(pc))) {
+               usage->maxrss=pc.PeakWorkingSetSize;
+               usage->ru_majflt=pc.PageFaultCount;
+               usage->ru_ixrss=pc.WorkingSetSize;
+       }
+       FILETIME ctime, etime; /* creation and exit times (unused) */
+       FILETIME utime, stime; /* user and system times */
+       if (GetProcessTimes(hProc,&ctime,&etime,&stime,&utime)) {
+               winFileTimeToStructTimeval(&stime,&usage->ru_stime);
+               winFileTimeToStructTimeval(&utime,&usage->ru_utime);
+       }
+       return 0;
+}
+#endif
+#endif
+
 int
 chroot(const char *dirname)
 {
index d7215b68d154d0ecd009c151a167ede581d84098..e8373f2b61031e50bc111144df2a556f691ecd09 100644 (file)
@@ -765,7 +765,7 @@ int WSASocket(int a, int t, int p, LPWSAPROTOCOL_INFO i, GROUP g, DWORD f)
 #define        RUSAGE_SELF     0               /* calling process */
 #define        RUSAGE_CHILDREN -1              /* terminated child processes */
 
-#define HAVE_RUSAGE 1
+//#define HAVE_RUSAGE 2
 struct rusage {
     struct timeval ru_utime;   /* user time used */
     struct timeval ru_stime;   /* system time used */
@@ -858,16 +858,10 @@ void syslog(int priority, const char *fmt, ...);
 #define WTERMSIG(w) ((w) & 0x7f)
 #define WSTOPSIG    WEXITSTATUS
 
+
 #endif
 
 /* prototypes */
 void WIN32_maperror(unsigned long WIN32_oserrno);
-#if !HAVE_GETPAGESIZE
-inline size_t
-getpagesize() {
-       return 4096;
-}
-#endif
-
 #endif /* _SQUID_WINDOWS_ */
 #endif /* SQUID_OS_MSWIN_H */
index aef3e05f3cbcf8f4ece6c7a1e8da3c15437a9c10..705318ea004bd3aedb5619a022750bff24905f6e 100644 (file)
@@ -677,7 +677,7 @@ SQUIDCEXTERN int SquidMain(int, char **);
 
 SQUIDCEXTERN int WIN32_pipe(int[2]);
 
-SQUIDCEXTERN int WIN32_getrusage(int, struct rusage *);
+//SQUIDCEXTERN int WIN32_getrusage(int, struct rusage *);
 SQUIDCEXTERN void WIN32_ExceptionHandlerInit(void);
 
 SQUIDCEXTERN int Win32__WSAFDIsSet(int fd, fd_set* set);
index 730b91f63a2979a5b953c60950d2612aec28d069..134116781023de4f59abcde3b367d8cc913215e7 100644 (file)
@@ -51,6 +51,9 @@
 #if HAVE_SYS_PRCTL_H
 #include <sys/prctl.h>
 #endif
+#if HAVE_WIN32_PSAPI
+#include <psapi.h>
+#endif
 
 #define DEAD_MSG "\
 The Squid Cache (version %s) died.\n\
@@ -236,7 +239,7 @@ 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();
@@ -266,18 +269,18 @@ squid_getrusage(struct rusage *r)
                 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);
+                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))) {
-            usage->ru_maxrss=(DWORD)(pmc.WorkingSetSize / getpagesize());
-            usage->ru_majflt=pmc.PageFaultCount;
+            r->ru_maxrss=(DWORD)(pmc.WorkingSetSize / getpagesize());
+            r->ru_majflt=pmc.PageFaultCount;
         } else {
             CloseHandle( hProcess );
             return;