#5367: Program in (-N1) runs 10 times slower than it with two threads (-N2)
--------------------------------------+-------------------------------------
Reporter: wuxb | Owner: simonmar
Type: bug | Status: closed
Priority: high | Milestone: 7.4.1
Component: Runtime System | Version: 7.0.4
Resolution: wontfix | Keywords: thread ffi
Testcase: | Blockedby:
Difficulty: | Os: Linux
Blocking: | Architecture: x86_64 (amd64)
Failure: Runtime performance bug |
--------------------------------------+-------------------------------------
Changes (by simonmar):
* status: new => closed
* resolution: => wontfix
Comment:
I've made it slightly faster (patch coming shortly), but the basic problem
here is `safe` foreign calls. In the threaded RTS, a `safe` foreign call
wakes up a second OS thread just in case the call blocks, so that the
other Haskell threads can continue running. Often the second OS thread
will find nothing to do, and will just go to sleep again. In the program
here, there are two Haskell threads, so the when the second OS thread runs
there is a Haskell thread on the run queue, and with a high probability
the original OS thread is in the middle of a foreign call so the second OS
thread "steals" the RTS and starts running the other Haskell thread.
Result is a lot of context-switching of OS threads, which is expensive.
With -N2 we're back in the situation where the newly woken OS thread has
nothing to do again, so it doesn't steal the RTS, and we don't get a lot
of context switching.
The fix is easy: put `unsafe` on your foreign calls, unless you really
need them to be `safe`.
I don't know of a way to improve this without more OS support. If we had
scheduler activations (Windows 7 has these) we could do this properly and
avoid the unnecessary context switching.
--
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/5367#comment:3>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler
_______________________________________________
Glasgow-haskell-bugs mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-bugs