Skip to content

Commit 16be619

Browse files
authored
Rollup merge of rust-lang#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 rust-lang/rustfmt#4132 and after investigating, it looked like just adjusting a few cfg's. I based it on how measureme added support in rust-lang/measureme#43. My testing on my macbook was just that librustc_data_structures builds now with both: - cargo build - cargo build --target wasm32-unknown-unknown
2 parents d3e24bd + 51b194f commit 16be619

File tree

1 file changed

+42
-31
lines changed

1 file changed

+42
-31
lines changed

src/librustc_data_structures/profiling.rs

+42-31
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,17 @@ use std::time::{Duration, Instant};
9797
use measureme::{EventId, EventIdBuilder, SerializableString, StringId};
9898
use parking_lot::RwLock;
9999

100-
/// MmapSerializatioSink is faster on macOS and Linux
101-
/// but FileSerializationSink is faster on Windows
102-
#[cfg(not(windows))]
103-
type SerializationSink = measureme::MmapSerializationSink;
104-
#[cfg(windows)]
105-
type SerializationSink = measureme::FileSerializationSink;
100+
cfg_if! {
101+
if #[cfg(any(windows, target_os = "wasi"))] {
102+
/// FileSerializationSink is faster on Windows
103+
type SerializationSink = measureme::FileSerializationSink;
104+
} else if #[cfg(target_arch = "wasm32")] {
105+
type SerializationSink = measureme::ByteVecSink;
106+
} else {
107+
/// MmapSerializatioSink is faster on macOS and Linux
108+
type SerializationSink = measureme::MmapSerializationSink;
109+
}
110+
}
106111

107112
type Profiler = measureme::Profiler<SerializationSink>;
108113

@@ -602,31 +607,37 @@ pub fn duration_to_secs_str(dur: std::time::Duration) -> String {
602607
}
603608

604609
// Memory reporting
605-
#[cfg(unix)]
606-
fn get_resident() -> Option<usize> {
607-
let field = 1;
608-
let contents = fs::read("/proc/self/statm").ok()?;
609-
let contents = String::from_utf8(contents).ok()?;
610-
let s = contents.split_whitespace().nth(field)?;
611-
let npages = s.parse::<usize>().ok()?;
612-
Some(npages * 4096)
613-
}
614-
615-
#[cfg(windows)]
616-
fn get_resident() -> Option<usize> {
617-
use std::mem::{self, MaybeUninit};
618-
use winapi::shared::minwindef::DWORD;
619-
use winapi::um::processthreadsapi::GetCurrentProcess;
620-
use winapi::um::psapi::{GetProcessMemoryInfo, PROCESS_MEMORY_COUNTERS};
621-
622-
let mut pmc = MaybeUninit::<PROCESS_MEMORY_COUNTERS>::uninit();
623-
match unsafe {
624-
GetProcessMemoryInfo(GetCurrentProcess(), pmc.as_mut_ptr(), mem::size_of_val(&pmc) as DWORD)
625-
} {
626-
0 => None,
627-
_ => {
628-
let pmc = unsafe { pmc.assume_init() };
629-
Some(pmc.WorkingSetSize as usize)
610+
cfg_if! {
611+
if #[cfg(windows)] {
612+
fn get_resident() -> Option<usize> {
613+
use std::mem::{self, MaybeUninit};
614+
use winapi::shared::minwindef::DWORD;
615+
use winapi::um::processthreadsapi::GetCurrentProcess;
616+
use winapi::um::psapi::{GetProcessMemoryInfo, PROCESS_MEMORY_COUNTERS};
617+
618+
let mut pmc = MaybeUninit::<PROCESS_MEMORY_COUNTERS>::uninit();
619+
match unsafe {
620+
GetProcessMemoryInfo(GetCurrentProcess(), pmc.as_mut_ptr(), mem::size_of_val(&pmc) as DWORD)
621+
} {
622+
0 => None,
623+
_ => {
624+
let pmc = unsafe { pmc.assume_init() };
625+
Some(pmc.WorkingSetSize as usize)
626+
}
627+
}
628+
}
629+
} else if #[cfg(unix)] {
630+
fn get_resident() -> Option<usize> {
631+
let field = 1;
632+
let contents = fs::read("/proc/self/statm").ok()?;
633+
let contents = String::from_utf8(contents).ok()?;
634+
let s = contents.split_whitespace().nth(field)?;
635+
let npages = s.parse::<usize>().ok()?;
636+
Some(npages * 4096)
637+
}
638+
} else {
639+
fn get_resident() -> Option<usize> {
640+
None
630641
}
631642
}
632643
}

0 commit comments

Comments
 (0)