-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Emulate
clock
, times
, and getrusage
using the monotonic clock.
Upcoming WASI snapshots omit the `PROCESS_CPUTIME` clock, since WASI has no inherent concept of processes, and since implementations which don't use a process for each instance don't have a way to implement it efficiently. However, `clock`, `times`, and `getrusage` are useful functions, so provide optional emulated version of them, using the `MONOTONIC` clock. This means these implementations will measure not just the program's own CPU time, but also time spent suspended while other programs are running. Due to this difference in behavior, put these implementations behind a flag. Users must pass `-D_WASI_EMULATED_PROCESS_CLOCK` and link with `-lwasi-emulated-process-clocks` to enable them.
- Loading branch information
1 parent
322bd4f
commit b9b64a6
Showing
15 changed files
with
117 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#define _WASI_EMULATED_PROCESS_CLOCKS | ||
#include <time.h> | ||
#include <wasi/api.h> | ||
#include <common/time.h> | ||
|
||
_Static_assert( | ||
CLOCKS_PER_SEC == NSEC_PER_SEC, | ||
"This implementation assumes that `clock` is in nanoseconds" | ||
); | ||
|
||
// Snapshot of the monotonic clock at the start of the program. | ||
static __wasi_timestamp_t start; | ||
|
||
// Use a priority of 10 to run fairly early in the implementation-reserved | ||
// constructor priority range. | ||
__attribute__((constructor(10))) | ||
static void init(void) { | ||
(void)__wasi_clock_time_get(__WASI_CLOCKID_MONOTONIC, 0, &start); | ||
} | ||
|
||
clock_t __clock(void) { | ||
// Use `MONOTONIC` instead of `PROCESS_CPUTIME_ID` since WASI doesn't have | ||
// an inherent concept of a process. Note that this means we'll incorrectly | ||
// include time from other processes, so this function is only declared by | ||
// the headers if `_WASI_EMULATED_PROCESS_CLOCKS` is defined. | ||
__wasi_timestamp_t now = 0; | ||
(void)__wasi_clock_time_get(__WASI_CLOCKID_MONOTONIC, 0, &now); | ||
return now - start; | ||
} | ||
|
||
__attribute__((__weak__, __alias__("__clock"))) | ||
clock_t clock(void); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#define _WASI_EMULATED_PROCESS_CLOCKS | ||
#include <sys/resource.h> | ||
#include <errno.h> | ||
#include <time.h> | ||
#include <wasi/api.h> | ||
#include <common/time.h> | ||
|
||
clock_t __clock(void); | ||
|
||
int getrusage(int who, struct rusage *r_usage) { | ||
switch (who) { | ||
case RUSAGE_SELF: { | ||
__wasi_timestamp_t usertime = __clock(); | ||
*r_usage = (struct rusage) { | ||
.ru_utime = timestamp_to_timeval(usertime) | ||
}; | ||
return 0; | ||
} | ||
case RUSAGE_CHILDREN: | ||
*r_usage = (struct rusage) {}; | ||
return 0; | ||
default: | ||
errno = EINVAL; | ||
return -1; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#define _WASI_EMULATED_PROCESS_CLOCKS | ||
#include <time.h> | ||
#include <sys/times.h> | ||
#include <wasi/api.h> | ||
#include <common/time.h> | ||
|
||
_Static_assert( | ||
CLOCKS_PER_SEC == NSEC_PER_SEC, | ||
"This implementation assumes that `clock` is in nanoseconds" | ||
); | ||
|
||
clock_t __clock(void); | ||
|
||
clock_t times(struct tms *buffer) { | ||
__wasi_timestamp_t user = __clock(); | ||
*buffer = (struct tms){ | ||
.tms_utime = user, | ||
.tms_cutime = user | ||
}; | ||
|
||
__wasi_timestamp_t realtime = 0; | ||
(void)__wasi_clock_time_get(__WASI_CLOCKID_MONOTONIC, 0, &realtime); | ||
return realtime; | ||
} |
30 changes: 0 additions & 30 deletions
30
libc-bottom-half/cloudlibc/src/libc/sys/resource/getrusage.c
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters