fpco / weigh

Measure allocations of a Haskell functions/values

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Inconsistent result, bytes in negative

psibi opened this issue · comments

Using weigh, I'm getting inconsistent result on re runs and also negative amount of bytes allocated.

~/g/f/g/cryto-perf $ stack exec cryto-perf-exe

Case                          Allocated  GCs
\x -> HasInt x                       16    0
\x -> HasPacked (HasInt x)           56    0
\x -> HasUnpacked (HasInt x)      -,280    0
~/g/f/g/cryto-perf $ stack exec cryto-perf-exe

Case                          Allocated  GCs
\x -> HasInt x                       16    0
\x -> HasPacked (HasInt x)           56    0
\x -> HasUnpacked (HasInt x)         16    0
~/g/f/g/cryto-perf $ stack exec cryto-perf-exe

Case                          Allocated  GCs
\x -> HasInt x                    -,280    0
\x -> HasPacked (HasInt x)        -,264    0
\x -> HasUnpacked (HasInt x)         16    0
Click to expand the code used to reproduce the above error
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE BangPatterns #-}

module Main where

import Control.DeepSeq
import GHC.Generics
import Weigh

data HasInt =
  HasInt !Int
  deriving (Generic)

instance NFData HasInt

data HasPacked =
  HasPacked HasInt
  deriving (Generic)

instance NFData HasPacked

data HasUnpacked =
  HasUnpacked {-# UNPACK #-}!HasInt
  deriving (Generic)

instance NFData HasUnpacked

packing :: Weigh ()
packing = do
  func "\\x -> HasInt x" (\x -> HasInt x) 5
  func "\\x -> HasPacked (HasInt x)" (\x -> HasPacked (HasInt x)) 5
  func "\\x -> HasUnpacked (HasInt x)" (\x -> HasUnpacked (HasInt x)) 5

main :: IO ()
main = mainWith packing

Other details:

GHC: 8.6.3
weigh vesion: weigh 0.0.13

Architecture / Distribution:

$ uname -a
Linux elric 4.15.0-43-generic #46-Ubuntu SMP Thu Dec 6 14:45:28 UTC 2018 x86_64 x86_64
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 18.04.1 LTS
Release:        18.04
Codename:       bionic

I'll have to look at the API of GHC 8.6.3. Probably something has changed again, like what happened in GHC 8.2: #13 (comment)

Note that this problem also persists with:

  • GHC-8.4.4
  • GHC-8.2.
  • GHC-8.0.2 - I was only able to reproduce the inconsistent results.
  • GHC-8.0.1 - I was only able to reproduce the inconsistent results.
  • GHC-7.10.3 - I was only able to reproduce the inconsistent results.

I stopped testing for other GHC compilers after this.

Can you test this:

$ stack unpack weigh
$ cd weigh*
$ stack test

And share the output here?

Test seems to pass fine:

Click to see the log
~/g/weigh-0.0.13 $ stack test
weigh-0.0.13: test (suite: weigh-test)


Integers

  Case                Allocated  GCs
  integers count 0            0    0
  integers count 1           16    0
  integers count 2           32    0
  integers count 3           48    0
  integers count 10         160    0
  integers count 100      1,600    0

IO actions

  Case                      Allocated  GCs
  integers count IO CAF 0          16    0
  integers count IO func 0         24    0
  integers count IO CAF 1          32    0
  integers count IO func 1         40    0

Ints

  Case                Allocated  GCs
  ints count 1                0    0
  ints count 10               0    0
  ints count 1000000          0    0

Structs

  Case                             Allocated  GCs
  \_ -> IntegerStruct 0 0                  0    0
  \x -> IntegerStruct x 0                 24    0
  \x -> IntegerStruct x x                 24    0
  \x -> IntegerStruct (x+1) x             40    0
  \x -> IntegerStruct (x+1) (x+1)         40    0
  \x -> IntegerStruct (x+1) (x+2)         56    0

Packing

  Case                          Allocated  GCs
  \x -> HasInt x                       16    0
  \x -> HasUnpacked (HasInt x)         16    0
  \x -> HasPacked (HasInt x)           56    0

fst

  Case  Allocated  GCs
  id            0    0

snd

  Case  Allocated  GCs
  id          496    0

weigh-0.0.13: Test suite weigh-test passed

OK. Can you produce a test case that I can run locally that reproduces this?

@chrisdone Will a git repo work ? I see that your test case already contains that code and it is executing fine.

Here is a sample repo reproducing the case: https://github.com/psibi/weigh-issue-33

I've run into this too and I believe it's caused by linking against the threaded RTS. Note that weigh itself doesn't have "-threaded" in the ghc-options for its test suite. If you remove "-threaded" from your ghc-options does the problem go away? (I can also test this myself later when I have the time)

@agrue That makes it work, thanks! @chrisdone Will you accept a PR documenting this in README.md ?

Good tip, thanks! I came across the same thing.

I updated the README with this. But I'll probably also look into adding a runtime check for threadedness and throw an error if it's enabled.