2009-03-24 10:40:39 -04:00
|
|
|
/* This file is part of the MPIR Library.
|
|
|
|
|
|
|
|
The MPIR Library is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License as published
|
|
|
|
by the Free Software Foundation; either version 2.1 of the License, or (at
|
|
|
|
your option) any later version.
|
|
|
|
The MPIR Library is distributed in the hope that it will be useful, but
|
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
|
|
|
License for more details.
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
|
|
along with the MPIR Library; see the file COPYING.LIB. If not, write
|
|
|
|
to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
2009-02-27 09:24:25 -05:00
|
|
|
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
|
|
|
|
#include <windows.h>
|
2009-03-24 10:40:39 -04:00
|
|
|
#include <psapi.h>
|
2009-02-27 09:24:25 -05:00
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include "getrusage.h"
|
|
|
|
|
2009-03-24 10:40:39 -04:00
|
|
|
typedef union file_t
|
|
|
|
{ FILETIME ft;
|
|
|
|
long long lt;
|
|
|
|
} file_t;
|
2009-02-27 09:24:25 -05:00
|
|
|
|
2009-03-24 10:40:39 -04:00
|
|
|
int getrusage(int who, rusage *usage)
|
2009-02-27 09:24:25 -05:00
|
|
|
{
|
2009-03-24 10:40:39 -04:00
|
|
|
HANDLE proc_hand;
|
|
|
|
file_t c_time, x_time, s_time, u_time;
|
|
|
|
int cb = 0, err = -1;
|
2009-02-27 09:24:25 -05:00
|
|
|
|
|
|
|
if(who != RUSAGE_SELF)
|
|
|
|
{
|
2009-03-24 10:40:39 -04:00
|
|
|
errno = (who == RUSAGE_CHILDREN ? ENODATA : EINVAL);
|
|
|
|
return err;
|
2009-02-27 09:24:25 -05:00
|
|
|
}
|
|
|
|
|
2009-03-24 10:40:39 -04:00
|
|
|
proc_hand = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, GetCurrentProcessId());
|
2009-02-27 09:24:25 -05:00
|
|
|
|
2009-03-24 10:40:39 -04:00
|
|
|
if(GetProcessTimes(proc_hand, &(c_time.ft), &(x_time.ft), &(s_time.ft), &(u_time.ft)))
|
2009-02-27 09:24:25 -05:00
|
|
|
{
|
2009-03-24 10:40:39 -04:00
|
|
|
PROCESS_MEMORY_COUNTERS ctrs;
|
|
|
|
|
|
|
|
/* The units returned by GetProcessTimes are 100 nanoseconds */
|
|
|
|
u_time.lt = (u_time.lt + 5) / 10;
|
|
|
|
s_time.lt = (s_time.lt + 5) / 10;
|
2009-02-27 09:24:25 -05:00
|
|
|
|
2009-03-24 10:40:39 -04:00
|
|
|
usage->ru_utime.tv_sec = (long)(u_time.lt / 1000000ll);
|
|
|
|
usage->ru_stime.tv_sec = (long)(s_time.lt / 1000000ll);
|
|
|
|
usage->ru_utime.tv_usec = (long)(u_time.lt % 1000000ll);
|
|
|
|
usage->ru_stime.tv_usec = (long)(s_time.lt % 1000000ll);
|
2009-02-27 09:24:25 -05:00
|
|
|
|
2009-03-24 10:40:39 -04:00
|
|
|
if(GetProcessMemoryInfo(proc_hand, &ctrs, sizeof(ctrs)))
|
2009-02-27 09:24:25 -05:00
|
|
|
{
|
2009-03-24 10:40:39 -04:00
|
|
|
PERFORMANCE_INFORMATION perf_info;
|
|
|
|
GetPerformanceInfo(&perf_info, sizeof(perf_info));
|
|
|
|
usage->ru_maxrss = (DWORD) (ctrs.WorkingSetSize / perf_info.PageSize);
|
|
|
|
usage->ru_majflt = ctrs.PageFaultCount;
|
|
|
|
err = 0;
|
|
|
|
}
|
|
|
|
}
|
2009-02-27 09:24:25 -05:00
|
|
|
|
2009-03-24 10:40:39 -04:00
|
|
|
if(err)
|
|
|
|
errno = EACCES;
|
|
|
|
CloseHandle(proc_hand);
|
|
|
|
return err;
|
2009-02-27 09:24:25 -05:00
|
|
|
}
|