2015-05-30 18 views
8

Tôi đang gặp phải rò rỉ CPU nhỏ bằng GHC 7.8.3 và Yesod 1.4.9.Haskell rò rỉ CPU nhỏ

Khi tôi chạy trang web của mình với time và dừng nó (Ctrl + C) sau 1 phút mà không làm bất cứ điều gì (chỉ cần chạy, không yêu cầu gì cả), nó mất 1 giây. Nó đại diện cho khoảng 1,7% CPU.

$ time mysite 
^C 

real 1m0.226s 
user 0m1.024s 
sys  0m0.060s 

Nếu tôi tắt bộ thu gom rác nhàn rỗi, nó sẽ giảm xuống 0,35 giây (0,6% CPU). Mặc dù nó tốt hơn, nó vẫn tiêu thụ CPU mà không làm bất cứ điều gì.

$ time mysite +RTS -I0 # Disable idle GC 
^C 

real 1m0.519s 
user 0m0.352s 
sys  0m0.064s 

$ time mysite +RTS -I0 
^C 

real 4m0.676s 
user 0m0.888s 
sys  0m0.468s 

$ time mysite +RTS -I0 
^C 

real 7m28.282s 
user 0m1.452s 
sys  0m0.976s 

So với một lệnh cat chờ đợi vô thời hạn cho một cái gì đó trên đầu vào tiêu chuẩn:

$ time cat 
^C 

real 1m1.349s 
user 0m0.000s 
sys  0m0.000s 

Có điều gì khác trong Haskell mà không tiêu thụ CPU trong nền?

Đây có phải là sự rò rỉ từ Yesod không?

Hay đó là điều tôi đã làm trong chương trình của mình? (Tôi đã chỉ thêm chức năng xử lý, tôi không làm tính toán song song)

Sửa 2015-05-31 19:25

Dưới đây là thực hiện với -s cờ:

$ time mysite +RTS -I0 -s 
^C  23,138,184 bytes allocated in the heap 
     4,422,096 bytes copied during GC 
     2,319,960 bytes maximum residency (4 sample(s)) 
     210,584 bytes maximum slop 
       6 MB total memory in use (0 MB lost due to fragmentation) 

            Tot time (elapsed) Avg pause Max pause 
    Gen 0  30 colls,  0 par 0.00s 0.00s  0.0001s 0.0003s 
    Gen 1   4 colls,  0 par 0.03s 0.04s  0.0103s 0.0211s 

    TASKS: 5 (1 bound, 4 peak workers (4 total), using -N1) 

    SPARKS: 0 (0 converted, 0 overflowed, 0 dud, 0 GC'd, 0 fizzled) 

    INIT time 0.00s ( 0.00s elapsed) 
    MUT  time 0.86s (224.38s elapsed) 
    GC  time 0.03s ( 0.05s elapsed) 
    RP  time 0.00s ( 0.00s elapsed) 
    PROF time 0.00s ( 0.00s elapsed) 
    EXIT time 0.00s ( 0.00s elapsed) 
    Total time 0.90s (224.43s elapsed) 

    Alloc rate 26,778,662 bytes per MUT second 

    Productivity 96.9% of total user, 0.4% of total elapsed 

gc_alloc_block_sync: 0 
whitehole_spin: 0 
gen[0].sync: 0 
gen[1].sync: 0 


real 3m44.447s 
user 0m0.896s 
sys  0m0.320s 

và với hồ sơ:

$ time mysite +RTS -I0 
^C  23,024,424 bytes allocated in the heap 
     19,367,640 bytes copied during GC 
     2,319,960 bytes maximum residency (94 sample(s)) 
     211,312 bytes maximum slop 
       6 MB total memory in use (0 MB lost due to fragmentation) 

            Tot time (elapsed) Avg pause Max pause 
    Gen 0  27 colls,  0 par 0.00s 0.00s  0.0002s 0.0005s 
    Gen 1  94 colls,  0 par 1.09s 1.04s  0.0111s 0.0218s 

    TASKS: 5 (1 bound, 4 peak workers (4 total), using -N1) 

    SPARKS: 0 (0 converted, 0 overflowed, 0 dud, 0 GC'd, 0 fizzled) 

    INIT time 0.00s ( 0.00s elapsed) 
    MUT  time 1.00s (201.66s elapsed) 
    GC  time 1.07s ( 1.03s elapsed) 
    RP  time 0.00s ( 0.00s elapsed) 
    PROF time 0.02s ( 0.02s elapsed) 
    EXIT time 0.00s ( 0.00s elapsed) 
    Total time 2.09s (202.68s elapsed) 

    Alloc rate 23,115,591 bytes per MUT second 

    Productivity 47.7% of total user, 0.5% of total elapsed 

gc_alloc_block_sync: 0 
whitehole_spin: 0 
gen[0].sync: 0 
gen[1].sync: 0 


real 3m22.697s 
user 0m2.088s 
sys  0m0.060s 

mysite.prof:

Sun May 31 19:16 2015 Time and Allocation Profiling Report (Final) 

     mysite +RTS -N -p -s -h -i0.1 -I0 -RTS 

    total time =  0.05 secs (49 ticks @ 1000 us, 1 processor) 
    total alloc = 17,590,528 bytes (excludes profiling overheads) 

COST CENTRE   MODULE     %time %alloc 

MAIN     MAIN     98.0 93.7 
acquireSeedSystem.\.\ System.Random.MWC  2.0 0.0 
toByteString   Data.Serialize.Builder 0.0 3.9 


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

MAIN      MAIN          5684   0 98.0 93.7 100.0 100.0 
createSystemRandom   System.Random.MWC       11396   0 0.0 0.0  2.0 0.3 
    withSystemRandom   System.Random.MWC       11397   0 0.0 0.1  2.0 0.3 
    acquireSeedSystem  System.Random.MWC       11399   0 0.0 0.0  2.0 0.2 
    acquireSeedSystem.\  System.Random.MWC       11401   1 0.0 0.2  2.0 0.2 
    acquireSeedSystem.\.\ System.Random.MWC       11403   1 2.0 0.0  2.0 0.0 
sndS      Data.Serialize.Put       11386   21 0.0 0.0  0.0 0.0 
put      Data.Serialize        11384   21 0.0 0.0  0.0 0.0 
unPut      Data.Serialize.Put       11383   21 0.0 0.0  0.0 0.0 
toByteString    Data.Serialize.Builder      11378   21 0.0 3.9  0.0 4.0 
    flush.\     Data.Serialize.Builder      11393   21 0.0 0.0  0.0 0.0 
    withSize     Data.Serialize.Builder      11388   0 0.0 0.0  0.0 0.0 
    withSize.\    Data.Serialize.Builder      11389   21 0.0 0.0  0.0 0.0 
    runBuilder    Data.Serialize.Builder      11390   21 0.0 0.0  0.0 0.0 
    runBuilder    Data.Serialize.Builder      11382   21 0.0 0.0  0.0 0.0 
unstream/resize   Data.Text.Internal.Fusion     11372   174 0.0 0.1  0.0 0.1 
CAF      GHC.IO.Encoding       11322   0 0.0 0.0  0.0 0.0 
CAF      GHC.IO.FD         11319   0 0.0 0.0  0.0 0.0 
CAF      GHC.IO.Handle.FD       11318   0 0.0 0.2  0.0 0.2 
CAF      GHC.Event.Thread       11304   0 0.0 0.0  0.0 0.0 
CAF      GHC.Conc.Signal       11292   0 0.0 0.0  0.0 0.0 
CAF      GHC.IO.Encoding.Iconv      11288   0 0.0 0.0  0.0 0.0 
CAF      GHC.TopHandler        11284   0 0.0 0.0  0.0 0.0 
CAF      GHC.Event.Control       11271   0 0.0 0.0  0.0 0.0 
CAF      Main          11263   0 0.0 0.0  0.0 0.0 
    main      Main          11368   1 0.0 0.0  0.0 0.0 
CAF      Application        11262   0 0.0 0.0  0.0 0.0 
CAF      Foundation         11261   0 0.0 0.0  0.0 0.0 
CAF      Model          11260   0 0.0 0.1  0.0 0.3 
    unstream/resize   Data.Text.Internal.Fusion     11375   35 0.0 0.1  0.0 0.1 
CAF      Settings         11259   0 0.0 0.1  0.0 0.2 
    unstream/resize   Data.Text.Internal.Fusion     11370   20 0.0 0.1  0.0 0.1 
CAF      Database.Persist.Postgresql     6229   0 0.0 0.3  0.0 0.9 
    unstream/resize   Data.Text.Internal.Fusion     11373   93 0.0 0.6  0.0 0.6 
CAF      Database.PostgreSQL.Simple.Transaction  6224   0 0.0 0.0  0.0 0.0 
CAF      Database.PostgreSQL.Simple.TypeInfo.Static 6222   0 0.0 0.0  0.0 0.0 
CAF      Database.PostgreSQL.Simple.Internal   6219   0 0.0 0.0  0.0 0.0 
CAF      Yesod.Static        6210   0 0.0 0.0  0.0 0.0 
CAF      Crypto.Hash.Conduit       6193   0 0.0 0.0  0.0 0.0 
CAF      Yesod.Default.Config2      6192   0 0.0 0.0  0.0 0.0 
    unstream/resize   Data.Text.Internal.Fusion     11371   1 0.0 0.0  0.0 0.0 
CAF      Yesod.Core.Internal.Util     6154   0 0.0 0.0  0.0 0.0 
CAF      Text.Libyaml        6121   0 0.0 0.0  0.0 0.0 
CAF      Data.Yaml         6120   0 0.0 0.0  0.0 0.0 
CAF      Data.Yaml.Internal       6119   0 0.0 0.0  0.0 0.0 
    unstream/resize   Data.Text.Internal.Fusion     11369   1 0.0 0.0  0.0 0.0 
CAF      Database.Persist.Quasi      6055   0 0.0 0.0  0.0 0.0 
    unstream/resize   Data.Text.Internal.Fusion     11376   1 0.0 0.0  0.0 0.0 
CAF      Database.Persist.Sql.Internal    6046   0 0.0 0.0  0.0 0.0 
    unstream/resize   Data.Text.Internal.Fusion     11377   6 0.0 0.0  0.0 0.0 
CAF      Data.Pool         6036   0 0.0 0.0  0.0 0.0 
CAF      Network.HTTP.Client.TLS      6014   0 0.0 0.0  0.0 0.0 
CAF      System.X509.Unix       6010   0 0.0 0.0  0.0 0.0 
CAF      Crypto.Hash.MD5        5927   0 0.0 0.0  0.0 0.0 
CAF      Data.Serialize        5873   0 0.0 0.0  0.0 0.0 
    put      Data.Serialize        11385   1 0.0 0.0  0.0 0.0 
CAF      Data.Serialize.Put       5872   0 0.0 0.0  0.0 0.0 
    withSize     Data.Serialize.Builder      11387   1 0.0 0.0  0.0 0.0 
CAF      Data.Serialize.Builder      5870   0 0.0 0.0  0.0 0.0 
    flush      Data.Serialize.Builder      11392   1 0.0 0.0  0.0 0.0 
    toByteString    Data.Serialize.Builder      11391   0 0.0 0.0  0.0 0.0 
    defaultSize    Data.Serialize.Builder      11379   1 0.0 0.0  0.0 0.0 
    defaultSize.overhead  Data.Serialize.Builder      11381   1 0.0 0.0  0.0 0.0 
    defaultSize.k   Data.Serialize.Builder      11380   1 0.0 0.0  0.0 0.0 
CAF      Crypto.Random.Entropy.Unix     5866   0 0.0 0.0  0.0 0.0 
CAF      Network.HTTP.Client.Manager     5861   0 0.0 0.0  0.0 0.0 
    unstream/resize   Data.Text.Internal.Fusion     11374   3 0.0 0.0  0.0 0.0 
CAF      System.Random.MWC       5842   0 0.0 0.0  0.0 0.0 
    coff      System.Random.MWC       11405   1 0.0 0.0  0.0 0.0 
    ioff      System.Random.MWC       11404   1 0.0 0.0  0.0 0.0 
    acquireSeedSystem   System.Random.MWC       11398   1 0.0 0.0  0.0 0.0 
    acquireSeedSystem.random System.Random.MWC       11402   1 0.0 0.0  0.0 0.0 
    acquireSeedSystem.nbytes System.Random.MWC       11400   1 0.0 0.0  0.0 0.0 
    createSystemRandom  System.Random.MWC       11394   1 0.0 0.0  0.0 0.0 
    withSystemRandom   System.Random.MWC       11395   1 0.0 0.0  0.0 0.0 
CAF      Data.Streaming.Network.Internal    5833   0 0.0 0.0  0.0 0.0 
CAF      Data.Scientific        5728   0 0.0 0.1  0.0 0.1 
CAF      Data.Text.Array        5722   0 0.0 0.0  0.0 0.0 
CAF      Data.Text.Internal       5718   0 0.0 0.0  0.0 0.0 

Sửa 2015-06-01 08:40

Bạn có thể duyệt mã nguồn tại kho sau → https://github.com/Zigazou/Ouep

+0

Bất kỳ quá trình tải và khởi tạo nào sẽ tính ('cat' không hiển thị bất kỳ thứ gì vì nó tải trong <1ms). Liệu nó có tăng thêm theo thời gian không? Bằng bao nhiêu? –

+0

Có, nó tăng theo thời gian. Tôi đã phát hiện ra hành vi này khi tôi đang xem 'top', xem trang web của tôi trong mười quy trình đầu tiên. Vì vậy, tôi quyết định thử nghiệm với 'time' để có con số tốt hơn so với' top'. – zigazou

+0

Thời gian '0.352s' có thể là thiết lập thời gian chạy + Yesod. Để so sánh tốt hơn, hãy thử chờ 1m và chờ 2m và trừ đi lần: điều này sẽ cho bạn ước lượng CPU/s tốt hơn. – chi

Trả lời

2

Tìm thấy một related bug trong bug tracker Yesod. Ran chương trình của tôi như thế này:

myserver + RTS -I0 Phát triển -RTS

Và bây giờ sử dụng CPU nhàn rỗi giảm xuống còn gần như không có gì, so với 14% hoặc lâu hơn trước (máy tính ARM). Các I0 (đó là tôi và không) tùy chọn tắt bộ sưu tập rác định kỳ, mà mặc định là 0,3 giây tôi nghĩ. Bạn không chắc chắn về những tác động đó đối với việc đáp ứng ứng dụng hoặc sử dụng bộ nhớ, nhưng đối với tôi ít nhất điều này chắc chắn là thủ phạm.