2005-02-14 21:58:07 -05:00
|
|
|
! Copyright (C) 2004, 2005 Slava Pestov.
|
|
|
|
! See http://factor.sf.net/license.txt for BSD license.
|
|
|
|
IN: memory
|
2005-02-20 19:03:37 -05:00
|
|
|
USING: kernel-internals errors generic kernel lists math
|
|
|
|
namespaces prettyprint stdio unparser vectors words ;
|
2004-11-25 21:53:27 -05:00
|
|
|
|
2005-02-18 19:02:06 -05:00
|
|
|
! Printing an overview of heap usage.
|
|
|
|
|
2005-02-14 21:58:07 -05:00
|
|
|
: kb. 1024 /i unparse write " KB" write ;
|
2004-11-25 21:53:27 -05:00
|
|
|
|
2005-02-14 21:58:07 -05:00
|
|
|
: (room.) ( free total -- )
|
|
|
|
2dup swap - swap ( free used total )
|
|
|
|
kb. " total " write
|
|
|
|
kb. " used " write
|
|
|
|
kb. " free" print ;
|
|
|
|
|
|
|
|
: room. ( -- )
|
|
|
|
room
|
|
|
|
"Data space: " write (room.)
|
|
|
|
"Code space: " write (room.) ;
|
2004-11-25 21:53:27 -05:00
|
|
|
|
2005-02-18 19:02:06 -05:00
|
|
|
! Some words for iterating through the heap.
|
2005-02-14 21:58:07 -05:00
|
|
|
|
2005-02-17 22:49:19 -05:00
|
|
|
: (each-object) ( quot -- )
|
|
|
|
next-object dup [
|
|
|
|
swap dup slip (each-object)
|
|
|
|
] [
|
|
|
|
2drop
|
|
|
|
] ifte ; inline
|
|
|
|
|
|
|
|
: each-object ( quot -- )
|
|
|
|
#! Applies the quotation to each object in the image.
|
|
|
|
[
|
|
|
|
begin-scan (each-object)
|
|
|
|
] [
|
|
|
|
end-scan rethrow
|
|
|
|
] catch ; inline
|
|
|
|
|
2005-02-14 21:58:07 -05:00
|
|
|
: instances ( class -- list )
|
2005-02-17 22:49:19 -05:00
|
|
|
#! Return a list of all instances of a built-in or tuple
|
2005-02-18 19:02:06 -05:00
|
|
|
#! class in the image.
|
2005-02-17 22:49:19 -05:00
|
|
|
[
|
|
|
|
[
|
|
|
|
dup class pick = [ , ] [ drop ] ifte
|
|
|
|
] each-object drop
|
|
|
|
] make-list ;
|
2005-02-18 19:02:06 -05:00
|
|
|
|
2005-02-20 19:03:37 -05:00
|
|
|
GENERIC: (each-slot) ( quot obj -- ) inline
|
|
|
|
|
|
|
|
M: arrayed (each-slot) ( quot array -- )
|
|
|
|
dup array-capacity [
|
|
|
|
[
|
|
|
|
( quot obj n -- )
|
|
|
|
swap array-nth swap dup slip
|
|
|
|
] 2keep
|
|
|
|
] repeat 2drop ;
|
|
|
|
|
|
|
|
M: object (each-slot) ( quot obj -- )
|
|
|
|
dup class "slots" word-property [
|
|
|
|
pick pick >r >r car slot swap call r> r>
|
|
|
|
] each 2drop ;
|
|
|
|
|
|
|
|
: each-slot ( obj quot -- )
|
|
|
|
#! Apply the quotation to each slot value of the object.
|
|
|
|
swap (each-slot) ; inline
|
|
|
|
|
|
|
|
: refers? ( to obj -- ? )
|
|
|
|
f swap [ pick eq? or ] each-slot nip ;
|
|
|
|
|
|
|
|
: references ( obj -- list )
|
|
|
|
#! Return a list of all objects that refer to a given object
|
|
|
|
#! in the image.
|
|
|
|
[ ] [
|
|
|
|
pick over refers? [ swons ] [ drop ] ifte
|
|
|
|
] each-object nip ;
|
|
|
|
|
2005-02-18 20:37:01 -05:00
|
|
|
: vector+ ( n index vector -- )
|
|
|
|
[ vector-nth + ] 2keep set-vector-nth ;
|
|
|
|
|
|
|
|
: heap-stat-step ( counts sizes obj -- )
|
|
|
|
[ dup size swap type rot vector+ ] keep
|
|
|
|
1 swap type rot vector+ ;
|
|
|
|
|
|
|
|
: zero-vector ( n -- vector )
|
|
|
|
[ drop 0 ] vector-project ;
|
|
|
|
|
|
|
|
: heap-stats ( -- stats )
|
|
|
|
#! Return a list of instance count/total size pairs.
|
|
|
|
num-types zero-vector num-types zero-vector
|
|
|
|
[ >r 2dup r> heap-stat-step ] each-object
|
|
|
|
swap vector>list swap vector>list zip ;
|
|
|
|
|
2005-02-18 19:02:06 -05:00
|
|
|
: heap-stat. ( type instances bytes -- )
|
|
|
|
dup 0 = [
|
|
|
|
3drop
|
|
|
|
] [
|
|
|
|
rot builtin-type word-name write ": " write
|
|
|
|
unparse write " bytes, " write
|
|
|
|
unparse write " instances" print
|
|
|
|
] ifte ;
|
|
|
|
|
|
|
|
: heap-stats. ( -- )
|
|
|
|
#! Print heap allocation breakdown.
|
|
|
|
0 heap-stats [ dupd uncons heap-stat. 1 + ] each drop ;
|