1 /* getrusage replacement for systems which lack it.
3 Copyright (C) 2012-2020 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18 /* Written by Bruno Haible, 2012. */
23 #include <sys/resource.h>
31 #if defined _WIN32 && ! defined __CYGWIN__
33 # define WIN32_LEAN_AND_MEAN
38 # include <sys/times.h>
43 getrusage (int who
, struct rusage
*usage_p
)
45 if (who
== RUSAGE_SELF
|| who
== RUSAGE_CHILDREN
)
47 /* Clear all unsupported members of 'struct rusage'. */
48 memset (usage_p
, '\0', sizeof (struct rusage
));
50 #if defined _WIN32 && ! defined __CYGWIN__
51 if (who
== RUSAGE_SELF
)
53 /* Fill in the ru_utime and ru_stime members. */
54 FILETIME creation_time
;
59 if (GetProcessTimes (GetCurrentProcess (),
60 &creation_time
, &exit_time
,
61 &kernel_time
, &user_time
))
63 /* Convert to microseconds, rounding. */
64 uint64_t kernel_usec
=
65 ((((uint64_t) kernel_time
.dwHighDateTime
<< 32)
66 | (uint64_t) kernel_time
.dwLowDateTime
)
69 ((((uint64_t) user_time
.dwHighDateTime
<< 32)
70 | (uint64_t) user_time
.dwLowDateTime
)
73 usage_p
->ru_utime
.tv_sec
= user_usec
/ 1000000U;
74 usage_p
->ru_utime
.tv_usec
= user_usec
% 1000000U;
75 usage_p
->ru_stime
.tv_sec
= kernel_usec
/ 1000000U;
76 usage_p
->ru_stime
.tv_usec
= kernel_usec
% 1000000U;
80 /* Fill in the ru_utime and ru_stime members. */
84 if (times (&time
) != (clock_t) -1)
86 /* Number of clock ticks per second. */
87 unsigned int clocks_per_second
= sysconf (_SC_CLK_TCK
);
89 if (clocks_per_second
> 0)
97 if (who
== RUSAGE_CHILDREN
)
99 user_ticks
= time
.tms_cutime
;
100 system_ticks
= time
.tms_cstime
;
104 user_ticks
= time
.tms_utime
;
105 system_ticks
= time
.tms_stime
;
109 (((uint64_t) user_ticks
* (uint64_t) 1000000U)
110 + clocks_per_second
/ 2) / clocks_per_second
;
112 (((uint64_t) system_ticks
* (uint64_t) 1000000U)
113 + clocks_per_second
/ 2) / clocks_per_second
;
115 usage_p
->ru_utime
.tv_sec
= user_usec
/ 1000000U;
116 usage_p
->ru_utime
.tv_usec
= user_usec
% 1000000U;
117 usage_p
->ru_stime
.tv_sec
= system_usec
/ 1000000U;
118 usage_p
->ru_stime
.tv_usec
= system_usec
% 1000000U;