2014-01-29 17:44:53 +00:00
|
|
|
{- git-annex command-line actions
|
fix inversion of control in CommandSeek (no behavior changes)
I've been disliking how the command seek actions were written for some
time, with their inversion of control and ugly workarounds.
The last straw to fix it was sync --content, which didn't fit the
Annex [CommandStart] interface well at all. I have not yet made it take
advantage of the changed interface though.
The crucial change, and probably why I didn't do it this way from the
beginning, is to make each CommandStart action be run with exceptions
caught, and if it fails, increment a failure counter in annex state.
So I finally remove the very first code I wrote for git-annex, which
was before I had exception handling in the Annex monad, and so ran outside
that monad, passing state explicitly as it ran each CommandStart action.
This was a real slog from 1 to 5 am.
Test suite passes.
Memory usage is lower than before, sometimes by a couple of megabytes, and
remains constant, even when running in a large repo, and even when
repeatedly failing and incrementing the error counter. So no accidental
laziness space leaks.
Wall clock speed is identical, even in large repos.
This commit was sponsored by an anonymous bitcoiner.
2014-01-20 08:11:42 +00:00
|
|
|
-
|
2017-10-17 21:54:38 +00:00
|
|
|
- Copyright 2010-2017 Joey Hess <id@joeyh.name>
|
fix inversion of control in CommandSeek (no behavior changes)
I've been disliking how the command seek actions were written for some
time, with their inversion of control and ugly workarounds.
The last straw to fix it was sync --content, which didn't fit the
Annex [CommandStart] interface well at all. I have not yet made it take
advantage of the changed interface though.
The crucial change, and probably why I didn't do it this way from the
beginning, is to make each CommandStart action be run with exceptions
caught, and if it fails, increment a failure counter in annex state.
So I finally remove the very first code I wrote for git-annex, which
was before I had exception handling in the Annex monad, and so ran outside
that monad, passing state explicitly as it ran each CommandStart action.
This was a real slog from 1 to 5 am.
Test suite passes.
Memory usage is lower than before, sometimes by a couple of megabytes, and
remains constant, even when running in a large repo, and even when
repeatedly failing and incrementing the error counter. So no accidental
laziness space leaks.
Wall clock speed is identical, even in large repos.
This commit was sponsored by an anonymous bitcoiner.
2014-01-20 08:11:42 +00:00
|
|
|
-
|
2019-03-13 19:48:14 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
fix inversion of control in CommandSeek (no behavior changes)
I've been disliking how the command seek actions were written for some
time, with their inversion of control and ugly workarounds.
The last straw to fix it was sync --content, which didn't fit the
Annex [CommandStart] interface well at all. I have not yet made it take
advantage of the changed interface though.
The crucial change, and probably why I didn't do it this way from the
beginning, is to make each CommandStart action be run with exceptions
caught, and if it fails, increment a failure counter in annex state.
So I finally remove the very first code I wrote for git-annex, which
was before I had exception handling in the Annex monad, and so ran outside
that monad, passing state explicitly as it ran each CommandStart action.
This was a real slog from 1 to 5 am.
Test suite passes.
Memory usage is lower than before, sometimes by a couple of megabytes, and
remains constant, even when running in a large repo, and even when
repeatedly failing and incrementing the error counter. So no accidental
laziness space leaks.
Wall clock speed is identical, even in large repos.
This commit was sponsored by an anonymous bitcoiner.
2014-01-20 08:11:42 +00:00
|
|
|
-}
|
|
|
|
|
2015-11-05 21:22:45 +00:00
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
|
2014-01-29 17:44:53 +00:00
|
|
|
module CmdLine.Action where
|
fix inversion of control in CommandSeek (no behavior changes)
I've been disliking how the command seek actions were written for some
time, with their inversion of control and ugly workarounds.
The last straw to fix it was sync --content, which didn't fit the
Annex [CommandStart] interface well at all. I have not yet made it take
advantage of the changed interface though.
The crucial change, and probably why I didn't do it this way from the
beginning, is to make each CommandStart action be run with exceptions
caught, and if it fails, increment a failure counter in annex state.
So I finally remove the very first code I wrote for git-annex, which
was before I had exception handling in the Annex monad, and so ran outside
that monad, passing state explicitly as it ran each CommandStart action.
This was a real slog from 1 to 5 am.
Test suite passes.
Memory usage is lower than before, sometimes by a couple of megabytes, and
remains constant, even when running in a large repo, and even when
repeatedly failing and incrementing the error counter. So no accidental
laziness space leaks.
Wall clock speed is identical, even in large repos.
This commit was sponsored by an anonymous bitcoiner.
2014-01-20 08:11:42 +00:00
|
|
|
|
2016-01-20 20:36:33 +00:00
|
|
|
import Annex.Common
|
fix inversion of control in CommandSeek (no behavior changes)
I've been disliking how the command seek actions were written for some
time, with their inversion of control and ugly workarounds.
The last straw to fix it was sync --content, which didn't fit the
Annex [CommandStart] interface well at all. I have not yet made it take
advantage of the changed interface though.
The crucial change, and probably why I didn't do it this way from the
beginning, is to make each CommandStart action be run with exceptions
caught, and if it fails, increment a failure counter in annex state.
So I finally remove the very first code I wrote for git-annex, which
was before I had exception handling in the Annex monad, and so ran outside
that monad, passing state explicitly as it ran each CommandStart action.
This was a real slog from 1 to 5 am.
Test suite passes.
Memory usage is lower than before, sometimes by a couple of megabytes, and
remains constant, even when running in a large repo, and even when
repeatedly failing and incrementing the error counter. So no accidental
laziness space leaks.
Wall clock speed is identical, even in large repos.
This commit was sponsored by an anonymous bitcoiner.
2014-01-20 08:11:42 +00:00
|
|
|
import qualified Annex
|
2015-04-10 21:53:58 +00:00
|
|
|
import Annex.Concurrent
|
fix inversion of control in CommandSeek (no behavior changes)
I've been disliking how the command seek actions were written for some
time, with their inversion of control and ugly workarounds.
The last straw to fix it was sync --content, which didn't fit the
Annex [CommandStart] interface well at all. I have not yet made it take
advantage of the changed interface though.
The crucial change, and probably why I didn't do it this way from the
beginning, is to make each CommandStart action be run with exceptions
caught, and if it fails, increment a failure counter in annex state.
So I finally remove the very first code I wrote for git-annex, which
was before I had exception handling in the Annex monad, and so ran outside
that monad, passing state explicitly as it ran each CommandStart action.
This was a real slog from 1 to 5 am.
Test suite passes.
Memory usage is lower than before, sometimes by a couple of megabytes, and
remains constant, even when running in a large repo, and even when
repeatedly failing and incrementing the error counter. So no accidental
laziness space leaks.
Wall clock speed is identical, even in large repos.
This commit was sponsored by an anonymous bitcoiner.
2014-01-20 08:11:42 +00:00
|
|
|
import Types.Command
|
2016-09-09 16:57:42 +00:00
|
|
|
import Types.Concurrency
|
2015-11-05 21:22:45 +00:00
|
|
|
import Messages.Concurrent
|
2015-04-10 21:08:07 +00:00
|
|
|
import Types.Messages
|
2017-05-25 22:27:01 +00:00
|
|
|
import Remote.List
|
2015-04-10 21:08:07 +00:00
|
|
|
|
2017-10-17 21:54:38 +00:00
|
|
|
import Control.Concurrent
|
2015-04-10 21:08:07 +00:00
|
|
|
import Control.Concurrent.Async
|
2017-10-17 21:54:38 +00:00
|
|
|
import Control.Concurrent.STM
|
2015-04-10 21:08:07 +00:00
|
|
|
import Control.Exception (throwIO)
|
2019-05-10 17:24:31 +00:00
|
|
|
import GHC.Conc
|
2015-04-10 21:08:07 +00:00
|
|
|
import Data.Either
|
2017-10-17 21:54:38 +00:00
|
|
|
import qualified Data.Map.Strict as M
|
2015-11-05 21:22:45 +00:00
|
|
|
import qualified System.Console.Regions as Regions
|
|
|
|
|
fix inversion of control in CommandSeek (no behavior changes)
I've been disliking how the command seek actions were written for some
time, with their inversion of control and ugly workarounds.
The last straw to fix it was sync --content, which didn't fit the
Annex [CommandStart] interface well at all. I have not yet made it take
advantage of the changed interface though.
The crucial change, and probably why I didn't do it this way from the
beginning, is to make each CommandStart action be run with exceptions
caught, and if it fails, increment a failure counter in annex state.
So I finally remove the very first code I wrote for git-annex, which
was before I had exception handling in the Annex monad, and so ran outside
that monad, passing state explicitly as it ran each CommandStart action.
This was a real slog from 1 to 5 am.
Test suite passes.
Memory usage is lower than before, sometimes by a couple of megabytes, and
remains constant, even when running in a large repo, and even when
repeatedly failing and incrementing the error counter. So no accidental
laziness space leaks.
Wall clock speed is identical, even in large repos.
This commit was sponsored by an anonymous bitcoiner.
2014-01-20 08:11:42 +00:00
|
|
|
{- Runs a command, starting with the check stage, and then
|
2014-12-30 16:49:34 +00:00
|
|
|
- the seek stage. Finishes by running the continutation, and
|
|
|
|
- then showing a count of any failures. -}
|
2015-07-08 16:33:27 +00:00
|
|
|
performCommandAction :: Command -> CommandSeek -> Annex () -> Annex ()
|
|
|
|
performCommandAction Command { cmdcheck = c, cmdname = name } seek cont = do
|
fix inversion of control in CommandSeek (no behavior changes)
I've been disliking how the command seek actions were written for some
time, with their inversion of control and ugly workarounds.
The last straw to fix it was sync --content, which didn't fit the
Annex [CommandStart] interface well at all. I have not yet made it take
advantage of the changed interface though.
The crucial change, and probably why I didn't do it this way from the
beginning, is to make each CommandStart action be run with exceptions
caught, and if it fails, increment a failure counter in annex state.
So I finally remove the very first code I wrote for git-annex, which
was before I had exception handling in the Annex monad, and so ran outside
that monad, passing state explicitly as it ran each CommandStart action.
This was a real slog from 1 to 5 am.
Test suite passes.
Memory usage is lower than before, sometimes by a couple of megabytes, and
remains constant, even when running in a large repo, and even when
repeatedly failing and incrementing the error counter. So no accidental
laziness space leaks.
Wall clock speed is identical, even in large repos.
This commit was sponsored by an anonymous bitcoiner.
2014-01-20 08:11:42 +00:00
|
|
|
mapM_ runCheck c
|
|
|
|
Annex.changeState $ \s -> s { Annex.errcounter = 0 }
|
2015-07-08 16:33:27 +00:00
|
|
|
seek
|
2015-04-10 21:08:07 +00:00
|
|
|
finishCommandActions
|
2014-12-30 16:49:34 +00:00
|
|
|
cont
|
fix inversion of control in CommandSeek (no behavior changes)
I've been disliking how the command seek actions were written for some
time, with their inversion of control and ugly workarounds.
The last straw to fix it was sync --content, which didn't fit the
Annex [CommandStart] interface well at all. I have not yet made it take
advantage of the changed interface though.
The crucial change, and probably why I didn't do it this way from the
beginning, is to make each CommandStart action be run with exceptions
caught, and if it fails, increment a failure counter in annex state.
So I finally remove the very first code I wrote for git-annex, which
was before I had exception handling in the Annex monad, and so ran outside
that monad, passing state explicitly as it ran each CommandStart action.
This was a real slog from 1 to 5 am.
Test suite passes.
Memory usage is lower than before, sometimes by a couple of megabytes, and
remains constant, even when running in a large repo, and even when
repeatedly failing and incrementing the error counter. So no accidental
laziness space leaks.
Wall clock speed is identical, even in large repos.
This commit was sponsored by an anonymous bitcoiner.
2014-01-20 08:11:42 +00:00
|
|
|
showerrcount =<< Annex.getState Annex.errcounter
|
|
|
|
where
|
|
|
|
showerrcount 0 = noop
|
2016-11-16 01:29:54 +00:00
|
|
|
showerrcount cnt = giveup $ name ++ ": " ++ show cnt ++ " failed"
|
fix inversion of control in CommandSeek (no behavior changes)
I've been disliking how the command seek actions were written for some
time, with their inversion of control and ugly workarounds.
The last straw to fix it was sync --content, which didn't fit the
Annex [CommandStart] interface well at all. I have not yet made it take
advantage of the changed interface though.
The crucial change, and probably why I didn't do it this way from the
beginning, is to make each CommandStart action be run with exceptions
caught, and if it fails, increment a failure counter in annex state.
So I finally remove the very first code I wrote for git-annex, which
was before I had exception handling in the Annex monad, and so ran outside
that monad, passing state explicitly as it ran each CommandStart action.
This was a real slog from 1 to 5 am.
Test suite passes.
Memory usage is lower than before, sometimes by a couple of megabytes, and
remains constant, even when running in a large repo, and even when
repeatedly failing and incrementing the error counter. So no accidental
laziness space leaks.
Wall clock speed is identical, even in large repos.
This commit was sponsored by an anonymous bitcoiner.
2014-01-20 08:11:42 +00:00
|
|
|
|
|
|
|
{- Runs one of the actions needed to perform a command.
|
|
|
|
- Individual actions can fail without stopping the whole command,
|
2018-11-15 19:59:43 +00:00
|
|
|
- including by throwing non-async exceptions.
|
fix inversion of control in CommandSeek (no behavior changes)
I've been disliking how the command seek actions were written for some
time, with their inversion of control and ugly workarounds.
The last straw to fix it was sync --content, which didn't fit the
Annex [CommandStart] interface well at all. I have not yet made it take
advantage of the changed interface though.
The crucial change, and probably why I didn't do it this way from the
beginning, is to make each CommandStart action be run with exceptions
caught, and if it fails, increment a failure counter in annex state.
So I finally remove the very first code I wrote for git-annex, which
was before I had exception handling in the Annex monad, and so ran outside
that monad, passing state explicitly as it ran each CommandStart action.
This was a real slog from 1 to 5 am.
Test suite passes.
Memory usage is lower than before, sometimes by a couple of megabytes, and
remains constant, even when running in a large repo, and even when
repeatedly failing and incrementing the error counter. So no accidental
laziness space leaks.
Wall clock speed is identical, even in large repos.
This commit was sponsored by an anonymous bitcoiner.
2014-01-20 08:11:42 +00:00
|
|
|
-
|
2015-04-10 21:08:07 +00:00
|
|
|
- When concurrency is enabled, a thread is forked off to run the action
|
|
|
|
- in the background, as soon as a free slot is available.
|
|
|
|
|
|
|
|
- This should only be run in the seek stage.
|
|
|
|
-}
|
|
|
|
commandAction :: CommandStart -> Annex ()
|
2019-05-10 17:24:31 +00:00
|
|
|
commandAction a = Annex.getState Annex.concurrency >>= \case
|
|
|
|
NonConcurrent -> run
|
|
|
|
Concurrent n -> runconcurrent n
|
|
|
|
ConcurrentPerCpu -> runconcurrent =<< liftIO getNumProcessors
|
2015-04-10 21:08:07 +00:00
|
|
|
where
|
2019-05-10 17:24:31 +00:00
|
|
|
run = void $ includeCommandAction a
|
|
|
|
|
|
|
|
runconcurrent n = do
|
2015-04-10 21:08:07 +00:00
|
|
|
ws <- Annex.getState Annex.workers
|
|
|
|
(st, ws') <- if null ws
|
|
|
|
then do
|
2017-05-25 22:27:01 +00:00
|
|
|
-- Generate the remote list now, to avoid
|
|
|
|
-- each thread generating it, which would
|
|
|
|
-- be more expensive and could cause
|
|
|
|
-- threads to contend over eg, calls to
|
|
|
|
-- setConfig.
|
|
|
|
_ <- remoteList
|
2015-04-10 21:53:58 +00:00
|
|
|
st <- dupState
|
2015-04-10 21:08:07 +00:00
|
|
|
return (st, replicate (n-1) (Left st))
|
|
|
|
else do
|
|
|
|
l <- liftIO $ drainTo (n-1) ws
|
|
|
|
findFreeSlot l
|
2015-11-04 20:19:00 +00:00
|
|
|
w <- liftIO $ async
|
2016-09-09 16:57:42 +00:00
|
|
|
$ snd <$> Annex.run st (inOwnConsoleRegion (Annex.output st) run)
|
2015-04-10 21:08:07 +00:00
|
|
|
Annex.changeState $ \s -> s { Annex.workers = Right w:ws' }
|
|
|
|
|
2018-10-01 18:12:06 +00:00
|
|
|
commandActions :: [CommandStart] -> Annex ()
|
|
|
|
commandActions = mapM_ commandAction
|
|
|
|
|
2015-04-10 21:08:07 +00:00
|
|
|
{- Waits for any forked off command actions to finish.
|
|
|
|
-
|
|
|
|
- Merge together the cleanup actions of all the AnnexStates used by
|
|
|
|
- threads, into the current Annex's state, so they'll run at shutdown.
|
|
|
|
-
|
|
|
|
- Also merge together the errcounters of the AnnexStates.
|
|
|
|
-}
|
|
|
|
finishCommandActions :: Annex ()
|
|
|
|
finishCommandActions = do
|
2015-11-05 21:22:45 +00:00
|
|
|
ws <- Annex.getState Annex.workers
|
|
|
|
Annex.changeState $ \s -> s { Annex.workers = [] }
|
|
|
|
l <- liftIO $ drainTo 0 ws
|
2015-04-10 21:53:58 +00:00
|
|
|
forM_ (lefts l) mergeState
|
2015-04-10 21:08:07 +00:00
|
|
|
|
|
|
|
{- Wait for Asyncs from the list to finish, replacing them with their
|
|
|
|
- final AnnexStates, until the list of remaining Asyncs is not larger
|
|
|
|
- than the specified size, then returns the new list.
|
|
|
|
-
|
|
|
|
- If the action throws an exception, it is propigated, but first
|
|
|
|
- all other actions are waited for, to allow for a clean shutdown.
|
|
|
|
-}
|
|
|
|
drainTo
|
|
|
|
:: Int
|
|
|
|
-> [Either Annex.AnnexState (Async Annex.AnnexState)]
|
|
|
|
-> IO [Either Annex.AnnexState (Async Annex.AnnexState)]
|
|
|
|
drainTo sz l
|
|
|
|
| null as || sz >= length as = return l
|
|
|
|
| otherwise = do
|
|
|
|
(done, ret) <- waitAnyCatch as
|
|
|
|
let as' = filter (/= done) as
|
|
|
|
case ret of
|
|
|
|
Left e -> do
|
|
|
|
void $ drainTo 0 (map Left sts ++ map Right as')
|
|
|
|
throwIO e
|
|
|
|
Right st -> do
|
|
|
|
drainTo sz $ map Left (st:sts) ++ map Right as'
|
|
|
|
where
|
|
|
|
(sts, as) = partitionEithers l
|
|
|
|
|
|
|
|
findFreeSlot :: [Either Annex.AnnexState (Async Annex.AnnexState)] -> Annex (Annex.AnnexState, [Either Annex.AnnexState (Async Annex.AnnexState)])
|
|
|
|
findFreeSlot = go []
|
|
|
|
where
|
|
|
|
go c [] = do
|
2015-04-10 21:53:58 +00:00
|
|
|
st <- dupState
|
2015-04-10 21:08:07 +00:00
|
|
|
return (st, c)
|
|
|
|
go c (Left st:rest) = return (st, c ++ rest)
|
|
|
|
go c (v:rest) = go (v:c) rest
|
|
|
|
|
|
|
|
{- Like commandAction, but without the concurrency. -}
|
|
|
|
includeCommandAction :: CommandStart -> CommandCleanup
|
2018-11-15 19:59:43 +00:00
|
|
|
includeCommandAction a = account =<< tryNonAsync (callCommandAction a)
|
fix inversion of control in CommandSeek (no behavior changes)
I've been disliking how the command seek actions were written for some
time, with their inversion of control and ugly workarounds.
The last straw to fix it was sync --content, which didn't fit the
Annex [CommandStart] interface well at all. I have not yet made it take
advantage of the changed interface though.
The crucial change, and probably why I didn't do it this way from the
beginning, is to make each CommandStart action be run with exceptions
caught, and if it fails, increment a failure counter in annex state.
So I finally remove the very first code I wrote for git-annex, which
was before I had exception handling in the Annex monad, and so ran outside
that monad, passing state explicitly as it ran each CommandStart action.
This was a real slog from 1 to 5 am.
Test suite passes.
Memory usage is lower than before, sometimes by a couple of megabytes, and
remains constant, even when running in a large repo, and even when
repeatedly failing and incrementing the error counter. So no accidental
laziness space leaks.
Wall clock speed is identical, even in large repos.
This commit was sponsored by an anonymous bitcoiner.
2014-01-20 08:11:42 +00:00
|
|
|
where
|
unify exception handling into Utility.Exception
Removed old extensible-exceptions, only needed for very old ghc.
Made webdav use Utility.Exception, to work after some changes in DAV's
exception handling.
Removed Annex.Exception. Mostly this was trivial, but note that
tryAnnex is replaced with tryNonAsync and catchAnnex replaced with
catchNonAsync. In theory that could be a behavior change, since the former
caught all exceptions, and the latter don't catch async exceptions.
However, in practice, nothing in the Annex monad uses async exceptions.
Grepping for throwTo and killThread only find stuff in the assistant,
which does not seem related.
Command.Add.undo is changed to accept a SomeException, and things
that use it for rollback now catch non-async exceptions, rather than
only IOExceptions.
2014-08-08 01:55:44 +00:00
|
|
|
account (Right True) = return True
|
|
|
|
account (Right False) = incerr
|
2018-11-19 17:18:08 +00:00
|
|
|
account (Left err) = case fromException err of
|
|
|
|
Just exitcode -> liftIO $ exitWith exitcode
|
|
|
|
Nothing -> do
|
|
|
|
toplevelWarning True (show err)
|
|
|
|
implicitMessage showEndFail
|
|
|
|
incerr
|
2014-01-20 17:31:03 +00:00
|
|
|
incerr = do
|
2015-04-30 19:04:01 +00:00
|
|
|
Annex.incError
|
2014-01-20 17:31:03 +00:00
|
|
|
return False
|
fix inversion of control in CommandSeek (no behavior changes)
I've been disliking how the command seek actions were written for some
time, with their inversion of control and ugly workarounds.
The last straw to fix it was sync --content, which didn't fit the
Annex [CommandStart] interface well at all. I have not yet made it take
advantage of the changed interface though.
The crucial change, and probably why I didn't do it this way from the
beginning, is to make each CommandStart action be run with exceptions
caught, and if it fails, increment a failure counter in annex state.
So I finally remove the very first code I wrote for git-annex, which
was before I had exception handling in the Annex monad, and so ran outside
that monad, passing state explicitly as it ran each CommandStart action.
This was a real slog from 1 to 5 am.
Test suite passes.
Memory usage is lower than before, sometimes by a couple of megabytes, and
remains constant, even when running in a large repo, and even when
repeatedly failing and incrementing the error counter. So no accidental
laziness space leaks.
Wall clock speed is identical, even in large repos.
This commit was sponsored by an anonymous bitcoiner.
2014-01-20 08:11:42 +00:00
|
|
|
|
2014-01-20 17:31:03 +00:00
|
|
|
{- Runs a single command action through the start, perform and cleanup
|
|
|
|
- stages, without catching errors. Useful if one command wants to run
|
|
|
|
- part of another command. -}
|
2015-04-10 21:08:07 +00:00
|
|
|
callCommandAction :: CommandStart -> CommandCleanup
|
2016-01-19 21:46:46 +00:00
|
|
|
callCommandAction = fromMaybe True <$$> callCommandAction'
|
|
|
|
|
|
|
|
{- Like callCommandAction, but returns Nothing when the command did not
|
|
|
|
- perform any action. -}
|
|
|
|
callCommandAction' :: CommandStart -> Annex (Maybe Bool)
|
2017-12-28 21:29:38 +00:00
|
|
|
callCommandAction' a = callCommandActionQuiet a >>= \case
|
|
|
|
Nothing -> return Nothing
|
|
|
|
Just r -> implicitMessage (showEndResult r) >> return (Just r)
|
|
|
|
|
|
|
|
callCommandActionQuiet :: CommandStart -> Annex (Maybe Bool)
|
|
|
|
callCommandActionQuiet = start
|
fix inversion of control in CommandSeek (no behavior changes)
I've been disliking how the command seek actions were written for some
time, with their inversion of control and ugly workarounds.
The last straw to fix it was sync --content, which didn't fit the
Annex [CommandStart] interface well at all. I have not yet made it take
advantage of the changed interface though.
The crucial change, and probably why I didn't do it this way from the
beginning, is to make each CommandStart action be run with exceptions
caught, and if it fails, increment a failure counter in annex state.
So I finally remove the very first code I wrote for git-annex, which
was before I had exception handling in the Annex monad, and so ran outside
that monad, passing state explicitly as it ran each CommandStart action.
This was a real slog from 1 to 5 am.
Test suite passes.
Memory usage is lower than before, sometimes by a couple of megabytes, and
remains constant, even when running in a large repo, and even when
repeatedly failing and incrementing the error counter. So no accidental
laziness space leaks.
Wall clock speed is identical, even in large repos.
This commit was sponsored by an anonymous bitcoiner.
2014-01-20 08:11:42 +00:00
|
|
|
where
|
|
|
|
start = stage $ maybe skip perform
|
|
|
|
perform = stage $ maybe failure cleanup
|
|
|
|
cleanup = stage $ status
|
|
|
|
stage = (=<<)
|
2016-01-19 21:46:46 +00:00
|
|
|
skip = return Nothing
|
2017-12-28 21:29:38 +00:00
|
|
|
failure = return (Just False)
|
|
|
|
status = return . Just
|
2015-11-05 21:22:45 +00:00
|
|
|
|
|
|
|
{- Do concurrent output when that has been requested. -}
|
|
|
|
allowConcurrentOutput :: Annex a -> Annex a
|
2018-10-04 16:47:27 +00:00
|
|
|
allowConcurrentOutput a = do
|
|
|
|
fromcmdline <- Annex.getState Annex.concurrency
|
|
|
|
fromgitcfg <- annexJobs <$> Annex.getGitConfig
|
2019-05-10 17:24:31 +00:00
|
|
|
let usegitcfg = Annex.changeState $
|
|
|
|
\c -> c { Annex.concurrency = fromgitcfg }
|
2018-10-04 16:47:27 +00:00
|
|
|
case (fromcmdline, fromgitcfg) of
|
|
|
|
(NonConcurrent, NonConcurrent) -> a
|
2019-05-10 17:24:31 +00:00
|
|
|
(Concurrent n, _) -> do
|
|
|
|
raisecapabilitiesto n
|
|
|
|
goconcurrent
|
|
|
|
(ConcurrentPerCpu, _) -> goconcurrent
|
2018-10-04 16:47:27 +00:00
|
|
|
(NonConcurrent, Concurrent n) -> do
|
2019-05-10 17:24:31 +00:00
|
|
|
usegitcfg
|
|
|
|
raisecapabilitiesto n
|
|
|
|
goconcurrent
|
|
|
|
(NonConcurrent, ConcurrentPerCpu) -> do
|
|
|
|
usegitcfg
|
|
|
|
goconcurrent
|
2015-11-05 21:22:45 +00:00
|
|
|
where
|
2019-05-10 17:24:31 +00:00
|
|
|
goconcurrent = do
|
2018-11-15 18:26:40 +00:00
|
|
|
withMessageState $ \s -> case outputType s of
|
|
|
|
NormalOutput -> ifM (liftIO concurrentOutputSupported)
|
|
|
|
( Regions.displayConsoleRegions $
|
|
|
|
goconcurrent' True
|
|
|
|
, goconcurrent' False
|
|
|
|
)
|
|
|
|
_ -> goconcurrent' False
|
2018-10-04 16:47:27 +00:00
|
|
|
goconcurrent' b = bracket_ (setup b) cleanup a
|
2019-05-10 17:24:31 +00:00
|
|
|
|
2018-10-04 16:47:27 +00:00
|
|
|
setup = setconcurrentoutputenabled
|
2019-05-10 17:24:31 +00:00
|
|
|
|
2015-11-05 21:22:45 +00:00
|
|
|
cleanup = do
|
|
|
|
finishCommandActions
|
2018-10-04 16:47:27 +00:00
|
|
|
setconcurrentoutputenabled False
|
2019-05-10 17:24:31 +00:00
|
|
|
|
2018-10-04 16:47:27 +00:00
|
|
|
setconcurrentoutputenabled b = Annex.changeState $ \s ->
|
2016-09-09 16:57:42 +00:00
|
|
|
s { Annex.output = (Annex.output s) { concurrentOutputEnabled = b } }
|
2017-10-17 21:54:38 +00:00
|
|
|
|
2019-05-10 17:24:31 +00:00
|
|
|
raisecapabilitiesto n = do
|
|
|
|
c <- liftIO getNumCapabilities
|
|
|
|
when (n > c) $
|
|
|
|
liftIO $ setNumCapabilities n
|
|
|
|
|
2017-10-17 21:54:38 +00:00
|
|
|
{- Ensures that only one thread processes a key at a time.
|
|
|
|
- Other threads will block until it's done. -}
|
|
|
|
onlyActionOn :: Key -> CommandStart -> CommandStart
|
|
|
|
onlyActionOn k a = onlyActionOn' k run
|
|
|
|
where
|
2017-12-05 19:00:50 +00:00
|
|
|
-- Run whole action, not just start stage, so other threads
|
|
|
|
-- block until it's done.
|
2017-12-28 21:29:38 +00:00
|
|
|
run = callCommandActionQuiet a >>= \case
|
2017-12-05 19:00:50 +00:00
|
|
|
Nothing -> return Nothing
|
|
|
|
Just r' -> return $ Just $ return $ Just $ return r'
|
2017-10-17 21:54:38 +00:00
|
|
|
|
|
|
|
onlyActionOn' :: Key -> Annex a -> Annex a
|
|
|
|
onlyActionOn' k a = go =<< Annex.getState Annex.concurrency
|
|
|
|
where
|
|
|
|
go NonConcurrent = a
|
2019-05-10 17:24:31 +00:00
|
|
|
go (Concurrent _) = goconcurrent
|
|
|
|
go ConcurrentPerCpu = goconcurrent
|
|
|
|
goconcurrent = do
|
2017-10-17 21:54:38 +00:00
|
|
|
tv <- Annex.getState Annex.activekeys
|
|
|
|
bracket (setup tv) id (const a)
|
|
|
|
setup tv = liftIO $ do
|
|
|
|
mytid <- myThreadId
|
|
|
|
atomically $ do
|
|
|
|
m <- readTVar tv
|
|
|
|
case M.lookup k m of
|
|
|
|
Just tid
|
|
|
|
| tid /= mytid -> retry
|
|
|
|
| otherwise -> return (return ())
|
|
|
|
Nothing -> do
|
|
|
|
writeTVar tv $! M.insert k mytid m
|
|
|
|
return $ liftIO $ atomically $
|
|
|
|
modifyTVar tv $ M.delete k
|