Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add os.cputime() #159

Merged
merged 1 commit into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions quickjs-libc.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include <dlfcn.h>
#include <termios.h>
#include <sys/ioctl.h>
#include <sys/resource.h>
#include <sys/wait.h>

#if defined(__APPLE__)
Expand Down Expand Up @@ -1948,6 +1949,21 @@ static JSValue js_os_signal(JSContext *ctx, JSValueConst this_val,
return JS_UNDEFINED;
}

#ifndef _WIN32
static JSValue js_os_cputime(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
struct rusage ru;
int64_t cputime;

cputime = 0;
if (!getrusage(RUSAGE_SELF, &ru))
cputime = (int64_t)ru.ru_utime.tv_sec * 1000000 + ru.ru_utime.tv_usec;

return JS_NewInt64(ctx, cputime);
}
#endif

#if defined(__linux__) || defined(__APPLE__)
static int64_t get_time_us(void)
{
Expand Down Expand Up @@ -3617,6 +3633,7 @@ static const JSCFunctionListEntry js_os_funcs[] = {
OS_FLAG(SIGTSTP),
OS_FLAG(SIGTTIN),
OS_FLAG(SIGTTOU),
JS_CFUNC_DEF("cputime", 0, js_os_cputime ),
#endif
JS_CFUNC_DEF("now", 0, js_os_now ),
JS_CFUNC_DEF("setTimeout", 2, js_os_setTimeout ),
Expand Down
2 changes: 1 addition & 1 deletion tests/microbench.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ var clocks_per_sec = 1000000;
var max_iterations = 100;
var clock_threshold = 2000; /* favoring short measuring spans */
var min_n_argument = 1;
var get_clock = os.now;
var get_clock = os.cputime ?? os.now;

function log_one(text, n, ti) {
var ref;
Expand Down