3 # Thomas Nagy, 2007-2010 (ita)
6 debugging helpers for parallel compilation, outputs
7 a svg file in the build directory
10 import os
, time
, sys
, threading
11 try: from Queue
import Queue
12 except: from queue
import Queue
13 import Runner
, Options
, Utils
, Task
, Logs
14 from Constants
import *
20 opt
.add_option('--dtitle', action
='store', default
='Parallel build representation for %r' % ' '.join(sys
.argv
),
21 help='title for the svg diagram', dest
='dtitle')
22 opt
.add_option('--dwidth', action
='store', type='int', help='diagram width', default
=1000, dest
='dwidth')
23 opt
.add_option('--dtime', action
='store', type='float', help='recording interval in seconds', default
=0.009, dest
='dtime')
24 opt
.add_option('--dband', action
='store', type='int', help='band width', default
=22, dest
='dband')
25 opt
.add_option('--dmaxtime', action
='store', type='float', help='maximum time, for drawing fair comparisons', default
=0, dest
='dmaxtime')
42 info
= [] # list of (text,color)
44 def map_to_color(name
):
48 cls
= Task
.TaskBase
.classes
[name
]
50 return color2code
['RED']
53 if cls
.color
in color2code
:
54 return color2code
[cls
.color
]
55 return color2code
['RED']
59 tsk
=Runner
.TaskConsumer
.ready
.get()
60 tsk
.master
.set_running(1, id(threading
.currentThread()), tsk
)
61 Runner
.process_task(tsk
)
62 tsk
.master
.set_running(-1, id(threading
.currentThread()), tsk
)
63 Runner
.TaskConsumer
.loop
= loop
66 old_start
= Runner
.Parallel
.start
71 except AttributeError:
72 raise ValueError('use def options(opt): opt.load("parallel_debug")!')
74 self
.taskinfo
= Queue()
77 Runner
.Parallel
.start
= do_start
79 def set_running(self
, by
, i
, tsk
):
80 self
.taskinfo
.put( (i
, id(tsk
), time
.time(), tsk
.__class
__.__name
__, self
.processed
, self
.count
, by
) )
81 Runner
.Parallel
.set_running
= set_running
84 return name
.replace(' ', '_').replace('.', '_')
86 def process_colors(producer
):
87 # first, cast the parameters
91 tup
= producer
.taskinfo
.get(False)
97 ini
= float(tmp
[0][2])
110 info
.append((name
, map_to_color(name
)))
111 info
.sort(key
=lambda x
: x
[0])
117 acc
.append("%d %d %f %r %d %d %d" % (x
[0], x
[1], x
[2] - ini
, x
[3], x
[4], x
[5], thread_count
))
118 f
= open('pdebug.dat', 'w')
119 #Utils.write('\n'.join(acc))
120 f
.write('\n'.join(acc
))
122 tmp
= [lst
[:2] + [float(lst
[2]) - ini
] + lst
[3:] for lst
in tmp
]
127 st
[l
[0]] = len(st
.keys())
128 tmp
= [ [st
[lst
[0]]] + lst
[1:] for lst
in tmp
]
129 THREAD_AMOUNT
= len(st
.keys())
134 st
[l
[1]] = len(st
.keys())
135 tmp
= [ [lst
[0]] + [st
[lst
[1]]] + lst
[2:] for lst
in tmp
]
138 BAND
= Options
.options
.dband
142 for x
in range(len(tmp
)):
152 for y
in range(x
+ 1, len(tmp
)):
156 #print id, thread_id, begin, end
157 #acc.append( ( 10*thread_id, 10*(thread_id+1), 10*begin, 10*end ) )
158 acc
.append( (BAND
* begin
, BAND
*thread_id
, BAND
*end
- BAND
*begin
, BAND
, line
[3]) )
161 if Options
.options
.dmaxtime
< 0.1:
168 gwidth
= BAND
* Options
.options
.dmaxtime
170 ratio
= float(Options
.options
.dwidth
) / gwidth
171 gwidth
= Options
.options
.dwidth
173 gheight
= BAND
* (THREAD_AMOUNT
+ len(info
) + 1.5)
177 out
.append("""<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>
178 <!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.0//EN\"
179 \"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd\">
180 <svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" version=\"1.0\"
181 x=\"%r\" y=\"%r\" width=\"%r\" height=\"%r\"
182 id=\"svg602\" xml:space=\"preserve\">
184 <style type='text/css' media='screen'>
185 g.over rect { stroke:#FF0000; fill-opacity:0.4 }
188 <script type='text/javascript'><![CDATA[
189 var svg = document.getElementsByTagName('svg')[0];
190 var svgNS = svg.getAttribute('xmlns');
191 svg.addEventListener('mouseover',function(e){
192 var g = e.target.parentNode;
193 var x = document.getElementById('r_'+g.id);
195 g.setAttribute('class', g.getAttribute('class')+' over');
196 x.setAttribute('class', x.getAttribute('class')+' over');
200 svg.addEventListener('mouseout',function(e){
201 var g = e.target.parentNode;
202 var x = document.getElementById('r_'+g.id);
204 g.setAttribute('class',g.getAttribute('class').replace(' over',''));
205 x.setAttribute('class',x.getAttribute('class').replace(' over',''));
210 function showInfo(evt, txt) {
211 tooltip = document.getElementById('tooltip');
213 var t = document.getElementById('tooltiptext');
214 t.firstChild.data = txt;
216 var x = evt.clientX+10;
217 if (x > 200) { x -= t.getComputedTextLength() + 16; }
218 var y = evt.clientY+30;
219 tooltip.setAttribute("transform", "translate(" + x + "," + y + ")");
220 tooltip.setAttributeNS(null,"visibility","visible");
222 var r = document.getElementById('tooltiprect');
223 r.setAttribute('width', t.getComputedTextLength()+6)
227 function hideInfo(evt) {
228 tooltip = document.getElementById('tooltip');
229 tooltip.setAttributeNS(null,"visibility","hidden");
234 <!-- inkscape requires a big rectangle or it will not export the pictures properly -->
237 width='%r' height='%r' z-index='10'
238 style=\"font-size:10;fill:#ffffff;fill-opacity:0.01;fill-rule:evenodd;stroke:#ffffff;\"
241 """ % (0, 0, gwidth
+ 4, gheight
+ 4, 0, 0, gwidth
+ 4, gheight
+ 4))
244 if Options
.options
.dtitle
:
245 out
.append("""<text x="%d" y="%d" style="font-size:15px; text-anchor:middle; font-style:normal;font-weight:normal;fill:#000000;fill-opacity:1;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;font-family:Bitstream Vera Sans">%s</text>
246 """ % (gwidth
/2, gheight
- 5, Options
.options
.dtitle
))
250 for (x
, y
, w
, h
, clsname
) in acc
:
252 groups
[clsname
].append((x
, y
, w
, h
))
254 groups
[clsname
] = [(x
, y
, w
, h
)]
258 out
.append("<g id='%s'>\n" % name2class(cls
))
260 for (x
, y
, w
, h
) in groups
[cls
]:
263 width='%r' height='%r' z-index='11'
264 style=\"font-size:10;fill:%s;fill-rule:evenodd;stroke:#000000;stroke-width:0.2px;\"
265 />\n""" % (2 + x
*ratio
, 2 + y
, w
*ratio
, h
, map_to_color(cls
)))
272 for (text
, color
) in info
:
275 out
.append("""<g id='r_%s'><rect
277 width='%r' height='%r'
278 style=\"font-size:10;fill:%s;fill-rule:evenodd;stroke:#000000;stroke-width:0.2px;\"
279 />\n""" % (name2class(text
), 2 + BAND
, 5 + (cnt
+ 0.5) * BAND
, b
, b
, color
))
283 style="font-size:12px;font-style:normal;font-weight:normal;fill:#000000;fill-opacity:1;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;font-family:Bitstream Vera Sans"
284 x="%r" y="%d">%s</text></g>\n""" % (2 + 2 * BAND
, 5 + (cnt
+ 0.5) * BAND
+ 10, text
))
288 <g transform="translate(0,0)" visibility="hidden" id="tooltip">
289 <rect id="tooltiprect" y="-15" x="-3" width="1" height="20" style="stroke:black;fill:#edefc2;stroke-width:1"/>
290 <text id="tooltiptext" style="font-family:Arial; font-size:12;fill:black;"> </text>
293 out
.append("\n</svg>")
295 #node = producer.bld.path.make_node('pdebug.svg')
296 f
= open('pdebug.svg', 'w')
297 f
.write("".join(out
))