Search code examples
haskellghcstm

How to discover if a transaction is frequently aborting?


I'm trying to debug a program that uses STM. The ThreadScope readings is pointing out a very high CPU activity as you can see here:

enter image description here

So I'm trying to find out if this is happening due to a transaction that frequently aborts. The first thing that I thought was using something like this to test:

atomically $ do
  someWork 
  ...
`orElse` do
  unsafeIOToSTM $ traceEventIO "transaction aborted!"
  retry

But I'm not sure if this is correct or if this is the best approach to debugging in an STM scenario. Any ideas?


Solution

  • Use stm-stats package. It provides trackSTM that you should use instead of atomically, and dumpSTMStats :: IO () that will provide something like this:

    STM transaction statistics (2011-10-09 12:28:37.188951 UTC):
    Transaction     Commits    Retries      Ratio
    _anonymous_           1          0       0.00
    reader                1         23      23.00
    writer               23          0       0.00
    

    (Transaction names will be generated automatically, but there's helpers to set your own.)