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-08-21 20:50:14 -04:00
|
|
|
USING: errors generic hashtables io kernel kernel-internals
|
|
|
|
lists math namespaces parser prettyprint sequences strings
|
|
|
|
unparser vectors words ;
|
2004-11-25 21:53:27 -05:00
|
|
|
|
2005-05-12 01:02:39 -04:00
|
|
|
: generations 15 getenv ;
|
|
|
|
|
|
|
|
: full-gc generations 1 - gc ;
|
|
|
|
|
2005-04-30 14:27:40 -04:00
|
|
|
: save
|
|
|
|
#! Save the current image.
|
|
|
|
"image" get save-image ;
|
2005-05-11 00:43:52 -04:00
|
|
|
|
2005-02-18 19:02:06 -05:00
|
|
|
! Printing an overview of heap usage.
|
|
|
|
|
2005-08-21 20:50:14 -04:00
|
|
|
: kb.
|
|
|
|
1024 /i number>string
|
|
|
|
6 CHAR: \s pad-left 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
|
2005-05-11 00:43:52 -04:00
|
|
|
0 swap [
|
2005-08-21 20:50:14 -04:00
|
|
|
"Generation " write over pprint ":" write
|
2005-05-11 00:43:52 -04:00
|
|
|
uncons (room.) 1 +
|
|
|
|
] each drop
|
2005-05-11 00:52:27 -04:00
|
|
|
"Semi-space: " write kb. terpri
|
|
|
|
"Cards: " write kb. terpri
|
|
|
|
"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-08-19 21:46:12 -04:00
|
|
|
: (each-object) ( quot -- )
|
|
|
|
next-object [ swap [ call ] keep (each-object) ] when* ;
|
|
|
|
inline
|
|
|
|
|
2005-02-17 22:49:19 -05:00
|
|
|
: each-object ( quot -- )
|
2005-04-25 03:33:33 -04:00
|
|
|
#! Applies the quotation to each object in the image. We
|
|
|
|
#! use the lower-level >c and c> words here to avoid
|
|
|
|
#! copying the stacks.
|
|
|
|
[ end-scan rethrow ] >c
|
2005-08-19 21:46:12 -04:00
|
|
|
begin-scan (each-object) drop
|
|
|
|
f c> call ; inline
|
2005-02-17 22:49:19 -05:00
|
|
|
|
2005-02-20 20:38:24 -05:00
|
|
|
: instances ( quot -- list )
|
|
|
|
#! Return a list of all object that return true when the
|
|
|
|
#! quotation is applied to them.
|
2005-02-17 22:49:19 -05:00
|
|
|
[
|
|
|
|
[
|
2005-02-20 20:38:24 -05:00
|
|
|
[ swap call ] 2keep rot [ , ] [ drop ] ifte
|
2005-02-17 22:49:19 -05:00
|
|
|
] each-object drop
|
2005-08-25 15:27:38 -04:00
|
|
|
] [ ] make ;
|
2005-02-18 19:02:06 -05:00
|
|
|
|
2005-08-15 03:25:39 -04:00
|
|
|
G: each-slot ( obj quot -- )
|
2005-08-22 15:33:18 -04:00
|
|
|
[ over ] standard-combination ; inline
|
2005-02-20 19:03:37 -05:00
|
|
|
|
2005-07-25 17:13:35 -04:00
|
|
|
M: array each-slot ( array quot -- ) each ;
|
2005-02-20 19:03:37 -05:00
|
|
|
|
2005-07-25 17:13:35 -04:00
|
|
|
M: object each-slot ( obj quot -- )
|
|
|
|
over class "slots" word-prop [
|
|
|
|
-rot [ >r swap first slot r> call ] 2keep
|
2005-02-20 19:03:37 -05:00
|
|
|
] each 2drop ;
|
|
|
|
|
|
|
|
: 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
|
2005-04-25 03:33:33 -04:00
|
|
|
#! in the image. If only one reference exists, find
|
|
|
|
#! something referencing that, and so on.
|
2005-02-20 20:38:24 -05:00
|
|
|
[ dupd refers? ] instances nip ;
|
2005-02-20 19:03:37 -05:00
|
|
|
|
2005-04-12 13:35:27 -04:00
|
|
|
: seq+ ( n index vector -- )
|
|
|
|
[ nth + ] 2keep set-nth ;
|
2005-02-18 20:37:01 -05:00
|
|
|
|
|
|
|
: heap-stat-step ( counts sizes obj -- )
|
2005-04-12 13:35:27 -04:00
|
|
|
[ dup size swap type rot seq+ ] keep
|
|
|
|
1 swap type rot seq+ ;
|
2005-02-18 20:37:01 -05:00
|
|
|
|
2005-07-24 22:59:23 -04:00
|
|
|
: heap-stats ( -- counts sizes )
|
2005-02-18 20:37:01 -05:00
|
|
|
#! Return a list of instance count/total size pairs.
|
|
|
|
num-types zero-vector num-types zero-vector
|
2005-07-24 22:59:23 -04:00
|
|
|
[ >r 2dup r> heap-stat-step ] each-object ;
|
2005-02-18 20:37:01 -05:00
|
|
|
|
2005-02-18 19:02:06 -05:00
|
|
|
: heap-stat. ( type instances bytes -- )
|
|
|
|
dup 0 = [
|
|
|
|
3drop
|
|
|
|
] [
|
2005-08-14 18:13:16 -04:00
|
|
|
rot type>class word-name write ": " write
|
2005-08-21 20:50:14 -04:00
|
|
|
pprint " bytes, " write
|
|
|
|
pprint " instances" print
|
2005-02-18 19:02:06 -05:00
|
|
|
] ifte ;
|
|
|
|
|
|
|
|
: heap-stats. ( -- )
|
|
|
|
#! Print heap allocation breakdown.
|
2005-07-24 22:59:23 -04:00
|
|
|
0 heap-stats [ >r >r dup r> r> heap-stat. 1 + ] 2each drop ;
|
2005-04-26 00:35:55 -04:00
|
|
|
|
|
|
|
: orphans ( word -- list )
|
|
|
|
#! Orphans are forgotten but still referenced.
|
2005-07-19 04:23:33 -04:00
|
|
|
[ word? ] instances [ interned? not ] subset ;
|