2015-08-20 29 views
33

如何告知stack使用-prof構建我的可執行文件及其所有依賴項?使用堆棧進行性能分析構建

只需將其添加到ghc-options.cabal文件中是不夠的,因爲它只會嘗試在啓用分析功能的情況下構建可執行文件,從而失敗。

回答

51

剖析與構建棧1.0.0和更新

若要分析構建支持:

stack build --profile 

您可能需要運行stack clean第一,但this should be fixed in Stack 1.5.0

要分析:

stack exec -- <your program> +RTS <profiling options> 

其中用於<profiling options>你可能想-p時間分析或-h的內存分析。對於時間分析,配置文件顯示在./<your program>.prof中,對於內存分析,配置文件顯示在./<your program>.hp中。

有關更多性能分析選項,請參閱GHC profiling documentation

探查與堆棧版本1.0.0建立之前(即,從2015年)

爲了建立與啓用分析:

stack build --executable-profiling --library-profiling --ghc-options="-fprof-auto -rtsopts" 

要分析:

stack exec -- <your program> +RTS <profiling options> 

示例堆棧1.0。 0 and newer

假設你有一個包叫做test wi日通過main這裏定義一個可執行test

module Main where 

main :: IO() 
main = do 
    print $ foo 0 

foo :: Int -> Int 
foo x = fooSub (x+1) 
    where 
    fooSub x = bar (x+1) 

bar :: Int -> Int 
bar x = barSub (x+1) 
    where 
    barSub x = barSubSub (x+1) 
     where 
     barSubSub x = x+1 

然後做stack build --profile && stack exec -- test +RTS -p將產生一個文件./test.prof包括

                       individual  inherited 
COST CENTRE     MODULE    SRC      no.  entries %time %alloc %time %alloc 

    [... many lines omitted ...] 
    main      Main     src/Main.hs:(4,1)-(5,15) 97   0 0.0 0.0  0.0 0.0 
    foo      Main     src/Main.hs:(8,1)-(10,24) 98   1 0.0 0.0  0.0 0.0 
    foo.fooSub    Main     src/Main.hs:10:5-24   99   1 0.0 0.0  0.0 0.0 
    bar     Main     src/Main.hs:(13,1)-(17,46) 100   1 0.0 0.0  0.0 0.0 
     bar.barSub   Main     src/Main.hs:(15,5)-(17,46) 101   1 0.0 0.0  0.0 0.0 
     bar.barSub.barSubSub Main     src/Main.hs:17:9-46  102   1 0.0 0.0  0.0 0.0 
main      Main     src/Main.hs:(4,1)-(5,15) 95   0 0.0 20.5  0.0 20.5 

即存在於所有定義的分析信息,包括where 局部定義條款。

如果你只是想剖析頂級的定義,你可以用 建立GHC選項-fprof-auto-top代替:做stack build --profile --ghc-options=-fprof-auto-top && stack exec -- test +RTS -p產生./test.prof包括

                   individual  inherited 
COST CENTRE MODULE    SRC      no.  entries %time %alloc %time %alloc 

[... many lines omitted ...] 
    main  Main     src/Main.hs:(4,1)-(5,15) 97   0 0.0 0.0  0.0 0.0 
    foo  Main     src/Main.hs:(8,1)-(10,24) 98   1 0.0 0.0  0.0 0.0 
    bar  Main     src/Main.hs:(13,1)-(17,46) 99   1 0.0 0.0  0.0 0.0 
main  Main     src/Main.hs:(4,1)-(5,15) 95   0 0.0 20.5  0.0 20.5 

代替。

最後,請注意stack build --profile也打開堆棧 痕跡。如果您更改程序,以便barSubSub x = error $ show x,然後運行stack build --profile && stack exec test產生

test: 4 
CallStack (from HasCallStack): 
    error, called at src/Main.hs:17:23 in main:Main 
CallStack (from -prof): 
    Main.bar.barSub.barSubSub (src/Main.hs:17:9-36) 
    Main.bar.barSub (src/Main.hs:(15,5)-(17,36)) 
    Main.bar (src/Main.hs:(13,1)-(17,36)) 
    Main.foo.fooSub (src/Main.hs:10:5-24) 
    Main.foo (src/Main.hs:(8,1)-(10,24)) 
    Main.main (src/Main.hs:(4,1)-(5,15)) 
    Main.CAF:lvl8_r4Fc (<no location info>) 

很酷!

+3

這不起作用。使用stack exec my-exe + RTS -p顯示可執行文件沒有用-prof編譯,並且試圖這樣做表明ld找不到這些庫的配置文件版本。 –

+2

這對我有效:'stack install --enable-executable-profiling --enable-library-profiling --ghc-options =「 - fprof-auto -rtsopts」' – Dfr

+0

如何在構建後執行它/ profile已完成? –

13

我有這個問題,以及發現問題是在調用:

stack exec my-exe +RTS -p通行證-p堆棧,而不是我,exe文件。這工作:

stack exec -- my-exe +RTS -p 
4

對於stack buildstack benchstack test你可以只使用stack build/bench/test --profile。您可能必須首先使用stack clean以使其通過分析重新編譯。

對於stack build你仍然必須通過+RTS -p,或者你需要的任何選項(參見GHC User Guide)運行可執行文件中@Tomáš Janoušek answer時。

您還可以在debugging section of the stack user guide找到更多的信息。

+1

您也可以將「--force-dirty」添加到堆棧構建命令的末尾,以強制重新編譯。 – John