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

Mark some blocking FFI imports as "safe" (fix high cpu usage in xmonad) #78

Merged
merged 1 commit into from
Aug 15, 2021

Conversation

liskin
Copy link
Member

@liskin liskin commented Aug 9, 2021

GHC RTS doesn't expect "unsafe" FFI calls to block, so when the parallel garbage collector kicks in, blocking unsafe calls cause all the other GC threads to wait in a busy loop.

This can be reproduced by using XMonad.Actions.Submap (which uses XMaskEvent in a blocking fashion) together with +RTS -N. To confirm the parallel GC hypothesis, +RTS -N -qg (disables parallel GC) works fine. (For more details about the reproducer, see the linked xmonad-contrib issues.)

The fix is to mark potentially blocking calls "safe", as XNextEvent has been for the last 14 years and as documented in: https://downloads.haskell.org/~ghc/9.0.1/docs/html/users_guide/exts/ffi.html#foreign-imports-and-multi-threading

Fixes: xmonad/xmonad-contrib#262
Fixes: xmonad/xmonad-contrib#483
Related: 344d942 ("XNextEvent should be safe, for correct behaviour with threads")

GHC RTS doesn't expect "unsafe" FFI calls to block, so when the parallel
garbage collector kicks in, blocking unsafe calls cause all the other GC
threads to wait in a busy loop.

This can be reproduced by using XMonad.Actions.Submap (which uses
XMaskEvent in a blocking fashion) together with +RTS -N. To confirm the
parallel GC hypothesis, +RTS -N -qg (disables parallel GC) works fine.
(For more details about the reproducer, see the linked xmonad-contrib
issues.)

The fix is to mark potentially blocking calls "safe", as XNextEvent has
been for the last 14 years and as documented in:
https://downloads.haskell.org/~ghc/9.0.1/docs/html/users_guide/exts/ffi.html#foreign-imports-and-multi-threading

Fixes: xmonad/xmonad-contrib#262
Fixes: xmonad/xmonad-contrib#483
Related: 344d942 ("XNextEvent should be *safe*, for correct behaviour with threads")
@slotThe
Copy link
Member

slotThe commented Aug 15, 2021

Can confirm that this works for me so I'm going to merge

@slotThe slotThe merged commit 6ada774 into xmonad:master Aug 15, 2021
@liskin liskin deleted the safe-blocking-ffi branch August 15, 2021 15:37
liskin added a commit that referenced this pull request Aug 15, 2021
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 this pull request may close these issues.

High CPU Usage with submaps High cpu usage on layered submap
2 participants