-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsageview
executable file
·777 lines (634 loc) · 21.9 KB
/
sageview
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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
#!/bin/sh
# \
exec /usr/local/bin/wish8.0 $0 -- $@
#
namespace eval SageViewer {
variable datafile "./sage.out"
variable totaltimes
variable proccounts
variable timers
variable totaltime
variable program_name
variable program_args
variable absolute
variable coretime
variable sagetime
variable stat_absolute
variable stat_sage
variable stat_core
variable rcfile
# GUI related fields
variable top
variable dataLB
# config related
variable dataview "proctime"
variable units "percent"
variable precision "2"
variable printcmd "lpr"
variable includewait 1
# filter stuff
variable showlist "*"
variable showexptype "match"
variable excllist ""
variable exclexptype "match"
variable exclnumcalls 0
variable exclpercentage 0.0
proc main {argv} {
parseArgs $argv
readData
createWindow
run
}
proc parseArgs {argv} {
variable datafile
variable rcfile
global tcl_platform
global env
if { $tcl_platform(platform) == "windows" } {
set rcfile $env(HOME)/sageview.ini
} {
set rcfile ~/.sageview
}
# parse the command line options
for {set argc 0} {[string match -* [lindex $argv $argc]]} {incr argc} {
switch -exact -- [lindex $argv $argc] {
-V -
-version {
puts "sageviewer version %%VERSION%%"
exit 0
}
-h -
-help {
puts "
syntax: sageview ?sageviewer options? program ?options?
sageviewer options:
-V | -version
Prints version of sageviewer and exits
-d | -data <file>
Specifies sage input data file. Default is ./sage.out
-rc <rcfile>
Specifies the initialization file to use for reading and saving.
Default is $rcfile
"
exit 0
}
-d -
-data {
incr argc
set datafile [lindex $argv $argc]
}
-rc {
incr argc
set rcfile [lindex $argv $argc]
}
default {
puts stderr "unknown option: '[lindex $argv $argc]'"
exit 1
}
}
}
}
proc readData {} {
variable datafile
variable totaltimes
variable proccounts
variable timers
variable totaltime
variable program_name
variable program_args
variable absolute
variable coretime
variable sagetime
# source in the data file to populate the data arrays
source $datafile
if { ! [info exists VERSION] } {
tk_dialog .error Error "No version information in sage data file" error 0 Exit
exit 1
} else {
switch $VERSION {
1.0 {
}
default {
tk_dialog .error Error "This sage viewer does not support sage data version $VERSION" error 0 Exit
exit 1
}
}
}
}
proc createWindow {} {
variable dataLB
wm withdraw .
# create the toplevel window
set top [toplevel .top]
wm title $top "SageViewer"
# create the menubar frame
set menubarF [frame .top.mbf]
pack $menubarF -side top -fill x
# menubuttons
#
# Sage
set mb [menubutton $menubarF.mbSage -text Sage -underline 0]
pack $mb -side left -padx 4
set m [menu $mb.m]
$mb configure -menu $m
$m add command -label "Print..." -command [namespace code printwindow]
$m add command -label "Exit" -command "destroy .top"
# View
set mb [menubutton $menubarF.mbView -text View -underline 0]
pack $mb -side left -padx 4
set m [menu $mb.m]
$mb configure -menu $m
set mcdata [menu $m.mcdata]
set var [namespace current]::dataview
set cmd [namespace current]::fillData
$mcdata add radiobutton -label "Proc time" -variable $var -value "proctime" -command $cmd
$mcdata add radiobutton -label "Proc time & descendants" -variable $var -value "proctimedesc" -command $cmd
$mcdata add radiobutton -label "Number of calls" -variable $var -value "numcalls" -command $cmd
set var [namespace current]::includewait
$mcdata add separator
$mcdata add checkbutton -label "Include .wait. in calculations" -variable $var -command $cmd
$m add cascade -label "Data" -menu $mcdata
set mcunits [menu $m.mcunits]
set var [namespace current]::units
$mcunits add radiobutton -label "Clicks" -variable $var -value "clicks" -command $cmd
$mcunits add radiobutton -label "Seconds" -variable $var -value "seconds" -command $cmd
$mcunits add radiobutton -label "% of .total." -variable $var -value "percent" -command $cmd
$m add cascade -label "Units" -menu $mcunits
set mcprec [menu $m.mcprec]
set var [namespace current]::precision
$mcprec add radiobutton -label "0." -variable $var -value "0" -command $cmd
$mcprec add radiobutton -label "0.0" -variable $var -value "1" -command $cmd
$mcprec add radiobutton -label "0.00" -variable $var -value "2" -command $cmd
$mcprec add radiobutton -label "0.000" -variable $var -value "3" -command $cmd
$mcprec add radiobutton -label "0.0000" -variable $var -value "4" -command $cmd
$mcprec add radiobutton -label "0.00000" -variable $var -value "5" -command $cmd
$m add cascade -label "Precision" -menu $mcprec
$m add separator
$m add command -label "Save Settings" -command [namespace code savesettings]
# Filter
set mb [menubutton $menubarF.mbFilter -text Filter -underline 0]
pack $mb -side left -padx 4
set m [menu $mb.m]
$mb configure -menu $m
$m add command -label "Show procs..." -command [namespace code showdlg]
$m add command -label "Exclude procs..." -command [namespace code excldlg]
# help
set mb [menubutton $menubarF.mbHelp -text Help -underline 0]
pack $mb -side right -padx 2
set m [menu $mb.m]
$mb configure -menu $m
$m add command -label "About" -command [namespace code aboutwindow]
# create the frame for the data listbox
set bodyF [frame $top.bodyF]
pack $bodyF -side top -fill both -expand yes
# stats
variable program_name
variable program_args
set statsF [labelledframe $bodyF.statsF "Program Stats"]
grid [label $statsF.l1 -text "program:"] -row 0 -column 0 -sticky e
grid [label $statsF.l2 -text "$program_name $program_args"] -row 0 -column 1 -sticky w
grid [label $statsF.l3 -text "total execution time:"] -row 1 -column 0 -sticky e
grid [label $statsF.l4 -textvariable [namespace current]::stat_absolute] -row 1 -column 1 -sticky w
grid [label $statsF.l5 -text "sage overhead:"] -row 2 -column 0 -sticky e
grid [label $statsF.l6 -textvariable [namespace current]::stat_sage] -row 2 -column 1 -sticky w
grid [label $statsF.l7 -text "Tcl/Tk core time:"] -row 3 -column 0 -sticky e
grid [label $statsF.l8 -textvariable [namespace current]::stat_core] -row 3 -column 1 -sticky w
# listbox
set dataF [frame $bodyF.dataF]
pack $dataF -side top -fill both -expand yes -pady 6
set dataLBF [frame $dataF.f]
pack $dataLBF -side top -fill both -expand yes
set dataLB [listbox $dataLBF.dataLB -width 80 \
-yscroll [list $dataLBF.s1 set] -xscroll [list $dataF.s set]]
pack $dataLB -side left -fill both -expand yes
set font [font create -family Courier]
$dataLB configure -font $font
# Y scrollbar
set dataSB [scrollbar $dataLBF.s1 -command [list $dataLB yview]]
pack $dataSB -side left -fill y
# X scrollbar
set dataSB [scrollbar $dataF.s -command [list $dataLB xview] -orient h]
pack $dataSB -side top -fill x
# the find entry
set findF [frame $bodyF.findF]
pack $findF -side top -pady 6 -fill both
set l [label $findF.l -text "find:"]
pack $l -side left
set e [entry $findF.e -width 20]
pack $e -side left
bind $e <Return> "[namespace current]::find_cmd $e"
set b [button $findF.b1 -text "find it" -command "[namespace current]::find_cmd $e"]
pack $b -side left -padx 6
# proc details button
set b [button $findF.b2 -text "Show Proc Details" -command "[namespace current]::show_details"]
pack $b -side right -padx 7
# read in any config settings
readsettings
# fill in the listbox with the data
fillData
}
proc show_details {} {
tk_dialog .msg "Unfinished Work" "This feature is not complete" error 0 ok
}
proc find_cmd {e} {
variable dataLB
set find [$e get]
# get the current selection
if { [$dataLB curselection] == "" } {
set index 0
} {
set index [$dataLB curselection]
incr index
if { $index >= [$dataLB index end] } {
tk_dialog .msg "Text Not Found" "The text '$find' was not found" error 0 "ok"
return
}
}
# get the listbox elements
set lbdata [$dataLB get $index end]
# search each one for the data
foreach entry $lbdata {
if { [string first $find $entry] != -1 } {
# set the current selection here
$dataLB selection clear 0 end
$dataLB selection set $index $index
$dataLB see $index
return
}
incr index
}
# didn't find it
tk_dialog .msg "Text Not Found" "The text '$find' was not found" error 0 "ok"
}
proc fillData {} {
variable timers
variable totaltimes
variable proccounts
variable totaltime
variable dataview
variable dataLB
variable units
variable precision
variable absolute
variable coretime
variable sagetime
variable stat_absolute
variable stat_sage
variable stat_core
variable includewait
variable showlist; variable excllist
variable showexptype; variable exclexptype
variable exclnumcalls; variable exclpercentage
# modify the absolute value if we don't want wait time included
if { ! $includewait } {
set modabsolute [expr {$absolute - $timers(.wait.)}]
set modtotaltime [expr {$totaltime - $timers(.wait.)}]
} {
set modabsolute $absolute
set modtotaltime $totaltime
}
# first get the stat times in the chosen units
set stat_sageper [format "%*.*f%%" [expr { $precision + 4}] $precision [percentageOfGlobal $modabsolute $sagetime]]
set stat_coreper [format "%*.*f%%" [expr { $precision + 4}] $precision [percentageOfGlobal $modabsolute $coretime]]
if { $units == "clicks" } {
set stat_absolute "$modabsolute clicks"
set stat_sage "$sagetime clicks ($stat_sageper)"
set stat_core "$coretime clicks ($stat_coreper)"
} {
set stat_absolute [format "%*.*f secs" [expr {$precision + 6}] $precision [clicks2seconds $modabsolute]]
set stat_sage [format "%*.*f secs " [expr {$precision + 6}] $precision [clicks2seconds $sagetime]]
append stat_sage "($stat_sageper)"
set stat_core [format "%*.*f secs " [expr {$precision + 6}] $precision [clicks2seconds $coretime]]
append stat_core "($stat_coreper)"
}
switch $dataview {
proctime {
set arr timers
}
proctimedesc {
set arr totaltimes
}
numcalls {
set arr proccounts
}
}
set global ".global."
set data ""
if { $dataview != "numcalls" } {
# put the total time in
lappend data [list ".total." [convertData $modtotaltime $modtotaltime]]
}
foreach name [array names $arr] {
if { $name == ".wait." && $includewait == 0 } continue
if { $name == $global && $includewait == 0 } {
# since waits are done as a descendant of global, we need to remove
# the wait time from global
set Ltotaltimes [expr {$totaltimes($name) - $timers(.wait.)}]
} else {
set Ltotaltimes $totaltimes($name)
}
# is this a match for our show expression?
if { ! [foundAMatch $showlist $showexptype $name] } {
continue
}
# how about the exclusion list?
if { [foundAMatch $excllist $exclexptype $name] } {
continue
}
# check the num calls exclusion
if { $proccounts($name) < $exclnumcalls } {
continue
}
# how about the percentage exclusion
if { $exclpercentage != 0.0 } {
switch $dataview {
proctime {
if { [percentageOfGlobal $modtotaltime $timers($name)] < $exclpercentage } {
continue
}
}
proctimedesc {
if { [percentageOfGlobal $modtotaltime $Ltotaltimes] < $exclpercentage } {
continue
}
}
}
}
# which data do we care about?
switch $dataview {
proctime {
lappend data [list $name [convertData $timers($name) $modtotaltime]]
}
proctimedesc {
lappend data [list $name [convertData $Ltotaltimes $modtotaltime]]
}
numcalls {
lappend data [list $name $proccounts($name)]
}
}
}
# now sort the list
if { $units != "clicks" && $dataview != "numcalls" } {
set data [lsort -real -index 1 -decreasing $data]
} {
set data [lsort -integer -index 1 -decreasing $data]
}
# put the data into the listbox
$dataLB delete 0 end
foreach elem $data {
foreach {proc value} $elem {}
if { $dataview == "numcalls" } {
$dataLB insert end [format "%6d calls %s" $value $proc]
} else {
if { $units == "clicks" } {
$dataLB insert end [format "%15d clicks %s" $value $proc]
} elseif { $units == "percent" } {
$dataLB insert end [format "%*.*f%% %s" [expr {$precision + 4}] $precision $value $proc]
} else {
$dataLB insert end [format "%*.*f secs %s" [expr {$precision + 6}] $precision $value $proc]
}
}
}
}
proc convertData {value globalvalue} {
variable units
switch $units {
clicks {
return $value
}
seconds {
return [clicks2seconds $value]
}
percent {
return [percentageOfGlobal $globalvalue $value]
}
}
return $value
}
# this proc converts the output of [clock clicks] to seconds.microseconds
# your definition of a click may be different from mine (system dependent)
# so you may have to monkey with this code
proc clicks2seconds {clicks} {
return [expr {$clicks / 1000000.0}]
}
# this proc returns the percentage the item is of $global
proc percentageOfGlobal {global item} {
set global $global.0
set item $item.0
#return [expr {(($global - ($global - $item)) / $global) * 100.0}]
return [expr {($item / $global) * 100.0}]
}
proc readsettings {} {
variable rcfile
variable dataview
variable units
variable precision
variable printcmd
variable includewait
variable showlist
variable showexptype
variable excllist
variable exclexptype
variable exclnumcalls
variable exclpercentage
global tcl_platform
if { [file exists $rcfile] } {
source $rcfile
if { [info vars geometry] == "geometry" } {
wm geometry .top $geometry
}
fillData
}
}
proc savesettings {} {
variable rcfile
variable dataview
variable units
variable precision
variable printcmd
variable includewait
variable showlist
variable showexptype
variable excllist
variable exclexptype
variable exclnumcalls
variable exclpercentage
set f [open $rcfile w]
puts $f "set dataview $dataview"
puts $f "set units $units"
puts $f "set precision $precision"
puts $f "set printcmd {$printcmd}"
puts $f "set geometry [wm geometry .top]"
puts $f "set includewait $includewait"
puts $f "set showlist {$showlist}"
puts $f "set showexptype {$showexptype}"
puts $f "set excllist {$excllist}"
puts $f "set exclexptype {$exclexptype}"
puts $f "set exclnumcalls {$exclnumcalls}"
puts $f "set exclpercentage {$exclpercentage}"
close $f
}
proc aboutwindow {} {
set war {Sage comes with ABSOLUTELY NO WARRANTY. \
This is free software, and you are welcome to redistribute it \
under certain conditions; See the file "LICENSE" for details. \
}
set top [toplevel .about]
pack [frame $top.f -background white] -fill both
pack [label $top.f.l -font -*-palatino-*-r-*-*-*-140-*-*-*-*-*-* -text "Sageview version 1.0 John E. Stump" -background white] -side top -padx 5 -pady 3
pack [message $top.f.m -text $war -aspect 200 -width 400 -foreground red -background gray95 -borderwidth 3 -relief raised] -side top -padx 5 -pady 3
set instrument [image create photo -file wizard3.gif]
pack [label $top.f.li -image $instrument -borderwidth 0] -side top -pady 8
pack [button $top.f.b -text Ok -command "destroy $top"] -side bottom -pady 4
# make this window modal
grab $top
tkwait window $top
}
proc printwindow {} {
set top [toplevel .print]
set f [frame $top.f1]
pack $f -side top -fill x -pady 7
set l [label $f.l -text "Print command:"]
pack $l -side left -padx 3
set e [entry $f.e -width 40 -textvariable [namespace current]::printcmd]
pack $e -side left
set f [frame $top.f2]
pack $f -side top -pady 3
set b [button $f.b1 -text "Print" -command [namespace code [list print_cmd $top]]]
pack $b -side left -padx 10
set b [button $f.b2 -text "Cancel" -command "destroy $top"]
pack $b -side left -padx 10
grab $top
tkwait window $top
}
proc print_cmd {top} {
variable printcmd
variable program_name; variable program_args
variable stat_absolute
variable stat_sage
variable stat_core
variable dataLB
if { [catch {set f [open "|$printcmd" w]} rc] } {
tk_dialog .msg "Error Printing" "Received an error opening pipe to printer:\n$rc" error 0 ok
return
}
puts $f "Sageview v1.0\n\n"
puts $f " Program: $program_name $program_args"
puts $f "Total execution time: $stat_absolute"
puts $f " Sage overhead time: $stat_sage"
puts $f " Tcl/Tk Core time: $stat_core"
puts $f "\n\n"
foreach lbdata [$dataLB get 0 end] {
puts $f $lbdata
}
close $f
destroy $top
}
proc excldlg {} {
set ns [namespace current]
set top [toplevel .excl]
wm title $top "Exclude Procs"
set f [frame $top.f1]
pack $f -side top -pady 5 -padx 6 -fill x
set l [label $f.l -text "Current expression(s):"]
pack $l -side left
set e [entry $f.e -width 40 -textvariable ${ns}::excllist]
pack $e -side right -expand yes -fill x
set f [frame $top.f2]
pack $f -side top -pady 4 -padx 6
set r [radiobutton $f.r1 -text "string match" -variable ${ns}::exclexptype \
-value "match"]
pack $r -side left -padx 6
set r [radiobutton $f.r2 -text "regexp" -variable ${ns}::exclexptype \
-value "regexp"]
pack $r -side left -padx 6
set f [frame $top.f3]
pack $f -side top -pady 5
set l [label $f.l -text "Exclude procs called <"]
pack $l -side left
set e [entry $f.e -width 2 -textvariable ${ns}::exclnumcalls]
pack $e -side left
set l [label $f.l2 -text "times"]
pack $l -side left
set f [frame $top.f4]
pack $f -side top -pady 5
set l [label $f.l -text "Exclude procs <"]
pack $l -side left
set e [entry $f.e -width 4 -textvariable ${ns}::exclpercentage]
pack $e -side left
set l [label $f.l2 -text "% of .total."]
pack $l -side left
set f [frame $top.f5]
pack $f -side top -pady 10
set b [button $f.b1 -text "Ok" -command "destroy $top;${ns}::fillData"]
pack $b -side left -padx 15
set b [button $f.b3 -text "Apply" -command "${ns}::fillData"]
pack $b -side left -padx 15
set b [button $f.b2 -text "Cancel" -command "destroy $top"]
pack $b -side left -padx 15
}
proc showdlg {} {
set ns [namespace current]
set top [toplevel .show]
wm title $top "Show Procs"
set f [frame $top.f1]
pack $f -side top -pady 5 -padx 6 -fill x
set l [label $f.l -text "Current expression(s):"]
pack $l -side left
set e [entry $f.e -width 40 -textvariable ${ns}::showlist]
pack $e -side right -expand yes -fill x
set f [frame $top.f2]
pack $f -side top -pady 4 -padx 6
set r [radiobutton $f.r1 -text "string match" -variable ${ns}::showexptype \
-value "match"]
pack $r -side left -padx 6
set r [radiobutton $f.r2 -text "regexp" -variable ${ns}::showexptype \
-value "regexp"]
pack $r -side left -padx 6
set f [frame $top.f3]
pack $f -side top -pady 10
set b [button $f.b1 -text "Ok" -command "destroy $top;${ns}::fillData"]
pack $b -side left -padx 15
set b [button $f.b3 -text "Apply" -command "${ns}::fillData"]
pack $b -side left -padx 15
set b [button $f.b2 -text "Cancel" -command "destroy $top"]
pack $b -side left -padx 15
}
proc foundAMatch {list type item} {
foreach elem $list {
if { $type == "match" } {
if { $elem == "*" } {
# this is always a match
return 1
}
if { [string match $elem $item] } {
return 1
}
} elseif { $type == "regexp" } {
if { $elem == ".*" } {
return 1
}
if { [regexp $elem $item] } {
return 1
}
} else {
throw "internal error: unknown list matching type: '$type'"
}
}
# couldn't a single match
return 0
}
proc labelledframe {path text} {
set frame [frame $path -borderwidth 2 -relief solid]
set label [label [file rootname $path].l -text $text]
pack $frame -fill both -pady [expr {[winfo reqheight $label] / 2}]
place $label -relx 0.0 -x 7
return $frame
}
proc run {} {
# run until the sage viewer window goes away
tkwait window .top
exit 0
}
}
::SageViewer::main $argv