newlib-cygwin/winsup/cygwin/resource.cc

100 lines
2.4 KiB
C++
Raw Normal View History

2000-02-18 03:38:33 +08:00
/* resource.cc: getrusage () and friends.
Copyright 1996, 1997, 1998, 2000 Cygnus Solutions.
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 <errno.h>
#include "cygerrno.h"
#include "sync.h"
#include "sigproc.h"
#include "pinfo.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;
* exceptions.cc (set_console_handler): Don't allocate console_handler_thread_waiter. It is obsolete. (ctrl_c_handler): Don't use console_handler_thread_waiter. * path.cc (hash_path_name): Fix handling of relative names. Make case insensitive. * path.h (suffix_info): Use initializers. * pinfo.h (_pinfo): Avoid initializers for null case. * resource.cc (fill_rusage): Zero rest of rusage structure. * security.cc (set_process_privileges): Don't reopen parent process. Just use hMainProc. * signal.cc (signal): Track when a signal handler has been used. (sigaction): Ditto. * sigproc.cc (pchildren): Use default initializer. (zombies): Ditto. (sigproc_terminate): Avoid closing handles that will be closed on exit anyway. (wait_sig): Send signal to "parent" on EXECing, not FORKing. (wait_subproc): Send SIGCHLD here rather than in proc_wait to avoid potential muto conflicts. * sigproc.h (sigthread): Don't initialize to zero. It's the default. * spawn.cc (spawn_guts): Fill in resources from exec parent prior to termination. * sync.h (muto): Don't initialize to zero. * syscalls.cc (close_all_files): Use one lock around entire loop and call fhandler close/release stuff directly. (_read): Don't use ready_for_read if there are not signal handlers active. * dcrt0.cc (dll_crt0_1): Fix display of "title". (do_exit): Use pinfo exit method to exit. (__api_fatal): Ditto. * exceptions.cc (signal_exit): Ditto. * fork.cc (fork_child): Remove debugging stuff. Use pinfo_fixup_after fork in place of exec_fixup_after_fork. * pinfo.cc (pinfo_fixup_after_fork): New method. (pinfo_fixup_in_spawned_child): Ditto. (_pinfo::exit): New method. (_pinfo::init): Remove recursion. Detect pathological case where pinfo structure already exists for new pid. * pinfo.h (_pinfo): Reorganize slightly. Add new method and new function declarations. * sigproc.cc (proc_exists): Previous simplification was a little to simple. Try harder to detect if a process exists. (proc_terminate): Use PID_EXITED setting to determine if process is still around. (WFSO): Remove debugging statement. (WFMO): Ditto. * spawn.cc (exec_fixup_after_fork): Eliminate. (spawn_guts): Always set old_title to NULL. Is it really needed? Move hexec_proc to pinfo.cc. Call pinfo_fixup_in_spawned_child to eliminate handle link after a spawn. * include/sys/cygwin.h: Remove PID_NOT_IN_USE. Add PID_EXITED.
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);
}
extern "C"
int
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, hMainProc);
*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;
}