#define DISABLED
module HSBencher.Internal.App
(defaultMainWithBechmarks, defaultMainModifyConfig,
Flag(..), all_cli_options, fullUsageInfo)
where
import Prelude hiding (log)
import Control.Applicative
import Control.Concurrent
import Control.Monad.Reader
import Control.Exception (evaluate, handle, SomeException, throwTo, fromException, AsyncException(ThreadKilled),try)
import Debug.Trace
import Data.Time.Clock (getCurrentTime, diffUTCTime)
import Data.Time.Clock.POSIX (utcTimeToPOSIXSeconds)
import Data.Maybe (isJust, fromJust, catMaybes, fromMaybe)
import Data.Monoid
import Data.Dynamic
import qualified Data.Map as M
import qualified Data.Set as S
import Data.Word (Word64)
import Data.IORef
import Data.List (intercalate, sortBy, intersperse, isPrefixOf, tails, isInfixOf, delete)
import qualified Data.Set as Set
import Data.Version (versionBranch, versionTags)
import GHC.Conc (getNumProcessors)
import Numeric (showFFloat)
import System.Console.GetOpt (getOpt, getOpt', ArgOrder(Permute), OptDescr(Option), ArgDescr(..), usageInfo)
import System.Environment (getArgs, getEnv, getEnvironment, getProgName)
import System.Directory
import System.Posix.Env (setEnv)
import System.Random (randomIO)
import System.Exit
import System.FilePath (splitFileName, (</>), takeDirectory)
import System.Process (system, waitForProcess, getProcessExitCode, runInteractiveCommand,
createProcess, CreateProcess(..), CmdSpec(..), StdStream(..), readProcess)
import System.IO (Handle, hPutStrLn, stderr, openFile, hClose, hGetContents, hIsEOF, hGetLine,
IOMode(..), BufferMode(..), hSetBuffering)
import System.IO.Unsafe (unsafePerformIO)
import qualified Data.ByteString.Char8 as B
import Text.Printf
import Text.PrettyPrint.GenericPretty (Out(doc))
import qualified System.IO.Streams as Strm
import qualified System.IO.Streams.Concurrent as Strm
import qualified System.IO.Streams.Process as Strm
import qualified System.IO.Streams.Combinators as Strm
#ifdef USE_HYDRAPRINT
import UI.HydraPrint (hydraPrint, HydraConf(..), DeleteWinWhen(..), defaultHydraConf, hydraPrintStatic)
import Scripting.Parallel.ThreadPool (parForM)
#endif
import HSBencher.Types
import HSBencher.Internal.Utils
import HSBencher.Internal.Logging
import HSBencher.Internal.Config
import HSBencher.Methods.Builtin
import HSBencher.Internal.MeasureProcess
import Paths_hsbencher (version)
hsbencherVersion :: String
hsbencherVersion = concat $ intersperse "." $ map show $
versionBranch version
generalUsageStr :: String
generalUsageStr = unlines $
[
" ",
" Note: This bench harness was built against hsbencher library version "++hsbencherVersion
]
gc_stats_flag :: String
gc_stats_flag = " -s "
exedir :: String
exedir = "./bin"
pruneThreadedOpts :: [String] -> [String]
pruneThreadedOpts = filter (`notElem` ["-qa", "-qb"])
path :: [FilePath] -> FilePath
path [] = ""
path ls = foldl1 (</>) ls
compileOne :: (Int,Int) -> Benchmark DefaultParamMeaning -> [(DefaultParamMeaning,ParamSetting)] -> BenchM BuildResult
compileOne (iterNum,totalIters) Benchmark{target=testPath,cmdargs} cconf = do
Config{shortrun, resultsOut, stdOut, buildMethods, pathRegistry, doClean} <- ask
let (diroffset,testRoot) = splitFileName testPath
flags = toCompileFlags cconf
paths = toCmdPaths cconf
bldid = makeBuildID testPath flags
log "\n--------------------------------------------------------------------------------"
log$ " Compiling Config "++show iterNum++" of "++show totalIters++
": "++testRoot++" (args \""++unwords cmdargs++"\") confID "++ show bldid
log "--------------------------------------------------------------------------------\n"
matches <- lift$
filterM (fmap isJust . (`filePredCheck` testPath) . canBuild) buildMethods
when (null matches) $ do
logT$ "ERROR, no build method matches path: "++testPath
logT$ " Tried methods: "++show(map methodName buildMethods)
logT$ " With file preds: "
forM buildMethods $ \ meth ->
logT$ " "++ show (canBuild meth)
lift exitFailure
logT$ printf "Found %d methods that can handle %s: %s"
(length matches) testPath (show$ map methodName matches)
let BuildMethod{methodName,clean,compile,concurrentBuild} = head matches
when (length matches > 1) $
logT$ " WARNING: resolving ambiguity, picking method: "++methodName
let pathR = (M.union (M.fromList paths) pathRegistry)
when doClean $ clean pathR bldid testPath
x <- compile pathR bldid flags testPath
logT$ "Compile finished, result: "++ show x
return x
runOne :: (Int,Int) -> BuildID -> BuildResult -> Benchmark DefaultParamMeaning -> [(DefaultParamMeaning,ParamSetting)] -> BenchM ()
runOne (iterNum, totalIters) _bldid bldres
Benchmark{target=testPath, cmdargs=args_, progname, benchTimeOut}
runconfig = do
let numthreads = foldl (\ acc (x,_) ->
case x of
Threads n -> n
_ -> acc)
0 runconfig
sched = foldl (\ acc (x,_) ->
case x of
Variant s -> s
_ -> acc)
"none" runconfig
let runFlags = toRunFlags runconfig
envVars = toEnvVars runconfig
conf@Config{ runTimeOut, trials, shortrun, argsBeforeFlags, harvesters } <- ask
let args = if shortrun then shortArgs args_ else args_
fullargs = if argsBeforeFlags
then args ++ runFlags
else runFlags ++ args
testRoot = fetchBaseName testPath
log$ "\n--------------------------------------------------------------------------------"
log$ " Running Config "++show iterNum++" of "++show totalIters ++": "++testPath
log$ nest 3 $ show$ doc$ map snd runconfig
log$ "--------------------------------------------------------------------------------\n"
pwd <- lift$ getCurrentDirectory
logT$ "(In directory "++ pwd ++")"
logT$ "Next run 'who', reporting users other than the current user. This may help with detectivework."
whos <- lift$ runLines$ "who"
let whos' = map ((\ (h:_)->h) . words) whos
user <- lift$ getEnv "USER"
logT$ "Who_Output: "++ unwords (filter (/= user) whos')
nruns <- forM [1..trials] $ \ i -> do
log$ printf " Running trial %d of %d" i trials
log " ------------------------"
let doMeasure cmddescr = do
SubProcess {wait,process_out,process_err} <-
lift$ measureProcess harvesters cmddescr
err2 <- lift$ Strm.map (B.append " [stderr] ") process_err
both <- lift$ Strm.concurrentMerge [process_out, err2]
mv <- echoStream (not shortrun) both
lift$ takeMVar mv
x <- lift wait
return x
case bldres of
StandAloneBinary binpath -> do
let command = binpath++" "++unwords fullargs
logT$ " Executing command: " ++ command
let timeout = if benchTimeOut == Nothing
then runTimeOut
else benchTimeOut
case timeout of
Just t -> logT$ " Setting timeout: " ++ show t
Nothing -> return ()
doMeasure CommandDescr{ command=ShellCommand command, envVars, timeout, workingDir=Nothing }
RunInPlace fn -> do
let cmd = fn fullargs envVars
logT$ " Generated in-place run command: "++show cmd
doMeasure cmd
let pads n s = take (max 1 (n length s)) $ repeat ' '
padl n x = pads n x ++ x
padr n x = x ++ pads n x
let thename = case progname of
Just s -> s
Nothing -> testRoot
(_t1,_t2,_t3,_p1,_p2,_p3) <-
if all isError nruns then do
log $ "\n >>> MIN/MEDIAN/MAX (TIME,PROD) -- got only ERRORS: " ++show nruns
logOn [ResultsFile]$
printf "# %s %s %s %s %s" (padr 35 thename) (padr 20$ intercalate "_" args)
(padr 8$ sched) (padr 3$ show numthreads) (" ALL_ERRORS"::String)
return ("","","","","","")
else do
let goodruns = filter (not . isError) nruns
sorted = sortBy (\ a b -> compare (gettime a) (gettime b)) goodruns
minR = head sorted
maxR = last sorted
medianR = sorted !! (length sorted `quot` 2)
let ts@[t1,t2,t3] = map (\x -> showFFloat Nothing x "")
[gettime minR, gettime medianR, gettime maxR]
prods@[p1,p2,p3] = map mshow [getprod minR, getprod medianR, getprod maxR]
mshow Nothing = "0"
mshow (Just x) = showFFloat (Just 2) x ""
formatted = (padl 15$ unwords $ ts)
++" "++ unwords prods
log $ "\n >>> MIN/MEDIAN/MAX (TIME,PROD) " ++ formatted
logOn [ResultsFile]$
printf "%s %s %s %s %s" (padr 35 thename) (padr 20$ intercalate "_" args)
(padr 8$ sched) (padr 3$ show numthreads) formatted
let jittimes0 = map getjittime goodruns
misses = length (filter (==Nothing) jittimes0)
jittimes <- if misses == length goodruns
then return ""
else if misses == 0
then return $ unwords (map (show . fromJust) jittimes0)
else do log $ "WARNING: got JITTIME for some runs: "++show jittimes0
log " Zeroing those that did not report."
return $ unwords (map (show . fromMaybe 0) jittimes0)
let result =
emptyBenchmarkResult
{ _PROGNAME = case progname of
Just s -> s
Nothing -> testRoot
, _VARIANT = sched
, _ARGS = args
, _THREADS = numthreads
, _MINTIME = gettime minR
, _MEDIANTIME = gettime medianR
, _MAXTIME = gettime maxR
, _MINTIME_PRODUCTIVITY = getprod minR
, _MEDIANTIME_PRODUCTIVITY = getprod medianR
, _MEDIANTIME_ALLOCRATE = getallocrate medianR
, _MEDIANTIME_MEMFOOTPRINT = getmemfootprint medianR
, _MAXTIME_PRODUCTIVITY = getprod maxR
, _RUNTIME_FLAGS = unwords runFlags
, _ALLTIMES = unwords$ map (show . gettime) goodruns
, _ALLJITTIMES = jittimes
, _TRIALS = trials
}
result' <- liftIO$ augmentResultWithConfig conf result
conf2@Config{ plugIns } <- ask
forM_ plugIns $ \ (SomePlugin p) -> do
result <- liftIO$ try (plugUploadRow p conf2 result') :: ReaderT Config IO (Either SomeException ())
case result of
Left _ -> logT$"plugUploadRow:Failed"
Right () -> logT$"plugUploadRow: Successful"
return ()
return (t1,t2,t3,p1,p2,p3)
return ()
printBenchrunHeader :: BenchM ()
printBenchrunHeader = do
Config{trials, maxthreads, pathRegistry,
logOut, resultsOut, stdOut, benchversion, shortrun, gitInfo=(branch,revision,depth) } <- ask
liftIO $ do
let ls :: [IO String]
ls = [ e$ "# TestName Variant NumThreads MinTime MedianTime MaxTime Productivity1 Productivity2 Productivity3"
, e$ "# "
, e$ "# `date`"
, e$ "# `uname -a`"
, e$ "# Ran by: `whoami` "
, e$ "# Determined machine to have "++show maxthreads++" hardware threads."
, e$ "# "
, e$ "# Running each test for "++show trials++" trial(s)."
, e$ "# Git_Branch: " ++ branch
, e$ "# Git_Hash: " ++ revision
, e$ "# Git_Depth: " ++ show depth
, e$ "# Path registry: "++show pathRegistry
]
ls' <- sequence ls
forM_ ls' $ \line -> do
Strm.write (Just$ B.pack line) resultsOut
Strm.write (Just$ B.pack line) logOut
Strm.write (Just$ B.pack line) stdOut
return ()
where
e :: String -> IO String
e s =
runSL ("echo \""++s++"\"")
defaultMain :: IO ()
defaultMain = do
error "FINISHME: defaultMain requires reading benchmark list from a file. Implement it!"
defaultMainWithBechmarks :: [Benchmark DefaultParamMeaning] -> IO ()
defaultMainWithBechmarks benches = do
defaultMainModifyConfig (\ conf -> conf{ benchlist=benches })
fullUsageInfo :: String
fullUsageInfo =
"\nUSAGE: naked command line arguments are patterns that select the benchmarks to run.\n"++
(concat (map (uncurry usageInfo) all_cli_options)) ++
generalUsageStr
removePlugin p cfg =
cfg { plugIns = filter byNom (plugIns cfg)}
where
byNom (SomePlugin p1) = plugName p1 /= plugName p
defaultMainModifyConfig :: (Config -> Config) -> IO ()
defaultMainModifyConfig modConfig = do
id <- myThreadId
writeIORef main_threadid id
my_name <- getProgName
cli_args <- getArgs
let (options,plainargs,_unrec,errs) = getOpt' Permute (concat$ map snd all_cli_options) cli_args
let recomp = null [ () | NoRecomp <- options]
showHelp = not$ null [ () | ShowHelp <- options]
gotVersion = not$ null [ () | ShowVersion <- options]
cabalAllowed = not$ null [ () | NoCabal <- options]
parBench = not$ null [ () | ParBench <- options]
when gotVersion $ do
putStrLn$ "hsbencher version "++ hsbencherVersion
exitSuccess
let printHelp :: [OptDescr ()] -> IO ()
printHelp opts =
error "FINISHME"
putStrLn$ "\n"++hsbencher_tag++"Harvesting environment data to build Config."
conf0 <- getConfig options []
let conf1 = modConfig conf0
let allplugs = plugIns conf1
when (not (null errs) || showHelp) $ do
unless showHelp $ putStrLn$ "Errors parsing command line options:"
mapM_ (putStr . (" "++)) errs
putStrLn$ "\nUSAGE: [set ENV VARS] "++my_name++" [CMDLN OPTS]"
putStrLn$ "\nNote: \"CMDLN OPTS\" includes patterns that select which benchmarks"
putStrLn$ " to run, based on name."
mapM putStr (map (uncurry usageInfo) all_cli_options)
putStrLn ""
forM_ allplugs $ \ (SomePlugin p) -> do
putStrLn $ ((uncurry usageInfo) (plugCmdOpts p))
putStrLn$ generalUsageStr
if showHelp then exitSuccess else exitFailure
let pconfs = [ (plugName p, SomePluginConf p pconf)
| (SomePlugin p) <- (plugIns conf1)
, let (_pusage,popts) = plugCmdOpts p
, let (o2,_,_,_) = getOpt' Permute popts cli_args
, let pconf = foldFlags p o2 (defaultPlugConf p)
]
let conf2 = conf1 { plugInConfs = M.fromList pconfs }
putStrLn$ hsbencher_tag++(show$ length allplugs)++" plugins configured, now initializing them."
conf_final <- foldM (\ cfg (SomePlugin p) ->
do result <- try (plugInitialize p cfg) :: IO (Either SomeException Config)
case result of
Left _ ->
return $ removePlugin p cfg
Right c -> return c
) conf2 allplugs
putStrLn$ hsbencher_tag++" plugin init complete."
let cutlist = case plainargs of
[] -> benchlist conf_final
patterns -> filter (\ Benchmark{target,cmdargs,progname} ->
any (\pat ->
isInfixOf pat target ||
isInfixOf pat (fromMaybe "" progname) ||
any (isInfixOf pat) cmdargs
)
patterns)
(benchlist conf_final)
let conf2@Config{envs,benchlist,stdOut} = conf_final{benchlist=cutlist}
hasMakefile <- doesFileExist "Makefile"
cabalFile <- runLines "ls *.cabal"
let hasCabalFile = (cabalFile /= []) && cabalAllowed
rootDir <- getCurrentDirectory
runReaderT
(do
unless (null plainargs) $ do
let len = (length cutlist)
logT$"There were "++show len++" benchmarks matching patterns: "++show plainargs
when (len == 0) $ do
error$ "Expected at least one pattern to match!. All benchmarks: \n"++
(case conf_final of
Config{benchlist=ls} ->
(unlines [ (target ++ (unwords cmdargs))
| Benchmark{cmdargs,target} <- ls
]))
logT$"Beginning benchmarking, root directory: "++rootDir
let globalBinDir = rootDir </> "bin"
when recomp $ do
logT$"Clearing any preexisting files in ./bin/"
lift$ do
dde <- doesDirectoryExist globalBinDir
when dde $ removeDirectoryRecursive globalBinDir
lift$ createDirectoryIfMissing True globalBinDir
logT "Writing header for result data file:"
printBenchrunHeader
unless recomp $ log "[!!!] Skipping benchmark recompilation!"
let
benches' = map (\ b -> b { configs= compileOptsOnly (configs b) })
benchlist
cccfgs = map (enumerateBenchSpace . configs) benches'
cclengths = map length cccfgs
totalcomps = sum cclengths
log$ "\n--------------------------------------------------------------------------------"
logT$ "Running all benchmarks for all settings ..."
logT$ "Compiling: "++show totalcomps++" total configurations of "++ show (length benchlist)++" benchmarks"
let indent n str = unlines $ map (replicate n ' ' ++) $ lines str
printloop _ [] = return ()
printloop mp (Benchmark{target,cmdargs,configs} :tl) = do
log$ " * Benchmark/args: "++target++" "++show cmdargs
case M.lookup configs mp of
Nothing -> log$ indent 4$ show$ doc configs
Just trg0 -> log$ " ...same config space as "++show trg0
printloop (M.insertWith (\ _ x -> x) configs target mp) tl
printloop M.empty benchlist
log$ "--------------------------------------------------------------------------------"
if parBench then do
unless rtsSupportsBoundThreads $ error (my_name++" was NOT compiled with -threaded. Can't do --par.")
else do
let allruns = map (enumerateBenchSpace . configs) benchlist
allrunsLens = map length allruns
totalruns = sum allrunsLens
let
runloop :: Int
-> M.Map BuildID (Int, Maybe BuildResult)
-> M.Map FilePath BuildID
-> [(Benchmark DefaultParamMeaning, [(DefaultParamMeaning,ParamSetting)])]
-> BenchM ()
runloop _ _ _ [] = return ()
runloop !iter !board !lastConfigured (nextrun:rest) = do
let (bench,params) = nextrun
ccflags = toCompileFlags params
bid = makeBuildID (target bench) ccflags
case M.lookup bid board of
Nothing -> error$ "HSBencher: Internal error: Cannot find entry in map for build ID: "++show bid
Just (ccnum, Nothing) -> do
res <- compileOne (ccnum,totalcomps) bench params
let board' = M.insert bid (ccnum, Just res) board
lastC' = M.insert (target bench) bid lastConfigured
runOne (iter,totalruns) bid res bench params
runloop (iter+1) board' lastC' rest
Just (ccnum, Just bldres) ->
let proceed = do runOne (iter,totalruns) bid bldres bench params
runloop (iter+1) board lastConfigured rest
in
case bldres of
StandAloneBinary _ -> proceed
RunInPlace _ ->
case M.lookup (target bench) lastConfigured of
Nothing -> error$"HSBencher: Internal error, RunInPlace in the board but not lastConfigured!: "
++(target bench)++ " build id "++show bid
Just bid2 ->
if bid == bid2
then do logT$ "Skipping rebuild of in-place benchmark: "++bid
proceed
else runloop iter (M.insert bid (ccnum,Nothing) board) lastConfigured (nextrun:rest)
initBoard _ [] acc = acc
initBoard !iter ((bench,params):rest) acc =
let bid = makeBuildID (target bench) $ toCompileFlags params
base = fetchBaseName (target bench)
dfltdest = globalBinDir </> base ++"_"++bid in
case M.lookup bid acc of
Just _ -> initBoard iter rest acc
Nothing ->
let elm = if recomp
then (iter, Nothing)
else (iter, Just (StandAloneBinary dfltdest))
in
initBoard (iter+1) rest (M.insert bid elm acc)
zippedruns = (concat$ zipWith (\ b cfs -> map (b,) cfs) benchlist allruns)
unless recomp $ logT$ "Recompilation disabled, assuming standalone binaries are in the expected places!"
let startBoard = initBoard 1 zippedruns M.empty
Config{skipTo} <- ask
case skipTo of
Nothing -> runloop 1 startBoard M.empty zippedruns
Just ix -> do logT$" !!! WARNING: SKIPPING AHEAD in configuration space; jumping to: "++show ix
runloop ix startBoard M.empty (drop (ix1) zippedruns)
log$ "\n--------------------------------------------------------------------------------"
log " Finished with all test configurations."
log$ "--------------------------------------------------------------------------------"
liftIO$ exitSuccess
)
conf2
catParallelOutput :: [Strm.InputStream B.ByteString] -> Strm.OutputStream B.ByteString -> IO ()
catParallelOutput strms stdOut = do
case 4 of
#ifdef USE_HYDRAPRINT
1 -> do
hydraPrintStatic defaultHydraConf (zip (map show [1..]) strms)
2 -> do
srcs <- Strm.fromList (zip (map show [1..]) strms)
hydraPrint defaultHydraConf{deleteWhen=Never} srcs
#endif
3 -> do
strms2 <- mapM Strm.lines strms
interleaved <- Strm.concurrentMerge strms2
Strm.connect interleaved stdOut
4 -> do
strms2 <- mapM Strm.lines strms
merged <- Strm.concatInputStreams strms2
Strm.connect merged stdOut
didComplete :: RunResult -> Bool
didComplete RunCompleted{} = True
didComplete _ = False
isError :: RunResult -> Bool
isError ExitError{} = True
isError _ = False
getprod :: RunResult -> Maybe Double
getprod RunCompleted{productivity} = productivity
getprod RunTimeOut{} = Nothing
getprod x = error$"Cannot get productivity from: "++show x
getallocrate :: RunResult -> Maybe Word64
getallocrate RunCompleted{allocRate} = allocRate
getallocrate _ = Nothing
getmemfootprint :: RunResult -> Maybe Word64
getmemfootprint RunCompleted{memFootprint} = memFootprint
getmemfootprint _ = Nothing
gettime :: RunResult -> Double
gettime RunCompleted{realtime} = realtime
gettime RunTimeOut{} = posInf
gettime x = error$"Cannot get realtime from: "++show x
getjittime :: RunResult -> Maybe Double
getjittime RunCompleted{jittime} = jittime
getjittime _ = Nothing
posInf :: Double
posInf = 1/0
shortArgs :: [String] -> [String]
shortArgs _ls = []
nest :: Int -> String -> String
nest n str = remlastNewline $ unlines $
map (replicate n ' ' ++) $
lines str
where
remlastNewline str =
case reverse str of
'\n':rest -> reverse rest
_ -> str