factor/basis/tools/memory/memory.factor

99 lines
2.8 KiB
Factor
Raw Normal View History

! Copyright (C) 2005, 2009 Slava Pestov.
2007-09-20 18:09:08 -04:00
! See http://factorcode.org/license.txt for BSD license.
2009-05-16 11:45:17 -04:00
USING: kernel sequences arrays generic assocs io math
namespaces parser prettyprint strings io.styles words
2008-06-09 06:22:21 -04:00
system sorting splitting grouping math.parser classes memory
combinators fry vm specialized-arrays accessors continuations
classes.struct ;
SPECIALIZED-ARRAY: gc-event
2007-09-20 18:09:08 -04:00
IN: tools.memory
2008-04-07 22:04:51 -04:00
<PRIVATE
: kilobytes ( n -- str )
1024 /i number>string
2008-12-03 20:10:41 -05:00
dup length 4 > [ 3 cut* "," glue ] when
" KB" append ;
: fancy-table. ( obj alist -- )
[ [ nip first ] [ second call( obj -- str ) ] 2bi 2array ] with map
simple-table. ;
: copying-room. ( copying-sizes -- )
{
{ "Size:" [ size>> kilobytes ] }
{ "Occupied:" [ occupied>> kilobytes ] }
{ "Free:" [ free>> kilobytes ] }
} fancy-table. ;
: nursery-room. ( data-room -- )
"- Nursery space" print nursery>> copying-room. ;
: aging-room. ( data-room -- )
"- Aging space" print aging>> copying-room. ;
: mark-sweep-table. ( mark-sweep-sizes -- )
{
{ "Size:" [ size>> kilobytes ] }
{ "Occupied:" [ occupied>> kilobytes ] }
{ "Total free:" [ total-free>> kilobytes ] }
{ "Contiguous free:" [ contiguous-free>> kilobytes ] }
{ "Free block count:" [ free-block-count>> number>string ] }
} fancy-table. ;
: tenured-room. ( data-room -- )
"- Tenured space" print tenured>> mark-sweep-table. ;
: misc-room. ( data-room -- )
"- Miscellaneous buffers" print
{
{ "Card array:" [ cards>> kilobytes ] }
{ "Deck array:" [ decks>> kilobytes ] }
{ "Mark stack:" [ mark-stack>> kilobytes ] }
} fancy-table. ;
: data-room. ( -- )
"==== DATA HEAP" print nl
data-room data-heap-room memory>struct {
[ nursery-room. nl ]
[ aging-room. nl ]
[ tenured-room. nl ]
[ misc-room. ]
} cleave ;
: code-room. ( -- )
"==== CODE HEAP" print nl
code-room mark-sweep-sizes memory>struct mark-sweep-table. ;
PRIVATE>
: room. ( -- ) data-room. nl code-room. ;
<PRIVATE
2008-04-07 22:04:51 -04:00
2008-12-03 09:46:16 -05:00
: heap-stat-step ( obj counts sizes -- )
[ [ class ] dip inc-at ]
[ [ [ size ] [ class ] bi ] dip at+ ] bi-curry* bi ;
2008-04-07 22:04:51 -04:00
PRIVATE>
2007-09-20 18:09:08 -04:00
: heap-stats ( -- counts sizes )
[ ] instances H{ } clone H{ } clone
[ '[ _ _ heap-stat-step ] each ] 2keep ;
2007-09-20 18:09:08 -04:00
: heap-stats. ( -- )
heap-stats dup keys natural-sort standard-table-style [
[ { "Class" "Bytes" "Instances" } [ write-cell ] each ] with-row
2007-09-20 18:09:08 -04:00
[
[
dup pprint-cell
dup pick at pprint-cell
pick at pprint-cell
] with-row
] each 2drop
] tabular-output nl ;
: collect-gc-events ( quot -- events )
enable-gc-events [ ] [ disable-gc-events drop ] cleanup
disable-gc-events byte-array>gc-event-array ;