Skip to content

GHC uses 36 cores to run this sequential program

Summary

"Idle time" garbage collection appears to keep all the available hardware threads busy, even though there is a compute-intensive Haskell thread running. On our 18-core i9, with 36 hardware threads, the CPU utilization of the offending process rises to 3600%.

The problem appears only to affect Linux systems (including under WSL; we have not observed it happening under native Windows or MacOS). Disabling idle-time garbage collection with the -I0 flag solves the problem.

Steps to reproduce

Here is the test program I used to demonstrate the effect, in Foo.hs:

{-# LANGUAGE MagicHash #-}

import GHC.Exts

fib :: Int# -> Int#
fib 0# = 1#
fib 1# = 1#
fib n = fib (n-#1#) +# fib (n-#2#)

main = print (I# (fib 50#))

Compile with ghc -threaded -rtsopts -O2 Foo.hs. Run as follows:

john@quviq-office-server:~$ time ./Foo +RTS -N
20365011074

real    1m26.463s
user    46m53.904s
sys     0m25.617s
john@quviq-office-server:~$ time ./Foo +RTS -N -I0
20365011074

real    1m5.203s
user    1m5.192s
sys     0m0.028s

Observe the CPU utilization of this process using top. You will see that the first run uses every available hardware thread... all those in the processor if no other heavy task is running... while the second run has only 100% CPU utilization.

The "idle time" GC has increased the real time by 32%, and the user time by 4200%.

In this example I used unboxed types so that there should be no memory allocation at all during the main computation. Using boxed integers instead does not change the observed behaviour. I have seen similar behaviour running real computationally-intensive sequential applications (although in that case only 10-12 hardware threads were used).

Expected behavior

I expect the CPU utilization to be close to 100% in both cases. I don't expect "idle time" GC to run at all, because there should be no idle time--there is a computationally intensive Haskell thread running throughout.

Environment

  • GHC version used: 8.10.5
  • Operating System: Ubuntu 18.04.5 LTS
  • System Architecture: Intel(R) Core(TM) i9-9980XE CPU @ 3.00GHz (36 hardware threads)

I've observed similar behaviour using ghc 8.4.3 on Ubuntu 18.04 LTS under WSL2 on Windows 10.

To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information