From: Francesco Chemolli Date: Sun, 29 Jan 2012 13:53:57 +0000 (+0100) Subject: Fix getrusage emulation on windows. X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6941bb15e5068f4ac7dcd5dea386384ce259a808;p=thirdparty%2Fsquid.git Fix getrusage emulation on windows. --- diff --git a/compat/mswin.cc b/compat/mswin.cc index 5aeff1f6cc..33925f7f41 100644 --- a/compat/mswin.cc +++ b/compat/mswin.cc @@ -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) { diff --git a/compat/os/mswin.h b/compat/os/mswin.h index d7215b68d1..e8373f2b61 100644 --- a/compat/os/mswin.h +++ b/compat/os/mswin.h @@ -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 */ diff --git a/src/protos.h b/src/protos.h index aef3e05f3c..705318ea00 100644 --- a/src/protos.h +++ b/src/protos.h @@ -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); diff --git a/src/tools.cc b/src/tools.cc index 730b91f63a..1341167810 100644 --- a/src/tools.cc +++ b/src/tools.cc @@ -51,6 +51,9 @@ #if HAVE_SYS_PRCTL_H #include #endif +#if HAVE_WIN32_PSAPI +#include +#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;