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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521 | ###############################################################################
# Copyright (c) 2018-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.
# =============================================================================
# @plugin
# Compute and Display Hierarchy Statistics
# @namespace
# HierarchyStatistics
# @section
# Analyze the Loaded Database
# @description
# Compute and display statistics about the design hierarchy, e.g. number
# of instances of each cell, depth of hierarchical modules, etc.
# The statistics can also be exported as a CSV file.
#
# The plugin script can also be run in batch mode via:
# starsh hierarchyStatistics.tcl -batch ZDB_FILE OUTPUT_FILE
# @files
# hierarchyStatistics.tcl
# @example
# demo/rtl/aquarius/aquarius.f
# @cmdline
# -userware @files[0]
# -F @example[0]
# @tag
# rtl spice verilog gui report
###############################################################################
namespace eval HierarchyStatistics {
variable _Name "Hierarchy Statistics"
variable _W ""
variable _Show
variable _Table
variable _Depth
variable _Insts
variable _Count
variable _Modules
}
# =============================================================================
# Init - Initialize the plugin.
# =============================================================================
#
proc HierarchyStatistics::Init {} {
variable _Name
##
# Add menu entries.
#
gui menu command [list $_Name "Show"] \
[list ::HierarchyStatistics::_showWidget]
gui menu customizeEntry [list $_Name "Show"] \
[list ::HierarchyStatistics::_customizeMenu "show"]
gui menu command [list $_Name "Hide"] \
[list ::HierarchyStatistics::_closeWidget]
gui menu customizeEntry [list $_Name "Hide"] \
[list ::HierarchyStatistics::_customizeMenu "hide"]
_showWidget
}
# =============================================================================
# Finit - Finalize the plugin.
# =============================================================================
#
proc HierarchyStatistics::Finit {} {
variable _Name
_closeWidget
gui menu removeEntry [list $_Name]
}
# =============================================================================
# Batch - Batch mode.
# =============================================================================
#
proc HierarchyStatistics::Batch {db channel} {
set table {}
dict for {name data} [HierarchyStatistics::_compute $db] {
set row [list $name]
lappend row [dict get $data type]
lappend row [dict get $data count]
lappend row [dict get $data depth]
lappend row [dict get $data ports]
lappend row [dict get $data children]
lappend row [dict get $data childrenR]
lappend table $row
}
HierarchyStatistics::_printCSVHeader $channel
foreach row [lsort -index 6 -decreasing -dictionary $table] {
HierarchyStatistics::_printCSVRow $channel $row
}
}
# -----------------------------------------------------------------------------
# _showWidget -
# -----------------------------------------------------------------------------
#
proc HierarchyStatistics::_showWidget {} {
variable _Name
variable _W
variable _Show
variable _Table
variable _Depth
variable _Insts
variable _Count
variable _Modules
if {$_W ne ""} {
return
}
set _Show All
set _Depth [dict create]
set _Insts [dict create]
set _Count [dict create]
set _Modules [list]
set _W [gui window insertCustomWidget \
-pluginNamespace "HierarchyStatistics" \
-destroyCallback ::HierarchyStatistics::_onWidgetClosed \
$_Name]
ttk::frame $_W.buttons
ttk::button $_W.buttons.refresh \
-text Refresh \
-command HierarchyStatistics::_refresh
ttk::button $_W.buttons.export \
-text "Export (CSV)" \
-command HierarchyStatistics::_export
ttk::combobox $_W.buttons.filter \
-textvariable HierarchyStatistics::_Show \
-values {All Modules Primitives} \
-state readonly
bind $_W.buttons.filter <<ComboboxSelected>> \
HierarchyStatistics::_updateFilter
grid $_W.buttons.refresh \
-row 0 -column 0 -sticky nws -pady 2 -padx 4
grid $_W.buttons.export \
-row 0 -column 1 -sticky nws -pady 2 -padx 4
grid [ttk::label $_W.buttons.filter_label -text "Show:"] \
-row 0 -column 2 -sticky nws -pady 2 -padx 2
grid $_W.buttons.filter \
-row 0 -column 3 -sticky nws -pady 2 -padx 2
grid rowconfigure $_W.buttons 0 -weight 1
grid columnconfigure $_W.buttons 3 -weight 1
ttk::frame $_W.l
set _Table $_W.l.table
tablelist::tablelist $_Table \
-columns { \
0 "Name" left \
0 "Type" left \
0 "Count" right \
0 "Depth" right \
0 "Ports" right \
0 "Children" right \
0 "Children*" right \
} \
-labelcommand tablelist::sortByColumn \
-stripebackground #f0f0f0
$_Table columnconfigure 0 -sortmode dictionary
$_Table columnconfigure 1 -sortmode dictionary
$_Table columnconfigure 2 -sortmode integer
$_Table columnconfigure 3 -sortmode integer
$_Table columnconfigure 4 -sortmode integer
$_Table columnconfigure 5 -sortmode integer
$_Table columnconfigure 6 -sortmode integer
$_Table sortbycolumn 2 -decreasing
bind [$_Table bodytag] <Double-1> {HierarchyStatistics::_double}
gui window autoscroll $_W.l $_Table
grid $_W.buttons -row 0 -column 0 -sticky news
grid $_W.l -row 1 -column 0 -sticky news
grid rowconfigure $_W 1 -weight 1
grid columnconfigure $_W 0 -weight 1
gui database runAndRegisterChangedCallback HierarchyStatistics::_refresh
}
# -----------------------------------------------------------------------------
# _closeWidget -
# -----------------------------------------------------------------------------
#
proc HierarchyStatistics::_closeWidget {} {
variable _Name
variable _W
if {$_W ne ""} {
gui window removeCustomWidget $_Name
}
}
# -----------------------------------------------------------------------------
# _onWidgetClosed -
# -----------------------------------------------------------------------------
#
proc HierarchyStatistics::_onWidgetClosed {} {
variable _W
if {$_W eq ""} {
return
}
set _W ""
gui database removeChangedCallback HierarchyStatistics::_refresh
}
# -----------------------------------------------------------------------------
# _customizeMenu -
# -----------------------------------------------------------------------------
#
proc HierarchyStatistics::_customizeMenu {mode menu} {
variable _W
switch -exact -- $mode {
"show" {
set state [expr {($_W eq "") ? "normal" : "disabled"}]
}
"hide" {
set state [expr {($_W eq "") ? "disabled" : "normal"}]
}
default {
error "Bad mode: $mode"
}
}
$menu entryconfigure end -state $state
}
# -----------------------------------------------------------------------------
# _compute - Compute the statistics table.
# -----------------------------------------------------------------------------
#
proc HierarchyStatistics::_compute {db} {
set table [dict create]
$db oper tsort
set cells {}
$db foreach primitive primitive_oid {
set name [$db oid oname $primitive_oid]
lappend cells $name
dict set table $name type [$db oid type $primitive_oid]
dict set table $name count 0
dict set table $name depth 0
dict set table $name ports [$db count port $primitive_oid]
dict set table $name children 0
dict set table $name childrenR 0
}
$db foreach module module_oid {
set name [$db oid oname $module_oid]
lappend cells $name
set type [$db oid type $module_oid]
if {[$db flag $module_oid is undefined]} {
set type "blackbox"
}
set depth 0
set children [$db count inst $module_oid]
set childrenR $children
$db foreach inst $module_oid inst_oid {
set cell_name [$db oid cname $inst_oid]
incr childrenR [dict get $table $cell_name childrenR]
set cell_depth [dict get $table $cell_name depth]
set depth [expr {max($depth, (1 + $cell_depth))}]
}
dict set table $name type $type
dict set table $name count 0
dict set table $name depth $depth
dict set table $name ports [$db count port $module_oid]
dict set table $name children $children
dict set table $name childrenR $childrenR
}
foreach name [lreverse $cells] {
set count [dict get $table $name count]
if {$count == 0} {
set count 1
}
set cell_oid [$db search cell $name]
if {[dict get $table $name type] eq "module"} {
$db foreach inst $cell_oid inst_oid {
set cell_name [$db oid cname $inst_oid]
set cell_count [dict get $table $cell_name count]
dict set table $cell_name count [expr {$count + $cell_count}]
}
}
}
return $table
}
# -----------------------------------------------------------------------------
# _refresh - Refresh the cell table with statistics from the current database.
# -----------------------------------------------------------------------------
#
proc HierarchyStatistics::_refresh {{db {}}} {
variable _Table
variable _Count
variable _Depth
variable _Insts
variable _Modules
set sortcolumn [$_Table sortcolumn]
set sortorder [$_Table sortorder]
$_Table delete 0 end
if {$db == {}} {
set db [gui database get]
}
if {$db == {}} {
return
}
set table [HierarchyStatistics::_compute $db]
dict for {name data} $table {
$_Table insert end [list \
$name \
[dict get $data type] \
[dict get $data count] \
[dict get $data depth] \
[dict get $data ports] \
[dict get $data children] \
[dict get $data childrenR] \
]
}
if {$sortcolumn != -1} {
$_Table sortbycolumn $sortcolumn -$sortorder
} else {
$_Table resetsortinfo
}
HierarchyStatistics::_updateFilter
}
# -----------------------------------------------------------------------------
# _updateFilter - Update the list of shown cells depending on the selected
# filter option.
# -----------------------------------------------------------------------------
#
proc HierarchyStatistics::_updateFilter {} {
variable _Show
variable _Table
switch -exact -- $_Show {
"All" {
set hide [dict create blackbox 0 module 0 primitive 0]
}
"Modules" {
set hide [dict create blackbox 0 module 0 primitive 1]
}
"Primitives" {
set hide [dict create blackbox 1 module 1 primitive 0]
}
default {
error "bad switch: $_Show"
}
}
set index 0
foreach row [$_Table get 0 end] {
set type [lindex $row 1]
$_Table rowconfigure $index -hide [dict get $hide $type]
incr index
}
}
# -----------------------------------------------------------------------------
# _double - Double click handler (show the clicked cell in the schematics view).
# -----------------------------------------------------------------------------
#
proc HierarchyStatistics::_double {} {
variable _Table
set db [gui database get]
if {$db == {}} {
return
}
set name [$_Table cellcget [$_Table curselection],0 -text]
set type [$_Table cellcget [$_Table curselection],1 -text]
if {$type eq "blackbox"} {
set type "module"
}
set oid [$db search $type $name]
gui window show Schem
if {[$db isModule $oid] && [$db isTop $oid]} {
gui schem setCurrentModule $oid
gui tree setCurrentModule $oid
gui schem zoom fullfit
} else {
gui goto [list $oid]
}
}
# -----------------------------------------------------------------------------
# _printCSVHeader - Print a CSV file header.
# -----------------------------------------------------------------------------
#
proc HierarchyStatistics::_printCSVHeader {channel} {
puts $channel "Name,Type,Count,Depth,Ports,Children,Children*"
}
# -----------------------------------------------------------------------------
# _printCSVRow - Print a CSV file row.
# -----------------------------------------------------------------------------
#
proc HierarchyStatistics::_printCSVRow {channel row} {
if {[llength $row] != 7} {
error "bad row: $row"
}
##
# quote string columns (name, type)
#
set row [lreplace $row 0 0 \"[lindex $row 0]\"]
set row [lreplace $row 1 1 \"[lindex $row 1]\"]
puts $channel [join $row ","]
}
# -----------------------------------------------------------------------------
# _export - Export the current table contents to a CSV file.
# -----------------------------------------------------------------------------
#
proc HierarchyStatistics::_export {} {
variable _Table
##
# ask the user for an export file name.
#
set title "Export Hierarchy Statistics as CSV"
set file_types {{"CSV Files" .csv}}
set file_name [gui window fileDialog saveFile $title $file_types]
if {$file_name eq ""} {
return
}
set out [open $file_name w]
HierarchyStatistics::_printCSVHeader $out
set index 0
foreach row [$_Table get 0 end] {
if {![$_Table rowcget $index -hide]} {
HierarchyStatistics::_printCSVRow $out $row
}
incr index
}
close $out
}
##
# Switch between Batch mode and GUI/Plugin mode.
#
if {[lindex $argv 0] eq "-batch"} {
##
# Batch mode
#
if {[llength $argv] != 3} {
puts stderr \
"USAGE: ... hierarchyStatistics.tcl -batch ZDB_FILE OUTPUT_FILE"
puts stderr \
" If OUTPUT_FILE is \"-\", print to stdout."
exit 1
}
set db_file [lindex $argv 1]
set out_file [lindex $argv 2]
set db [zdb open $db_file]
if {$out_file ne "-"} {
set f [open $out_file w]
HierarchyStatistics::Batch $db $f
close $f
} else {
HierarchyStatistics::Batch $db stdout
}
$db close
} else {
##
# GUI/Plugin mode
#
HierarchyStatistics::Init
}
|