newlib/winsup/cygwin/resource.cc

199 lines
4.7 KiB
C++
Raw Normal View History

2000-02-17 20:38:33 +01:00
/* resource.cc: getrusage () and friends.
Copyright 1996, 1997, 1998, 2000, 2001, 2002, 2003, 2005, 2008, 2009, 2010,
2011, 2012, 2013 Red Hat, Inc.
2000-02-17 20:38:33 +01: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"
#include <unistd.h>
#include <sys/param.h>
#include "pinfo.h"
#include "psapi.h"
#include "cygtls.h"
#include "path.h"
#include "fhandler.h"
#include "pinfo.h"
#include "dtable.h"
#include "cygheap.h"
#include "ntdll.h"
2000-02-17 20:38:33 +01: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)
{
Streamline time/times functionality. Remove last remains of former Windows 9x compatibility. * fhandler_disk_file.cc (fhandler_base::fstat_helper): Drop now unneeded casts in calls to_timestruc_t. (fhandler_base::utimens_fs): Ditto for timespec_to_filetime. * fhandler_proc.cc (format_proc_stat): Ditto for to_time_t. * hires.h (class hires_ms): Remove unused member initime_ns. Remove declarations for timeGetTime_ns and prime. (hires_ms::uptime): Remove. * posix_ipc.cc (ipc_cond_timedwait): Ditto for timespec_to_filetime. * fhandler_registry.cc (fhandler_registry::fstat): Add cast. * resource.cc (fill_rusage): Call NtQueryInformationProcess rather than GetProcessTimes to deal with LARGE_INTEGER rather than FILETIME. * times.cc: Simplify time handling. Throughout, use LARGE_INTEGER rather than FILETIME to simplify computations. Throughout use {u}int64_t rather than {unsigned} long long. Drop unneeded casts since NSPERSEC is 64 bit anyway. (systime_ns): Remove. (times): Call NtQuerySystemInformation to fetch boot time. Call NtQueryInformationProcess rather than GetProcessTimes to deal with LARGE_INTEGER rather than FILETIME. Call GetSystemTimeAsFileTime. (totimeval): Use constant 1000000 as in other functions. (time_t_to_filetime): Remove. (to_time_t): Change return type to time_t. (time_as_timestruc_t): Rename filetime to systime. (time): Ditto. Add cast. (hires_ns::nsecs): Fix return type cast. (hires_ms::timeGetTime_ns): Remove. (hires_ns::prime): Remove. (hires_ms::nsecs): Drop call to prime. Call GetSystemTimeAsFileTime directly. Subtract FACTOR here since it's the only function needing to do so. (minperiod): Cosmetically change to ULONG. (hires_ns::resolution): Fix return type cast. (hires_ms::resolution): Simplify, rely on NtQueryTimerResolution. * winsup.h: Align time related prototypes to above changes.
2013-06-14 11:09:41 +02:00
KERNEL_USER_TIMES kut;
2000-02-17 20:38:33 +01:00
struct timeval tv;
Streamline time/times functionality. Remove last remains of former Windows 9x compatibility. * fhandler_disk_file.cc (fhandler_base::fstat_helper): Drop now unneeded casts in calls to_timestruc_t. (fhandler_base::utimens_fs): Ditto for timespec_to_filetime. * fhandler_proc.cc (format_proc_stat): Ditto for to_time_t. * hires.h (class hires_ms): Remove unused member initime_ns. Remove declarations for timeGetTime_ns and prime. (hires_ms::uptime): Remove. * posix_ipc.cc (ipc_cond_timedwait): Ditto for timespec_to_filetime. * fhandler_registry.cc (fhandler_registry::fstat): Add cast. * resource.cc (fill_rusage): Call NtQueryInformationProcess rather than GetProcessTimes to deal with LARGE_INTEGER rather than FILETIME. * times.cc: Simplify time handling. Throughout, use LARGE_INTEGER rather than FILETIME to simplify computations. Throughout use {u}int64_t rather than {unsigned} long long. Drop unneeded casts since NSPERSEC is 64 bit anyway. (systime_ns): Remove. (times): Call NtQuerySystemInformation to fetch boot time. Call NtQueryInformationProcess rather than GetProcessTimes to deal with LARGE_INTEGER rather than FILETIME. Call GetSystemTimeAsFileTime. (totimeval): Use constant 1000000 as in other functions. (time_t_to_filetime): Remove. (to_time_t): Change return type to time_t. (time_as_timestruc_t): Rename filetime to systime. (time): Ditto. Add cast. (hires_ns::nsecs): Fix return type cast. (hires_ms::timeGetTime_ns): Remove. (hires_ns::prime): Remove. (hires_ms::nsecs): Drop call to prime. Call GetSystemTimeAsFileTime directly. Subtract FACTOR here since it's the only function needing to do so. (minperiod): Cosmetically change to ULONG. (hires_ns::resolution): Fix return type cast. (hires_ms::resolution): Simplify, rely on NtQueryTimerResolution. * winsup.h: Align time related prototypes to above changes.
2013-06-14 11:09:41 +02:00
memset (&kut, 0, sizeof kut);
memset (r, 0, sizeof *r);
NtQueryInformationProcess (h, ProcessTimes, &kut, sizeof kut, NULL);
totimeval (&tv, &kut.KernelTime, 0, 0);
2000-02-17 20:38:33 +01:00
add_timeval (&r->ru_stime, &tv);
Streamline time/times functionality. Remove last remains of former Windows 9x compatibility. * fhandler_disk_file.cc (fhandler_base::fstat_helper): Drop now unneeded casts in calls to_timestruc_t. (fhandler_base::utimens_fs): Ditto for timespec_to_filetime. * fhandler_proc.cc (format_proc_stat): Ditto for to_time_t. * hires.h (class hires_ms): Remove unused member initime_ns. Remove declarations for timeGetTime_ns and prime. (hires_ms::uptime): Remove. * posix_ipc.cc (ipc_cond_timedwait): Ditto for timespec_to_filetime. * fhandler_registry.cc (fhandler_registry::fstat): Add cast. * resource.cc (fill_rusage): Call NtQueryInformationProcess rather than GetProcessTimes to deal with LARGE_INTEGER rather than FILETIME. * times.cc: Simplify time handling. Throughout, use LARGE_INTEGER rather than FILETIME to simplify computations. Throughout use {u}int64_t rather than {unsigned} long long. Drop unneeded casts since NSPERSEC is 64 bit anyway. (systime_ns): Remove. (times): Call NtQuerySystemInformation to fetch boot time. Call NtQueryInformationProcess rather than GetProcessTimes to deal with LARGE_INTEGER rather than FILETIME. Call GetSystemTimeAsFileTime. (totimeval): Use constant 1000000 as in other functions. (time_t_to_filetime): Remove. (to_time_t): Change return type to time_t. (time_as_timestruc_t): Rename filetime to systime. (time): Ditto. Add cast. (hires_ns::nsecs): Fix return type cast. (hires_ms::timeGetTime_ns): Remove. (hires_ns::prime): Remove. (hires_ms::nsecs): Drop call to prime. Call GetSystemTimeAsFileTime directly. Subtract FACTOR here since it's the only function needing to do so. (minperiod): Cosmetically change to ULONG. (hires_ns::resolution): Fix return type cast. (hires_ms::resolution): Simplify, rely on NtQueryTimerResolution. * winsup.h: Align time related prototypes to above changes.
2013-06-14 11:09:41 +02:00
totimeval (&tv, &kut.UserTime, 0, 0);
2000-02-17 20:38:33 +01:00
add_timeval (&r->ru_utime, &tv);
VM_COUNTERS vmc;
NTSTATUS status = NtQueryInformationProcess (h, ProcessVmCounters, &vmc,
sizeof vmc, NULL);
if (NT_SUCCESS (status))
{
r->ru_maxrss += (long) (vmc.WorkingSetSize / 1024);
r->ru_majflt += vmc.PageFaultCount;
}
2000-02-17 20:38:33 +01:00
}
extern "C" int
2000-02-17 20:38:33 +01:00
getrusage (int intwho, struct rusage *rusage_in)
{
int res = 0;
struct rusage r;
if (intwho == RUSAGE_SELF)
{
memset (&r, 0, sizeof (r));
fill_rusage (&r, GetCurrentProcess ());
2000-02-17 20:38:33 +01:00
*rusage_in = r;
}
else if (intwho == RUSAGE_CHILDREN)
*rusage_in = myself->rusage_children;
else
{
set_errno (EINVAL);
res = -1;
}
syscall_printf ("%R = getrusage(%d, %p)", res, intwho, rusage_in);
2000-02-17 20:38:33 +01:00
return res;
}
extern "C" int
getrlimit (int resource, struct rlimit *rlp)
{
MEMORY_BASIC_INFORMATION m;
myfault efault;
if (efault.faulted (EFAULT))
return -1;
rlp->rlim_cur = RLIM_INFINITY;
rlp->rlim_max = RLIM_INFINITY;
switch (resource)
{
case RLIMIT_CPU:
case RLIMIT_FSIZE:
case RLIMIT_DATA:
case RLIMIT_AS:
break;
case RLIMIT_STACK:
if (!VirtualQuery ((LPCVOID) &m, &m, sizeof m))
debug_printf ("couldn't get stack info, returning def.values. %E");
else
{
2013-04-23 11:44:36 +02:00
rlp->rlim_cur = (rlim_t) &m - (rlim_t) m.AllocationBase;
rlp->rlim_max = (rlim_t) m.BaseAddress + m.RegionSize
- (rlim_t) m.AllocationBase;
}
break;
case RLIMIT_NOFILE:
rlp->rlim_cur = getdtablesize ();
if (rlp->rlim_cur < OPEN_MAX)
rlp->rlim_cur = OPEN_MAX;
rlp->rlim_max = OPEN_MAX_MAX;
break;
case RLIMIT_CORE:
rlp->rlim_cur = cygheap->rlim_core;
break;
default:
set_errno (EINVAL);
return -1;
}
return 0;
}
extern "C" int
setrlimit (int resource, const struct rlimit *rlp)
{
myfault efault;
if (efault.faulted (EFAULT))
return -1;
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. */
if (getrlimit (resource, &oldlimits) < 0)
return -1;
if (oldlimits.rlim_cur == rlp->rlim_cur &&
oldlimits.rlim_max == rlp->rlim_max)
/* No change in resource requirements, succeed immediately */
return 0;
if (rlp->rlim_cur > rlp->rlim_max)
{
set_errno (EINVAL);
return -1;
}
switch (resource)
{
case RLIMIT_CORE:
cygheap->rlim_core = rlp->rlim_cur;
break;
case RLIMIT_NOFILE:
if (rlp->rlim_cur != RLIM_INFINITY)
return setdtablesize (rlp->rlim_cur);
break;
default:
set_errno (EINVAL);
return -1;
}
return 0;
}