Skip to content

Why does cargo miri test run sequentially on a single thread? #2805

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

Closed
Voultapher opened this issue Mar 6, 2023 · 9 comments
Closed

Why does cargo miri test run sequentially on a single thread? #2805

Voultapher opened this issue Mar 6, 2023 · 9 comments

Comments

@Voultapher
Copy link

Unless I'm doing something wrong, why is the miri test runner not running the tests in parallel? I have a project that would greatly benefit from this.

@Voultapher Voultapher changed the title Why does cargo miri test run sequentially on a single thread? Why does cargo miri test run sequentially on a single thread? Mar 6, 2023
@bjorn3
Copy link
Member

bjorn3 commented Mar 6, 2023

When using cargo miri test the test harness is interpreted by miri itself. Miri can't run multiple interpreted threads at the same time. It has to switch between them inside the same OS thread. The fast majority of rustc can't be accessed from multiple threads and every MIR statement and terminator miri evaluates calls back into these non-thread safe parts of rustc several times. There is a project ongoing to allow rustc to work with multiple threads, but even then various data structures of miri itself likely don't like accesses from multiple threads at the same time.

@Voultapher
Copy link
Author

Ok, thanks for the info. What about forking?

@Voultapher
Copy link
Author

Because if I have to wait 2+ seconds per test, forking will certainly be worth it.

@bjorn3
Copy link
Member

bjorn3 commented Mar 6, 2023

The test harness supports running tests in subprocesses when using -Zpanic-abort-tests -Cpanic=abort. However miri itself doesn't support this. For running regular processes it may not be too hard (assuming posix_spawn is used instead of fork+exec), but spawning the test process again would require for miri to detect this somehow and intercept it to run the test crate in miri again. This would mean that the test crate is recompiled for every test though unless the test harness is modified to reuse the process for the next test. As for forking the miri process, that is not possible in a safe way. After a fork basically the only thing you can do is exec and preparing state for the exec unless you do it very close to the start of the process such that you know for sure no background threads have been spawned. In case of rustc and thus miri one of the first things that happens is spawning a new thread to run the rest of the compilation on.

@bjorn3
Copy link
Member

bjorn3 commented Mar 6, 2023

Maybe a wrapper around cargo miri test could be written which lists all tests and then spawns a couple of cargo miri test processes with each the test list filtered such that they all execute equal parts of the test suite?

@saethlin
Copy link
Member

saethlin commented Mar 6, 2023

If you have nextest installed, we specifically support cargo miri nextest run in order to support running tests in parallel. The nextest model is process-based, it executes cargo miri test once to list all the tests then launches many cargo miri test processes in parallel, each selecting a single test to run.

But I think currently, cargo miri nextest run defaults to one job, so you'd need to run cargo miri nextest run -j32 for example.

@Voultapher
Copy link
Author

Wow that's exactly what I was looking for 5min -> 25 seconds.

@oli-obk oli-obk closed this as completed Mar 6, 2023
@RalfJung
Copy link
Member

RalfJung commented Mar 7, 2023

Could be worth adding a section on nextest to our readme?

@saethlin
Copy link
Member

#2811

bors added a commit that referenced this issue Mar 12, 2023
Add a section on using nextest

Inspired by this question: #2805
saethlin pushed a commit to saethlin/rust that referenced this issue Mar 16, 2023
Add a section on using nextest

Inspired by this question: rust-lang/miri#2805
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

5 participants