summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2017-07-28 20:58:43 -0400
committerjoe <joe@jerkface.net>2017-07-28 20:58:43 -0400
commitd0ff6c3ac977035f3493b679978da73517550028 (patch)
treef00c62d6e95a7f10ff58580601c5651465310cc0
parente4ecff0ba5ad4b392a419ab7723c5df49513d1fa (diff)
More details in threads listing.
-rw-r--r--examples/dhtd.hs20
-rw-r--r--src/Control/Concurrent/Lifted/Instrument.hs13
2 files changed, 20 insertions, 13 deletions
diff --git a/examples/dhtd.hs b/examples/dhtd.hs
index 9bb7c5a7..ef225f0c 100644
--- a/examples/dhtd.hs
+++ b/examples/dhtd.hs
@@ -60,10 +60,14 @@ import Network.BitTorrent.DHT.ContactInfo as Peers
60import qualified Data.MinMaxPSQ as MM 60import qualified Data.MinMaxPSQ as MM
61 61
62showReport :: [(String,String)] -> String 62showReport :: [(String,String)] -> String
63showReport kvs = do 63showReport kvs = showColumns $ map (\(x,y)->[x,y]) kvs
64 let colwidth = maximum $ map (length . fst) kvs 64
65 (k,v) <- kvs 65showColumns rows = do
66 concat [ printf " %-*s" (colwidth+1) k, v, "\n" ] 66 let cols = transpose rows
67 ws = map (maximum . map (succ . length)) cols
68 fs <- rows
69 _ <- take 1 fs -- Guard against empty rows so that 'last' is safe.
70 " " ++ concat (zipWith (printf "%-*s") (init ws) (init fs)) ++ last fs ++ "\n"
67 71
68 72
69marshalForClient :: String -> String 73marshalForClient :: String -> String
@@ -282,8 +286,12 @@ clientSession s@Session{..} sock cnum h = do
282 ("threads", _) -> cmd0 $ do 286 ("threads", _) -> cmd0 $ do
283 ts <- threadsInformation 287 ts <- threadsInformation
284 tm <- getCurrentTime 288 tm <- getCurrentTime
285 let r = map (\PerThread{..} -> (show lbl,show (diffUTCTime tm startTime))) ts 289 r <- forM ts $ \(tid,PerThread{..}) -> do
286 hPutClient h $ showReport r 290 stat <- threadStatus tid
291 let showStat (ThreadBlocked reason) = show reason
292 showStat stat = show stat
293 return [show lbl,show (diffUTCTime tm startTime),showStat stat]
294 hPutClient h $ showColumns r
287#endif 295#endif
288 ("mem", s) -> cmd0 $ do 296 ("mem", s) -> cmd0 $ do
289 case s of 297 case s of
diff --git a/src/Control/Concurrent/Lifted/Instrument.hs b/src/Control/Concurrent/Lifted/Instrument.hs
index 957b67bb..7ee78c8b 100644
--- a/src/Control/Concurrent/Lifted/Instrument.hs
+++ b/src/Control/Concurrent/Lifted/Instrument.hs
@@ -45,26 +45,25 @@ fork action = do
45 t <- Raw.fork $ do 45 t <- Raw.fork $ do
46 tid <- myThreadId 46 tid <- myThreadId
47 tm <- liftBase getCurrentTime 47 tm <- liftBase getCurrentTime
48 bracket_ (modifyThreads $ Map.insert tid (PerThread "" tm)) 48 bracket_ (modifyThreads $! Map.insert tid (PerThread "" tm))
49 (modifyThreads $ Map.delete tid) 49 (modifyThreads $! Map.delete tid)
50 action 50 action
51 return t 51 return t
52 52
53labelThread :: ThreadId -> String -> IO () 53labelThread :: ThreadId -> String -> IO ()
54labelThread tid s = do 54labelThread tid s = do
55 GHC.labelThread tid s 55 GHC.labelThread tid s
56 modifyThreads $ Map.adjust (\pt -> pt { lbl = s }) tid 56 modifyThreads $! Map.adjust (\pt -> pt { lbl = s }) tid
57{-# INLINE labelThread #-} 57{-# INLINE labelThread #-}
58 58
59threadsInformation :: IO [PerThread] 59threadsInformation :: IO [(ThreadId,PerThread)]
60threadsInformation = do 60threadsInformation = do
61 m <- threads <$> readMVar globals 61 m <- threads <$> readMVar globals
62 return $ Map.elems m 62 return $ Map.toList m
63 63
64 64
65modifyThreads :: MonadBase IO m => (Map.Map ThreadId PerThread -> Map.Map ThreadId PerThread) -> m () 65modifyThreads :: MonadBase IO m => (Map.Map ThreadId PerThread -> Map.Map ThreadId PerThread) -> m ()
66modifyThreads f = do 66modifyThreads f = do
67 g <- takeMVar globals 67 g <- takeMVar globals
68 let f' st = st { threads = f (threads st) } 68 let f' st = st { threads = f (threads st) }
69 r = f' g 69 putMVar globals $! f' g
70 putMVar globals r