|
16 | 16 | │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
17 | 17 | │ PERFORMANCE OF THIS SOFTWARE. │
|
18 | 18 | ╚─────────────────────────────────────────────────────────────────────────────*/
|
19 |
| -#include "libc/calls/calls.h" |
20 | 19 | #include "libc/calls/struct/rusage.h"
|
| 20 | +#include "libc/calls/struct/timespec.h" |
21 | 21 | #include "libc/calls/struct/timeval.h"
|
22 | 22 | #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" |
28 | 23 | #include "libc/runtime/clktck.h"
|
29 |
| -#include "libc/runtime/sysconf.h" |
| 24 | +#include "libc/sysv/consts/clock.h" |
30 | 25 | #include "libc/sysv/consts/rusage.h"
|
31 |
| -#include "libc/time.h" |
32 | 26 |
|
33 |
| -static dontinline long ConvertMicros(struct timeval tv) { |
| 27 | +static long MicrosToTicks(struct timeval tv) { |
34 | 28 | return tv.tv_sec * CLK_TCK + tv.tv_usec / (1000000 / CLK_TCK);
|
35 | 29 | }
|
36 | 30 |
|
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); |
62 | 33 | }
|
63 | 34 |
|
64 | 35 | /**
|
65 | 36 | * Returns accounting data for process on time-sharing system.
|
| 37 | + * @return number of `CLK_TCK` from `CLOCK_BOOTTIME` epoch |
66 | 38 | */
|
67 | 39 | 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); |
70 | 53 | }
|
0 commit comments