Skip to content

Commit 4705705

Browse files
committed
Fix bugs in times() function
1 parent c8e10ee commit 4705705

File tree

2 files changed

+20
-38
lines changed

2 files changed

+20
-38
lines changed

libc/proc/getrusage-nt.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,8 @@ textwindows int sys_getrusage_nt(int who, struct rusage *usage) {
5858
return einval();
5959
}
6060

61-
if (!usage) {
61+
if (!usage)
6262
return 0;
63-
}
6463

6564
if (!(who == RUSAGE_THREAD ? GetThreadTimes : GetProcessTimes)(
6665
me, &ftCreation, &ftExit, &ftKernel, &ftUser) ||

libc/proc/times.c

Lines changed: 19 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -16,55 +16,38 @@
1616
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
1717
│ PERFORMANCE OF THIS SOFTWARE. │
1818
╚─────────────────────────────────────────────────────────────────────────────*/
19-
#include "libc/calls/calls.h"
2019
#include "libc/calls/struct/rusage.h"
20+
#include "libc/calls/struct/timespec.h"
2121
#include "libc/calls/struct/timeval.h"
2222
#include "libc/calls/struct/tms.h"
23-
#include "libc/calls/syscall_support-nt.internal.h"
24-
#include "libc/dce.h"
25-
#include "libc/fmt/wintime.internal.h"
26-
#include "libc/nt/accounting.h"
27-
#include "libc/nt/runtime.h"
2823
#include "libc/runtime/clktck.h"
29-
#include "libc/runtime/sysconf.h"
24+
#include "libc/sysv/consts/clock.h"
3025
#include "libc/sysv/consts/rusage.h"
31-
#include "libc/time.h"
3226

33-
static dontinline long ConvertMicros(struct timeval tv) {
27+
static long MicrosToTicks(struct timeval tv) {
3428
return tv.tv_sec * CLK_TCK + tv.tv_usec / (1000000 / CLK_TCK);
3529
}
3630

37-
static dontinline long times2(struct tms *out_times, struct rusage *ru) {
38-
struct timeval tv;
39-
struct NtFileTime CreationTime, ExitTime, KernelTime, UserTime;
40-
if (!IsWindows()) {
41-
if (getrusage(RUSAGE_SELF, ru) == -1)
42-
return -1;
43-
out_times->tms_utime = ConvertMicros(ru->ru_utime);
44-
out_times->tms_stime = ConvertMicros(ru->ru_stime);
45-
if (getrusage(RUSAGE_CHILDREN, ru) == -1)
46-
return -1;
47-
out_times->tms_cutime = ConvertMicros(ru->ru_utime);
48-
out_times->tms_cstime = ConvertMicros(ru->ru_stime);
49-
} else {
50-
if (!GetProcessTimes(GetCurrentProcess(), &CreationTime, &ExitTime,
51-
&KernelTime, &UserTime)) {
52-
return __winerr();
53-
}
54-
out_times->tms_utime = ReadFileTime(UserTime);
55-
out_times->tms_stime = ReadFileTime(KernelTime);
56-
out_times->tms_cutime = 0;
57-
out_times->tms_cstime = 0;
58-
}
59-
if (gettimeofday(&tv, NULL) == -1)
60-
return -1;
61-
return ConvertMicros(tv);
31+
static long NanosToTicks(struct timespec ts) {
32+
return ts.tv_sec * CLK_TCK + ts.tv_nsec / (1000000000 / CLK_TCK);
6233
}
6334

6435
/**
6536
* Returns accounting data for process on time-sharing system.
37+
* @return number of `CLK_TCK` from `CLOCK_BOOTTIME` epoch
6638
*/
6739
long times(struct tms *out_times) {
68-
struct rusage ru;
69-
return times2(out_times, &ru);
40+
struct timespec bt;
41+
struct rusage rus, ruc;
42+
if (getrusage(RUSAGE_SELF, &rus))
43+
return -1;
44+
if (getrusage(RUSAGE_CHILDREN, &ruc))
45+
return -1;
46+
if (clock_gettime(CLOCK_BOOTTIME, &bt))
47+
return -1;
48+
out_times->tms_utime = MicrosToTicks(rus.ru_utime);
49+
out_times->tms_stime = MicrosToTicks(rus.ru_stime);
50+
out_times->tms_cutime = MicrosToTicks(ruc.ru_utime);
51+
out_times->tms_cstime = MicrosToTicks(ruc.ru_stime);
52+
return NanosToTicks(bt);
7053
}

0 commit comments

Comments
 (0)