 ! Copyright (C) 2005, 2009 Slava Pestov.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: kernel sequences arrays generic assocs io math
-namespaces parser prettyprint strings io.styles words
-system sorting splitting grouping math.parser classes memory
-combinators fry vm specialized-arrays accessors continuations
-classes.struct generalizations ;
+USING: accessors arrays assocs classes classes.struct
+combinators combinators.smart continuations fry generalizations
+generic grouping io io.styles kernel make math math.parser
+math.statistics memory namespaces parser prettyprint sequences
+sorting specialized-arrays splitting strings system vm words ;
 SPECIALIZED-ARRAY: gc-event
 IN: tools.memory
 
 <PRIVATE
 
 : commas ( n -- str )
-    number>string
-    reverse 3 group "," join reverse ;
+    dup 0 < [ neg commas "-" prepend ] [
+        number>string
+        reverse 3 group "," join reverse
+    ] if ;
 
 : kilobytes ( n -- str )
     1024 /i commas " KB" append ;
 
 : micros>string ( n -- str )
-    commas " microseconds" append ;
+    commas " µs" append ;
 
 : fancy-table. ( obj alist -- )
     [ [ nip first ] [ second call( obj -- str ) ] 2bi 2array ] with map

    enable-gc-events [ ] [ disable-gc-events drop ] cleanup
    disable-gc-events byte-array>gc-event-array ; inline

: gc-op-string ( op -- string )
    {
        { collect-nursery-op      [ "copying from nursery" ] }
        { collect-aging-op        [ "copying from aging"   ] }
        { collect-to-tenured-op   [ "copying to tenured"   ] }
        { collect-full-op         [ "mark and sweep"       ] }
        { collect-compact-op      [ "mark and compact"     ] }
        { collect-growing-heap-op [ "grow heap"            ] }
    } case ;

: space-reclaimed ( event -- bytes )
    [ data-heap-before>> ] [ data-heap-after>> ] bi
    [ [ nursery>> ] [ aging>> ] [ tenured>> ] tri [ occupied>> ] tri@ + + ] bi@ - ;

: gc-event. ( event -- )
    {

        { "Total time:" [ total-time>> micros>string ] }
        { "Space reclaimed:" [ space-reclaimed kilobytes ] }
    } fancy-table. ;

: gc-stats. ( events -- )
    compute-gc-stats
    [ gc-stats-table simple-table. nl ]
    [ aggregate-stats-table simple-table. ]
    bi ;

: heap-sizes. ( events -- )
    heap-sizes simple-table. ;
