summaryrefslogtreecommitdiff
path: root/ipl/mprogs/exprsum.icn
blob: 802d3b6e46845ffdb88bbe869e224ffadab1bbd9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
############################################################################
#
#	File:     exprsum.icn
#
#	Subject:  Program to tabulate operator and function evaluation
#
#	Author:   Ralph E. Griswold
#
#	Date:     February 20, 1995
#
############################################################################
#
#   This file is in the public domain.
#
############################################################################
#
#  This tool tabulates operator and function activity.  It is called as
#
#	exprsum prog
#
#  where prog is a program compiled under MT Icon whose events are to
#  be tabulated.
#	
#  The options supported are:
#
#	-o s	write output to file s; default &output.
#
#	-t	record time spent in monitoring.
#
############################################################################
#
#  Requires:  Version 9 MT Icon and event monitoring.
#
############################################################################
#
#  Links:  evinit, evnames, options, procname
#
############################################################################
#
#  Includes:  evdefs.icn
#
############################################################################

link evaltree			# maintenance of call tree
link evinit			# event monitoring initialization
link evnames			# mapping of events to names
link options			# command-line options
link procname			# string name for procedure

$include "evdefs.icn"		# event code and mask definitions

global callcount
global calltbl
global failtbl
global namemap
global names
global output
global remvtbl
global resmtbl
global retntbl
global susptbl

$define NameColumn	14
$define	ValueColumn	10

procedure main(args)
   local opts, itime

   namemap := evnames()

   opts := options(args, "o:t")

   output := open(\opts["o"], "w") | &output
   if \opts["t"] then itime := &time

   #  Load and initialize the source program.

   EvInit(args) | stop("*** cannot load source program")

   #  Assign tables to for the various kinds of activity.

   every calltbl | retntbl | susptbl | failtbl | resmtbl | remvtbl := table(0)

   #  Process the events using the procedure note().

   evaltree(FncMask ++ OperMask, note)

   #  Format the results.

   format(output)

   write(output, "\nelapsed time: ", &time - \itime, "ms")

end

procedure format(output)

   write(output,
      left("name", NameColumn),
      right("calls", ValueColumn),
      right("returns", ValueColumn),
      right("suspends", ValueColumn),
      right("failures", ValueColumn),
      right("resumps", ValueColumn),
      right("removals", ValueColumn)
      )
   write(output)

   # sort names by number of calls

   names := sort(calltbl, 4)

   while callcount := pull(names) do {
      name := pull(names)
      write(output,
         left(name, NameColumn),
         right(callcount, ValueColumn),
         right(retntbl[name], ValueColumn),
         right(susptbl[name], ValueColumn),
         right(failtbl[name], ValueColumn),
         right(resmtbl[name], ValueColumn),
         right(remvtbl[name], ValueColumn)
         )
      }

   write(output,
      "\n",
      left("total", NameColumn),
      right(tblsum(calltbl), ValueColumn),
      right(tblsum(retntbl), ValueColumn),
      right(tblsum(susptbl), ValueColumn),
      right(tblsum(failtbl), ValueColumn),
      right(tblsum(resmtbl), ValueColumn),
      right(tblsum(remvtbl), ValueColumn)
      )

end

procedure note(new, old)

   case &eventcode of {
      !CallCodes:	calltbl[procname(new.node, 1)] +:= 1
      !ReturnCodes:	retntbl[procname(old.node, 1)] +:= 1
      !SuspendCodes:	susptbl[procname(old.node, 1)] +:= 1
      !FailCodes:	failtbl[procname(old.node, 1)] +:= 1
      !ResumeCodes:	resmtbl[procname(new.node, 1)] +:= 1
      !RemoveCodes:	remvtbl[procname(old.node, 1)] +:= 1
      }

   return

end

procedure tblsum(tbl)
   local count

   count := 0
   every count +:= !tbl

   return count

end