2000-02-18 03:38:33 +08:00
|
|
|
/* resource.cc: getrusage () and friends.
|
|
|
|
|
2011-05-11 17:07:20 +08:00
|
|
|
Copyright 1996, 1997, 1998, 2000, 2001, 2002, 2009, 2010, 2011 Red Hat, Inc.
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
Written by Steve Chamberlain (sac@cygnus.com), Doug Evans (dje@cygnus.com),
|
|
|
|
Geoffrey Noer (noer@cygnus.com) of Cygnus Support.
|
|
|
|
Rewritten by Sergey S. Okhapkin (sos@prospect.com.ru)
|
|
|
|
|
|
|
|
This file is part of Cygwin.
|
|
|
|
|
|
|
|
This software is a copyrighted work licensed under the terms of the
|
|
|
|
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
|
|
|
|
details. */
|
|
|
|
|
|
|
|
#include "winsup.h"
|
2001-01-18 17:30:37 +08:00
|
|
|
#include <unistd.h>
|
2000-08-22 13:10:20 +08:00
|
|
|
#include "pinfo.h"
|
2001-10-22 07:44:43 +08:00
|
|
|
#include "psapi.h"
|
2005-07-03 10:40:30 +08:00
|
|
|
#include "cygtls.h"
|
2009-01-17 18:16:42 +08:00
|
|
|
#include "path.h"
|
|
|
|
#include "fhandler.h"
|
|
|
|
#include "pinfo.h"
|
|
|
|
#include "dtable.h"
|
2010-04-20 18:44:52 +08:00
|
|
|
#include "cygheap.h"
|
2011-05-11 17:07:20 +08:00
|
|
|
#include "ntdll.h"
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
/* add timeval values */
|
|
|
|
static void
|
|
|
|
add_timeval (struct timeval *tv1, struct timeval *tv2)
|
|
|
|
{
|
|
|
|
tv1->tv_sec += tv2->tv_sec;
|
|
|
|
tv1->tv_usec += tv2->tv_usec;
|
|
|
|
if (tv1->tv_usec >= 1000000)
|
|
|
|
{
|
|
|
|
tv1->tv_usec -= 1000000;
|
|
|
|
tv1->tv_sec++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* add rusage values of r2 to r1 */
|
|
|
|
void __stdcall
|
|
|
|
add_rusage (struct rusage *r1, struct rusage *r2)
|
|
|
|
{
|
|
|
|
add_timeval (&r1->ru_utime, &r2->ru_utime);
|
|
|
|
add_timeval (&r1->ru_stime, &r2->ru_stime);
|
|
|
|
r1->ru_maxrss += r2->ru_maxrss;
|
|
|
|
r1->ru_ixrss += r2->ru_ixrss;
|
|
|
|
r1->ru_idrss += r2->ru_idrss;
|
|
|
|
r1->ru_isrss += r2->ru_isrss;
|
|
|
|
r1->ru_minflt += r2->ru_minflt;
|
|
|
|
r1->ru_majflt += r2->ru_majflt;
|
|
|
|
r1->ru_nswap += r2->ru_nswap;
|
|
|
|
r1->ru_inblock += r2->ru_inblock;
|
|
|
|
r1->ru_oublock += r2->ru_oublock;
|
|
|
|
r1->ru_msgsnd += r2->ru_msgsnd;
|
|
|
|
r1->ru_msgrcv += r2->ru_msgrcv;
|
|
|
|
r1->ru_nsignals += r2->ru_nsignals;
|
|
|
|
r1->ru_nvcsw += r2->ru_nvcsw;
|
|
|
|
r1->ru_nivcsw += r2->ru_nivcsw;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* FIXME: what about other fields? */
|
|
|
|
void __stdcall
|
|
|
|
fill_rusage (struct rusage *r, HANDLE h)
|
|
|
|
{
|
|
|
|
FILETIME creation_time = {0,0};
|
|
|
|
FILETIME exit_time = {0,0};
|
|
|
|
FILETIME kernel_time = {0,0};
|
|
|
|
FILETIME user_time = {0,0};
|
|
|
|
|
|
|
|
struct timeval tv;
|
|
|
|
|
2000-10-15 09:37:07 +08:00
|
|
|
memset (r, 0, sizeof (*r));
|
2000-02-18 03:38:33 +08:00
|
|
|
GetProcessTimes (h, &creation_time, &exit_time, &kernel_time, &user_time);
|
|
|
|
totimeval (&tv, &kernel_time, 0, 0);
|
|
|
|
add_timeval (&r->ru_stime, &tv);
|
|
|
|
totimeval (&tv, &user_time, 0, 0);
|
|
|
|
add_timeval (&r->ru_utime, &tv);
|
2001-10-22 07:44:43 +08:00
|
|
|
|
2011-05-11 17:07:20 +08:00
|
|
|
VM_COUNTERS vmc;
|
|
|
|
NTSTATUS status = NtQueryInformationProcess (h, ProcessVmCounters, &vmc,
|
|
|
|
sizeof vmc, NULL);
|
|
|
|
if (NT_SUCCESS (status))
|
2001-10-22 07:44:43 +08:00
|
|
|
{
|
2011-05-11 17:07:20 +08:00
|
|
|
r->ru_maxrss += (long) (vmc.WorkingSetSize / 1024);
|
|
|
|
r->ru_majflt += vmc.PageFaultCount;
|
2001-10-22 07:44:43 +08:00
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2002-10-08 04:06:16 +08:00
|
|
|
extern "C" int
|
2000-02-18 03:38:33 +08:00
|
|
|
getrusage (int intwho, struct rusage *rusage_in)
|
|
|
|
{
|
|
|
|
int res = 0;
|
|
|
|
struct rusage r;
|
|
|
|
|
|
|
|
if (intwho == RUSAGE_SELF)
|
|
|
|
{
|
|
|
|
memset (&r, 0, sizeof (r));
|
2009-12-19 04:32:04 +08:00
|
|
|
fill_rusage (&r, GetCurrentProcess ());
|
2000-02-18 03:38:33 +08:00
|
|
|
*rusage_in = r;
|
|
|
|
}
|
|
|
|
else if (intwho == RUSAGE_CHILDREN)
|
|
|
|
*rusage_in = myself->rusage_children;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
set_errno (EINVAL);
|
|
|
|
res = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
syscall_printf ("%d = getrusage (%d, %p)", res, intwho, rusage_in);
|
|
|
|
return res;
|
|
|
|
}
|
2000-12-27 08:13:57 +08:00
|
|
|
|
2001-10-22 08:10:46 +08:00
|
|
|
extern "C" int
|
2000-12-27 08:13:57 +08:00
|
|
|
getrlimit (int resource, struct rlimit *rlp)
|
|
|
|
{
|
2001-08-29 06:48:23 +08:00
|
|
|
MEMORY_BASIC_INFORMATION m;
|
|
|
|
|
2005-07-03 10:40:30 +08:00
|
|
|
myfault efault;
|
|
|
|
if (efault.faulted (EFAULT))
|
2001-06-28 10:19:57 +08:00
|
|
|
return -1;
|
2000-12-27 08:13:57 +08:00
|
|
|
|
|
|
|
rlp->rlim_cur = RLIM_INFINITY;
|
|
|
|
rlp->rlim_max = RLIM_INFINITY;
|
|
|
|
|
|
|
|
switch (resource)
|
|
|
|
{
|
|
|
|
case RLIMIT_CPU:
|
|
|
|
case RLIMIT_FSIZE:
|
|
|
|
case RLIMIT_DATA:
|
2001-08-29 06:48:23 +08:00
|
|
|
break;
|
2000-12-27 08:13:57 +08:00
|
|
|
case RLIMIT_STACK:
|
2001-08-29 06:48:23 +08:00
|
|
|
if (!VirtualQuery ((LPCVOID) &m, &m, sizeof m))
|
|
|
|
debug_printf ("couldn't get stack info, returning def.values. %E");
|
|
|
|
else
|
2001-09-08 05:32:07 +08:00
|
|
|
{
|
2001-08-29 06:48:23 +08:00
|
|
|
rlp->rlim_cur = (DWORD) &m - (DWORD) m.AllocationBase;
|
|
|
|
rlp->rlim_max = (DWORD) m.BaseAddress + m.RegionSize
|
|
|
|
- (DWORD) m.AllocationBase;
|
|
|
|
}
|
2001-08-16 22:20:09 +08:00
|
|
|
break;
|
2000-12-27 08:13:57 +08:00
|
|
|
case RLIMIT_NOFILE:
|
2001-08-16 22:29:21 +08:00
|
|
|
rlp->rlim_cur = getdtablesize ();
|
2003-07-09 09:33:06 +08:00
|
|
|
if (rlp->rlim_cur < OPEN_MAX)
|
|
|
|
rlp->rlim_cur = OPEN_MAX;
|
2009-09-22 12:09:03 +08:00
|
|
|
rlp->rlim_max = OPEN_MAX_MAX;
|
2000-12-27 08:13:57 +08:00
|
|
|
break;
|
|
|
|
case RLIMIT_CORE:
|
2010-04-20 18:44:52 +08:00
|
|
|
rlp->rlim_cur = cygheap->rlim_core;
|
2000-12-27 08:13:57 +08:00
|
|
|
break;
|
|
|
|
case RLIMIT_AS:
|
|
|
|
rlp->rlim_cur = 0x80000000UL;
|
|
|
|
rlp->rlim_max = 0x80000000UL;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
set_errno (EINVAL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-10-22 08:10:46 +08:00
|
|
|
extern "C" int
|
2000-12-27 08:13:57 +08:00
|
|
|
setrlimit (int resource, const struct rlimit *rlp)
|
|
|
|
{
|
2005-07-03 10:40:30 +08:00
|
|
|
myfault efault;
|
|
|
|
if (efault.faulted (EFAULT))
|
2001-06-28 10:19:57 +08:00
|
|
|
return -1;
|
2001-01-05 17:01:18 +08:00
|
|
|
|
|
|
|
struct rlimit oldlimits;
|
|
|
|
|
|
|
|
// Check if the request is to actually change the resource settings.
|
|
|
|
// If it does not result in a change, take no action and do not
|
|
|
|
// fail.
|
2002-09-22 11:38:57 +08:00
|
|
|
if (getrlimit (resource, &oldlimits) < 0)
|
2001-01-05 17:01:18 +08:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (oldlimits.rlim_cur == rlp->rlim_cur &&
|
|
|
|
oldlimits.rlim_max == rlp->rlim_max)
|
|
|
|
// No change in resource requirements, succeed immediately
|
|
|
|
return 0;
|
2000-12-27 08:13:57 +08:00
|
|
|
|
|
|
|
switch (resource)
|
|
|
|
{
|
|
|
|
case RLIMIT_CORE:
|
2010-04-20 18:44:52 +08:00
|
|
|
cygheap->rlim_core = rlp->rlim_cur;
|
2000-12-27 08:13:57 +08:00
|
|
|
break;
|
2001-01-18 17:30:37 +08:00
|
|
|
case RLIMIT_NOFILE:
|
|
|
|
if (rlp->rlim_cur != RLIM_INFINITY)
|
2001-09-08 05:32:07 +08:00
|
|
|
return setdtablesize (rlp->rlim_cur);
|
2001-01-18 17:30:37 +08:00
|
|
|
break;
|
2000-12-27 08:13:57 +08:00
|
|
|
default:
|
|
|
|
set_errno (EINVAL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|