rust-in-action / code

Source code for the book Rust in Action
https://www.manning.com/books/rust-in-action?a_aid=rust&a_bid=0367c58f&chan=github
1.9k stars 445 forks source link

ch6/ch6-particles does not compile on MacOS with `zsh: illegal hardware instruction cargo run` #93

Open Nsandomeno opened 1 year ago

Nsandomeno commented 1 year ago

OS: MacOS

When in the root ch6-particles crate created with cargo, the following error is generated:

zsh: illegal hardware instruction  cargo run

Oddly, the programs behaves as expected (well, without the standard output) when commenting out the eprintln! line.

From my findings so far this appears to an OS specific issue and any suggestions would be much appreciated!

pjstein commented 1 year ago

Hey @Nsandomeno, I ran into this as well and found that I could work around it by guarding the calls to eprintln!. Borrowed the run_guarded function from here and wrapped my calls to eprintln! with that. Worked like a charm.

Nsandomeno commented 1 year ago

Hey @Nsandomeno, I ran into this as well and found that I could work around it by guarding the calls to eprintln!. Borrowed the run_guarded function from here and wrapped my calls to eprintln! with that. Worked like a charm.

Thank you, @pjstein ! I did the same. Any Idea why we ran into this? Just a little catch when using MacOS perhaps?

undavide commented 1 year ago

Could you guys please post a snip? That'd help the newbie here :-) Thanks!

ckoopmann commented 1 year ago

I went ahead and just applied the workaround as suggested by @pjstein https://github.com/rust-in-action/code/pull/106

leshec commented 1 year ago

Thanks @ckoopmann @pjstein @Nsandomeno @undavide worked like a charm

livexia commented 1 year ago

Thanks for the solution, and this is what I found: Does the println macro allocate heap memory?.

edzzn commented 1 year ago

Just pasting the snippet here from: https://github.com/andrewhickman/logging-allocator/blob/master/src/lib.rs#L42-L57

We need to add a new function

pub fn run_guarded<F>(f: F)
where
    F: FnOnce(),
{
    thread_local! {
        static GUARD: Cell<bool> = Cell::new(false);
    }

    GUARD.with(|guard| {
        if !guard.replace(true) {
            f();
            guard.set(false)
        }
    })
}

and then use it to wrap the eprintln! on the alloc function

    unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
        let start = Instant::now();
        let ptr = System.alloc(layout);
        let end = Instant::now();
        let time_taken = end - start;
        let bytes_requested = layout.size();

        run_guarded(|| { 
            eprintln!("{}\t{}", bytes_requested, time_taken.as_nanos());
        });
        ptr
    }
sm13 commented 3 months ago

Hey @Nsandomeno, I ran into this as well and found that I could work around it by guarding the calls to eprintln!. Borrowed the run_guarded function from here and wrapped my calls to eprintln! with that. Worked like a charm.

Thank you, @pjstein ! I did the same. Any Idea why we ran into this? Just a little catch when using MacOS perhaps?

While I was unaware of this thread, I found this from the Rust Lang repo.