exp2l: Work around a NetBSD 10.0/i386 bug.
[gnulib.git] / lib / getrusage.c
bloba332f7a5bd7ebca84aad40cbc3e03a5726c6e0fb
1 /* getrusage replacement for systems which lack it.
3 Copyright (C) 2012-2024 Free Software Foundation, Inc.
5 This file is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation, either version 3 of the
8 License, or (at your option) any later version.
10 This file 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18 /* Written by Bruno Haible, 2012. */
20 #include <config.h>
22 /* Specification. */
23 #include <sys/resource.h>
25 #include <errno.h>
26 #include <string.h>
28 /* Get uint64_t. */
29 #include <stdint.h>
31 #if defined _WIN32 && ! defined __CYGWIN__
33 # define WIN32_LEAN_AND_MEAN
34 # include <windows.h>
36 #else
38 # include <sys/times.h>
39 # include <unistd.h>
40 #endif
42 int
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;
55 FILETIME exit_time;
56 FILETIME kernel_time;
57 FILETIME user_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)
67 + 5) / 10;
68 uint64_t user_usec =
69 ((((uint64_t) user_time.dwHighDateTime << 32)
70 | (uint64_t) user_time.dwLowDateTime)
71 + 5) / 10;
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;
79 #else
80 /* Fill in the ru_utime and ru_stime members. */
82 struct tms time;
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)
91 clock_t user_ticks;
92 clock_t system_ticks;
94 uint64_t user_usec;
95 uint64_t system_usec;
97 if (who == RUSAGE_CHILDREN)
99 user_ticks = time.tms_cutime;
100 system_ticks = time.tms_cstime;
102 else
104 user_ticks = time.tms_utime;
105 system_ticks = time.tms_stime;
108 user_usec =
109 (((uint64_t) user_ticks * (uint64_t) 1000000U)
110 + clocks_per_second / 2) / clocks_per_second;
111 system_usec =
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;
122 #endif
124 return 0;
126 else
128 errno = EINVAL;
129 return -1;