| 1 | #define _WASI_EMULATED_PROCESS_CLOCKS |
| 2 | #include <sys/resource.h> |
| 3 | #include <errno.h> |
| 4 | #include <time.h> |
| 5 | #include <wasi/api.h> |
| 6 | #include <common/time.h> |
| 7 | |
| 8 | // `clock` is a weak symbol so that application code can override it. |
| 9 | // We want to use the function in libc, so use the libc-internal name. |
| 10 | clock_t __clock(void); |
| 11 | |
| 12 | int getrusage(int who, struct rusage *r_usage) { |
| 13 | switch (who) { |
| 14 | case RUSAGE_SELF: { |
| 15 | __wasi_timestamp_t usertime = __clock(); |
| 16 | *r_usage = (struct rusage) { |
| 17 | .ru_utime = timestamp_to_timeval(usertime) |
| 18 | 	}; |
| 19 | return 0; |
| 20 | } |
| 21 | case RUSAGE_CHILDREN: |
| 22 | *r_usage = (struct rusage) {}; |
| 23 | return 0; |
| 24 | default: |
| 25 | errno = EINVAL; |
| 26 | return -1; |
| 27 | } |
| 28 | } |