loom icon indicating copy to clipboard operation
loom copied to clipboard

Testing spinlock with Loom

Open cmnord opened this issue 3 years ago • 8 comments

I am trying to model a spinlock with Loom and running into a similar issue to #52 and #115. In particular, if I run my test loom with

RUSTFLAGS="--cfg loom" RUST_BACKTRACE=1 cargo test --test loom -- --test-threads=1 --nocapture

I get thread panicked while panicking. aborting. because Model exeeded maximum number of branches. This is often caused by an algorithm requiring the processor to make progress, e.g. spin locks..

#52 says:

the value of max_branches is to avoid an infinite loop when trying to model concurrent algorithms that are unbounded. For example, if you have a spin lock, if the thread that is spinning never yields, the model can go forever. If you have a legit case, you can increase the value of max_branches.

Does that mean that I can't test my spinlock with Loom, or do I need to add something to my spinlock implementation or my test to make it work with Loom? My spinlock implementation and Loom code are in this branch.

Thanks!

cmnord avatar Jul 28 '20 00:07 cmnord

Try increasing the value of max_branches by a bit?

hawkw avatar Jul 28 '20 00:07 hawkw

You need to yield in the loop before retrying: https://docs.rs/loom/0.3.5/loom/thread/fn.yield_now.html

This tells loom there is a loop going on and to try some other branches.

carllerche avatar Jul 28 '20 16:07 carllerche

So how did you test spinlocks in the end? I tried yield_now, but that's incredibly slow. (like tens of minutes)

tower120 avatar Sep 13 '21 12:09 tower120

Loom is slow. Tokio's CI setup takes more than an hour because that's how long time our slowest loom test takes. You could try to mess with LOOM_MAX_PREEMPTIONS to have it test less of the state space.

Darksonn avatar Sep 13 '21 14:09 Darksonn

I see, thanks. You should add note about hour-scale "slow" in the docs. I was confident that it hang up.

tower120 avatar Sep 13 '21 14:09 tower120

BTW, can it, theoretically, become multi-threaded?

tower120 avatar Sep 13 '21 14:09 tower120

Yes, but nobody has put in the work.

Generally, loom will print the iteration number as it runs, so you can use that to see whether it has hung up. (If running in test, make sure to enable prints in the test.)

Darksonn avatar Sep 13 '21 14:09 Darksonn

It does show! But for me, it just output everything at once, once test done. Maybe, that's just how clion+rust console work though...

Thanks, I'll try with "pure" console.

tower120 avatar Sep 13 '21 14:09 tower120