diff options
author | Joe Crayne <joe@jerkface.net> | 2020-01-03 15:35:23 -0500 |
---|---|---|
committer | Joe Crayne <joe@jerkface.net> | 2020-01-03 17:26:06 -0500 |
commit | 31b799222cb76cd0002d9a3cc5b340a7b6fed139 (patch) | |
tree | 8b834e455529fb270375e4967d1acad56553544f /lifted-concurrent | |
parent | 1e03ed3670a8386ede93a09fa0c67785e7da6478 (diff) |
server library.
Diffstat (limited to 'lifted-concurrent')
-rw-r--r-- | lifted-concurrent/lifted-concurrent.cabal | 3 | ||||
-rw-r--r-- | lifted-concurrent/src/Control/Concurrent/ThreadUtil.hs | 31 | ||||
-rw-r--r-- | lifted-concurrent/src/DebugUtil.hs | 43 |
3 files changed, 77 insertions, 0 deletions
diff --git a/lifted-concurrent/lifted-concurrent.cabal b/lifted-concurrent/lifted-concurrent.cabal index bbf254e3..8fe4b6a9 100644 --- a/lifted-concurrent/lifted-concurrent.cabal +++ b/lifted-concurrent/lifted-concurrent.cabal | |||
@@ -19,6 +19,8 @@ library | |||
19 | exposed-modules: | 19 | exposed-modules: |
20 | Control.Concurrent.Lifted.Instrument | 20 | Control.Concurrent.Lifted.Instrument |
21 | , Control.Concurrent.Async.Lifted.Instrument | 21 | , Control.Concurrent.Async.Lifted.Instrument |
22 | , DebugUtil | ||
23 | , Control.Concurrent.ThreadUtil | ||
22 | other-modules: DebugTag | 24 | other-modules: DebugTag |
23 | other-extensions: FlexibleContexts | 25 | other-extensions: FlexibleContexts |
24 | build-depends: | 26 | build-depends: |
@@ -32,3 +34,4 @@ library | |||
32 | , transformers-base | 34 | , transformers-base |
33 | hs-source-dirs: src | 35 | hs-source-dirs: src |
34 | default-language: Haskell2010 | 36 | default-language: Haskell2010 |
37 | cpp-options: -DTHREAD_DEBUG | ||
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 #-} | ||
2 | module 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 | ||
13 | import Control.Concurrent.Lifted.Instrument | ||
14 | #else | ||
15 | import Control.Concurrent.Lifted | ||
16 | import GHC.Conc (labelThread) | ||
17 | |||
18 | forkLabeled :: String -> IO () -> IO ThreadId | ||
19 | forkLabeled lbl action = do | ||
20 | t <- forkIO action | ||
21 | labelThread t lbl | ||
22 | return t | ||
23 | {-# INLINE forkLabeled #-} | ||
24 | |||
25 | forkOSLabeled :: String -> IO () -> IO ThreadId | ||
26 | forkOSLabeled 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 #-} | ||
3 | module DebugUtil where | ||
4 | |||
5 | import Control.Monad | ||
6 | import Data.Time.Clock | ||
7 | import Data.List | ||
8 | import Text.Printf | ||
9 | import GHC.Conc (threadStatus,ThreadStatus(..)) | ||
10 | #ifdef THREAD_DEBUG | ||
11 | import Control.Concurrent.Lifted.Instrument | ||
12 | #else | ||
13 | import Control.Concurrent.Lifted | ||
14 | import GHC.Conc (labelThread) | ||
15 | #endif | ||
16 | |||
17 | showReport :: [(String,String)] -> String | ||
18 | showReport kvs = showColumns $ map (\(x,y)->[x,y]) kvs | ||
19 | |||
20 | showColumns :: [[String]] -> String | ||
21 | showColumns 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 | |||
29 | threadReport :: Bool -- ^ False to summarize search threads. | ||
30 | -> IO String | ||
31 | threadReport 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." ] | ||