Skip to content

Commit 24f9c04

Browse files
authored
Merge pull request RustPython#3514 from coolreader18/fix-wasi-time
Fix unsatisfied times import on wasi
2 parents a0c00b2 + 8c9fb2c commit 24f9c04

File tree

3 files changed

+27
-17
lines changed

3 files changed

+27
-17
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vm/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ hexf-parse = "0.2.1"
4848
indexmap = "1.0.2"
4949
ahash = "0.7.2"
5050
bitflags = "1.3"
51-
libc = "0.2.99"
51+
libc = "0.2.107"
5252
nix = "0.23"
5353
paste = "1.0.5"
5454
is-macro = "0.1"

vm/src/stdlib/time.rs

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ mod time {
2828
naive::{NaiveDate, NaiveDateTime, NaiveTime},
2929
Datelike, Timelike,
3030
};
31+
use std::time::Duration;
3132

3233
#[allow(dead_code)]
3334
pub(super) const SEC_TO_MS: i64 = 1000;
@@ -46,7 +47,7 @@ mod time {
4647
#[allow(dead_code)]
4748
pub(super) const NS_TO_US: i64 = 1000;
4849

49-
fn duration_since_system_now(vm: &VirtualMachine) -> PyResult<std::time::Duration> {
50+
fn duration_since_system_now(vm: &VirtualMachine) -> PyResult<Duration> {
5051
use std::time::{SystemTime, UNIX_EPOCH};
5152

5253
SystemTime::now()
@@ -56,19 +57,19 @@ mod time {
5657

5758
// TODO: implement proper monotonic time for wasm/wasi.
5859
#[cfg(not(any(unix, windows)))]
59-
fn get_monotonic_time(vm: &VirtualMachine) -> PyResult<std::time::Duration> {
60+
fn get_monotonic_time(vm: &VirtualMachine) -> PyResult<Duration> {
6061
duration_since_system_now(vm)
6162
}
6263

6364
// TODO: implement proper perf time for wasm/wasi.
6465
#[cfg(not(any(unix, windows)))]
65-
fn get_perf_time(vm: &VirtualMachine) -> PyResult<std::time::Duration> {
66+
fn get_perf_time(vm: &VirtualMachine) -> PyResult<Duration> {
6667
duration_since_system_now(vm)
6768
}
6869

6970
#[cfg(not(unix))]
7071
#[pyfunction]
71-
fn sleep(dur: std::time::Duration) {
72+
fn sleep(dur: Duration) {
7273
std::thread::sleep(dur);
7374
}
7475

@@ -233,7 +234,7 @@ mod time {
233234
target_os = "fuchsia",
234235
target_os = "emscripten",
235236
)))]
236-
fn get_thread_time(vm: &VirtualMachine) -> PyResult<std::time::Duration> {
237+
fn get_thread_time(vm: &VirtualMachine) -> PyResult<Duration> {
237238
Err(vm.new_not_implemented_error("thread time unsupported in this system".to_owned()))
238239
}
239240

@@ -247,34 +248,43 @@ mod time {
247248
Ok(get_thread_time(vm)?.as_nanos() as u64)
248249
}
249250

250-
#[cfg(any(windows, all(target_arch = "wasm32", not(target_os = "unknown"))))]
251+
#[cfg(any(windows, all(target_arch = "wasm32", target_arch = "emscripten")))]
251252
pub(super) fn time_muldiv(ticks: i64, mul: i64, div: i64) -> u64 {
252253
let intpart = ticks / div;
253254
let ticks = ticks % div;
254255
let remaining = (ticks * mul) / div;
255256
(intpart * mul + remaining) as u64
256257
}
257258

258-
#[cfg(all(target_arch = "wasm32", not(target_os = "unknown")))]
259-
fn get_process_time(vm: &VirtualMachine) -> PyResult<std::time::Duration> {
259+
#[cfg(all(target_arch = "wasm32", target_os = "emscripten"))]
260+
fn get_process_time(vm: &VirtualMachine) -> PyResult<Duration> {
260261
let t: libc::tms = unsafe {
261262
let mut t = std::mem::MaybeUninit::uninit();
262263
if libc::times(t.as_mut_ptr()) == -1 {
263264
return Err(vm.new_os_error("Failed to get clock time".to_owned()));
264265
}
265266
t.assume_init()
266267
};
267-
268-
#[cfg(target_os = "wasi")]
269-
let freq = 60;
270-
#[cfg(not(target_os = "wasi"))]
271268
let freq = unsafe { libc::sysconf(libc::_SC_CLK_TCK) };
272269

273-
Ok(std::time::Duration::from_nanos(
270+
Ok(Duration::from_nanos(
274271
time_muldiv(t.tms_utime, SEC_TO_NS, freq) + time_muldiv(t.tms_stime, SEC_TO_NS, freq),
275272
))
276273
}
277274

275+
// same as the get_process_time impl for most unixes
276+
#[cfg(all(target_arch = "wasm32", target_os = "wasi"))]
277+
pub(super) fn get_process_time(vm: &VirtualMachine) -> PyResult<Duration> {
278+
let time: libc::timespec = unsafe {
279+
let mut time = std::mem::MaybeUninit::uninit();
280+
if libc::clock_gettime(libc::CLOCK_PROCESS_CPUTIME_ID, time.as_mut_ptr()) == -1 {
281+
return Err(vm.new_os_error("Failed to get clock time".to_owned()));
282+
}
283+
time.assume_init()
284+
};
285+
Ok(Duration::new(time.tv_sec as u64, time.tv_nsec as u32))
286+
}
287+
278288
#[cfg(not(any(
279289
windows,
280290
target_os = "macos",
@@ -289,7 +299,7 @@ mod time {
289299
target_os = "redox",
290300
all(target_arch = "wasm32", not(target_os = "unknown"))
291301
)))]
292-
fn get_process_time(vm: &VirtualMachine) -> PyResult<std::time::Duration> {
302+
fn get_process_time(vm: &VirtualMachine) -> PyResult<Duration> {
293303
Err(vm.new_not_implemented_error("process time unsupported in this system".to_owned()))
294304
}
295305

0 commit comments

Comments
 (0)