sim: added run subcommand
authorJoey Hess <joeyh@joeyh.name>
Tue, 24 Sep 2024 16:01:54 +0000 (12:01 -0400)
committerJoey Hess <joeyh@joeyh.name>
Tue, 24 Sep 2024 16:06:34 +0000 (12:06 -0400)
And a nice sim of random preferred content expressions.

Annex/Sim.hs
Command/Sim.hs
doc/git-annex-sim.mdwn
doc/sims/randomwanted.mdwn [new file with mode: 0644]

index d052bad7636549f0679ea23193bfc389b5f32df7..42c3b8d4371a995cdd05e3b20fb5a6a6dd3f4047 100644 (file)
@@ -531,7 +531,7 @@ applySimCommand' (CommandNotPresent _ _) _ _ = error "applySimCommand' CommandNo
 
 handleStep :: Bool -> Int -> Int -> SimState SimRepo -> Annex (SimState SimRepo)
 handleStep muststabilize startn n st
-       | n > 0 = do
+       | n >= 0 = do
                let (st', actions) = getactions unsyncactions st
                (st'', restactions) <- runoneaction actions st'
                if null restactions
@@ -550,7 +550,7 @@ handleStep muststabilize startn n st
        | otherwise = checkstabalized st
   where
        runrest actions st' n'
-               | n' > 0 = do
+               | n' >= 0 = do
                        (st'', restactions) <- runoneaction actions st'
                        if null restactions
                                then handleStep muststabilize startn n' st'
index 56825773215566c158d1380a81b282027cb3bbf7..f14142b069a116a672dfad9b8ae2c046c7fa3276 100644 (file)
@@ -22,19 +22,21 @@ cmd = command "sim" SectionTesting
        paramCommand (withParams seek)
 
 seek :: CmdParams -> CommandSeek
-seek ("start":[]) = start Nothing
-seek ("start":simfile:[]) = start (Just simfile)
-seek ("end":[]) = do
-       simdir <- fromRawFilePath <$> fromRepo gitAnnexSimDir
-       whenM (liftIO $ doesDirectoryExist simdir) $ do
-               liftIO $ removeDirectoryRecursive simdir
-       showLongNote $ UnquotedString "Sim ended."
+seek ("start":[]) = startsim Nothing
+seek ("start":simfile:[]) = startsim (Just simfile)
+seek ("end":[]) = endsim
 seek ("show":[]) = do
        simdir <- fromRepo gitAnnexSimDir
        liftIO (restoreSim simdir) >>= \case
                Left err -> giveup err
-               Right st -> liftIO $ putStr $ generateSimFile $
-                       reverse $ simHistory st
+               Right st -> showsim st
+seek ("run":simfile:[]) = startsim' (Just simfile) >>= cleanup
+  where
+       cleanup st = do 
+               endsim
+               when (simFailed st) $ do
+                       showsim st
+                       giveup "Simulation shown above had errors."
 seek ps = case parseSimCommand ps of
        Left err -> giveup err
        Right simcmd -> do
@@ -48,8 +50,16 @@ seek ps = case parseSimCommand ps of
                                when (simFailed st' && not (simFailed st)) $
                                        giveup "Simulation had errors."
 
-start :: Maybe FilePath -> CommandSeek
-start simfile = do
+startsim :: Maybe FilePath -> CommandSeek
+startsim simfile = startsim' simfile >>= cleanup
+  where
+       cleanup st = do
+               liftIO $ suspendSim st
+               when (simFailed st) $
+                       giveup "Simulation had errors."
+
+startsim' :: Maybe FilePath -> Annex (SimState SimRepo)
+startsim' simfile = do
        simdir <- fromRawFilePath <$> fromRepo gitAnnexSimDir
        whenM (liftIO $ doesDirectoryExist simdir) $
                giveup "A sim was previously started. Use `git-annex sim end` to stop it before starting a new one."
@@ -68,12 +78,19 @@ start simfile = do
                repobyname <- mkGetExistingRepoByName
                createAnnexDirectory (toRawFilePath simdir)
                let st' = recordSeed st cs
-               st'' <- go st' repobyname cs
-               liftIO $ suspendSim st''
-               when (simFailed st'') $
-                       giveup "Simulation had errors."
+               go st' repobyname cs
 
        go st _ [] = return st
        go st repobyname (c:cs) = do
                st' <- runSimCommand c repobyname st
                go st' repobyname cs
+       
+endsim :: CommandSeek
+endsim = do
+       simdir <- fromRawFilePath <$> fromRepo gitAnnexSimDir
+       whenM (liftIO $ doesDirectoryExist simdir) $ do
+               liftIO $ removeDirectoryRecursive simdir
+       showLongNote $ UnquotedString "Sim ended."
+               
+showsim :: SimState SimRepo -> Annex ()
+showsim = liftIO . putStr . generateSimFile . reverse . simHistory
index 665e91fa63552969e2168958380b3ceb2545c5cb..09505ee9d4a35abda6f81adb760f526cb1c94c5c 100644 (file)
@@ -12,6 +12,8 @@ git annex sim show
 
 git annex sim end
 
+git annex sim run my.sim
+
 # DESCRIPTION
 
 This command simulates the behavior of git-annex in a network of
@@ -32,6 +34,10 @@ as well as analyzing the results of the simulation.
 
 Use "git annex sim end" to finish the simulation, and clean up.
 
+As a convenience, to run a sim from a file, and then stop it, use
+"git-annex sim run". If there is a problem running the sim, it will be
+shown before it is stopped.
+
 # THE SIM FILE
 
 This text file is used to configure the simulation and also to report on
diff --git a/doc/sims/randomwanted.mdwn b/doc/sims/randomwanted.mdwn
new file mode 100644 (file)
index 0000000..4896122
--- /dev/null
@@ -0,0 +1,13 @@
+# This is a simulation of random preferred content expressions.  
+# git-annex sim run this in a loop to explore if an expression can fail to
+# stabilize
+init foo  
+init bar  
+connect foo <-> bar  
+addmulti 10 .x 100.0kB 10.0MB foo  
+addmulti 10 .y 100.0kB 10.0MB foo  
+randomwanted bar largerthan=1MB include=*.x anything present  
+randomwanted foo largerthan=1MB include=*.x anything present  
+# 40 is the maximum possible steps, in case bar wants to get all 20 files,  
+# and foo wants to drop them all  
+stepstable 40