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

Profile memory_usage is broken on Mac OS Monterey #12039

Closed
AlexMikhalev opened this issue Apr 20, 2022 · 11 comments
Closed

Profile memory_usage is broken on Mac OS Monterey #12039

AlexMikhalev opened this issue Apr 20, 2022 · 11 comments

Comments

@AlexMikhalev
Copy link

I noticed that profile memory_usage doesn't work on Mac, I extracted https:/AlexMikhalev/profile and added simple basic example to illustrate.

use profile::memory_usage;
fn main() {

    let before = memory_usage().allocated;
    println!("Before {:?}",before.to_string());
    let result = 2 + 2;
    println!("After {:?}",memory_usage().allocated.to_string());
    assert_eq!(result, 4);
}
cargo run --example basic
   Compiling profile v0.0.0 (/Users/alexmikhalev/rust_code/profile)
    Finished dev [unoptimized + debuginfo] target(s) in 2.57s
     Running `target/debug/examples/basic`
Before "0b"
After "0b"

Works on Linux but doesn't work on Mac, which means caches are not cleaned up from here

rust-analyzer version: rust-analyzer version: 7d6fcbc 2021-12-06 stable

rustc version: (eg. output of rustc -V)

rustc -vV
rustc 1.60.0 (7737e0b5c 2022-04-04)
binary: rustc
commit-hash: 7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c
commit-date: 2022-04-04
host: x86_64-apple-darwin
release: 1.60.0
LLVM version: 14.0.0
@lnicola
Copy link
Member

lnicola commented Apr 20, 2022

As you can see in the implementation, this feature uses some special memory allocator APIs which are only available on Linux and jemalloc. Building with --feature jemalloc should make it work.

@lnicola
Copy link
Member

lnicola commented Apr 20, 2022

Note that there are some, potentially obsolete, reasons why we don't build with jemalloc by default: rust-lang/rust#36963 (comment).

@AlexMikhalev
Copy link
Author

Thank you, building with --features jemalloc works. I would suggest re-visiting building with jmallock support for rust-analyzer, I found cache cleaning in apply_change.rs quite clean and if mac os users don't have cache cleaned it may lead to performance degradation for rust-analyzer users.

@lnicola
Copy link
Member

lnicola commented Apr 20, 2022

You're not supposed to use that command unless you're debugging the memory usage. What do you mean by that?

@AlexMikhalev
Copy link
Author

AlexMikhalev commented Apr 20, 2022

Actually, while it runs it produces the same number on Mac - it's a different number between before and after on Linux how it should.

cargo run --example basic --features jemalloc
Before "35kb"
After "35kb"

with --features jemalloc it always returns "35kb" instead of 0b (without --features jemalloc) and is never near the correct number, even for large datasets.

@lnicola
Copy link
Member

lnicola commented Apr 20, 2022

Adding two numbers doesn't really allocate anything. What exactly are you trying to do?

@AlexMikhalev
Copy link
Author

    let before = memory_usage().allocated;
    println!("Before {:?}",before.to_string());
    let v: Vec<_> = (1..100000000).collect();
    println!("After {:?}",memory_usage().allocated.to_string());

still produces

Before "35kb"
After "35kb"

same as above two numbers.

@bjorn3
Copy link
Member

bjorn3 commented Apr 20, 2022

It is possible that the memory allocation is optimized away by LLVM. Try adding unsafe { std::arch::asm!("", in(reg) &v); } to ensure LLVM can't remove the allocation.

@AlexMikhalev
Copy link
Author

unsafe { std::arch::asm!("", in(reg) &v); } | ^^^^^^^^^^ argument never used
I am measuring memory usage of a larger dataset ~500Kb on linux and which still output 35Kb on mac.

@lnicola
Copy link
Member

lnicola commented Apr 20, 2022

Did you set the global allocator as in https:/rust-lang/rust-analyzer/blob/master/crates/rust-analyzer/src/bin/main.rs#L20=?

@AlexMikhalev
Copy link
Author

Thank you @lnicola, adding global allocator and jemallocator into dependencies/default features helped.
If you guys would publish the profile crate separately it will be re-usable and very handy.
I close the issue - resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants