Skip to content

Commit

Permalink
Rollup merge of rust-lang#71369 - ctaggart:wasm32_profiling, r=ecstat…
Browse files Browse the repository at this point in the history
…ic-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
  • Loading branch information
Dylan-DPC authored Apr 22, 2020
2 parents d3e24bd + 51b194f commit 16be619
Showing 1 changed file with 42 additions and 31 deletions.
73 changes: 42 additions & 31 deletions src/librustc_data_structures/profiling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>;

Expand Down Expand Up @@ -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
}
}
}

0 comments on commit 16be619

Please sign in to comment.