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

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

Open
Nsandomeno opened this issue Dec 7, 2022 · 8 comments · May be fixed by #106
Open

Comments

@Nsandomeno
Copy link

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
Copy link

pjstein commented Dec 22, 2022

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
Copy link
Author

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
Copy link

undavide commented Jan 5, 2023

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

@ckoopmann
Copy link

I went ahead and just applied the workaround as suggested by @pjstein
#106

@leshec
Copy link

leshec commented Apr 5, 2023

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

@livexia
Copy link

livexia commented May 25, 2023

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

@edzzn
Copy link

edzzn commented Nov 13, 2023

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
Copy link

sm13 commented Aug 31, 2024

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.

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

Successfully merging a pull request may close this issue.

8 participants