]> gitweb.factorcode.org Git - factor.git/blob - basis/tools/profiler/profiler.factor
Change a throw to rethrow so that we don't lose the original stack trace
[factor.git] / basis / tools / profiler / profiler.factor
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 ;
7 IN: tools.profiler
8
9 : profile ( quot -- )
10     [ t profiling call ] [ f profiling ] [ ] cleanup ;
11
12 : counters ( words -- assoc )
13     [ dup counter>> ] { } map>assoc ;
14
15 GENERIC: (profile.) ( obj -- )
16
17 TUPLE: usage-profile word ;
18
19 C: <usage-profile> usage-profile
20
21 M: word (profile.)
22     [ name>> "( no name )" or ] [ <usage-profile> ] bi write-object ;
23
24 TUPLE: vocab-profile vocab ;
25
26 C: <vocab-profile> vocab-profile
27
28 M: string (profile.)
29     dup <vocab-profile> write-object ;
30
31 M: method-body (profile.)
32     [ synopsis ] [ "method-generic" word-prop <usage-profile> ] bi
33     write-object ;
34
35 : counter. ( obj n -- )
36     [
37         [ [ (profile.) ] with-cell ] dip
38         [ number>string write ] with-cell
39     ] with-row ;
40
41 : counters. ( assoc -- )
42     [ second 0 > ] filter sort-values
43     standard-table-style [
44         [ counter. ] assoc-each
45     ] tabular-output ;
46
47 : profile. ( -- )
48     "Call counts for all words:" print
49     all-words counters counters. ;
50
51 : vocab-profile. ( vocab -- )
52     "Call counts for words in the " write
53     dup dup vocab write-object
54     " vocabulary:" print
55     words counters counters. ;
56
57 : usage-profile. ( word -- )
58     "Call counts for words which call " write
59     dup pprint
60     ":" print
61     [ smart-usage [ word? ] filter ]
62     [ compiled-generic-usage keys ]
63     [ compiled-usage keys ]
64     tri 3append prune counters counters. ;
65
66 : vocabs-profile. ( -- )
67     "Call counts for all vocabularies:" print
68     vocabs [
69         dup words
70         [ "predicating" word-prop not ] filter
71         [ counter>> ] map sum
72     ] { } map>assoc counters. ;
73
74 : method-profile. ( -- )
75     all-words [ subwords ] map concat
76     counters counters. ;