about summary refs log tree commit diff
diff options
context:
space:
mode:
authorDylan DPC <dylan.dpc@gmail.com>2020-04-22 23:19:22 +0200
committerGitHub <noreply@github.com>2020-04-22 23:19:22 +0200
commit16be619c6aec0db952be61ebb45c63a1ebe4b1a5 (patch)
tree6329630b2689e6bc3135ee7f2b5d2032dcddd19d
parentd3e24bd457df2c707f0ac8e1943ea3872b1a8b73 (diff)
parent51b194f09ab907a388ff7e9799940380d05f7347 (diff)
downloadrust-16be619c6aec0db952be61ebb45c63a1ebe4b1a5.tar.gz
rust-16be619c6aec0db952be61ebb45c63a1ebe4b1a5.zip
Rollup merge of #71369 - ctaggart:wasm32_profiling, r=ecstatic-morse
allow wasm32 compilation of librustc_data_structures/profiling.rs

I'm trying to use rustfmt from a wasm app. I ran into this compilation problem https://github.com/rust-lang/rustfmt/issues/4132 and after investigating, it looked like just adjusting a few cfg's. I based it on how measureme added support in https://github.com/rust-lang/measureme/pull/43.

My testing on my macbook was just that librustc_data_structures builds now with both:
- cargo build
- cargo build --target wasm32-unknown-unknown
-rw-r--r--src/librustc_data_structures/profiling.rs73
1 files changed, 42 insertions, 31 deletions
diff --git a/src/librustc_data_structures/profiling.rs b/src/librustc_data_structures/profiling.rs
index 23f3558cbdf..07d16c6483e 100644
--- a/src/librustc_data_structures/profiling.rs
+++ b/src/librustc_data_structures/profiling.rs
@@ -97,12 +97,17 @@ 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(not(windows))]
-type SerializationSink = measureme::MmapSerializationSink;
-#[cfg(windows)]
-type SerializationSink = measureme::FileSerializationSink;
+cfg_if! {
+    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 {
+        /// MmapSerializatioSink is faster on macOS and Linux
+        type SerializationSink = measureme::MmapSerializationSink;
+    }
+}
 
 type Profiler = measureme::Profiler<SerializationSink>;
 
@@ -602,31 +607,37 @@ pub fn duration_to_secs_str(dur: std::time::Duration) -> String {
 }
 
 // Memory reporting
-#[cfg(unix)]
-fn get_resident() -> Option<usize> {
-    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::<usize>().ok()?;
-    Some(npages * 4096)
-}
-
-#[cfg(windows)]
-fn get_resident() -> Option<usize> {
-    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::<PROCESS_MEMORY_COUNTERS>::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<usize> {
+            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::<PROCESS_MEMORY_COUNTERS>::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<usize> {
+            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::<usize>().ok()?;
+            Some(npages * 4096)
+        }
+    } else {
+        fn get_resident() -> Option<usize> {
+            None
         }
     }
 }