[GHC] #12419: Scheduling bug with forkOS + MVar
#12419: Scheduling bug with forkOS + MVar -----------------------------------------+--------------------------------- Reporter: luisgabriel | Owner: Type: bug | Status: new Priority: normal | Milestone: Component: Runtime System | Version: 8.0.1 Keywords: forkOS; scheduler | Operating System: Linux Architecture: x86_64 (amd64) | Type of failure: None/Unknown Test Case: | Blocked By: Blocking: | Related Tickets: Differential Rev(s): | Wiki Page: -----------------------------------------+--------------------------------- I have noticed a weird scheduling behavior when performing some experiments with the '''fasta''' benchmark [1] from The Computer Language Benchmarks Game. When I switch `forkIO` by `forkOS` the scheduler stops to assign work for some capabilities, and they stay idle for the whole execution of the program. ThreadScope view using forkIO: https://s31.postimg.org/r3mclspe3/fork_IO_N8_ghc8.png ThreadScope view using forkOS: https://s31.postimg.org/p9n265fff/fork_OS_N8_ghc8.png I was able to reproduce this behavior in both '''GHC 7.10.2''' and '''GHC 8.0.2'''. I was also able to reproduce it on two different machines running Ubuntu Server 14.04.3 LTS (kernel 3.19.0-25): - 2x10-core Intel Xeon E5-2660 v2 processors (Ivy Bridge), 2.20 GHz, with 256GB of DDR 1600MHz - 4-core Intel i7-3770 (IvyBridge) with 8 GB of DDR 1600MHz Source code + .eventlog files: https://dl.dropboxusercontent.com/u/5798150 /fasta-bug.zip [1] http://benchmarksgame.alioth.debian.org/u64q/program.php?test=fasta&lang=ghc... -- Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/12419> GHC <http://www.haskell.org/ghc/> The Glasgow Haskell Compiler
#12419: Scheduling bug with forkOS + MVar -------------------------------------+------------------------------------- Reporter: luisgabriel | Owner: Type: bug | Status: new Priority: normal | Milestone: Component: Runtime System | Version: 8.0.1 Resolution: | Keywords: forkOS; | scheduler Operating System: Linux | Architecture: x86_64 | (amd64) Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Changes (by luisgabriel): * Attachment "forkIO-N8-ghc8.png" added. ThreadScope view using forkIO -- Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/12419> GHC <http://www.haskell.org/ghc/> The Glasgow Haskell Compiler
#12419: Scheduling bug with forkOS + MVar -------------------------------------+------------------------------------- Reporter: luisgabriel | Owner: Type: bug | Status: new Priority: normal | Milestone: Component: Runtime System | Version: 8.0.1 Resolution: | Keywords: forkOS; | scheduler Operating System: Linux | Architecture: x86_64 | (amd64) Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Changes (by luisgabriel): * Attachment "forkOS-N8-ghc8.png" added. ThreadScope view using forkOS -- Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/12419> GHC <http://www.haskell.org/ghc/> The Glasgow Haskell Compiler
#12419: Scheduling bug with forkOS + MVar -------------------------------------+------------------------------------- Reporter: luisgabriel | Owner: Type: bug | Status: new Priority: normal | Milestone: Component: Runtime System | Version: 8.0.1 Resolution: | Keywords: forkOS; | scheduler Operating System: Linux | Architecture: x86_64 | (amd64) Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by simonmar): The .zip file doesn't download for me (I get an error from dropbox), could you try putting it up somewhere else? -- Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/12419#comment:1> GHC <http://www.haskell.org/ghc/> The Glasgow Haskell Compiler
#12419: Scheduling bug with forkOS + MVar -------------------------------------+------------------------------------- Reporter: luisgabriel | Owner: Type: bug | Status: new Priority: normal | Milestone: Component: Runtime System | Version: 8.0.1 Resolution: | Keywords: forkOS; | scheduler Operating System: Linux | Architecture: x86_64 | (amd64) Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by luisgabriel): Replying to [comment:1 simonmar]:
The .zip file doesn't download for me (I get an error from dropbox), could you try putting it up somewhere else?
Sure. Try this link: http://cin.ufpe.br/~lgnfl/files/fasta-bug.zip -- Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/12419#comment:2> GHC <http://www.haskell.org/ghc/> The Glasgow Haskell Compiler
#12419: Scheduling bug with forkOS + MVar -------------------------------------+------------------------------------- Reporter: luisgabriel | Owner: Type: bug | Status: new Priority: normal | Milestone: Component: Runtime System | Version: 8.0.1 Resolution: | Keywords: forkOS; | scheduler Operating System: Linux | Architecture: x86_64 | (amd64) Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Phab:D2430 Wiki Page: | -------------------------------------+------------------------------------- Changes (by simonmar): * differential: => Phab:D2430 Comment: I found one bug, see Phab:D2430. However, even after the patch the threadscope profiles don't look identical. I don't think there is an actual problem, just that the program itself isn't very parallel - if you zoom in, there's lots of time in each thread where no work is being done. The difference in scheduling is due to the way that `forkOS` has to hand- shake with the new thread to get its `ThreadId`. -- Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/12419#comment:3> GHC <http://www.haskell.org/ghc/> The Glasgow Haskell Compiler
#12419: Scheduling bug with forkOS + MVar -------------------------------------+------------------------------------- Reporter: luisgabriel | Owner: Type: bug | Status: new Priority: normal | Milestone: 8.0.2 Component: Runtime System | Version: 8.0.1 Resolution: | Keywords: forkOS; | scheduler Operating System: Linux | Architecture: x86_64 | (amd64) Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Phab:D2430 Wiki Page: | -------------------------------------+------------------------------------- Changes (by bgamari): * milestone: => 8.0.2 Comment: This should go in to 8.0.2. -- Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/12419#comment:4> GHC <http://www.haskell.org/ghc/> The Glasgow Haskell Compiler
#12419: Scheduling bug with forkOS + MVar -------------------------------------+------------------------------------- Reporter: luisgabriel | Owner: Type: bug | Status: new Priority: normal | Milestone: 8.0.2 Component: Runtime System | Version: 8.0.1 Resolution: | Keywords: forkOS; | scheduler Operating System: Linux | Architecture: x86_64 | (amd64) Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Phab:D2430 Wiki Page: | -------------------------------------+------------------------------------- Comment (by luisgabriel): Replying to [comment:3 simonmar]:
I found one bug, see Phab:D2430. However, even after the patch the threadscope profiles don't look identical. I don't think there is an actual problem, just that the program itself isn't very parallel - if you zoom in, there's lots of time in each thread where no work is being done. The difference in scheduling is due to the way that `forkOS` has to hand- shake with the new thread to get its `ThreadId`.
Indeed. This example is not very good. I did not expect the profiles to look identical, but it seemed a bit odd to me having a large number of capabilities in idle. Nevertheless, I'm happy to see you identified a bug in the scheduler. Thank you very much for the quick fix! Feel free to close this ticket when the patch lands. -- Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/12419#comment:5> GHC <http://www.haskell.org/ghc/> The Glasgow Haskell Compiler
#12419: Scheduling bug with forkOS + MVar -------------------------------------+------------------------------------- Reporter: luisgabriel | Owner: Type: bug | Status: new Priority: normal | Milestone: 8.0.2 Component: Runtime System | Version: 8.0.1 Resolution: | Keywords: forkOS; | scheduler Operating System: Linux | Architecture: x86_64 | (amd64) Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Phab:D2430 Wiki Page: | -------------------------------------+------------------------------------- Comment (by Simon Marlow <marlowsd@…>): In [changeset:"988ad8ba8e709eff3cea59728e481bb269fa6185/ghc" 988ad8ba/ghc]: {{{ #!CommitTicketReference repository="ghc" revision="988ad8ba8e709eff3cea59728e481bb269fa6185" Fix to thread migration Summary: If we had 2 threads on the run queue, say [A,B], and B is bound to the current Task, then we would fail to migrate any threads. This fixes it so that we would migrate A in that case. This will help parallelism a bit in programs that have lots of bound threads. Test Plan: Test program in #12419, which is actually not a great program but it does behave a bit better after this change. Reviewers: ezyang, niteria, bgamari, austin, erikd Subscribers: thomie Differential Revision: https://phabricator.haskell.org/D2430 GHC Trac Issues: #12419 }}} -- Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/12419#comment:6> GHC <http://www.haskell.org/ghc/> The Glasgow Haskell Compiler
#12419: Scheduling bug with forkOS + MVar -------------------------------------+------------------------------------- Reporter: luisgabriel | Owner: Type: bug | Status: merge Priority: normal | Milestone: 8.0.2 Component: Runtime System | Version: 8.0.1 Resolution: | Keywords: forkOS; | scheduler Operating System: Linux | Architecture: x86_64 | (amd64) Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Phab:D2430 Wiki Page: | -------------------------------------+------------------------------------- Changes (by simonmar): * status: new => merge Comment: This also depends on https://phabricator.haskell.org/rGHC55f5aed756cd5d464942dddcb33e0bd19b05f2a4 -- Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/12419#comment:7> GHC <http://www.haskell.org/ghc/> The Glasgow Haskell Compiler
#12419: Scheduling bug with forkOS + MVar -------------------------------------+------------------------------------- Reporter: luisgabriel | Owner: Type: bug | Status: new Priority: normal | Milestone: 8.0.2 Component: Runtime System | Version: 8.0.1 Resolution: | Keywords: forkOS; | scheduler Operating System: Linux | Architecture: x86_64 | (amd64) Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | Phab:D2430,Phab:2441 -------------------------------------+------------------------------------- Changes (by simonmar): * status: merge => new * differential: Phab:D2430 => Phab:D2430,Phab:2441 Comment: Please don't merge this yet, there's another fix incoming. -- Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/12419#comment:8> GHC <http://www.haskell.org/ghc/> The Glasgow Haskell Compiler
#12419: Scheduling bug with forkOS + MVar -------------------------------------+------------------------------------- Reporter: luisgabriel | Owner: Type: bug | Status: new Priority: normal | Milestone: 8.0.2 Component: Runtime System | Version: 8.0.1 Resolution: | Keywords: forkOS; | scheduler Operating System: Linux | Architecture: x86_64 | (amd64) Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Phab:D2430, Wiki Page: | Phab:2441 -------------------------------------+------------------------------------- Changes (by simonmar): * differential: Phab:D2430,Phab:2441 => Phab:D2430, Phab:2441 -- Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/12419#comment:9> GHC <http://www.haskell.org/ghc/> The Glasgow Haskell Compiler
#12419: Scheduling bug with forkOS + MVar -------------------------------------+------------------------------------- Reporter: luisgabriel | Owner: Type: bug | Status: new Priority: normal | Milestone: 8.0.2 Component: Runtime System | Version: 8.0.1 Resolution: | Keywords: forkOS; | scheduler Operating System: Linux | Architecture: x86_64 | (amd64) Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Phab:D2430, Wiki Page: | Phab:D2441 -------------------------------------+------------------------------------- Changes (by simonmar): * differential: Phab:D2430, Phab:2441 => Phab:D2430, Phab:D2441 -- Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/12419#comment:10> GHC <http://www.haskell.org/ghc/> The Glasgow Haskell Compiler
#12419: Scheduling bug with forkOS + MVar -------------------------------------+------------------------------------- Reporter: luisgabriel | Owner: Type: bug | Status: new Priority: normal | Milestone: 8.0.2 Component: Runtime System | Version: 8.0.1 Resolution: | Keywords: forkOS; | scheduler Operating System: Linux | Architecture: x86_64 | (amd64) Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Phab:D2430, Wiki Page: | Phab:D2441 -------------------------------------+------------------------------------- Comment (by Simon Marlow <marlowsd@…>): In [changeset:"89fa4e968f47cfb42d0dc33fc3bfffdce31d850e/ghc" 89fa4e9/ghc]: {{{ #!CommitTicketReference repository="ghc" revision="89fa4e968f47cfb42d0dc33fc3bfffdce31d850e" Another try to get thread migration right Summary: This is surprisingly tricky. There were linked list bugs in the previous version (D2430) that showed up as a test failure in setnumcapabilities001 (that's a great stress test!). This new version uses a different strategy that doesn't suffer from the problem that @ezyang pointed out in D2430. We now pre-calculate how many threads to keep for this capability, and then migrate any surplus threads off the front of the queue, taking care to account for threads that can't be migrated. Test Plan: 1. setnumcapabilities001 stress test with sanity checking (+RTS -DS) turned on: ``` cd testsuite/tests/concurrent/should_run make TEST=setnumcapabilities001 WAY=threaded1 EXTRA_HC_OPTS=-with- rtsopts=-DS CLEANUP=0 while true; do ./setnumcapabilities001.run/setnumcapabilities001 4 9 2000 || break; done ``` 2. The test case from #12419 Reviewers: niteria, ezyang, rwbarton, austin, bgamari, erikd Subscribers: thomie, ezyang Differential Revision: https://phabricator.haskell.org/D2441 GHC Trac Issues: #12419 }}} -- Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/12419#comment:11> GHC <http://www.haskell.org/ghc/> The Glasgow Haskell Compiler
#12419: Scheduling bug with forkOS + MVar -------------------------------------+------------------------------------- Reporter: luisgabriel | Owner: Type: bug | Status: merge Priority: normal | Milestone: 8.0.2 Component: Runtime System | Version: 8.0.1 Resolution: | Keywords: forkOS; | scheduler Operating System: Linux | Architecture: x86_64 | (amd64) Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Phab:D2430, Wiki Page: | Phab:D2441 -------------------------------------+------------------------------------- Changes (by simonmar): * status: new => merge -- Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/12419#comment:12> GHC <http://www.haskell.org/ghc/> The Glasgow Haskell Compiler
#12419: Scheduling bug with forkOS + MVar -------------------------------------+------------------------------------- Reporter: luisgabriel | Owner: Type: bug | Status: merge Priority: normal | Milestone: 8.0.2 Component: Runtime System | Version: 8.0.1 Resolution: | Keywords: forkOS; | scheduler Operating System: Linux | Architecture: x86_64 | (amd64) Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Phab:D2430, Wiki Page: | Phab:D2441 -------------------------------------+------------------------------------- Comment (by Ben Gamari <ben@…>): In [changeset:"9f1b6de8b3f788730fa5e2206fb709400299be7c/ghc" 9f1b6de8/ghc]: {{{ #!CommitTicketReference repository="ghc" revision="9f1b6de8b3f788730fa5e2206fb709400299be7c" Fix to thread migration Summary: If we had 2 threads on the run queue, say [A,B], and B is bound to the current Task, then we would fail to migrate any threads. This fixes it so that we would migrate A in that case. This will help parallelism a bit in programs that have lots of bound threads. Test Plan: Test program in #12419, which is actually not a great program but it does behave a bit better after this change. Reviewers: ezyang, niteria, bgamari, austin, erikd Subscribers: thomie Differential Revision: https://phabricator.haskell.org/D2430 GHC Trac Issues: #12419 (cherry picked from commit 988ad8ba8e709eff3cea59728e481bb269fa6185) }}} -- Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/12419#comment:13> GHC <http://www.haskell.org/ghc/> The Glasgow Haskell Compiler
#12419: Scheduling bug with forkOS + MVar -------------------------------------+------------------------------------- Reporter: luisgabriel | Owner: Type: bug | Status: merge Priority: normal | Milestone: 8.0.2 Component: Runtime System | Version: 8.0.1 Resolution: | Keywords: forkOS; | scheduler Operating System: Linux | Architecture: x86_64 | (amd64) Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Phab:D2430, Wiki Page: | Phab:D2441 -------------------------------------+------------------------------------- Comment (by Ben Gamari <ben@…>): In [changeset:"13ff3423e058a409b035acce5c1448237885ac84/ghc" 13ff3423/ghc]: {{{ #!CommitTicketReference repository="ghc" revision="13ff3423e058a409b035acce5c1448237885ac84" Another try to get thread migration right Summary: This is surprisingly tricky. There were linked list bugs in the previous version (D2430) that showed up as a test failure in setnumcapabilities001 (that's a great stress test!). This new version uses a different strategy that doesn't suffer from the problem that @ezyang pointed out in D2430. We now pre-calculate how many threads to keep for this capability, and then migrate any surplus threads off the front of the queue, taking care to account for threads that can't be migrated. Test Plan: 1. setnumcapabilities001 stress test with sanity checking (+RTS -DS) turned on: ``` cd testsuite/tests/concurrent/should_run make TEST=setnumcapabilities001 WAY=threaded1 EXTRA_HC_OPTS=-with- rtsopts=-DS CLEANUP=0 while true; do ./setnumcapabilities001.run/setnumcapabilities001 4 9 2000 || break; done ``` 2. The test case from #12419 Reviewers: niteria, ezyang, rwbarton, austin, bgamari, erikd Subscribers: thomie, ezyang Differential Revision: https://phabricator.haskell.org/D2441 GHC Trac Issues: #12419 (cherry picked from commit 89fa4e968f47cfb42d0dc33fc3bfffdce31d850e) }}} -- Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/12419#comment:14> GHC <http://www.haskell.org/ghc/> The Glasgow Haskell Compiler
#12419: Scheduling bug with forkOS + MVar -------------------------------------+------------------------------------- Reporter: luisgabriel | Owner: Type: bug | Status: closed Priority: normal | Milestone: 8.0.2 Component: Runtime System | Version: 8.0.1 Resolution: fixed | Keywords: forkOS; | scheduler Operating System: Linux | Architecture: x86_64 | (amd64) Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Phab:D2430, Wiki Page: | Phab:D2441 -------------------------------------+------------------------------------- Changes (by bgamari): * status: merge => closed * resolution: => fixed -- Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/12419#comment:15> GHC <http://www.haskell.org/ghc/> The Glasgow Haskell Compiler
participants (1)
-
GHC