Skip to content

Commit beda622

Browse files
author
Guy Bedford
committed
feat: support vCpuTime function
1 parent 199b238 commit beda622

File tree

10 files changed

+109
-7
lines changed

10 files changed

+109
-7
lines changed

.vscode/c_cpp_properties.json

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33
{
44
"name": "sm-wasi",
55
"includePath": [
6-
"${workspaceFolder}/runtime/spidermonkey/debug/include",
7-
"${workspaceFolder}/runtime/spidermonkey/release/include",
6+
"${workspaceFolder}/runtime/StarlingMonkey/include",
7+
"${workspaceFolder}/runtime/StarlingMonkey/builtins/web",
8+
"${workspaceFolder}/runtime/fastly/build-debug/",
89
"/opt/wasi-sdk/share/wasi-sysroot/include/",
9-
"${workspaceFolder}/runtime/js-compute-runtime",
10-
"${workspaceFolder}/runtime/js-compute-runtime/build/openssl-3.0.7/include",
11-
"${workspaceFolder}/runtime/js-compute-runtime/third_party/fmt/include"
10+
"${workspaceFolder}/runtime/fastly/host-api"
1211
],
1312
"defines": [
1413
"__wasi__"
@@ -22,8 +21,7 @@
2221
},
2322
"forcedInclude": [
2423
"${workspaceFolder}/.vscode/vscode-preinclude.h"
25-
],
26-
"compileCommands": "${workspaceFolder}/runtime/js-compute-runtime/compile_commands.json"
24+
]
2725
}
2826
],
2927
"version": 4

integration-tests/js-compute/fixtures/app/src/assertions-throwing.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,16 @@ export function assert(actual, expected, code) {
5454
}
5555
}
5656

57+
export { assert as strictEqual }
58+
59+
export function ok(truthy, code) {
60+
if (!truthy) {
61+
fail(
62+
`Expected ${code ? ' ' + code : ''}to be truthy - Found \`${JSON.stringify(prettyPrintSymbol(truthy))}\``,
63+
);
64+
}
65+
}
66+
5767
export async function assertResolves(func) {
5868
try {
5969
await func();
@@ -124,6 +134,8 @@ export function assertDoesNotThrow(func) {
124134
}
125135
}
126136

137+
export { deepEqual as deepStrictEqual }
138+
127139
export function deepEqual(a, b) {
128140
var aKeys;
129141
var bKeys;

integration-tests/js-compute/fixtures/app/src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import "./request-method.js";
4444
import "./response-json.js";
4545
import "./response-redirect.js";
4646
import "./response.js";
47+
import "./runtime.js";
4748
import "./secret-store.js";
4849
import "./server.js";
4950
import "./tee.js";
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import {
2+
pass,
3+
ok,
4+
strictEqual
5+
} from "./assertions-throwing.js";
6+
import { routes } from "./routes.js";
7+
import { vCpuTime } from "fastly:runtime";
8+
9+
routes.set("/runtime/get-vcpu-ms", () => {
10+
const cpuTime = vCpuTime();
11+
console.log(cpuTime);
12+
strictEqual(typeof cpuTime, 'number');
13+
console.log('3');
14+
ok(cpuTime > 0);
15+
ok(cpuTime < 1000);
16+
console.log('4');
17+
const arr = [];
18+
for (let i = 0; i < 1000; i++) {
19+
arr.push(i);
20+
}
21+
const cpuTime2 = vCpuTime();
22+
ok(cpuTime2 > cpuTime);
23+
ok(cpuTime2 - cpuTime > 1);
24+
ok(cpuTime2 - cpuTime < 1000);
25+
return pass();
26+
});

integration-tests/js-compute/fixtures/app/tests.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9066,5 +9066,16 @@
90669066
"status": 200,
90679067
"body": "ok"
90689068
}
9069+
},
9070+
"GET /runtime/get-vcpu-ms": {
9071+
"environments": ["compute", "viceroy"],
9072+
"downstream_request": {
9073+
"method": "GET",
9074+
"pathname": "/runtime/get-vcpu-ms"
9075+
},
9076+
"downstream_response": {
9077+
"body": "ok",
9078+
"status": 200
9079+
}
90699080
}
90709081
}

runtime/fastly/builtins/fastly.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,18 @@ bool Fastly::env_get(JSContext *cx, unsigned argc, JS::Value *vp) {
220220
return true;
221221
}
222222

223+
bool runtime_get_vcpu_time(JSContext *cx, unsigned argc, JS::Value *vp) {
224+
JS::CallArgs args = CallArgsFromVp(argc, vp);
225+
auto res = host_api::Runtime::get_vcpu_ms();
226+
if (auto *err = res.to_err()) {
227+
HANDLE_ERROR(cx, *err);
228+
return false;
229+
}
230+
fprintf(stderr, "GOT: %llu", res.unwrap());
231+
args.rval().setNumber(res.unwrap());
232+
return true;
233+
}
234+
223235
bool Env::env_get(JSContext *cx, unsigned argc, JS::Value *vp) {
224236
JS::CallArgs args = CallArgsFromVp(argc, vp);
225237
if (!args.requireAtLeast(cx, "fastly.env.get", 1))
@@ -412,6 +424,23 @@ bool install(api::Engine *engine) {
412424
return false;
413425
}
414426

427+
// fastly:runtime
428+
auto runtime_vcpu_time_get =
429+
JS_NewFunction(engine->cx(), &runtime_get_vcpu_time, 0, 0, "vCpuTime");
430+
RootedObject runtime_vcpu_time_get_obj(engine->cx(), JS_GetFunctionObject(runtime_vcpu_time_get));
431+
RootedValue runtime_vcpu_time_get_val(engine->cx(), ObjectValue(*runtime_vcpu_time_get_obj));
432+
RootedObject runtime_builtin(engine->cx(), JS_NewObject(engine->cx(), nullptr));
433+
if (!JS_SetProperty(engine->cx(), runtime_builtin, "vCpuTime", runtime_vcpu_time_get_val)) {
434+
return false;
435+
}
436+
if (!JS_SetProperty(engine->cx(), fastly, "vCpuTime", runtime_vcpu_time_get_val)) {
437+
return false;
438+
}
439+
RootedValue runtime_builtin_val(engine->cx(), JS::ObjectValue(*runtime_builtin));
440+
if (!engine->define_builtin_module("fastly:runtime", runtime_builtin_val)) {
441+
return false;
442+
}
443+
415444
// fastly:experimental
416445
RootedObject experimental(engine->cx(), JS_NewObject(engine->cx(), nullptr));
417446
RootedValue experimental_val(engine->cx(), JS::ObjectValue(*experimental));

runtime/fastly/host-api/fastly.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,9 @@ WASM_IMPORT("fastly_device_detection", "lookup")
749749
int device_detection_lookup(const char *user_agent, size_t user_agent_len, const char *buf,
750750
size_t buf_len, size_t *nwritten);
751751

752+
WASM_IMPORT("fastly_compute_runtime", "get_vcpu_ms")
753+
int runtime_get_vcpu_ms(uint64_t *vcpu_ms);
754+
752755
} // namespace fastly
753756
#ifdef __cplusplus
754757
}

runtime/fastly/host-api/host_api.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2773,4 +2773,16 @@ Result<HostString> DeviceDetection::lookup(std::string_view user_agent) {
27732773
return res;
27742774
}
27752775

2776+
Result<uint64_t> Runtime::get_vcpu_ms() {
2777+
Result<uint64_t> res;
2778+
uint64_t ret;
2779+
fastly::fastly_host_error err;
2780+
if (!convert_result(fastly::runtime_get_vcpu_ms(&ret), &err)) {
2781+
res.emplace_err(err);
2782+
} else {
2783+
res.emplace(ret);
2784+
}
2785+
return res;
2786+
}
2787+
27762788
} // namespace host_api

runtime/fastly/host-api/host_api_fastly.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,11 @@ class DeviceDetection final {
806806
static Result<HostString> lookup(std::string_view user_agent);
807807
};
808808

809+
class Runtime final {
810+
public:
811+
static Result<uint64_t> get_vcpu_ms();
812+
};
813+
809814
} // namespace host_api
810815

811816
#endif

src/bundle.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ export const TransactionCacheEntry = globalThis.TransactionCacheEntry;
9494
`,
9595
};
9696
}
97+
case 'runtime': {
98+
return {
99+
contents: `export const vCpuTime = globalThis.fastly.vCpuTime;`,
100+
};
101+
}
97102
}
98103
});
99104
},

0 commit comments

Comments
 (0)