Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow wasm32 compilation of librustc_data_structures/profiling.rs #71369

Merged
merged 6 commits into from Apr 23, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
73 changes: 42 additions & 31 deletions src/librustc_data_structures/profiling.rs
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
}
}
}