root/hiersort/animation/preprocess.tcl

Revision 1, 11.2 kB (checked in by thesz, 2 years ago)

Initial commit

  • Property svn:executable set to *
Line 
1 #!/usr/bin/tclsh
2 #
3 # preprocess.tcl
4 #
5 # A script to preprocess log files for use with animation tool.
6 # Slightly out-of-date.
7 #
8 # Copytight (C) 2007, 2008 Serguey Zefirov
9 #
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 3, or (at your option)
13 # any later version.
14 # See file COPYING or visit http://www.gnu.org/licenses for details.
15
16
17 # -----------------------------------------------------------------------------
18 # Failure.
19
20 proc fail {msg} {
21         puts stderr $msg
22         exit 1
23 }
24
25 # -----------------------------------------------------------------------------
26 # Arguments.
27
28 foreach {infn outfn} $argv break
29 if {![string length $outfn]} { fail "usage: preprocess input output" }
30
31 set inh [open $infn]
32
33 proc linein {} { global inh; return [gets $inh]}
34
35 proc checkre {___line ___re args} {
36         foreach ___a $args {
37                 upvar $___a $___a
38         }
39         if {![eval regexp [list $___re] [list $___line] _ $args]} {
40                 error "Failed [list $___re] on [list $___line]"
41         }
42 }
43 proc checkrein {___re args} {
44         set ___line [linein]
45         foreach ___a $args {
46                 upvar $___a $___a
47         }
48         eval checkre [list $___line] [list $___re] $args
49 }
50
51 checkrein {Command line options: [[](.*)[]]} options
52
53 set options [string map {{"} { } {,} { }} $options]
54 puts [list $options]
55
56 proc intarg {prefix} {
57         checkrein "$prefix: (\[0-9]+)" r
58         return $r
59 }
60
61 proc boolarg {prefix} {
62         checkrein "$prefix: (True|False)" r
63         return [string equal True $r]
64 }
65
66 set hierDepth 0
67 foreach opt $options {
68         regexp -- {hierDepth=([0-9]+)} $opt _ hierDepth
69         regexp -- {taskOption=taskSize=([0-9]+)} $opt _ taskSize
70 }
71
72 # -----------------------------------------------------------------------------
73 # Splitting.
74
75 set currTick 0
76 set maxQL 0
77
78 proc checkTick {} {
79         global tickInfo currTick totalTicks
80         set l [linein]
81         if {[regexp {Total ([0-9]+) ticks.} $l _ totalTicks]} {
82                 return 0
83         }
84         if {[regexp {Tick [0-9]+} $l]} {
85                 set tickInfo($currTick,indices) {}
86                 foreach {_ i j} [regexp -all -inline {OrderIndexes GroupReceivers .([0-9]+),([0-9]+).,OrderNode HostNodeC} $l] {
87                         lappend tickInfo($currTick,indices) $i $j
88                 }
89                 return 1
90         }
91         fail "Invalid line [list $l] for tick header."
92 }
93
94 proc processTick {} {
95         processFifo
96         global hierDepth
97         processNode $hierDepth
98         # Skip empty line.
99         linein
100 }
101
102 proc processFifo {} {
103         checkrein "FIFO ---"
104         global tickInfo currTick maxQL
105         linein
106         checkrein {cts: (True|False)} flag
107         set tickInfo($currTick,fifocts) [string equal True $flag]
108         linein
109         checkrein {que length: ([0-9]+)} quelen
110         set tickInfo($currTick,quelen) $quelen
111         checkrein {maxql: ([0-9]+)} maxQL
112 }
113
114 proc processPresorter {node} {
115         checkrein "presorter ---"
116         set maxbuflen [intarg maxlen]
117         set currbuflen [intarg tokensBufLen]
118         global tickInfo currTick
119         set tickInfo($currTick,$node,presortmaxbuflen) $maxbuflen
120         set tickInfo($currTick,$node,presortcurrbuflen) $currbuflen
121         linein
122         linein
123         linein
124         set tickInfo($currTick,$node,presortenabled) [boolarg "presorter rtr"]
125 }
126
127 proc processUpBuf {node} {
128         checkrein "upstream buffer ---"
129         global tickInfo currTick
130         set tickInfo($currTick,$node,upenabled) [boolarg "upstream rtr"]
131         set tickInfo($currTick,$node,upmaxbuflen) [intarg "buffer maxlen"]
132         set tickInfo($currTick,$node,upcurrbuflen) [intarg "buffer length"]
133         linein
134         linein
135 }
136
137 proc processSorter {leftright node} {
138         checkrein "$leftright sorter"
139         checkrein "sorter machine .* ---"
140         global tickInfo currTick
141         set tickInfo($currTick,$node,$leftright,sorter,maxlen) [intarg maxLen]
142         set tickInfo($currTick,$node,$leftright,sorter,curlen) [intarg currlen]
143         linein;linein;linein;linein;
144         set tickInfo($currTick,$node,$leftright,sorter,available) [boolarg rtr]
145 }
146
147
148 proc processNodeBody {nodeLevel} {
149         incr nodeLevel -1
150         checkrein {Node ([0-9]+..[0-9]+)} nodeindex
151         processPresorter $nodeindex
152         processUpBuf     $nodeindex
153         processSorter    left $nodeindex
154         processSorter    right $nodeindex
155         processNode $nodeLevel
156         processNode $nodeLevel
157 }
158
159 proc processMatcher {proc} {
160         checkrein "matcher ---"
161         global tickInfo currTick
162         set tickInfo($currTick,$proc,matchmaxlen) [intarg maxlen]
163         set tickInfo($currTick,$proc,matchcurrlen) [intarg tokensCount]
164         linein
165         set tickInfo($currTick,$proc,matchavailable) [boolarg rtr]
166 }
167
168 proc _tokdone {tok} {
169         if {![regexp {GroupWork [[][0-9]+,([0-9]+),([0-9]+),([0-9]+).*NodeS.*SInputX} $tok _ i j k]} {
170                 return {}
171         }
172         global taskSize
173         return [list $i $j [expr double($k)/$taskSize]]
174 }
175 proc processEU {proc} {
176         global tickInfo currTick
177         checkrein {proc info ---}
178         checkrein {processor (is|isn.t) blocked} blockflag
179         checkrein {processor (is|isn.t) idle} idleflag
180         linein
181         linein
182         set l1 [linein]
183         set l2 [linein]
184         foreach {i j v} [concat [_tokdone $l1] [_tokdone $l2]] {
185                 lappend tickInfo($currTick,sumtokens) $i $j $v
186         }
187         set worksetlen [intarg workSetLen]
188         set execwindow [intarg execWindow]
189         linein
190         linein
191         set bufavail [boolarg procRTR]
192         set tickInfo($currTick,$proc,blocked) [string equal is $blockflag]
193         set tickInfo($currTick,$proc,idle) [string equal is $idleflag]
194         set tickInfo($currTick,$proc,available) $bufavail
195         set tickInfo($currTick,$proc,runthreads) $worksetlen
196         set tickInfo($currTick,$proc,maxthreads) $execwindow
197 }
198
199 proc processProc {} {
200         checkrein {Processor ([0-9]+)} procindex
201         processMatcher $procindex
202         processUpBuf   $procindex
203         processEU      $procindex
204        
205 }
206
207 proc processNode {nodeLevel} {
208         if {$nodeLevel > 0} {
209                 processNodeBody $nodeLevel
210         } else {
211                 processProc
212         }
213 }
214
215 proc genNodes {l h} {
216         if {$l == $h} {
217                 return [list P $l]
218         }
219         set m [expr ($l+$h)/2]
220         set a [genNodes $l $m]
221         set b [genNodes [expr $m+1] $h]
222         return [list N $l..$h $a $b]
223 }
224
225 set tickInfo(nodes) [genNodes 0 [expr (1<<$hierDepth)-1]]
226
227 while {[checkTick]} {
228 # puts "tick $currTick"
229         set tickInfo($currTick,sumtokens) {}
230         processTick
231 # puts "tickInfo($currTick,sumtokens) [list $tickInfo($currTick,sumtokens)]"
232         incr currTick
233 }
234
235 # -----------------------------------------------------------------------------
236 # Getting maximum values.
237
238 foreach v {
239         sorterMaxFillLen presorterMaxFillLen upBufMaxFillLen matcherMaxFillLen
240         sorterMaxLen presorterMaxLen upBufMaxLen matcherMaxLen
241 } {
242         set $v 0
243 }
244
245 for {set currTick 0} {$currTick < $totalTicks} {incr currTick} {
246         set tickInfo($currTick,hiermemused) 0
247         set tickInfo($currTick,sortmemused) 0
248 }
249
250 foreach {t v} [array get tickInfo] {
251         set add 0
252         set sortadd 0
253         switch -glob -- $t {
254                 *presortcurrbuflen {
255                         set vn presorterMaxFillLen
256                         set add 1
257                 }
258                 *matchcurrlen {
259                         set vn matcherMaxFillLen
260                         set add 1
261                         set sortadd 1
262                 }
263                 *sorter,curlen {
264                         set vn sorterMaxFillLen
265                         set add 1
266                         set sortadd 1
267                 }
268                 *upcurrbuflen {
269                         set vn upBufMaxFillLen
270                 }
271                 *presortmaxbuflen {
272                         set vn presorterMaxLen
273                 }
274                 *matchmaxlen {
275                         set vn matcherMaxLen
276                 }
277                 *sorter,maxlen {
278                         set vn sorterMaxLen
279                 }
280                 *upmaxbuflen {
281                         set vn upBufMaxLen
282                 }
283                 default {
284                         set vn {}
285                 }
286         }
287         if {[string length $vn]} {
288                 set $vn [expr [set $vn]<$v?$v:[set $vn]]
289         }
290         set currTick [scan $t %d]
291         if {$add} {
292                 incr tickInfo($currTick,hiermemused) $v
293         }
294         if {$sortadd} {
295                 incr tickInfo($currTick,sortmemused) $v
296         }
297 }
298
299 foreach v {
300         sorterMaxFillLen presorterMaxFillLen upBufMaxFillLen matcherMaxFillLen
301         sorterMaxLen presorterMaxLen upBufMaxLen matcherMaxLen
302 } {
303         puts "$v [set $v]"
304 }
305
306 set maxHierMem 0
307 for {set currTick 0} {$currTick < $totalTicks} {incr currTick} {
308         set x $tickInfo($currTick,hiermemused)
309         set maxHierMem [expr $x<$maxHierMem?$maxHierMem:$x]
310 }
311
312 set hierMemAvailable 0
313 set sortMemAvailable 0
314 if 0 {
315 array set tt [array get tickInfo 0,*]
316 set tt(nodes) $tickInfo(nodes)
317 puts "parray tt:"
318 parray tt
319 }
320 foreach {t v} [array get tickInfo 0,*] {
321         switch -glob -- $t {
322                 *presortmaxbuflen -
323                 *matchmaxlen -
324                 *sorter,maxlen -
325                 *upmaxbuflen {
326                         incr hierMemAvailable $v
327                 }
328         }
329         switch -glob -- $t {
330                 *matchmaxlen -
331                 *sorter,maxlen {
332                         incr sortMemAvailable $v
333                 }
334         }
335 }
336
337 # -----------------------------------------------------------------------------
338 # Sorting information out.
339
340 proc noderatio {n curr max} {
341         global currTick tickInfo
342         return [expr double($tickInfo($currTick,$n,$curr))/$tickInfo($currTick,$n,$max)]
343 }
344
345 proc collectnodes {tree} {
346         global currTick tickInfo
347         foreach {n i suba subb} $tree break
348         if {![string equal $n N]} { return {} }
349         set la [collectnodes $suba]
350         set lb [collectnodes $subb]
351         set thisl [list                                                 \
352                 $i                                                      \
353                 [noderatio $i presortcurrbuflen presortmaxbuflen]       \
354                 [noderatio $i upcurrbuflen upmaxbuflen]                 \
355                 [noderatio $i left,sorter,curlen left,sorter,maxlen]    \
356                 [noderatio $i right,sorter,curlen right,sorter,maxlen]  \
357         ]
358         return [concat $thisl $la $lb]
359 }
360
361 proc procratio {i curr max} {
362         global tickInfo currTick
363         return [expr double($tickInfo($currTick,$i,$curr))/$tickInfo($currTick,$i,$max)]
364 }
365
366 proc collectprocessors {} {
367         global hierDepth
368         set res {}
369         for {set i 0} {$i < (1<<$hierDepth)} {incr i} {
370                 lappend res $i [procratio $i matchcurrlen matchmaxlen] [procratio $i upcurrbuflen upmaxbuflen] [procratio $i runthreads maxthreads]
371         }
372         return $res
373 }
374
375 array set collectedindices {}
376 for {set i 1} {$i <= $taskSize} {incr i} {
377         for {set j 1} {$j <= $taskSize} {incr j} {
378                 set collectedindices($i,$j) 0.0
379         }
380 }
381 proc collectindices {} {
382         global tickInfo currTick collectedindices taskSize
383         set ts {}
384         if {[info exists tickInfo($currTick,sumtokens)]} {
385                 set ts $tickInfo($currTick,sumtokens)
386         }
387         foreach {i j v} $ts {
388                 set collectedindices($i,$j) $v
389         }
390         foreach {i j} $tickInfo($currTick,indices) {
391                 set collectedindices($i,$j) 1.0
392         }
393         set r {}
394         for {set i 1} {$i <= $taskSize} {incr i} {
395                 for {set j 1} {$j <= $taskSize} {incr j} {
396                         lappend r $i $j $collectedindices($i,$j)
397                 }
398         }
399         return $r
400 }
401
402 proc optarg {opt} {
403         if {[regexp {.*=([0-9]+)} $opt _ v]} {
404                 return $v
405         }
406         return 0
407 }
408 proc determinevolumes {} {
409         global options
410         set presorterVolume 0
411         set upbufVolume 0
412         set sorterVolume 0
413         set matcherVolume 0
414         set threadsVolume 0
415         foreach o $options {
416                 set ov [optarg $o]
417                 switch -glob -- $o {
418                         --procParThreads=* {
419                                 set threadsVolume $ov
420                         }
421                         --procMatcherForceUp=* {
422                                 set matcherVolume $ov
423                         }
424                         --hierBaseSortSize=* {
425                                 set sorterVolume $ov
426                         }
427                         --procMatcherSize=* {
428                                 set matcherVolume $ov
429                         }
430                         --presorterMaxLen=* {
431                                 set presorterVolume $ov
432                         }
433                         --upBufMaxLen=* {
434                                 set upbufVolume $ov
435                         }
436                 }
437         }
438         set r [list $presorterVolume $upbufVolume $sorterVolume $matcherVolume $threadsVolume]
439         return $r
440 }
441
442 set tcl_precision 5
443 set outh [open $outfn w]
444 puts $outh [list resetAll]
445 puts $outh [list taskSize $taskSize]
446 puts $outh [list totalTicks $totalTicks]
447 puts $outh [list fifoMax $maxQL]
448 puts $outh [list hierDepth $hierDepth]
449 puts $outh [list hierMemMax $maxHierMem]
450 puts $outh [list hierMemAvailable $hierMemAvailable]
451 puts $outh [list sortMemAvailable $sortMemAvailable]
452 puts $outh [list hierNodes $tickInfo(nodes)]
453 puts $outh "circuitVolumes [determinevolumes]"
454 set results {}
455 for {set currTick 0} {$currTick < $totalTicks} {incr currTick} {
456         puts $outh "# ---------------------------------------------"
457         puts $outh [list currTick $currTick]
458         puts $outh [list fifoUse [expr double($tickInfo($currTick,quelen))/$maxQL]]
459         set results [collectindices]
460         puts $outh [list tickResults $results]
461         puts $outh [list memUse $tickInfo($currTick,hiermemused)]
462         puts $outh [list sortMemUse [expr double($tickInfo($currTick,sortmemused))/$sortMemAvailable]]
463         puts $outh [list nodesUse [collectnodes $tickInfo(nodes)]]
464         puts $outh [list procsUse [collectprocessors]]
465 }
466 close $outh
Note: See TracBrowser for help on using the browser.