factor/extra/tools/memory/memory.factor

85 lines
2.1 KiB
Factor
Raw Normal View History

2008-04-07 22:04:51 -04:00
! Copyright (C) 2005, 2008 Slava Pestov.
2007-09-20 18:09:08 -04:00
! See http://factorcode.org/license.txt for BSD license.
USING: kernel sequences vectors arrays generic assocs io math
namespaces parser prettyprint strings io.styles vectors words
2008-04-07 22:04:51 -04:00
system sorting splitting math.parser classes memory combinators ;
2007-09-20 18:09:08 -04:00
IN: tools.memory
2008-04-07 22:04:51 -04:00
<PRIVATE
: write-size ( n -- )
number>string
dup length 4 > [ 3 cut* "," swap 3append ] when
" KB" append write-cell ;
2007-09-20 18:09:08 -04:00
: write-total/used/free ( free total str -- )
[
write-cell
2008-04-07 22:04:51 -04:00
dup write-size
over - write-size
write-size
2007-09-20 18:09:08 -04:00
] with-row ;
: write-total ( n str -- )
[
write-cell
2008-04-07 22:04:51 -04:00
write-size
2007-09-20 18:09:08 -04:00
[ ] with-cell
[ ] with-cell
] with-row ;
: write-headings ( seq -- )
[ [ write-cell ] each ] with-row ;
: (data-room.) ( -- )
2008-04-07 22:04:51 -04:00
data-room 2 <groups> dup length [
[ first2 ] [ number>string "Generation " prepend ] bi*
write-total/used/free
] 2each
2007-09-20 18:09:08 -04:00
"Cards" write-total ;
2008-04-07 22:04:51 -04:00
: write-labelled-size ( n string -- )
[ write-cell write-size ] with-row ;
2007-09-20 18:09:08 -04:00
: (code-room.) ( -- )
2008-04-07 22:04:51 -04:00
code-room {
[ "Size:" write-labelled-size ]
[ "Used:" write-labelled-size ]
[ "Total free space:" write-labelled-size ]
[ "Largest free block:" write-labelled-size ]
} spread ;
: heap-stat-step ( counts sizes obj -- )
[ dup size swap class rot at+ ] keep
1 swap class rot at+ ;
PRIVATE>
2007-09-20 18:09:08 -04:00
: room. ( -- )
2008-04-07 22:04:51 -04:00
"==== DATA HEAP" print
2007-09-20 18:09:08 -04:00
standard-table-style [
{ "" "Total" "Used" "Free" } write-headings
(data-room.)
2008-04-07 22:04:51 -04:00
] tabular-output
nl
"==== CODE HEAP" print
standard-table-style [
2007-09-20 18:09:08 -04:00
(code-room.)
] tabular-output ;
: heap-stats ( -- counts sizes )
H{ } clone H{ } clone
[ >r 2dup r> heap-stat-step ] each-object ;
: heap-stats. ( -- )
heap-stats dup keys natural-sort standard-table-style [
{ "Class" "Bytes" "Instances" } write-headings
[
[
dup pprint-cell
dup pick at pprint-cell
pick at pprint-cell
] with-row
] each 2drop
] tabular-output ;