SCM Repository
Annotation of /trunk/src/compiler/common/stats.sml
Parent Directory
|
Revision Log
Revision 3349 - (view) (download)
1 : | jhr | 285 | (* stats.sml |
2 : | * | ||
3 : | jhr | 3349 | * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu) |
4 : | * | ||
5 : | * COPYRIGHT (c) 2015 The University of Chicago | ||
6 : | jhr | 285 | * All rights reserved. |
7 : | * | ||
8 : | * Collect statistics about the various optimizations and transformations. | ||
9 : | * | ||
10 : | * This module is lifted from the Manticore project | ||
11 : | *) | ||
12 : | |||
13 : | structure Stats :> sig | ||
14 : | |||
15 : | type counter | ||
16 : | |||
17 : | jhr | 1232 | val reportStats : bool ref |
18 : | |||
19 : | jhr | 285 | val newCounter : string -> counter |
20 : | |||
21 : | val tick : counter -> unit (* increment by one *) | ||
22 : | val bump : (counter * int) -> unit (* increment by an integer *) | ||
23 : | val count : counter -> int (* return current count *) | ||
24 : | val name : counter -> string (* return counter's name *) | ||
25 : | val reset : counter -> unit (* reset counter to zero *) | ||
26 : | |||
27 : | val sum : {from : counter, to : counter} -> int | ||
28 : | val sumAll : unit -> int | ||
29 : | |||
30 : | val resetAll : unit -> unit | ||
31 : | jhr | 1232 | val report : unit -> unit |
32 : | jhr | 285 | |
33 : | end = struct | ||
34 : | |||
35 : | structure A = Array | ||
36 : | |||
37 : | val maxNumCounters = 512 | ||
38 : | |||
39 : | val names = A.array(maxNumCounters, "") | ||
40 : | val counters = A.array(maxNumCounters, 0) | ||
41 : | |||
42 : | val nextCounter = ref 0 | ||
43 : | val reportStats = ref false | ||
44 : | |||
45 : | type counter = int | ||
46 : | |||
47 : | fun newCounter name = let | ||
48 : | val n = !nextCounter | ||
49 : | in | ||
50 : | if (n < maxNumCounters) | ||
51 : | then (A.update(names, n, name); nextCounter := n+1; n) | ||
52 : | else raise Fail "too many counters" | ||
53 : | end | ||
54 : | |||
55 : | fun tick i = ( | ||
56 : | jhr | 340 | if Log.enabled() |
57 : | then Log.msg(concat["++ ", A.sub(names, i), "\n"]) | ||
58 : | jhr | 285 | else (); |
59 : | A.update(counters, i, A.sub(counters, i)+1)) | ||
60 : | fun bump (i, n) = A.update(counters, i, A.sub(counters, i)+n) | ||
61 : | fun count i = A.sub(counters, i) | ||
62 : | fun name i = A.sub(names, i) | ||
63 : | fun reset i = A.update(counters, i, 0) | ||
64 : | |||
65 : | fun sum {from : counter, to : counter} = | ||
66 : | if (to < from) | ||
67 : | then 0 | ||
68 : | else ArraySlice.foldl | ||
69 : | (fn (n, s) => n+s) 0 | ||
70 : | (ArraySlice.slice(counters, from, SOME((to-from)+1))) | ||
71 : | |||
72 : | fun sumAll () = sum{from = 0, to = (!nextCounter - 1)} | ||
73 : | |||
74 : | fun resetAll () = A.modify (fn _ => 0) counters | ||
75 : | |||
76 : | jhr | 1232 | fun report () = if !reportStats |
77 : | jhr | 285 | then let |
78 : | jhr | 1232 | fun prl l = Log.msg(String.concat l) |
79 : | jhr | 285 | fun lp i = if (i < !nextCounter) |
80 : | then let | ||
81 : | val n = Array.sub(counters, i) | ||
82 : | in | ||
83 : | if n > 0 | ||
84 : | then prl [ | ||
85 : | StringCvt.padRight #" " 31 (Array.sub(names, i)), | ||
86 : | " ", Int.toString n, "\n" | ||
87 : | ] | ||
88 : | else (); | ||
89 : | lp (i+1) | ||
90 : | end | ||
91 : | else () | ||
92 : | in | ||
93 : | jhr | 1232 | lp 0 |
94 : | jhr | 285 | end |
95 : | else () | ||
96 : | |||
97 : | end | ||
98 : |
root@smlnj-gforge.cs.uchicago.edu | ViewVC Help |
Powered by ViewVC 1.0.0 |