]> gitweb.factorcode.org Git - factor.git/blob - basis/tools/profiler/profiler.factor
c79d8b443c00799363ba63611c31335b7f7e4fef
[factor.git] / basis / tools / profiler / profiler.factor
1 ! Copyright (C) 2007, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors words sequences math prettyprint kernel arrays
4 io io.styles namespaces assocs kernel.private strings
5 combinators sorting math.parser vocabs definitions
6 tools.profiler.private tools.crossref continuations generic
7 compiler.units compiler.crossref sets classes fry ;
8 FROM: sets => members ;
9 IN: tools.profiler
10
11 : profile ( quot -- )
12     [ t profiling call ] [ f profiling ] [ ] cleanup ; inline
13
14 : filter-counts ( alist -- alist' )
15     [ second 0 > ] filter ;
16
17 : map-counters ( obj quot -- alist )
18     { } map>assoc filter-counts ; inline
19
20 : counters ( words -- alist )
21     [ dup counter>> ] map-counters ;
22
23 : cumulative-counters ( obj quot -- alist )
24     '[ dup @ [ counter>> ] map-sum ] map-counters ; inline
25
26 : vocab-counters ( -- alist )
27     vocabs [ words [ predicate? not ] filter ] cumulative-counters ;
28
29 : generic-counters ( -- alist )
30     all-words [ subwords ] cumulative-counters ;
31
32 : methods-on ( class -- methods )
33     dup implementors [ method ] with map ;
34
35 : class-counters ( -- alist )
36     classes [ methods-on ] cumulative-counters ;
37
38 : method-counters ( -- alist )
39     all-words [ subwords ] map concat counters ;
40
41 : profiler-usage ( word -- words )
42     [ smart-usage [ word? ] filter ]
43     [ generic-call-sites-of keys ]
44     [ effect-dependencies-of keys ]
45     tri 3append members ;
46
47 : usage-counters ( word -- alist )
48     profiler-usage counters ;
49
50 : counters. ( assoc -- )
51     sort-values simple-table. ;
52
53 : profile. ( -- )
54     "Call counts for all words:" print
55     all-words counters counters. ;
56
57 : vocab-profile. ( vocab -- )
58     "Call counts for words in the " write
59     dup dup vocab write-object
60     " vocabulary:" print
61     words counters counters. ;
62
63 : usage-profile. ( word -- )
64     "Call counts for words which call " write
65     dup pprint
66     ":" print
67     usage-counters counters. ;
68
69 : vocabs-profile. ( -- )
70     "Call counts for all vocabularies:" print
71     vocab-counters counters. ;
72
73 : generic-profile. ( -- )
74     "Call counts for methods on generic words:" print
75     generic-counters counters. ;
76
77 : class-profile. ( -- )
78     "Call counts for methods on classes:" print
79     class-counters counters. ;
80
81 : method-profile. ( -- )
82     "Call counts for all methods:" print
83     method-counters counters. ;