From f72de476b72d05934b0f375a1f94e580ed7b803b Mon Sep 17 00:00:00 2001 From: Cameron Taggart Date: Tue, 21 Apr 2020 13:07:05 -0600 Subject: [PATCH] use cfg_if! and use FileSerializationSink for wasi --- src/librustc_data_structures/profiling.rs | 92 ++++++++++++++--------- 1 file changed, 55 insertions(+), 37 deletions(-) diff --git a/src/librustc_data_structures/profiling.rs b/src/librustc_data_structures/profiling.rs index 32c916f2ce3..b4bbfe080e2 100644 --- a/src/librustc_data_structures/profiling.rs +++ b/src/librustc_data_structures/profiling.rs @@ -97,14 +97,27 @@ use std::time::{Duration, Instant}; use measureme::{EventId, EventIdBuilder, SerializableString, StringId}; use parking_lot::RwLock; -/// MmapSerializatioSink is faster on macOS and Linux -/// but FileSerializationSink is faster on Windows -#[cfg(all(not(windows), not(target_arch = "wasm32")))] -type SerializationSink = measureme::MmapSerializationSink; -#[cfg(all(windows, not(target_arch = "wasm32")))] -type SerializationSink = measureme::FileSerializationSink; -#[cfg(target_arch = "wasm32")] -type SerializationSink = measureme::ByteVecSink; +cfg_if! { + if #[cfg(target_arch = "wasm32")] { + cfg_if! { + if #[cfg(target_os = "wasi")] { + type SerializationSink = measureme::FileSerializationSink; + } else { + type SerializationSink = measureme::ByteVecSink; + } + } + } else { + cfg_if! { + if #[cfg(windows)] { + /// FileSerializationSink is faster on Windows + type SerializationSink = measureme::FileSerializationSink; + } else { + /// MmapSerializatioSink is faster on macOS and Linux + type SerializationSink = measureme::MmapSerializationSink; + } + } + } +} type Profiler = measureme::Profiler; @@ -604,36 +617,41 @@ pub fn duration_to_secs_str(dur: std::time::Duration) -> String { } // Memory reporting -#[cfg(all(unix, not(target_arch = "wasm32")))] -fn get_resident() -> Option { - let field = 1; - let contents = fs::read("/proc/self/statm").ok()?; - let contents = String::from_utf8(contents).ok()?; - let s = contents.split_whitespace().nth(field)?; - let npages = s.parse::().ok()?; - Some(npages * 4096) -} +cfg_if! { + if #[cfg(target_arch = "wasm32")] { + fn get_resident() -> Option { + None + } + } else { + cfg_if! { + if #[cfg(windows)] { + fn get_resident() -> Option { + use std::mem::{self, MaybeUninit}; + use winapi::shared::minwindef::DWORD; + use winapi::um::processthreadsapi::GetCurrentProcess; + use winapi::um::psapi::{GetProcessMemoryInfo, PROCESS_MEMORY_COUNTERS}; -#[cfg(all(windows, not(target_arch = "wasm32")))] -fn get_resident() -> Option { - use std::mem::{self, MaybeUninit}; - use winapi::shared::minwindef::DWORD; - use winapi::um::processthreadsapi::GetCurrentProcess; - use winapi::um::psapi::{GetProcessMemoryInfo, PROCESS_MEMORY_COUNTERS}; - - let mut pmc = MaybeUninit::::uninit(); - match unsafe { - GetProcessMemoryInfo(GetCurrentProcess(), pmc.as_mut_ptr(), mem::size_of_val(&pmc) as DWORD) - } { - 0 => None, - _ => { - let pmc = unsafe { pmc.assume_init() }; - Some(pmc.WorkingSetSize as usize) + let mut pmc = MaybeUninit::::uninit(); + match unsafe { + GetProcessMemoryInfo(GetCurrentProcess(), pmc.as_mut_ptr(), mem::size_of_val(&pmc) as DWORD) + } { + 0 => None, + _ => { + let pmc = unsafe { pmc.assume_init() }; + Some(pmc.WorkingSetSize as usize) + } + } + } + } else { + fn get_resident() -> Option { + let field = 1; + let contents = fs::read("/proc/self/statm").ok()?; + let contents = String::from_utf8(contents).ok()?; + let s = contents.split_whitespace().nth(field)?; + let npages = s.parse::().ok()?; + Some(npages * 4096) + } + } } } } - -#[cfg(target_arch = "wasm32")] -fn get_resident() -> Option { - None -}