From df3776bc0f7f250a7650fcaea67ac4a869cbcb80 Mon Sep 17 00:00:00 2001 From: Cameron Taggart Date: Mon, 20 Apr 2020 18:09:11 -0600 Subject: [PATCH 1/6] allow wasm32 compilation of librustc_data_structures/profiling.rs --- src/librustc_data_structures/profiling.rs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/librustc_data_structures/profiling.rs b/src/librustc_data_structures/profiling.rs index 23f3558cbdfa3..7377b7a6c8bb8 100644 --- a/src/librustc_data_structures/profiling.rs +++ b/src/librustc_data_structures/profiling.rs @@ -99,10 +99,12 @@ use parking_lot::RwLock; /// MmapSerializatioSink is faster on macOS and Linux /// but FileSerializationSink is faster on Windows -#[cfg(not(windows))] +#[cfg(all(not(windows),not(target_arch="wasm32")))] type SerializationSink = measureme::MmapSerializationSink; -#[cfg(windows)] +#[cfg(all(windows,not(target_arch="wasm32")))] type SerializationSink = measureme::FileSerializationSink; +#[cfg(target_arch="wasm32")] +type SerializationSink = measureme::ByteVecSink; type Profiler = measureme::Profiler; @@ -602,7 +604,7 @@ pub fn duration_to_secs_str(dur: std::time::Duration) -> String { } // Memory reporting -#[cfg(unix)] +#[cfg(all(unix,not(target_arch="wasm32")))] fn get_resident() -> Option { let field = 1; let contents = fs::read("/proc/self/statm").ok()?; @@ -612,7 +614,7 @@ fn get_resident() -> Option { Some(npages * 4096) } -#[cfg(windows)] +#[cfg(all(windows,not(target_arch="wasm32")))] fn get_resident() -> Option { use std::mem::{self, MaybeUninit}; use winapi::shared::minwindef::DWORD; @@ -630,3 +632,8 @@ fn get_resident() -> Option { } } } + +#[cfg(target_arch="wasm32")] +fn get_resident() -> Option { + None +} From 6fb524a455363dc747651eaa2c80773309083c52 Mon Sep 17 00:00:00 2001 From: Cameron Taggart Date: Mon, 20 Apr 2020 20:47:27 -0600 Subject: [PATCH 2/6] ./x.py fmt --- src/librustc_data_structures/profiling.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/librustc_data_structures/profiling.rs b/src/librustc_data_structures/profiling.rs index 7377b7a6c8bb8..32c916f2ce3cb 100644 --- a/src/librustc_data_structures/profiling.rs +++ b/src/librustc_data_structures/profiling.rs @@ -99,11 +99,11 @@ 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")))] +#[cfg(all(not(windows), not(target_arch = "wasm32")))] type SerializationSink = measureme::MmapSerializationSink; -#[cfg(all(windows,not(target_arch="wasm32")))] +#[cfg(all(windows, not(target_arch = "wasm32")))] type SerializationSink = measureme::FileSerializationSink; -#[cfg(target_arch="wasm32")] +#[cfg(target_arch = "wasm32")] type SerializationSink = measureme::ByteVecSink; type Profiler = measureme::Profiler; @@ -604,7 +604,7 @@ pub fn duration_to_secs_str(dur: std::time::Duration) -> String { } // Memory reporting -#[cfg(all(unix,not(target_arch="wasm32")))] +#[cfg(all(unix, not(target_arch = "wasm32")))] fn get_resident() -> Option { let field = 1; let contents = fs::read("/proc/self/statm").ok()?; @@ -614,7 +614,7 @@ fn get_resident() -> Option { Some(npages * 4096) } -#[cfg(all(windows,not(target_arch="wasm32")))] +#[cfg(all(windows, not(target_arch = "wasm32")))] fn get_resident() -> Option { use std::mem::{self, MaybeUninit}; use winapi::shared::minwindef::DWORD; @@ -633,7 +633,7 @@ fn get_resident() -> Option { } } -#[cfg(target_arch="wasm32")] +#[cfg(target_arch = "wasm32")] fn get_resident() -> Option { None } From f72de476b72d05934b0f375a1f94e580ed7b803b Mon Sep 17 00:00:00 2001 From: Cameron Taggart Date: Tue, 21 Apr 2020 13:07:05 -0600 Subject: [PATCH 3/6] use cfg_if! and use FileSerializationSink for wasi --- src/librustc_data_structures/profiling.rs | 94 ++++++++++++++--------- 1 file changed, 56 insertions(+), 38 deletions(-) diff --git a/src/librustc_data_structures/profiling.rs b/src/librustc_data_structures/profiling.rs index 32c916f2ce3cb..b4bbfe080e2ac 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(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) +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}; + + 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 -} From d5963ed0c42c4556904c630727e265972f93e9a5 Mon Sep 17 00:00:00 2001 From: Cameron Taggart Date: Tue, 21 Apr 2020 16:36:08 -0600 Subject: [PATCH 4/6] accept cfg_if suggestion Co-Authored-By: bjorn3 --- src/librustc_data_structures/profiling.rs | 24 +++++++---------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/src/librustc_data_structures/profiling.rs b/src/librustc_data_structures/profiling.rs index b4bbfe080e2ac..1a841cc8a0cfc 100644 --- a/src/librustc_data_structures/profiling.rs +++ b/src/librustc_data_structures/profiling.rs @@ -98,24 +98,14 @@ use measureme::{EventId, EventIdBuilder, SerializableString, StringId}; use parking_lot::RwLock; cfg_if! { - if #[cfg(target_arch = "wasm32")] { - cfg_if! { - if #[cfg(target_os = "wasi")] { - type SerializationSink = measureme::FileSerializationSink; - } else { - type SerializationSink = measureme::ByteVecSink; - } - } + if #[cfg(any(windows, target_os = "wasi"))] { + /// FileSerializationSink is faster on Windows + type SerializationSink = measureme::FileSerializationSink; + } else if #[cfg(target_arch = "wasm32")] { + 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; - } - } + /// MmapSerializatioSink is faster on macOS and Linux + type SerializationSink = measureme::MmapSerializationSink; } } From 02241db72006ba2426fd50663bfa48b3dd88b9ae Mon Sep 17 00:00:00 2001 From: Cameron Taggart Date: Wed, 22 Apr 2020 09:12:44 -0600 Subject: [PATCH 5/6] suggested rearrangement of the cfg if statements Co-Authored-By: ecstatic-morse --- src/librustc_data_structures/profiling.rs | 62 +++++++++++------------ 1 file changed, 30 insertions(+), 32 deletions(-) diff --git a/src/librustc_data_structures/profiling.rs b/src/librustc_data_structures/profiling.rs index 1a841cc8a0cfc..e2a1ba3a24b10 100644 --- a/src/librustc_data_structures/profiling.rs +++ b/src/librustc_data_structures/profiling.rs @@ -607,41 +607,39 @@ pub fn duration_to_secs_str(dur: std::time::Duration) -> String { } // Memory reporting -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}; - - 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) - } + 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}; + + 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) - } } + } else if #[cfg(unix)] { + 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) + } + } else { + fn get_resident() -> Option { + None + } + } } } } From 51b194f09ab907a388ff7e9799940380d05f7347 Mon Sep 17 00:00:00 2001 From: Cameron Taggart Date: Wed, 22 Apr 2020 09:18:54 -0600 Subject: [PATCH 6/6] remove some extra } --- src/librustc_data_structures/profiling.rs | 58 +++++++++++------------ 1 file changed, 28 insertions(+), 30 deletions(-) diff --git a/src/librustc_data_structures/profiling.rs b/src/librustc_data_structures/profiling.rs index e2a1ba3a24b10..07d16c6483ec7 100644 --- a/src/librustc_data_structures/profiling.rs +++ b/src/librustc_data_structures/profiling.rs @@ -607,39 +607,37 @@ pub fn duration_to_secs_str(dur: std::time::Duration) -> String { } // Memory reporting - 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}; - - 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) - } +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}; + + 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 if #[cfg(unix)] { - 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) - } - } else { - fn get_resident() -> Option { - None - } } + } else if #[cfg(unix)] { + 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) + } + } else { + fn get_resident() -> Option { + None } } }