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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377 | ###############################################################################
# Copyright (c) 2017-2024 by Altair Engineering, Inc.
# All rights reserved.
#
# Altair Engineering, Inc. makes this software available as part of the Vision
# tool platform. As long as you are a licensee of the Vision tool platform
# you may make copies of the software and modify it to be used within the
# Vision tool platform, but you must include all of this notice on any copy.
# Redistribution without written permission to any third party, with or
# without modification, is not permitted.
# Altair Engineering, Inc. does not warrant that this software is error free
# or fit for any purpose. Altair Engineering, Inc. disclaims any liability for
# all claims, expenses, losses, damages and costs any user may incur as a
# result of using, copying or modifying the software.
# =============================================================================
# @userware
# Collect Statistics
# @section
# Create Reports
# @description
# Traverse Design and collect statistics.
# @files
# cust22/statistics.tcl
# @tag
# report
###############################################################################
##
# name from to
#
set binning {
"0.0..0.2" 0 2e-7
"0.3..0.5" 3e-7 5e-7
"1.0..3.0" 1e-6 3e-6
"4.0..7.0" 4e-6 7e-6
}
# -----------------------------------------------------------------------------
# _searchBin - Search for matching bin.
# -----------------------------------------------------------------------------
#
proc _searchBin {lvalue} {
global binning
foreach {name from to} $binning {
if {($lvalue >= $from) && ($lvalue <= $to)} {
return $name
}
}
return "?"
}
# -----------------------------------------------------------------------------
# _traverseAndCount - Traverse tree rooted at given 'top'.
# Count every primitive and every function.
# For transistors count every 'L' using buckets.
# Further speedup can be done by traversing and storing
# each module only once and add the counts on multiple
# instantiations.
# -----------------------------------------------------------------------------
#
proc _traverseAndCount {db top tabName} {
upvar 1 $tabName tab
$db foreach inst $top inst {
if {[$db isModule $inst]} {
set mod [$db moduleOf $inst]
_traverseAndCount $db $mod tab
}
##
# count functions
#
set func [$db primFuncOf $inst]
incr tab(func|$func)
##
# Count modules (subckt) and primitives (models)
#
set name [$db oid cname $inst]
if {[$db isModule $inst]} { set type "mod" } else { set type "prim" }
incr tab($type|$name)
##
# count length 'L'
#
set l [$db attr $inst getValue l]
if {$l != {}} {
set l [zdb formatvalue fromspice $l]
incr tab(lenfunc|$func|$l) ;# count L per func
incr tab(lenname|$name|$l) ;# count L per prim/mod-name
}
}
}
# -----------------------------------------------------------------------------
# _postprocessBinning - get all entries from tab with keys starting with $key
# ("lenfunc" or "lenname"). Second part of found keys
# contains "$func|$l" or "$name|$l". New counts get
# accumulated in given $binkey.
# -----------------------------------------------------------------------------
#
proc _postprocessBinning {tabName key binkey} {
upvar 1 $tabName tab
set keylen [expr {[string length $key] + 1}]
set result {}
foreach entry [array names tab $key|*] {
set str [string range $entry $keylen end] ;# func|L or name|L string
set f [split $str "|"]
set funcOrName [lindex $f 0]
set lvalue [lindex $f 1]
set count $tab($entry)
set bin [_searchBin $lvalue] ;# get bin for L value
incr tab($binkey|$funcOrName|$bin) $count
}
}
# -----------------------------------------------------------------------------
# _printSorted - print entries sorted by count
# -----------------------------------------------------------------------------
#
proc _printSorted {fp title res} {
puts $fp $title
foreach entry $res {
set name [lindex $entry 0]
set count [lindex $entry 1]
puts $fp [format " %-10s : %d" $name $count]
}
}
# -----------------------------------------------------------------------------
# _printAll -
# -----------------------------------------------------------------------------
#
proc _printAll {dlg results} {
##
# Open file
#
set fTypes {{"Text Files" .txt}}
set title "Save Statistic"
set fname [gui window fileDialog saveFile -parent $dlg $title $fTypes]
if {$fname eq ""} {
return
}
set fp [open $fname "w"]
##
# Print statistics
#
foreach {
no label key
} {
0 "Functions" func
1 "Primitives" prim
2 "Func-Binning" binfunc
3 "Name-Binning" binname
4 "Modules" mod
} {
set res [lindex $results $no]
_printSorted $fp $label $res
}
##
# Close file
#
close $fp
}
# -----------------------------------------------------------------------------
# _createListbox - Create listbox and insert entries sorted by count into list.
# -----------------------------------------------------------------------------
#
proc _createListbox {w tabName key} {
upvar 1 $tabName tab
##
# get name/count pairs
#
set keylen [expr {[string length $key] + 1}]
set result {}
foreach entry [array names tab $key|*] {
set name [string range $entry $keylen end]
lappend result [list $name $tab($entry)]
}
if {$result == {}} {
return {}
}
##
# create listbox
#
set listbox $w.list
set width 12
listbox $listbox -font TkFixedFont
##
# sort decreasing
#
set result [lsort -index 1 -integer -decreasing $result]
##
# fill listbox
#
foreach entry $result {
set name [lindex $entry 0]
set count [lindex $entry 1]
if {[string length $name] > 77} {
set name [string replace $name 75 end "..."]
}
set t [format "%s %d" $name $count]
$listbox insert end $t
set tw [string length $t]
if {$tw > 50} {
set tw 50
}
if {$width < $tw} {
set width $tw
}
}
incr width 2
$listbox configure -width $width
pack $listbox -expand true -fill both -side left
set height [llength $$result]
if {$height <= 12} {
$listbox configure -height $height
} else {
$listbox configure -height 12
##
# build scrollbar.
#
set sb $w.ysb
ttk::scrollbar $sb -orient vertical -command "$listbox yview"
$listbox configure -yscrollcommand "$sb set"
pack $sb -expand false -fill y -side right
}
return $result
}
# -----------------------------------------------------------------------------
# statistics - main entry
# -----------------------------------------------------------------------------
#
proc statistics {{top {}}} {
set results {}
##
# Get current db
#
set db [gui database get]
if {$db == ""} {
return
}
##
# We take the current module's root.
#
if {$top == {}} {
set top [gui schem getCurrentModule]
if {$top != ""} {
while {[$db hasParentMod $top]} {
set top [$db parentModule $top]
}
}
}
if {$top == ""} {
gui console printError "No database top module found"
return
}
##
# start recursive DFS traversal of hierarchy tree
#
_traverseAndCount $db $top Tab
##
# Postprocessing binning
#
_postprocessBinning Tab lenfunc binfunc
_postprocessBinning Tab lenname binname
##
# Create dialog
#
set dlg .statdlg
catch {destroy $dlg}
toplevel $dlg -class Dialog
set l $dlg.top
ttk::label $l -text "Root Module: [$db oid module $top]" -anchor w \
-relief groove
grid $l -row 0 -column 0 -columnspan 3 -sticky we -padx 5 -pady 2
foreach {
no label row col key
} {
0 "Functions" 1 0 func
1 "Primitives" 2 0 prim
2 "Func-Binning" 1 1 binfunc
3 "Name-Binning" 2 1 binname
4 "Modules" 1 2+2 mod
} {
##
# build label and frame.
#
set f $dlg.f$no
ttk::frame $f
set l $f.label
ttk::label $l -text $label -anchor w -font TkFixedFont
pack $l -side top -fill x -padx 5
ttk::frame $f.f
pack $f.f -side top -expand true -fill both -padx 3
##
# build listbox.
#
set res [_createListbox $f.f Tab $key]
lappend results $res
##
# Grid the frame.
#
if {[string match "*+*" $col]} {
set spanInfo [split $col "+"]
set col [lindex $spanInfo 0]
set rowSpan [list -rowspan [lindex $spanInfo 1]]
} else {
set rowSpan {}
}
grid $f -row $row -column $col -sticky news {*}$rowSpan
grid rowconfigure [winfo parent $f] $row -weight 1
grid columnconfigure [winfo parent $f] $col -weight 1
}
##
# build button line.
#
ttk::frame $dlg.f
ttk::button $dlg.f.ok -text "OK"
ttk::button $dlg.f.save -text "Save to File" \
-command [list _printAll $dlg $results]
pack $dlg.f.ok $dlg.f.save -side left -padx 2 -pady 2
bind $dlg <Return> "$dlg.f.ok invoke"
grid $dlg.f -row 3 -column 0 -columnspan 3
set title "Statistic"
gui window dialog \
$dlg \
$title \
-place "MOUSE" \
-focus $dlg.f.ok \
-buttons [list $dlg.f.ok]
destroy $dlg
}
# =============================================================================
# Extend the main menu and add a "Statistics" entry.
# =============================================================================
#
gui menu command {"Userware" "Statistics"} {statistics}
|