Skip to content

Commit 31c75e0

Browse files
authored
wasi: support monotonic clock on clock_time_get. (#156)
Signed-off-by: Takeshi Yoneda <[email protected]>
1 parent 5791899 commit 31c75e0

File tree

7 files changed

+78
-4
lines changed

7 files changed

+78
-4
lines changed

include/proxy-wasm/context.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,19 @@ class ContextBase : public RootInterface,
245245
#else
246246
unimplemented();
247247
return 0;
248+
#endif
249+
}
250+
uint64_t getMonotonicTimeNanoseconds() override {
251+
#if !defined(_MSC_VER)
252+
struct timespec tpe;
253+
clock_gettime(CLOCK_MONOTONIC, &tpe);
254+
uint64_t t = tpe.tv_sec;
255+
t *= 1000000000;
256+
t += tpe.tv_nsec;
257+
return t;
258+
#else
259+
unimplemented();
260+
return 0;
248261
#endif
249262
}
250263
std::string_view getConfiguration() override {

include/proxy-wasm/context_interface.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,9 @@ struct GeneralInterface {
557557
// Provides the current time in nanoseconds since the Unix epoch.
558558
virtual uint64_t getCurrentTimeNanoseconds() = 0;
559559

560+
// Provides the monotonic time in nanoseconds.
561+
virtual uint64_t getMonotonicTimeNanoseconds() = 0;
562+
560563
// Returns plugin configuration.
561564
virtual std::string_view getConfiguration() = 0;
562565

src/exports.cc

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -833,12 +833,19 @@ Word wasi_unstable_args_sizes_get(void *raw_context, Word argc_ptr, Word argv_bu
833833
Word wasi_unstable_clock_time_get(void *raw_context, Word clock_id, uint64_t precision,
834834
Word result_time_uint64_ptr) {
835835

836-
if (clock_id != 0 /* realtime */) {
836+
uint64_t result = 0;
837+
auto context = WASM_CONTEXT(raw_context);
838+
switch (clock_id) {
839+
case 0 /* realtime */:
840+
result = context->getCurrentTimeNanoseconds();
841+
break;
842+
case 1 /* monotonic */:
843+
result = context->getMonotonicTimeNanoseconds();
844+
break;
845+
default:
846+
// process_cputime_id and thread_cputime_id are not supported yet.
837847
return 58; // __WASI_ENOTSUP
838848
}
839-
840-
auto context = WASM_CONTEXT(raw_context);
841-
uint64_t result = context->getCurrentTimeNanoseconds();
842849
if (!context->wasm()->setDatatype(result_time_uint64_ptr, result)) {
843850
return 21; // __WASI_EFAULT
844851
}

test/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ cc_test(
3737
srcs = ["exports_test.cc"],
3838
copts = COPTS,
3939
data = [
40+
"//test/test_data:clock.wasm",
4041
"//test/test_data:env.wasm",
4142
],
4243
linkopts = LINKOPTS,

test/exports_test.cc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,5 +91,28 @@ TEST_P(TestVM, WithoutEnvironment) {
9191
EXPECT_EQ(context.log_msg(), "");
9292
}
9393

94+
TEST_P(TestVM, Clock) {
95+
initialize("clock.wasm");
96+
auto wasm_base = WasmBase(std::move(vm_), "vm_id", "", "", {}, {});
97+
ASSERT_TRUE(wasm_base.wasm_vm()->load(source_, false));
98+
99+
TestContext context(&wasm_base);
100+
current_context_ = &context;
101+
102+
wasm_base.registerCallbacks();
103+
104+
ASSERT_TRUE(wasm_base.wasm_vm()->link(""));
105+
106+
WasmCallVoid<0> run;
107+
wasm_base.wasm_vm()->getFunction("run", &run);
108+
ASSERT_TRUE(run);
109+
run(current_context_);
110+
111+
// Check logs.
112+
auto msg = context.log_msg();
113+
EXPECT_NE(std::string::npos, msg.find("monotonic: ")) << msg;
114+
EXPECT_NE(std::string::npos, msg.find("realtime: ")) << msg;
115+
}
116+
94117
} // namespace
95118
} // namespace proxy_wasm

test/test_data/BUILD

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,9 @@ wasm_rust_binary(
2222
srcs = ["env.rs"],
2323
wasi = True,
2424
)
25+
26+
wasm_rust_binary(
27+
name = "clock.wasm",
28+
srcs = ["clock.rs"],
29+
wasi = True,
30+
)

test/test_data/clock.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2021 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
use std::time::{Instant, SystemTime};
16+
17+
#[no_mangle]
18+
pub extern "C" fn run() {
19+
println!("monotonic: {:?}", Instant::now());
20+
println!("realtime: {:?}", SystemTime::now());
21+
}

0 commit comments

Comments
 (0)