From 9b4d9798def1ca954b8958fcd22b6a4088af3180 Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Wed, 20 Nov 2019 11:44:12 -0800 Subject: [PATCH] Avoid mem::uninitialized in the demo cpu_time For both Unix `getrusage` and Windows `GetProcessTimes`, we can pass `MaybeUninit` pointers to those FFI calls and `assume_init()` afterward. --- rayon-demo/src/cpu_time/unix.rs | 25 ++++++++++++++----------- rayon-demo/src/cpu_time/win.rs | 33 +++++++++++++++++++++------------ 2 files changed, 35 insertions(+), 23 deletions(-) diff --git a/rayon-demo/src/cpu_time/unix.rs b/rayon-demo/src/cpu_time/unix.rs index d6fdaaf92..31a158ff6 100644 --- a/rayon-demo/src/cpu_time/unix.rs +++ b/rayon-demo/src/cpu_time/unix.rs @@ -1,14 +1,17 @@ -use libc::{getrusage, rusage, RUSAGE_SELF}; -use std::mem; +use libc::{getrusage, RUSAGE_SELF}; +use std::mem::MaybeUninit; pub fn get_cpu_time() -> Option { - unsafe { - let mut usage: rusage = mem::uninitialized(); - getrusage(RUSAGE_SELF, &mut usage); - let user = 1_000_000_000 * (usage.ru_utime.tv_sec as u64) - + 1_000 * (usage.ru_utime.tv_usec as u64); - let system = 1_000_000_000 * (usage.ru_stime.tv_sec as u64) - + 1_000 * (usage.ru_stime.tv_usec as u64); - Some(user + system) - } + let usage = unsafe { + let mut usage = MaybeUninit::uninit(); + if getrusage(RUSAGE_SELF, usage.as_mut_ptr()) != 0 { + return None; + } + usage.assume_init() + }; + let user = + 1_000_000_000 * (usage.ru_utime.tv_sec as u64) + 1_000 * (usage.ru_utime.tv_usec as u64); + let system = + 1_000_000_000 * (usage.ru_stime.tv_sec as u64) + 1_000 * (usage.ru_stime.tv_usec as u64); + Some(user + system) } diff --git a/rayon-demo/src/cpu_time/win.rs b/rayon-demo/src/cpu_time/win.rs index 0060d92d6..6b66f038b 100644 --- a/rayon-demo/src/cpu_time/win.rs +++ b/rayon-demo/src/cpu_time/win.rs @@ -1,17 +1,26 @@ -use std::mem; -use winapi::shared::minwindef::FILETIME; +use std::mem::MaybeUninit; use winapi::um::processthreadsapi::{GetCurrentProcess, GetProcessTimes}; pub fn get_cpu_time() -> Option { - unsafe { + let (kernel, user) = unsafe { let process = GetCurrentProcess(); - let mut _creation: FILETIME = mem::uninitialized(); - let mut _exit: FILETIME = mem::uninitialized(); - let mut kernel: FILETIME = mem::uninitialized(); - let mut user: FILETIME = mem::uninitialized(); - GetProcessTimes(process, &mut _creation, &mut _exit, &mut kernel, &mut user); - let kernel = (kernel.dwHighDateTime as u64) << 32 | kernel.dwLowDateTime as u64; - let user = (user.dwHighDateTime as u64) << 32 | user.dwLowDateTime as u64; - Some(100 * (kernel + user)) - } + let mut _creation = MaybeUninit::uninit(); + let mut _exit = MaybeUninit::uninit(); + let mut kernel = MaybeUninit::uninit(); + let mut user = MaybeUninit::uninit(); + if GetProcessTimes( + process, + _creation.as_mut_ptr(), + _exit.as_mut_ptr(), + kernel.as_mut_ptr(), + user.as_mut_ptr(), + ) == 0 + { + return None; + } + (kernel.assume_init(), user.assume_init()) + }; + let kernel = (kernel.dwHighDateTime as u64) << 32 | kernel.dwLowDateTime as u64; + let user = (user.dwHighDateTime as u64) << 32 | user.dwLowDateTime as u64; + Some(100 * (kernel + user)) }