summaryrefslogtreecommitdiff
path: root/src/DebugUtil.hs
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2018-06-26 17:29:30 -0400
committerjoe <joe@jerkface.net>2018-06-26 17:29:30 -0400
commit2b2627f11e4f7544d86c78a474872f02665ef109 (patch)
treebc516de9e87e81d86827cb616c14ae63897b2500 /src/DebugUtil.hs
parent7123e72a7256a146d2d756394eab863c5d536fc9 (diff)
Thread report in test program.
Diffstat (limited to 'src/DebugUtil.hs')
-rw-r--r--src/DebugUtil.hs41
1 files changed, 41 insertions, 0 deletions
diff --git a/src/DebugUtil.hs b/src/DebugUtil.hs
new file mode 100644
index 00000000..e7a10397
--- /dev/null
+++ b/src/DebugUtil.hs
@@ -0,0 +1,41 @@
1{-# LANGUAGE CPP #-}
2module DebugUtil where
3
4import Control.Monad
5import Data.Time.Clock
6import Data.List
7import Text.Printf
8import GHC.Conc (threadStatus,ThreadStatus(..))
9#ifdef THREAD_DEBUG
10import Control.Concurrent.Lifted.Instrument
11#else
12import Control.Concurrent.Lifted
13import GHC.Conc (labelThread)
14#endif
15
16showReport :: [(String,String)] -> String
17showReport kvs = showColumns $ map (\(x,y)->[x,y]) kvs
18
19showColumns :: [[String]] -> String
20showColumns rows = do
21 let cols = transpose rows
22 ws = map (maximum . map (succ . length)) cols
23 fs <- rows
24 _ <- take 1 fs -- Guard against empty rows so that 'last' is safe.
25 " " ++ concat (zipWith (printf "%-*s") (init ws) (init fs)) ++ last fs ++ "\n"
26
27
28threadReport :: Bool -> IO String
29threadReport want_ss = do
30 threads <- threadsInformation
31 tm <- getCurrentTime
32 let (ss,ts) = partition (("search" `isPrefixOf`) . lbl . snd)
33 threads
34 r <- forM (if want_ss then threads else ts) $ \(tid,PerThread{..}) -> do
35 stat <- threadStatus tid
36 let showStat (ThreadBlocked reason) = show reason
37 showStat stat = show stat
38 return [show lbl,show (diffUTCTime tm startTime),showStat stat]
39 return $ unlines [ showColumns r
40 , (if want_ss then " There are " else " and ")
41 ++ show (length ss) ++ " search threads." ]