]>
Commit | Line | Data |
---|---|---|
04277e02 | 1 | /* Copyright (C) 1991-2019 Free Software Foundation, Inc. |
ebbad4cc | 2 | This file is part of the GNU C Library. |
28f540f4 | 3 | |
ebbad4cc | 4 | The GNU C Library is free software; you can redistribute it and/or |
41bdb6e2 AJ |
5 | modify it under the terms of the GNU Lesser General Public |
6 | License as published by the Free Software Foundation; either | |
7 | version 2.1 of the License, or (at your option) any later version. | |
28f540f4 | 8 | |
ebbad4cc UD |
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 | |
41bdb6e2 | 12 | Lesser General Public License for more details. |
28f540f4 | 13 | |
41bdb6e2 | 14 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 | 15 | License along with the GNU C Library; if not, see |
5a82c748 | 16 | <https://www.gnu.org/licenses/>. */ |
28f540f4 | 17 | |
28f540f4 RM |
18 | #include <stddef.h> |
19 | #include <sys/vtimes.h> | |
20 | #include <sys/resource.h> | |
21 | ||
22 | /* Return the number of 1/VTIMES_UNITS_PER_SECOND-second | |
23 | units in the `struct timeval' TV. */ | |
24 | #define TIMEVAL_TO_VTIMES(tv) \ | |
34a5a146 JM |
25 | ((tv.tv_sec * VTIMES_UNITS_PER_SECOND) \ |
26 | + (tv.tv_usec * VTIMES_UNITS_PER_SECOND / 1000000)) | |
28f540f4 RM |
27 | |
28 | /* If VT is not NULL, write statistics for WHO into *VT. | |
29 | Return 0 for success, -1 for failure. */ | |
30 | static int | |
76b87c03 | 31 | vtimes_one (struct vtimes *vt, enum __rusage_who who) |
28f540f4 RM |
32 | { |
33 | if (vt != NULL) | |
34 | { | |
35 | struct rusage usage; | |
36 | ||
50304ef0 | 37 | if (__getrusage (who, &usage) < 0) |
28f540f4 RM |
38 | return -1; |
39 | ||
8f2ece69 UD |
40 | vt->vm_utime = TIMEVAL_TO_VTIMES (usage.ru_utime); |
41 | vt->vm_stime = TIMEVAL_TO_VTIMES (usage.ru_stime); | |
28f540f4 RM |
42 | vt->vm_idsrss = usage.ru_idrss + usage.ru_isrss; |
43 | vt->vm_majflt = usage.ru_majflt; | |
44 | vt->vm_minflt = usage.ru_minflt; | |
45 | vt->vm_nswap = usage.ru_nswap; | |
46 | vt->vm_inblk = usage.ru_inblock; | |
47 | vt->vm_oublk = usage.ru_oublock; | |
48 | } | |
49 | return 0; | |
50 | } | |
51 | ||
52 | /* If CURRENT is not NULL, write statistics for the current process into | |
53 | *CURRENT. If CHILD is not NULL, write statistics for all terminated child | |
54 | processes into *CHILD. Returns 0 for success, -1 for failure. */ | |
55 | int | |
9d46370c | 56 | vtimes (struct vtimes *current, struct vtimes *child) |
28f540f4 | 57 | { |
8f2ece69 UD |
58 | if (vtimes_one (current, RUSAGE_SELF) < 0 |
59 | || vtimes_one (child, RUSAGE_CHILDREN) < 0) | |
28f540f4 RM |
60 | return -1; |
61 | return 0; | |
62 | } |