@@ -66,7 +66,10 @@ const BG_COLOR = getCssColor('--background');
6666const  CANVAS_BG  =  getCssColor ( '--canvas-background' ) ; 
6767const  AXES_COLOR  =  getCssColor ( '--canvas-axes' ) ; 
6868const  GRID_COLOR  =  getCssColor ( '--canvas-grid' ) ; 
69- const  BLOCK_COLOR  =  getCssColor ( '--canvas-block' ) ; 
69+ const  CODEGEN_COLOR  =  getCssColor ( '--canvas-codegen' ) ; 
70+ const  LINK_COLOR  =  getCssColor ( '--canvas-link' ) ; 
71+ // Final leftover section after link 
72+ const  OTHER_COLOR  =  getCssColor ( '--canvas-other' ) ; 
7073const  CUSTOM_BUILD_COLOR  =  getCssColor ( '--canvas-custom-build' ) ; 
7174const  NOT_CUSTOM_BUILD_COLOR  =  getCssColor ( '--canvas-not-custom-build' ) ; 
7275const  DEP_LINE_COLOR  =  getCssColor ( '--canvas-dep-line' ) ; 
@@ -134,21 +137,40 @@ function render_pipeline_graph() {
134137    let  unit  =  units [ i ] ; 
135138    let  y  =  i  *  Y_TICK_DIST  +  1 ; 
136139    let  x  =  px_per_sec  *  unit . start ; 
137-     let  rmeta_x  =  null ; 
138-     if  ( unit . rmeta_time  !=  null )  { 
139-       rmeta_x  =  x  +  px_per_sec  *  unit . rmeta_time ; 
140+ 
141+     const  sections  =  [ ] ; 
142+     if  ( unit . sections  !==  null )  { 
143+         // We have access to compilation sections 
144+         for  ( const  section  of  unit . sections )  { 
145+             const  [ name ,  { start,  end} ]  =  section ; 
146+             sections . push ( { 
147+                 name, 
148+                 start : x  +  px_per_sec  *  start , 
149+                 width : ( end  -  start )  *  px_per_sec 
150+             } ) ; 
151+         } 
152+     } 
153+     else  if  ( unit . rmeta_time  !=  null )  { 
154+         // We only know the rmeta time 
155+         sections . push ( { 
156+             name : "codegen" , 
157+             start : x  +  px_per_sec  *  unit . rmeta_time , 
158+             width : ( unit . duration  -  unit . rmeta_time )  *  px_per_sec 
159+         } ) ; 
140160    } 
141161    let  width  =  Math . max ( px_per_sec  *  unit . duration ,  1.0 ) ; 
142-     UNIT_COORDS [ unit . i ]  =  { x,  y,  width,  rmeta_x } ; 
162+     UNIT_COORDS [ unit . i ]  =  { x,  y,  width,  sections } ; 
143163
144164    const  count  =  unitCount . get ( unit . name )  ||  0 ; 
145165    unitCount . set ( unit . name ,  count  +  1 ) ; 
146166  } 
147167
168+   const  presentSections  =  new  Set ( ) ; 
169+ 
148170  // Draw the blocks. 
149171  for  ( i = 0 ;  i < units . length ;  i ++ )  { 
150172    let  unit  =  units [ i ] ; 
151-     let  { x,  y,  width,  rmeta_x }  =  UNIT_COORDS [ unit . i ] ; 
173+     let  { x,  y,  width,  sections }  =  UNIT_COORDS [ unit . i ] ; 
152174
153175    HIT_BOXES . push ( { x : X_LINE + x ,  y :MARGIN + y ,  x2 : X_LINE + x + width ,  y2 : MARGIN + y + BOX_HEIGHT ,  i : unit . i } ) ; 
154176
@@ -157,12 +179,12 @@ function render_pipeline_graph() {
157179    roundedRect ( ctx ,  x ,  y ,  width ,  BOX_HEIGHT ,  RADIUS ) ; 
158180    ctx . fill ( ) ; 
159181
160-     if   ( unit . rmeta_time   !=   null )  { 
161-       ctx . beginPath ( ) ; 
162-       ctx . fillStyle  =  BLOCK_COLOR ; 
163-       let   ctime   =   unit . duration   -   unit . rmeta_time ; 
164-       roundedRect ( ctx ,   rmeta_x ,   y ,   px_per_sec   *   ctime ,   BOX_HEIGHT ,   RADIUS ) ; 
165-       ctx . fill ( ) ; 
182+     for   ( const   section   of   sections )  { 
183+          ctx . beginPath ( ) ; 
184+          ctx . fillStyle  =  get_section_color ( section . name ) ; 
185+          roundedRect ( ctx ,   section . start ,   y ,   section . width ,   BOX_HEIGHT ,   RADIUS ) ; 
186+          ctx . fill ( ) ; 
187+          presentSections . add ( section . name ) ; 
166188    } 
167189    ctx . fillStyle  =  TEXT_COLOR ; 
168190    ctx . textAlign  =  'start' ; 
@@ -178,6 +200,110 @@ function render_pipeline_graph() {
178200    draw_dep_lines ( ctx ,  unit . i ,  false ) ; 
179201  } 
180202  ctx . restore ( ) ; 
203+ 
204+   // Draw a legend. 
205+   ctx . save ( ) ; 
206+   ctx . translate ( canvas_width  -  200 ,  MARGIN ) ; 
207+ 
208+   const  legend_entries  =  [ { 
209+     name : "Frontend/rest" , 
210+     color : NOT_CUSTOM_BUILD_COLOR , 
211+     line : false 
212+   } ] ; 
213+   if  ( presentSections . has ( "codegen" ) )  { 
214+     legend_entries . push ( { 
215+       name : "Codegen" , 
216+       color : CODEGEN_COLOR , 
217+       line : false 
218+     } ) ; 
219+   } 
220+   if  ( presentSections . has ( "link" ) )  { 
221+     legend_entries . push ( { 
222+       name : "Linking" , 
223+       color : LINK_COLOR , 
224+       line : false 
225+     } ) ; 
226+   } 
227+   if  ( presentSections . has ( "other" ) )  { 
228+     legend_entries . push ( { 
229+       name : "Other" , 
230+       color : OTHER_COLOR , 
231+       line : false 
232+     } ) ; 
233+   } 
234+   draw_legend ( ctx ,  160 ,  legend_entries ) ; 
235+   ctx . restore ( ) ; 
236+ } 
237+ 
238+ // Draw a legend at the current position of the ctx. 
239+ // entries should be an array of objects with the following scheme: 
240+ // { 
241+ //   "name": <name of the legend entry> [string], 
242+ //   "color": <color of the legend entry> [string], 
243+ //   "line": <should the entry be a thin line or a rectangle> [bool] 
244+ // } 
245+ function  draw_legend ( ctx ,  width ,  entries )  { 
246+   const  entry_height  =  20 ; 
247+ 
248+   // Add a bit of margin to the bottom and top 
249+   const  height  =  entries . length  *  entry_height  +  4 ; 
250+ 
251+   // Draw background 
252+   ctx . fillStyle  =  BG_COLOR ; 
253+   ctx . strokeStyle  =  TEXT_COLOR ; 
254+   ctx . lineWidth  =  1 ; 
255+   ctx . textBaseline  =  'middle' ; 
256+   ctx . textAlign  =  'start' ; 
257+   ctx . beginPath ( ) ; 
258+   ctx . rect ( 0 ,  0 ,  width ,  height ) ; 
259+   ctx . stroke ( ) ; 
260+   ctx . fill ( ) ; 
261+ 
262+   ctx . lineWidth  =  2 ; 
263+ 
264+   // Dimension of a block 
265+   const  block_height  =  15 ; 
266+   const  block_width  =  30 ; 
267+ 
268+   // Margin from the left edge 
269+   const  x_start  =  5 ; 
270+   // Width of the "mark" section (line/block) 
271+   const  mark_width  =  45 ; 
272+ 
273+   // Draw legend entries 
274+   let  y  =  12 ; 
275+   for  ( const  entry  of  entries )  { 
276+     ctx . beginPath ( ) ; 
277+ 
278+     if  ( entry . line )  { 
279+       ctx . strokeStyle  =  entry . color ; 
280+       ctx . moveTo ( x_start ,  y ) ; 
281+       ctx . lineTo ( x_start  +  mark_width ,  y ) ; 
282+       ctx . stroke ( ) ; 
283+     }  else  { 
284+       ctx . fillStyle  =  entry . color ; 
285+       ctx . fillRect ( x_start  +  ( mark_width  -  block_width )  /  2 ,  y  -  ( block_height  /  2 ) ,  block_width ,  block_height ) ; 
286+     } 
287+ 
288+     ctx . fillStyle  =  TEXT_COLOR ; 
289+     ctx . fillText ( entry . name ,  x_start  +  mark_width  +  4 ,  y  +  1 ) ; 
290+ 
291+     y  +=  entry_height ; 
292+   } 
293+ } 
294+ 
295+ // Determine the color of a section block based on the section name. 
296+ function  get_section_color ( name )  { 
297+     if  ( name  ===  "codegen" )  { 
298+         return  CODEGEN_COLOR ; 
299+     }  else  if  ( name  ===  "link" )  { 
300+         return  LINK_COLOR ; 
301+     }  else  if  ( name  ===  "other" )  { 
302+         return  OTHER_COLOR ; 
303+     }  else  { 
304+         // We do not know what section this is, so just use the default color 
305+         return  NOT_CUSTOM_BUILD_COLOR ; 
306+     } 
181307} 
182308
183309// Draws lines from the given unit to the units it unlocks. 
@@ -296,47 +422,23 @@ function render_timing_graph() {
296422  ctx . restore ( ) ; 
297423  ctx . save ( ) ; 
298424  ctx . translate ( canvas_width - 200 ,  MARGIN ) ; 
299-   // background 
300-   ctx . fillStyle  =  BG_COLOR ; 
301-   ctx . strokeStyle  =  TEXT_COLOR ; 
302-   ctx . lineWidth  =  1 ; 
303-   ctx . textBaseline  =  'middle' 
304-   ctx . textAlign  =  'start' ; 
305-   ctx . beginPath ( ) ; 
306-   ctx . rect ( 0 ,  0 ,  150 ,  82 ) ; 
307-   ctx . stroke ( ) ; 
308-   ctx . fill ( ) ; 
309- 
310-   ctx . fillStyle  =  TEXT_COLOR ; 
311-   ctx . beginPath ( ) ; 
312-   ctx . lineWidth  =  2 ; 
313-   ctx . strokeStyle  =  'red' ; 
314-   ctx . moveTo ( 5 ,  10 ) ; 
315-   ctx . lineTo ( 50 ,  10 ) ; 
316-   ctx . stroke ( ) ; 
317-   ctx . fillText ( 'Waiting' ,  54 ,  11 ) ; 
318- 
319-   ctx . beginPath ( ) ; 
320-   ctx . strokeStyle  =  'blue' ; 
321-   ctx . moveTo ( 5 ,  30 ) ; 
322-   ctx . lineTo ( 50 ,  30 ) ; 
323-   ctx . stroke ( ) ; 
324-   ctx . fillText ( 'Inactive' ,  54 ,  31 ) ; 
325- 
326-   ctx . beginPath ( ) ; 
327-   ctx . strokeStyle  =  'green' ; 
328-   ctx . moveTo ( 5 ,  50 ) ; 
329-   ctx . lineTo ( 50 ,  50 ) ; 
330-   ctx . stroke ( ) ; 
331-   ctx . fillText ( 'Active' ,  54 ,  51 ) ; 
332- 
333-   ctx . beginPath ( ) ; 
334-   ctx . fillStyle  =  cpuFillStyle 
335-   ctx . fillRect ( 15 ,  60 ,  30 ,  15 ) ; 
336-   ctx . fill ( ) ; 
337-   ctx . fillStyle  =  TEXT_COLOR ; 
338-   ctx . fillText ( 'CPU Usage' ,  54 ,  71 ) ; 
339- 
425+   draw_legend ( ctx ,  150 ,  [ { 
426+     name : "Waiting" , 
427+     color : "red" , 
428+     line : true 
429+   } ,  { 
430+     name : "Inactive" , 
431+     color : "blue" , 
432+     line : true 
433+   } ,  { 
434+     name : "Active" , 
435+     color : "green" , 
436+     line : true 
437+   } ,  { 
438+     name : "CPU Usage" , 
439+     color : cpuFillStyle , 
440+     line : false 
441+   } ] ) ; 
340442  ctx . restore ( ) ; 
341443} 
342444
0 commit comments