1 ! Copyright (C) 2005, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs binary-search classes
4 classes.struct combinators combinators.smart continuations fry
5 generalizations generic grouping io io.styles kernel make math
6 math.order math.parser math.statistics memory memory.private
7 layouts namespaces parser prettyprint sequences
8 sequences.generalizations sorting splitting strings system vm
9 words hints hashtables ;
15 dup 0 < [ neg commas "-" prepend ] [
17 reverse 3 group "," join reverse
20 : kilobytes ( n -- str )
21 1024 /i commas " KB" append ;
23 : nanos>string ( n -- str )
24 1000 /i commas " µs" append ;
26 : copying-room. ( copying-sizes -- )
28 { "Size:" [ size>> kilobytes ] }
29 { "Occupied:" [ occupied>> kilobytes ] }
30 { "Free:" [ free>> kilobytes ] }
33 : nursery-room. ( data-room -- )
34 "- Nursery space" print nursery>> copying-room. ;
36 : aging-room. ( data-room -- )
37 "- Aging space" print aging>> copying-room. ;
39 : mark-sweep-table. ( mark-sweep-sizes -- )
41 { "Size:" [ size>> kilobytes ] }
42 { "Occupied:" [ occupied>> kilobytes ] }
43 { "Total free:" [ total-free>> kilobytes ] }
44 { "Contiguous free:" [ contiguous-free>> kilobytes ] }
45 { "Free block count:" [ free-block-count>> number>string ] }
48 : tenured-room. ( data-room -- )
49 "- Tenured space" print tenured>> mark-sweep-table. ;
51 : misc-room. ( data-room -- )
52 "- Miscellaneous buffers" print
54 { "Card array:" [ cards>> kilobytes ] }
55 { "Deck array:" [ decks>> kilobytes ] }
56 { "Mark stack:" [ mark-stack>> kilobytes ] }
62 "== Data heap ==" print nl
63 data-room data-heap-room memory>struct {
72 : heap-stat-step ( obj counts sizes -- )
73 [ [ class ] dip inc-at ]
74 [ [ [ size ] [ class ] bi ] dip at+ ] bi-curry* bi ;
78 : heap-stats ( -- counts sizes )
79 [ ] instances H{ } clone H{ } clone
80 [ '[ _ _ heap-stat-step ] each ] 2keep ;
83 heap-stats dup keys natural-sort standard-table-style [
84 [ { "Class" "Bytes" "Instances" } [ write-cell ] each ] with-row
88 dup pick at pprint-cell
94 : collect-gc-events ( quot -- gc-events )
96 [ ] [ disable-gc-events drop ] cleanup
97 disable-gc-events [ gc-event memory>struct ] map ; inline
101 : gc-op-string ( op -- string )
103 { collect-nursery-op [ "Copying from nursery" ] }
104 { collect-aging-op [ "Copying from aging" ] }
105 { collect-to-tenured-op [ "Copying to tenured" ] }
106 { collect-full-op [ "Mark and sweep" ] }
107 { collect-compact-op [ "Mark and compact" ] }
108 { collect-growing-heap-op [ "Grow heap" ] }
111 : (space-occupied) ( data-heap-room code-heap-room -- n )
113 [ [ nursery>> ] [ aging>> ] [ tenured>> ] tri [ occupied>> ] tri@ ]
118 : space-occupied-before ( event -- bytes )
119 [ data-heap-before>> ] [ code-heap-before>> ] bi (space-occupied) ;
121 : space-occupied-after ( event -- bytes )
122 [ data-heap-after>> ] [ code-heap-after>> ] bi (space-occupied) ;
124 : space-reclaimed ( event -- bytes )
125 [ space-occupied-before ] [ space-occupied-after ] bi - ;
127 TUPLE: gc-stats collections times ;
129 : <gc-stats> ( -- stats )
132 V{ } clone >>times ; inline
134 : compute-gc-stats ( events -- stats )
137 dup op>> _ [ drop <gc-stats> ] cache
138 [ 1 + ] change-collections
139 [ total-time>> ] dip times>> push
143 : gc-stats-table-row ( pair -- row )
145 [ first gc-op-string ] [
151 [ mean >integer nanos>string ]
152 [ median >integer nanos>string ]
153 [ infimum nanos>string ]
154 [ supremum nanos>string ]
160 : gc-stats-table ( stats -- table )
161 [ gc-stats-table-row ] map
162 { "" "Number" "Total" "Mean" "Median" "Min" "Max" } prefix ;
168 : gc-event. ( event -- )
170 { "Event type:" [ op>> gc-op-string ] }
171 { "Total time:" [ total-time>> nanos>string ] }
172 { "Space reclaimed:" [ space-reclaimed kilobytes ] }
176 gc-events get [ gc-event. nl ] each ;
179 gc-events get compute-gc-stats gc-stats-table simple-table. ;
183 { "Collections:" [ length commas ] }
184 { "Cards scanned:" [ [ cards-scanned>> ] map-sum commas ] }
185 { "Decks scanned:" [ [ decks-scanned>> ] map-sum commas ] }
186 { "Code blocks scanned:" [ [ code-blocks-scanned>> ] map-sum commas ] }
187 { "Total time:" [ [ total-time>> ] map-sum nanos>string ] }
188 { "Card scan time:" [ [ card-scan-time>> ] map-sum nanos>string ] }
189 { "Code block scan time:" [ [ code-scan-time>> ] map-sum nanos>string ] }
190 { "Data heap sweep time:" [ [ data-sweep-time>> ] map-sum nanos>string ] }
191 { "Code heap sweep time:" [ [ code-sweep-time>> ] map-sum nanos>string ] }
192 { "Compaction time:" [ [ compaction-time>> ] map-sum nanos>string ] }
195 SINGLETONS: +unoptimized+ +optimized+ +profiling+ +pic+ ;
199 { parameters read-only }
200 { relocation read-only }
203 { entry-point read-only } ;
205 TUPLE: code-blocks { blocks sliced-groups } { cache hashtable } ;
209 : code-block-type ( n -- type )
210 { +unoptimized+ +optimized+ +profiling+ +pic+ } nth ;
212 : <code-block> ( seq -- code-block )
219 [ tag-bits get shift ]
220 } spread code-block boa ; inline
222 : <code-blocks> ( seq -- code-blocks )
223 6 <sliced-groups> H{ } clone \ code-blocks boa ;
225 SYMBOL: code-heap-start
226 SYMBOL: code-heap-end
228 : in-code-heap? ( address -- ? )
229 code-heap-start get code-heap-end get between? ;
231 : (lookup-return-address) ( addr seq -- code-block )
232 [ entry-point>> <=> ] with search nip ;
234 HINTS: (lookup-return-address) code-blocks ;
238 M: code-blocks length blocks>> length ; inline
240 FROM: sequences.private => nth-unsafe ;
242 M: code-blocks nth-unsafe
243 [ cache>> ] [ blocks>> ] bi
244 '[ _ nth-unsafe <code-block> ] cache ; inline
246 INSTANCE: code-blocks immutable-sequence
248 : code-blocks ( -- blocks )
249 (code-blocks) <code-blocks> ;
251 : with-code-blocks ( quot -- )
254 [ \ code-blocks set ]
255 [ first entry-point>> code-heap-start set ]
256 [ last [ entry-point>> ] [ size>> ] bi + code-heap-end set ] tri
258 ] with-scope ; inline
260 : lookup-return-address ( addr -- code-block )
262 [ \ code-blocks get (lookup-return-address) ] [ drop f ] if ;
266 : code-block-stats ( code-blocks -- counts sizes )
267 H{ } clone H{ } clone
268 [ '[ [ size>> ] [ type>> ] bi [ nip _ inc-at ] [ _ at+ ] 2bi ] each ]
271 : blocks ( n -- str ) number>string " blocks" append ;
273 : code-block-table-row ( string type counts sizes -- triple )
274 [ at 0 or blocks ] [ at 0 or kilobytes ] bi-curry* bi 3array ;
276 : code-block-table. ( counts sizes -- )
279 { "Optimized code:" +optimized+ }
280 { "Unoptimized code:" +unoptimized+ }
281 { "Inline caches:" +pic+ }
282 { "Profiling stubs:" +profiling+ }
284 ] 2dip '[ _ _ code-block-table-row ] { } assoc>map
290 "== Code heap ==" print nl
291 code-room mark-sweep-sizes memory>struct mark-sweep-table. nl
292 code-blocks code-block-stats code-block-table. ;
295 data-room. nl code-room. ;