Skip to content

Commit 40fd9c2

Browse files
authored
Merge pull request RustPython#3406 from DimitrisJim/clean_time
Clean up monotonic/perf_counter invocations.
2 parents 976ed1d + cc37088 commit 40fd9c2

File tree

1 file changed

+22
-51
lines changed

1 file changed

+22
-51
lines changed

vm/src/stdlib/time.rs

Lines changed: 22 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,18 @@ mod time {
5454
.map_err(|e| vm.new_value_error(format!("Time error: {:?}", e)))
5555
}
5656

57+
// TODO: implement proper monotonic time for wasm/wasi.
58+
#[cfg(not(any(unix, windows)))]
59+
fn get_monotonic_time(vm: &VirtualMachine) -> PyResult<std::time::Duration> {
60+
duration_since_system_now(vm)
61+
}
62+
63+
// TODO: implement proper perf time for wasm/wasi.
64+
#[cfg(not(any(unix, windows)))]
65+
fn get_perf_time(vm: &VirtualMachine) -> PyResult<std::time::Duration> {
66+
duration_since_system_now(vm)
67+
}
68+
5769
#[cfg(not(unix))]
5870
#[pyfunction]
5971
fn sleep(dur: std::time::Duration) {
@@ -66,7 +78,6 @@ mod time {
6678
Ok(duration_since_system_now(vm)?.as_nanos() as u64)
6779
}
6880

69-
#[pyfunction(name = "perf_counter")] // TODO: fix
7081
#[pyfunction]
7182
pub fn time(vm: &VirtualMachine) -> PyResult<f64> {
7283
_time(vm)
@@ -90,32 +101,24 @@ mod time {
90101
Ok(Date::now() / 1000.0)
91102
}
92103

93-
#[cfg(target_os = "wasi")]
94104
#[pyfunction]
95105
fn monotonic(vm: &VirtualMachine) -> PyResult<f64> {
96-
// TODO: implement proper monotonic time for other platforms.
97-
Ok(duration_since_system_now(vm)?.as_secs_f64())
106+
Ok(get_monotonic_time(vm)?.as_secs_f64())
98107
}
99108

100-
#[cfg(target_os = "wasi")]
101109
#[pyfunction]
102110
fn monotonic_ns(vm: &VirtualMachine) -> PyResult<u128> {
103-
// TODO: implement proper monotonic time for other platforms.
104-
Ok(duration_since_system_now(vm)?.as_nanos())
111+
Ok(get_monotonic_time(vm)?.as_nanos())
105112
}
106113

107-
#[cfg(target_os = "wasi")]
108114
#[pyfunction]
109115
fn perf_counter(vm: &VirtualMachine) -> PyResult<f64> {
110-
// TODO: implement proper monotonic time for other platforms.
111-
Ok(duration_since_system_now(vm)?.as_secs_f64())
116+
Ok(get_perf_time(vm)?.as_secs_f64())
112117
}
113118

114-
#[cfg(target_os = "wasi")]
115119
#[pyfunction]
116120
fn perf_counter_ns(vm: &VirtualMachine) -> PyResult<u128> {
117-
// TODO: implement proper monotonic time for other platforms.
118-
Ok(duration_since_system_now(vm)?.as_nanos())
121+
Ok(get_perf_time(vm)?.as_nanos())
119122
}
120123

121124
fn pyobj_to_naive_date_time(
@@ -574,24 +577,12 @@ mod unix {
574577
Err(vm.new_not_implemented_error("get_clock_info unsupported on this system".to_owned()))
575578
}
576579

577-
#[pyfunction]
578-
fn monotonic(vm: &VirtualMachine) -> PyResult<f64> {
579-
clock_gettime(vm.ctx.new_int(CLOCK_MONOTONIC), vm)
580+
pub(super) fn get_monotonic_time(vm: &VirtualMachine) -> PyResult<Duration> {
581+
get_clock_time(vm.ctx.new_int(CLOCK_MONOTONIC), vm)
580582
}
581583

582-
#[pyfunction]
583-
fn monotonic_ns(vm: &VirtualMachine) -> PyResult<u128> {
584-
clock_gettime_ns(vm.ctx.new_int(CLOCK_MONOTONIC), vm)
585-
}
586-
587-
#[pyfunction]
588-
fn perf_counter(vm: &VirtualMachine) -> PyResult<f64> {
589-
clock_gettime(vm.ctx.new_int(CLOCK_MONOTONIC), vm)
590-
}
591-
592-
#[pyfunction]
593-
fn perf_counter_ns(vm: &VirtualMachine) -> PyResult<u128> {
594-
clock_gettime_ns(vm.ctx.new_int(CLOCK_MONOTONIC), vm)
584+
pub(super) fn get_perf_time(vm: &VirtualMachine) -> PyResult<Duration> {
585+
get_clock_time(vm.ctx.new_int(CLOCK_MONOTONIC), vm)
595586
}
596587

597588
#[pyfunction]
@@ -740,7 +731,7 @@ mod windows {
740731
.clone()
741732
}
742733

743-
fn win_perf_counter(vm: &VirtualMachine) -> PyResult<Duration> {
734+
pub(super) fn get_perf_time(vm: &VirtualMachine) -> PyResult<Duration> {
744735
let now = unsafe {
745736
let mut performance_count = std::mem::MaybeUninit::uninit();
746737
QueryPerformanceCounter(performance_count.as_mut_ptr());
@@ -773,7 +764,7 @@ mod windows {
773764
Ok(time_increment)
774765
}
775766

776-
fn get_monotonic_clock(vm: &VirtualMachine) -> PyResult<Duration> {
767+
pub(super) fn get_monotonic_time(vm: &VirtualMachine) -> PyResult<Duration> {
777768
let ticks = unsafe { GetTickCount64() };
778769

779770
Ok(Duration::from_nanos(
@@ -817,26 +808,6 @@ mod windows {
817808
}))
818809
}
819810

820-
#[pyfunction]
821-
fn monotonic(vm: &VirtualMachine) -> PyResult<f64> {
822-
Ok(get_monotonic_clock(vm)?.as_secs_f64())
823-
}
824-
825-
#[pyfunction]
826-
fn monotonic_ns(vm: &VirtualMachine) -> PyResult<u128> {
827-
Ok(get_monotonic_clock(vm)?.as_nanos())
828-
}
829-
830-
#[pyfunction]
831-
fn perf_counter(vm: &VirtualMachine) -> PyResult<f64> {
832-
Ok(win_perf_counter(vm)?.as_secs_f64())
833-
}
834-
835-
#[pyfunction]
836-
fn perf_counter_ns(vm: &VirtualMachine) -> PyResult<u128> {
837-
Ok(win_perf_counter(vm)?.as_nanos())
838-
}
839-
840811
pub(super) fn get_thread_time(vm: &VirtualMachine) -> PyResult<Duration> {
841812
let (kernel_time, user_time) = unsafe {
842813
let mut _creation_time = std::mem::MaybeUninit::uninit();

0 commit comments

Comments
 (0)