...
  View open merge request
Commits (15)
variables:
DOCKER_REV: 2b69e99de97bd5bf1fbdbf45852231c3dcb602b6
DOCKER_REV: ba119705df5222fe74208a85019cb980e2c4318f
validate:
image: "registry.gitlab.haskell.org/ghc/ci-images/x86_64-linux-deb9:$DOCKER_REV"
validate-hadrian:
image: "registry.gitlab.haskell.org/ghc/ci-images/x86_64-linux-deb10:$DOCKER_REV"
tags:
- x86_64-linux
before_script:
- git clean -xdf
- sudo apt install -y time
- ghc --version
- cabal --version
script:
- make clean
- GHC=/opt/ghc/$GHC_VERSION/bin/ghc
- $GHC --version
- cabal update
- make boot mode=fast
- "make mode=fast NoFibRuns=1 2>&1 | tee log"
- "nofib-analyse/nofib-analyse log"
- |
# The following checks that `make distclean` removes any files reported
# by `git clean -fxd`
make distclean
files=$(git clean -nxd | cut -d" " -f3 | sed "/log/d")
if ! [ -z $files ]
then
echo "The following files weren't cleaned:\n$files"
exit 1
fi
- cabal new-run nofib-run -- -o out -w $GHC
- mkdir -p results
- $GHC --info > results/compiler-info
- cp _make/out/*.results.tsv results
artifacts:
paths:
- results
packages: shake
source-repository-package
type: git
location: https://github.com/bgamari/tabular
tag: 204b01f63dec6bf0e0aba2862c71e77b2ec3909b
TOP = ../..
include $(TOP)/mk/boilerplate.mk
SRC_DEPS = old-time
SRC_DEPS = old-time array
# kLongLivedTreeDepth = 17 :: Int
# kArraySize = 500000 :: Int
......
This diff is collapsed.
TOP = ../..
include $(TOP)/mk/boilerplate.mk
SRC_DEPS = array
FAST_OPTS = 100000
NORM_OPTS = 300000
SLOW_OPTS = 600000
SRC_DEPS = array
include $(TOP)/mk/target.mk
......@@ -35,6 +35,8 @@ instance Applicative Pure where
instance Monad Pure where
Pure x >>= k = k x
return = Pure
instance MonadFail Pure where
fail s = error s
instance MonadEval Pure where
......@@ -313,6 +315,8 @@ instance Monad Abs where
AbsState r s' -> runAbs (k r) s'
AbsFail m -> AbsFail m)
return = pure
instance MonadFail Abs where
fail s = Abs (\ n -> AbsFail s)
instance MonadEval Abs where
......
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
module CachegrindParse where
import Data.Maybe
import qualified Data.Map as M
newtype EventName = EventName { getEventName :: String }
deriving (Show, Eq, Ord)
parse :: FilePath -> IO (M.Map EventName Integer)
parse fname = parse' <$> readFile fname
parse' :: String -> M.Map EventName Integer
parse' content =
M.fromList $ zip summary events
where
events = case mapMaybe isEventList $ lines content of
[] -> error "No event list found"
[x] -> x
_ -> error "More than one event list found"
summary = case mapMaybe isSummaryList $ lines content of
[] -> error "No event summary found"
[x] -> x
_ -> error "More than one event summary found"
isEventList :: String -> Maybe [Integer]
isEventList line
| "summary:" : rest <- words line = Just $ map read rest
| otherwise = Nothing
isSummaryList :: String -> Maybe [EventName]
isSummaryList line
| "events:" : rest <- words line = Just $ map EventName rest
| otherwise = Nothing
This diff is collapsed.
Copyright (c) 2019, Ben Gamari
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* Neither the name of Ben Gamari nor the names of other
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE GADTs #-}
module LabelMatch where
import Control.Applicative
import Control.Monad
import Data.String
import qualified Data.Text as T
import Measurements (Label(..))
newtype LabelMatcher a = LabelMatcher (Label -> Maybe (Label, a))
deriving (Functor)
match :: LabelMatcher a -> Label -> Maybe a
match (LabelMatcher f) l = snd <$> f l
instance (a ~ ()) => IsString (LabelMatcher a) where
fromString = matchPart . fromString
instance Applicative LabelMatcher where
pure x = LabelMatcher $ \lbl -> Just (lbl, x)
(<*>) = ap
instance Alternative LabelMatcher where
LabelMatcher f <|> LabelMatcher g =
LabelMatcher $ \lbl -> f lbl <|> g lbl
empty = LabelMatcher $ const Nothing
instance Monad LabelMatcher where
LabelMatcher f >>= g = LabelMatcher $ \lbl ->
case f lbl of
Nothing -> Nothing
Just (lbl', x) -> let LabelMatcher h = g x
in h lbl'
matchPart :: T.Text -> LabelMatcher ()
matchPart s = LabelMatcher f
where
f (Label (x:xs))
| x == s = Just (Label xs, ())
f _ = Nothing
wildcard :: LabelMatcher String
wildcard = LabelMatcher f
where
f (Label (x:xs)) = Just (Label xs, T.unpack x)
f (Label []) = Nothing
end :: LabelMatcher ()
end = LabelMatcher f
where
f (Label []) = Just (Label [], ())
f (Label _) = Nothing
{-# LANGUAGE OverloadedStrings #-}
import Data.Foldable
import Options.Applicative
import qualified Data.ByteString.Lazy as BS
import qualified Measurements as Ms
import Measurements (Measurements, Label)
args :: Parser (FilePath, [FilePath], Bool)
args =
(,,)
<$> option str (long "output" <> short 'o' <> help "output file")
<*> some (argument str (metavar "FILE" <> help "results.json files"))
<*> switch (long "json" <> short 'j' <> help "produce JSON output")
main :: IO ()
main = do
(output, files, json) <- execParser $ info (helper <*> args) mempty
trees <- mapM Ms.readFile files
let result = fold trees
if json
then Ms.writeFile output result
else Ms.writeFileTsv output result
# The nofib build system
This documents usage of nofib's Shake-based build system. The build system,
`nofib-run`, automates running of the nofib benchmarks and the collection of a
variety of compile- and run-time metrics. These metrics are recorded in a
common semi-structured CSV file format, which can be compared and analysed with
the included `nofib-compare` utility.
## Preparation
The nofib benchmarks require that a number of native dependencies be installed
in the tested compilers user or global package database. To install these:
```
$ cabal v1-install --allow-newer -w /path/to/ghc old-time stm parallel random
```
## Usage
The `nofib-run` executable is used to run the benchmark suite and accepts a variety of arguments.
By default the results of the run will be placed in `_make/{compiler version}`.
The compiler version component of this path can be overridden with the
`--output` flag.
For instance,
```
$ cabal new-run -- nofib-run --compiler=/path/to/ghc --output=test
```
will produce a number of files in `_make/`:
```
$ ls -R _make
_make:
8.11.0.20200205
_make/8.11.0.20200205:
gc imaginary parallel real spectral
_make/8.11.0.20200205/gc:
cacheprof circsim constraints fibheaps gc_bench happy hash lcss linear mutstore1 mutstore2 spellcheck treejoin
_make/8.11.0.20200205/gc/cacheprof:
Arch_x86.hi Arch_x86.o.result Arch_x86.o.stats Generics.hi Generics.o.result Generics.o.stats Main.deps Main.o Main.o.results.csv Main.result
Arch_x86.o Arch_x86.o.results.csv config.txt Generics.o Generics.o.results.csv Main Main.hi Main.o.result Main.o.stats Main.results.csv
...
```
In the case of the `gc/cacheprof` benchmark we see the following were produced:
* build artifacts from the build of the benchmark (e.g. `*.o`, `*.hi`)
* for each object file a `.results.csv` file which contains the metrics for the object size
* `Main.results.csv`, which aggregates all of the above `*.results.csv` files
A number of classes of metrics are collected:
* object code size for each module
* runtime system statistics (e.g. mutator time, GC time, GC counts,
allocations) from the execution of `ghc` while compiling the testcase
* runtime system statistics from the execution of the testcase
### Cachegrind
The benchmarks can also be run under valgrind's
[cachegrind](https://valgrind.org/docs/manual/cg-manual.html) tool with the
`--cachegrind`, which simulates a simple cache hierarchy to allow (mostly)
deterministic modelling of instruction counts, memory, and cache effects.
When running with `--cachegrind` tests can be safely parallelised with the
`-j<n>` flag.
### Performance counters
The benchmarks can also be run under the Linux `perf` tool for collection of
(micro-)architectural event counts. This mode is enabled
## Data format
The output of `nofib-run` is a simple CSV format (defined by the `Measurements`
type in `src/Measurements.hs`) represents semi-structured string keys (known as
`Label`s) and floating-point values. In particular, labels have a path-like
structure with `/` being the component delimiter.
An example of this CSV is:
```
```
There also exists a JSON representation of this format,
```
{
}
```
## Comparing results
```
$ cabal new-run nofib-compare -- approach-b.results.csv approach-a.results.csv
```
import Distribution.Simple
main = defaultMain
cabal-version: >=1.10
name: nofib
version: 0.1.0.0
-- synopsis:
-- description:
-- bug-reports:
license: BSD3
license-file: LICENSE
author: Ben Gamari
maintainer: ben@smart-cactus.org
-- copyright:
-- category:
build-type: Simple
library
exposed-modules: Measurements
ghc-options: -Wall
hs-source-dirs: src
build-depends: base >=4.10 && <4.15,
aeson,
these >= 0.8,
containers,
cassava,
text,
filepath,
vector,
bytestring
default-language: Haskell2010
executable nofib-run
main-is: Main.hs
ghc-options: -Wall
hs-source-dirs: runner
other-modules: CachegrindParse, PerfStatParse, ParseResults, RunnerTypes
other-extensions: RecordWildCards, DeriveDataTypeable
build-depends: base >=4.10 && <4.15,
time >=1.8 && <1.10,
containers,
bytestring,
directory >=1.3 && <1.4,
filepath,
process >=1.6 && <1.7,
text,
optparse-applicative,
shake,
nofib
default-language: Haskell2010
executable nofib-compare
main-is: Compare.hs
ghc-options: -Wall
other-modules: LabelMatch
other-extensions: RecordWildCards, DeriveDataTypeable
build-depends: base >=4.10 && <4.15,
these,
containers,
directory,
filepath,
text,
aeson,
ansi-wl-pprint,
tabular,
these,
semialign,
optparse-applicative,
ansi-terminal,
nofib
default-language: Haskell2010
executable nofib-merge
main-is: Merge.hs
ghc-options: -Wall
build-depends: base >=4.10 && <4.15,
optparse-applicative,
cassava,
bytestring,
nofib
default-language: Haskell2010
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
module CachegrindParse where
import Data.Maybe
import qualified Data.Map as M
newtype EventName = EventName { getEventName :: String }
deriving (Show, Eq, Ord)
parse :: FilePath -> IO (M.Map EventName Integer)
parse fname = parse' <$> readFile fname
parse' :: String -> M.Map EventName Integer
parse' content =
M.fromList $ zip summary events
where
events = case mapMaybe isEventList $ lines content of
[] -> error "No event list found"
[x] -> x
_ -> error "More than one event list found"
summary = case mapMaybe isSummaryList $ lines content of
[] -> error "No event summary found"
[x] -> x
_ -> error "More than one event summary found"
isEventList :: String -> Maybe [Integer]
isEventList line
| "summary:" : rest <- words line = Just $ map read rest
| otherwise = Nothing
isSummaryList :: String -> Maybe [EventName]
isSummaryList line
| "events:" : rest <- words line = Just $ map EventName rest
| otherwise = Nothing
This diff is collapsed.
module ParseResults
( parseCodeSize
, parseRtsStats
) where
import qualified Data.Map.Strict as M
-- | Parse text section size out of Berkley-format @size@ output
parseCodeSize :: String -> Integer
parseCodeSize content
| header : sizes : _ <- lines content
, "text" : _ <- words header =
read $ head $ words sizes
| otherwise = error "unrecognized size output"
parseRtsStats :: String -> M.Map String Double
parseRtsStats = foldMap parseValue . readPairs . dropFirstLine
where
parseValue (name, value)
| (x, ""):_ <- reads value = M.singleton name x
| otherwise = M.empty
dropFirstLine = unlines . drop 1 . lines
readPairs :: String -> [(String, String)]
readPairs s
| (x, _):_ <- reads s = x
| otherwise = error $ "Failed to parse RTS statistics: " ++ s
{-# LANGUAGE OverloadedStrings #-}
module PerfStatParse (readPerfStat, EventName(..)) where
import qualified Data.Text as T
import qualified Data.Text.IO as TIO
import qualified Data.Map as M
newtype EventName = EventName { getEventName :: String }
deriving (Show, Eq, Ord)
-- | Read metrics from @perf stat -x,@ invocation.
readPerfStat :: FilePath -> IO (M.Map EventName [Double])
readPerfStat path = do
parsePerfStat <$> TIO.readFile path
parsePerfStat :: T.Text -> M.Map EventName [Double]
parsePerfStat = M.unionsWith (++) . map parseLine . T.lines
where
parseLine line
| "#" `T.isPrefixOf` line = mempty
| value:_:metric:_ <- T.splitOn "," line
, (val, "") : _ <- reads $ T.unpack value
= M.singleton (EventName $ T.unpack metric) [val]
| otherwise = mempty
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
module RunnerTypes where
-- Standard libraries
import Data.Char
import Data.Maybe
import System.Process
import qualified System.Directory as IO
import Data.String (IsString)
import Options.Applicative
-- Shake - build system
import Development.Shake.FilePath hiding (exe)
import qualified Measurements as Ms
import GHC.Read as R
import Text.Read.Lex as L
import Text.ParserCombinators.ReadPrec (pfail)
---------------------------------------------------------------------
-- ARGUMENT PARSING - mostly based on CmdArgs
-- | Testnames are either
-- * A single tests name component (eg "rfib")
-- * A subpath eg. "real/eff"
-- * A fully qualified path e.g. "spectral/simple"
newtype TestName = TestName { unTestName :: String }
deriving (Show, Eq, Ord, IsString)
-- | The directory which the given test lives in
testDir :: TestName -> FilePath
testDir (TestName dir) = dir
testLabel :: TestName -> Ms.Label
testLabel (TestName dir) = Ms.mkLabel dir
data Nofib
= Build
{clean :: Bool
,tests :: [TestName]
,threads :: Int
,compiler :: String
,compiler_args :: [String]
,output :: String -- ^ Where to put the results
,cachegrind :: Bool
,cachegrind_args :: [String]
,perf :: Bool
,perf_args :: [String]
,speed :: Speed
,rts_args :: [String]
,times :: Int
}
deriving (Show)
data Speed = Fast | Norm | Slow
deriving (Show)
instance Read Speed where
readPrec =
parens
( do L.Ident s <- lexP
case s of
"Slow" -> return Slow
"slow" -> return Slow
"Norm" -> return Norm
"norm" -> return Norm
"Fast" -> return Fast
"fast" -> return Fast
_ -> pfail
)
nofibMode :: Parser Nofib
nofibMode =
Build
<$> switch (short 'c' <> long "clean" <> help "Clean before building")
<*> many (argument (TestName <$> str) (metavar "TEST" <> help "Tests to run (omit for all)"))
<*> option auto (short 'j' <> long "threads" <> metavar "N" <> value 1 <> help "Number of threads, defaults to 1")
<*> option str (short 'w' <> long "compiler" <> metavar "HC" <> value "ghc" <> help "Compiler to use, defaults to `ghc`")
<*> many (option str (long "compiler-arg" <> help "Extra arguments to pass to the Compiler when building tests"))
<*> option str (short 'o' <> long "output" <> metavar "DIR" <> help "Where to put created files under ./_make, defaults to {compiler version}")
<*> switch (long "cachegrind" <> help "Run the tests under cachegrind")
<*> many (option str (long "cachegrind-arg" <> help "Extra arguments to pass to cachegrind"))
<*> switch (long "perf" <> help "Run the tests under `perf stat`")
<*> (many (option str (long "perf-arg" <> help "Extra arguments to pass to `perf stat`"))
<|> pure ["-e instructions,cycles,cache-misses,task-clock", "-r5"])
<*> option auto (long "speed" <> short 's' <> value Norm <> help "Test speed (Fast,Norm,Slow)")
<*> many (option str (long "rts-arg" <> help "Extra arguments to pass to runtime system when running"))
<*> option auto (long "times" <> short 't' <> value 1 <> help "Number of times to run each test")
-- | Create a clean set of arguments, with any defaults filled in
nofibArgs :: IO Nofib
nofibArgs = do
args <- execParser $ info (helper <*> nofibMode) (progDesc "nofib Benchmark Suite")
print args
case args of
build@Build{..} -> do
-- Turns "ghc" into /usr/bin/../ghc
compiler' <- fromMaybe (error "Couldn't find GHC at" ++ compiler) <$> IO.findExecutable compiler
compilerVer <- compilerVersion compiler
output' <- return $ "_make" </> (if null output then compilerVer else output)
return build{ output = output', compiler = compiler' }
-- | Find the default compiler string, e.g. ghc-7.4.1
compilerVersion :: FilePath -> IO String
compilerVersion compiler = do
(_,stdout,_) <- readProcessWithExitCode compiler ["--version"] ""
let ver = takeWhile (\x -> isDigit x || x == '.') $ dropWhile (not . isDigit) stdout
return $ if null ver then "unknown" else ver
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE OverloadedStrings #-}
module Measurements
( -- * Labels
-- | A label hierarchically identifies a type of measurement (e.g.
-- @T1234//bytes allocated@)
Label(..)
, mkLabel
, parseLabel
, encodeLabel
-- * Measurements
, Measurements(..)
, toMap
, fromMap
, prefix
, singleton
, fromList
, filterByLabel
, mapLabels
-- ** I/O with JSON representation
, writeFile'
, writeFile
, readFileJson
, readFileTsv
, readFile
-- ** I/O with CSV representation
, writeFileTsv
-- * Miscellaneous utilities
, mean, geomMean, stdDev
) where
import Data.Word (Word8)
import Data.String (IsString(..))
import Data.Bifunctor
import Data.Maybe
import Control.Applicative
import Control.Monad
import Data.Aeson
import qualified Data.Csv as Csv
import qualified Data.Vector as V
import qualified Data.Map.Strict as M
import qualified Data.ByteString.Lazy.Char8 as BSL
import qualified Data.Text as T
import System.FilePath
import Prelude hiding (readFile, writeFile)
newtype Label = Label { getLabel :: [T.Text] }
deriving (Eq, Ord, Show, Monoid, Semigroup,
ToJSON, FromJSON, ToJSONKey, FromJSONKey)
instance IsString Label where
fromString s = Label [fromString s]
instance Csv.ToField Label where
toField = Csv.toField . encodeLabel
instance Csv.FromField Label where
parseField = pure . parseLabel <=< Csv.parseField
mkLabel :: String -> Label
mkLabel = fromString
parseLabel :: T.Text -> Label
parseLabel = Label . T.splitOn "//"
encodeLabel :: Label -> T.Text
encodeLabel (Label parts) = T.intercalate "//" parts
newtype Measurements a = Measurements { toList :: [(Label, a)] }
deriving (Show, Functor, Monoid, Semigroup, ToJSON, FromJSON)
toMap :: ([a] -> b) -> Measurements a -> M.Map Label b
toMap f (Measurements xs) =
fmap f $ M.fromListWith (<>) [ (x, [y]) | (x,y) <- xs ]
fromMap :: M.Map Label a -> Measurements a
fromMap = Measurements . M.toList
prefix :: Label -> Measurements a -> Measurements a
prefix lbl (Measurements xs) = Measurements $ map (first (lbl<>)) xs
singleton :: Label -> a -> Measurements a
singleton lbl x = Measurements [(lbl, x)]
fromList :: [(Label, a)] -> Measurements a
fromList = Measurements
filterByLabel :: (Label -> Bool) -> Measurements a -> Measurements a
filterByLabel f (Measurements xs) = Measurements $ filter (f . fst) xs
mapLabels :: (Label -> Maybe Label) -> Measurements a -> Measurements a
mapLabels f (Measurements xs) =
Measurements $ mapMaybe (\(k,v) -> (\k' -> (k', v)) <$> f k) xs
writeFile' :: ToJSON a => FilePath -> Measurements a -> IO ()
writeFile' fname = BSL.writeFile fname . encode
writeFile :: FilePath -> Measurements Double -> IO ()
writeFile = writeFile'
readFileJson :: FromJSON a => FilePath -> IO (Measurements a)
readFileJson fname = eitherDecodeFileStrict fname >>= either fail pure
readFileTsv :: FilePath -> IO (Measurements Double)
readFileTsv path = either err f . Csv.decodeWith opts Csv.NoHeader <$> BSL.readFile path
where
f = Measurements . map (first parseLabel) . V.toList
err e = error $ path ++ ": failed to read TSV: " ++ show e
opts :: Csv.DecodeOptions
opts = Csv.defaultDecodeOptions { Csv.decDelimiter = tabChar }
tabChar :: Word8
tabChar = 9
readFile :: FilePath -> IO (Measurements Double)
readFile fname
| "json" <- takeExtension fname = readFileJson fname
| otherwise = readFileTsv fname
writeFileTsv :: FilePath -> Measurements Double -> IO ()
writeFileTsv fname = BSL.writeFile fname . Csv.encodeByNameWith opts header . map f . toList
where
f (label, value) = Csv.namedRecord [ "label" Csv..= label, "value" Csv..= value ]
header = V.fromList ["label", "value"]
opts :: Csv.EncodeOptions
opts = Csv.defaultEncodeOptions { Csv.encDelimiter = tabChar
, Csv.encIncludeHeader = False
, Csv.encQuoting = Csv.QuoteNone
}
mean :: RealFrac a => [a] -> a
mean xs = sum xs / realToFrac (length xs)
geomMean :: RealFloat a => [a] -> a
geomMean = exp . mean . map log
stdDev :: RealFloat a => [a] -> a
stdDev xs = sqrt $ mean $ map (\x -> (x-m)^(2::Int)) xs
where m = mean xs