简体   繁体   English

GHC分析文件和图表是矛盾的

[英]GHC profiling file and chart are contradictory

I have a sieve of Eratosthenes program written in ST.Strict , and I was profiling it when I saw that it was taking a ridiculous amount of memory: 我在ST.Strict编写了一个Eratosthenes程序的筛子,当我看到它占用了大量的内存时,我正在剖析它:

Sun Jul 10 18:27 2016 Time and Allocation Profiling Report  (Final)

   Primes +RTS -hc -p -K1000M -RTS 10000000

total time  =        2.32 secs   (2317 ticks @ 1000 us, 1 processor)
total alloc = 5,128,702,952 bytes  (excludes profiling overheads)

(where 10^7) is the amount of primes I asked it to generate. (其中10 ^ 7)是我要求它生成的素数。

Weirdly, the profiling graph shows something completely different: 奇怪的是,分析图表显示了完全不同的东西:

剖析图

Am I misreading something in one of these graphs? 我在其中一张图中误读了什么吗? Or is there something wrong with one of these tools? 或者这些工具之一有什么问题吗?

For reference, my code is 作为参考,我的代码是

{-# LANGUAGE BangPatterns #-}

import Prelude hiding (replicate, read)
import qualified Text.Read as T
import Data.Vector.Unboxed.Mutable(replicate, write, read)
import Control.Monad.ST.Strict
import Data.STRef
import Control.Monad.Primitive

import Control.Monad

import System.Environment

main = print . length . primesUpTo . T.read . head =<< getArgs

primesUpTo :: Int -> [Int]
primesUpTo n = runST $ do
        primes <- replicate n True
        write primes 0 False
        write primes 1 False
        sieve 2 primes
        return []
        -- Removed to avoid the memory allocation of creating the list for profiling purposes
        -- filterM (read primes) [0..n-1]

    where
    sieve !i primes | i * i >= n    = return primes
    sieve !i primes = do
        v <- read primes i
        counter <- newSTRef $ i * i
        when v $ whileM_ ((< n) <$!> readSTRef counter) $ do
            curr_count <- readSTRef counter
            write primes curr_count False
            writeSTRef counter (curr_count + i)
        sieve (i + 1) primes

whileM_ :: (Monad m) => m Bool -> m a -> m ()
whileM_ condition body = do
    cond <- condition
    when cond $ do
        body
        whileM_ condition body

This seems to confuse many people. 这似乎让很多人感到困惑。

 total alloc = 5,128,702,952 bytes (excludes profiling overheads) 

This is literally the total size of all the allocations ever performed by your program, including "temporary" objects that become dead almost immediately after being allocated. 这实际上是程序执行的所有分配的总大小,包括在分配后几乎立即死亡的“临时”对象。 Allocation itself is nearly free, and generally Haskell programs allocate at a rate of around 1-2 GB/s. 分配本身几乎是免费的,通常Haskell程序以大约1-2 GB / s的速率分配。

Weirdly, the profiling graph shows something completely different: 奇怪的是,分析图表显示了完全不同的东西:

Indeed, the profiling graph shows the total size of all the objects that are live on the heap at any particular time. 实际上,分析图显示了在任何特定时间堆上的所有对象的总大小。 This reflects the space usage of your program. 这反映了程序的空间使用情况。 If your program runs in constant space, then the number shown in this graph will stay constant. 如果您的程序在恒定空间中运行,则此图中显示的数字将保持不变。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM