diff options
author | joe <joe@jerkface.net> | 2018-06-26 17:29:30 -0400 |
---|---|---|
committer | joe <joe@jerkface.net> | 2018-06-26 17:29:30 -0400 |
commit | 2b2627f11e4f7544d86c78a474872f02665ef109 (patch) | |
tree | bc516de9e87e81d86827cb616c14ae63897b2500 /src/DebugUtil.hs | |
parent | 7123e72a7256a146d2d756394eab863c5d536fc9 (diff) |
Thread report in test program.
Diffstat (limited to 'src/DebugUtil.hs')
-rw-r--r-- | src/DebugUtil.hs | 41 |
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 #-} | ||
2 | module DebugUtil where | ||
3 | |||
4 | import Control.Monad | ||
5 | import Data.Time.Clock | ||
6 | import Data.List | ||
7 | import Text.Printf | ||
8 | import GHC.Conc (threadStatus,ThreadStatus(..)) | ||
9 | #ifdef THREAD_DEBUG | ||
10 | import Control.Concurrent.Lifted.Instrument | ||
11 | #else | ||
12 | import Control.Concurrent.Lifted | ||
13 | import GHC.Conc (labelThread) | ||
14 | #endif | ||
15 | |||
16 | showReport :: [(String,String)] -> String | ||
17 | showReport kvs = showColumns $ map (\(x,y)->[x,y]) kvs | ||
18 | |||
19 | showColumns :: [[String]] -> String | ||
20 | showColumns 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 | |||
28 | threadReport :: Bool -> IO String | ||
29 | threadReport 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." ] | ||