1 ! Copyright (C) 2007, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors words sequences math prettyprint kernel arrays io
4 io.styles namespaces assocs kernel.private strings combinators
5 sorting math.parser vocabs definitions tools.profiler.private
6 continuations generic compiler.units sets classes fry ;
10 [ t profiling call ] [ f profiling ] [ ] cleanup ;
12 : filter-counts ( alist -- alist' )
13 [ second 0 > ] filter ;
15 : map-counters ( obj quot -- alist )
16 { } map>assoc filter-counts ; inline
18 : counters ( words -- alist )
19 [ dup counter>> ] map-counters ;
21 : cumulative-counters ( obj quot -- alist )
22 '[ dup @ [ counter>> ] sigma ] map-counters ; inline
24 : vocab-counters ( -- alist )
25 vocabs [ words [ predicate? not ] filter ] cumulative-counters ;
27 : generic-counters ( -- alist )
28 all-words [ subwords ] cumulative-counters ;
30 : methods-on ( class -- methods )
31 dup implementors [ method ] with map ;
33 : class-counters ( -- alist )
34 classes [ methods-on ] cumulative-counters ;
36 : method-counters ( -- alist )
37 all-words [ subwords ] map concat counters ;
39 : profiler-usage ( word -- words )
40 [ smart-usage [ word? ] filter ]
41 [ compiled-generic-usage keys ]
42 [ compiled-usage keys ]
45 : usage-counters ( word -- alist )
46 profiler-usage counters ;
48 : counters. ( assoc -- )
49 sort-values simple-table. ;
52 "Call counts for all words:" print
53 all-words counters counters. ;
55 : vocab-profile. ( vocab -- )
56 "Call counts for words in the " write
57 dup dup vocab write-object
59 words counters counters. ;
61 : usage-profile. ( word -- )
62 "Call counts for words which call " write
65 usage-counters counters. ;
67 : vocabs-profile. ( -- )
68 "Call counts for all vocabularies:" print
69 vocab-counters counters. ;
71 : generic-profile. ( -- )
72 "Call counts for methods on generic words:" print
73 generic-counters counters. ;
75 : class-profile. ( -- )
76 "Call counts for methods on classes:" print
77 class-counters counters. ;
79 : method-profile. ( -- )
80 "Call counts for all methods:" print
81 method-counters counters. ;