initial import
[glibc.git] / sysdeps / generic / vtimes.c
blobfaa053ff79d4ed0089e55c9e65450954f2230970
1 /* Copyright (C) 1991 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If
16 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, MA 02139, USA. */
19 #include <ansidecl.h>
20 #include <stddef.h>
21 #include <sys/vtimes.h>
22 #include <sys/resource.h>
24 /* Return the number of 1/VTIMES_UNITS_PER_SECOND-second
25 units in the `struct timeval' TV. */
26 #define TIMEVAL_TO_VTIMES(tv) \
27 ((tv.tv_sec * VTIMES_UNITS_PER_SECOND) + \
28 (tv.tv_usec * VTIMES_UNITS_PER_SECOND / 1000000))
30 /* If VT is not NULL, write statistics for WHO into *VT.
31 Return 0 for success, -1 for failure. */
32 static int
33 DEFUN(vtimes_one, (vt, who),
34 struct vtimes *vt AND enum __rusage_who who)
36 if (vt != NULL)
38 struct rusage usage;
40 if (getrusage(who, &usage) < 0)
41 return -1;
43 vt->vm_utime = TIMEVAL_TO_VTIMES(usage.ru_utime);
44 vt->vm_stime = TIMEVAL_TO_VTIMES(usage.ru_stime);
45 vt->vm_idsrss = usage.ru_idrss + usage.ru_isrss;
46 vt->vm_majflt = usage.ru_majflt;
47 vt->vm_minflt = usage.ru_minflt;
48 vt->vm_nswap = usage.ru_nswap;
49 vt->vm_inblk = usage.ru_inblock;
50 vt->vm_oublk = usage.ru_oublock;
52 return 0;
55 /* If CURRENT is not NULL, write statistics for the current process into
56 *CURRENT. If CHILD is not NULL, write statistics for all terminated child
57 processes into *CHILD. Returns 0 for success, -1 for failure. */
58 int
59 DEFUN(vtimes, (current, child),
60 struct vtimes *current AND struct vtimes *child)
62 if (vtimes_one(current, RUSAGE_SELF) < 0 ||
63 vtimes_one(child, RUSAGE_CHILDREN) < 0)
64 return -1;
65 return 0;