summaryrefslogtreecommitdiff
path: root/lifted-concurrent/src
diff options
context:
space:
mode:
authorJoe Crayne <joe@jerkface.net>2020-01-03 15:35:23 -0500
committerJoe Crayne <joe@jerkface.net>2020-01-03 17:26:06 -0500
commit31b799222cb76cd0002d9a3cc5b340a7b6fed139 (patch)
tree8b834e455529fb270375e4967d1acad56553544f /lifted-concurrent/src
parent1e03ed3670a8386ede93a09fa0c67785e7da6478 (diff)
server library.
Diffstat (limited to 'lifted-concurrent/src')
-rw-r--r--lifted-concurrent/src/Control/Concurrent/ThreadUtil.hs31
-rw-r--r--lifted-concurrent/src/DebugUtil.hs43
2 files changed, 74 insertions, 0 deletions
diff --git a/lifted-concurrent/src/Control/Concurrent/ThreadUtil.hs b/lifted-concurrent/src/Control/Concurrent/ThreadUtil.hs
new file mode 100644
index 00000000..a258d933
--- /dev/null
+++ b/lifted-concurrent/src/Control/Concurrent/ThreadUtil.hs
@@ -0,0 +1,31 @@
1{-# LANGUAGE CPP #-}
2module Control.Concurrent.ThreadUtil
3 (
4#ifdef THREAD_DEBUG
5 module Control.Concurrent.Lifted.Instrument
6#else
7 module Control.Control.Lifted
8 , module GHC.Conc
9#endif
10 ) where
11
12#ifdef THREAD_DEBUG
13import Control.Concurrent.Lifted.Instrument
14#else
15import Control.Concurrent.Lifted
16import GHC.Conc (labelThread)
17
18forkLabeled :: String -> IO () -> IO ThreadId
19forkLabeled lbl action = do
20 t <- forkIO action
21 labelThread t lbl
22 return t
23{-# INLINE forkLabeled #-}
24
25forkOSLabeled :: String -> IO () -> IO ThreadId
26forkOSLabeled lbl action = do
27 t <- forkOS action
28 labelThread t lbl
29 return t
30{-# INLINE forkOSLabeled #-}
31#endif
diff --git a/lifted-concurrent/src/DebugUtil.hs b/lifted-concurrent/src/DebugUtil.hs
new file mode 100644
index 00000000..e73f9061
--- /dev/null
+++ b/lifted-concurrent/src/DebugUtil.hs
@@ -0,0 +1,43 @@
1{-# LANGUAGE CPP #-}
2{-# LANGUAGE RecordWildCards #-}
3module DebugUtil where
4
5import Control.Monad
6import Data.Time.Clock
7import Data.List
8import Text.Printf
9import GHC.Conc (threadStatus,ThreadStatus(..))
10#ifdef THREAD_DEBUG
11import Control.Concurrent.Lifted.Instrument
12#else
13import Control.Concurrent.Lifted
14import GHC.Conc (labelThread)
15#endif
16
17showReport :: [(String,String)] -> String
18showReport kvs = showColumns $ map (\(x,y)->[x,y]) kvs
19
20showColumns :: [[String]] -> String
21showColumns rows = do
22 let cols = transpose rows
23 ws = map (maximum . map (succ . length)) cols
24 fs <- rows
25 _ <- take 1 fs -- Guard against empty rows so that 'last' is safe.
26 " " ++ concat (zipWith (printf "%-*s") (init ws) (init fs)) ++ last fs ++ "\n"
27
28
29threadReport :: Bool -- ^ False to summarize search threads.
30 -> IO String
31threadReport want_ss = do
32 threads <- threadsInformation
33 tm <- getCurrentTime
34 let (ss,ts) = partition (("search" `isPrefixOf`) . lbl . snd)
35 threads
36 r <- forM (if want_ss then threads else ts) $ \(tid,PerThread{..}) -> do
37 stat <- threadStatus tid
38 let showStat (ThreadBlocked reason) = show reason
39 showStat stat = show stat
40 return [show lbl,show (diffUTCTime tm startTime),showStat stat]
41 return $ unlines [ showColumns r
42 , (if want_ss then " There are " else " and ")
43 ++ show (length ss) ++ " search threads." ]