summaryrefslogtreecommitdiff
path: root/ghc/utils/parallel/qp2ap.pl
blob: b3c3bcf12294fac9fc870e37b8cfcfa473802843 (plain)
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
#! /usr/local/bin/perl
##############################################################################
# Time-stamp: <Wed Jul 24 1996 22:05:31 Stardate: [-31]7859.39 hwloidl>
#
# Usage: qp2ap [options] <max-x> <max-y> <prg> <date>
#
# Filter that transforms a quasi-parallel profile (a .qp file) at stdin to  
# a PostScript file at stdout, showing an activity profile with one horizontal
# line for each task (thickness of the line shows if it's active or suspended).
#
# Options:
#  -o <file> ... write .ps file to <file>
#  -m        ... create mono PostScript file instead a color one.
#  -O        ... optimise i.e. try to minimise the size of the .ps file.
#  -s <n>    ... scaling factor of y axis (default: 1)
#  -w <n>    ... width of lines denoting running threads (default: 2) 
#  -v        ... be talkative. 
#  -h        ... print help message (this header).
#
##############################################################################


require "getopts.pl";

&Getopts('hvms:w:OlD');  

do process_options();

if ( $opt_v ) {
    do print_verbose_message();
}

# ---------------------------------------------------------------------------
# Init
# ---------------------------------------------------------------------------

$y_scaling = 0;
$gtid = 1;               # number of process so far = $gtid-1

$xmin = 100;
$xmax = 790;

$scalex = $xmin;
$labelx = $scalex - 45;
$markx =  $scalex - 30;
$major = $scalex - 5;
$majorticks = 10;

# $pmax = 40;
$ymin = 50;
$ymax = 500;

if ( ($ymax - $ymin)/$pmax < 3 ) {
    print STDERR "Warning: Too many tasks! Distance will be smaller than 3 pixels.\n";
}

if ( !$width ) { 
    $width = 2/3 * ($ymax - $ymin)/$pmax;
}

do write_prolog();
do print_y_axis();

# ---------------------------------------------------------------------------
# Main Part
# ---------------------------------------------------------------------------

while(<STDIN>) {
    next if /^[^0-9]/;   # ignore lines not beginning with a digit (esp. last)
    chop;
    ($time, $event, $tid, $addr, $tid2, $addr2) = split;

    if ( $event eq "*G") {
    	    $TID{$addr} = $gtid++;
	    $START{$addr} = $time;
    }

    elsif ($event eq "*A") {
    	    $TID{$addr} = $gtid++;
	    $SUSPEND{$addr} = $time;
    }

    elsif ($event eq "G*" || $event eq "GR" ) {
	do psout($START{$addr},$time,$TID{$addr},"runlineto");
#	$STOP{$addr} = $time;
    }

    elsif ($event eq "GA" || $event eq "GC" || $event eq "GY") {
	do psout($START{$addr},$time,$TID{$addr},"runlineto");
	$SUSPEND{$addr} = $time;
    }
	
    elsif ($event eq "RA") {
	$SUSPEND{$addr} = $time;
    }

    elsif ($event eq "YR") {
	do psout($SUSPEND{$addr},$time,$TID{$addr},"fetchlineto");
    }

    elsif ($event eq "CA" || $event eq "YA" ) {
	do psout($SUSPEND{$addr},$time,$TID{$addr},"fetchlineto");
	$SUSPEND{$addr} = $time;
    }

    elsif ($event eq "AC" || $event eq "AY" ) {
	do psout($SUSPEND{$addr},$time,$TID{$addr},"suspendlineto");
	$SUSPEND{$addr} = $time;
    }

    elsif ($event eq "RG") {
	$START{$addr} = $time;
    }

    elsif ($event eq "AG") {
	do psout($SUSPEND{$addr},$time,$TID{$addr},"suspendlineto");
	$START{$addr} = $time;
    } 

    elsif ($event eq "CG" || $event eq "YG" ) {
	do psout($SUSPEND{$addr},$time,$TID{$addr},"fetchlineto");
	$START{$addr} = $time;
    } elsif ( $event eq "B*" || $event eq "*B" || $event eq "BB" ) {
	print STDERR "Ignoring spark event $event at $time\n"  if $opt_v;
    } else {
	print STDERR "Unexpected event $event at $time\n";
    }

    print("%% $time: $event $addr $TID{$addr}\n\n")  if $opt_D;
}

# ---------------------------------------------------------------------------

# Logo
print("HE14 setfont\n");
if ( $opt_m ) {
    print("50 550 asciilogo\n");                     
} else {
    print("50 550 logo\n");                          #
}

# Epilogue
print("showpage\n");

if ( $gtid-1 != $pmax ) {
    if ( $pedantic ) {
	die "Error: Calculated max no. of tasks ($gtid-1) does not agree with stated max. no. of tasks ($pmax)\n";
    } else {
	print STDERR  "Warning: Calculated total no. of tasks ($gtid-1) does not agree with stated total no. of tasks ($pmax)\n" if $opt_v;
	$y_scaling = $pmax/($gtid-1); 
    }
}


exit 0;

# ---------------------------------------------------------------------------

sub psout {
    local($x1, $x2, $y, $cmd) = @_;
    print("% ($x1,$y) -- ($x2,$y) $cmd\n")  if $opt_D;
    $x1 = int(($x1/$tmax) * ($xmax-$xmin) + $xmin);
    $x2 = int(($x2/$tmax) * ($xmax-$xmin) + $xmin);
    $y = int(($y/$pmax) * ($ymax-$ymin) + $ymin);
    if ( $x1 == $x2 ) {
	$x2 = $x1 + 1;
    }
    
    if ( $opt_l ) {
	print("newpath\n");
	print("$x1 $y moveto\n");
	print("$x2 $y $cmd\n");
	print("stroke\n");
    } elsif ( $opt_O ) {
	print "$x1 $x2 $y " .
	    ( $cmd eq "runlineto" ? "G RL\n" :
	      $cmd eq "suspendlineto" ? "R SL\n" :
	      $cmd eq "fetchlineto" ? "B FL\n" :
	      "\n% ERROR: Unknown command $cmd\n");

    } else {
	print "$x2 $y $x1 $y " . 
	    ( $cmd eq "runlineto" ? "green run\n" :
	      $cmd eq "suspendlineto" ? "red suspend\n" :
	      $cmd eq "fetchlineto" ? "blue fetch\n" :
	      "\n% ERROR: Unknown command $cmd\n");
    }	    
}

# -----------------------------------------------------------------------------

sub get_date {
    local ($date);

    chop($date = `date`);
    return ($date);
}

# -----------------------------------------------------------------------------

sub write_prolog {
    local ($now);

    $now = do get_date();

    print("%!PS-Adobe-2.0\n");
    print("%%BoundingBox:    0 0 560 800\n");
    print("%%Title:          Per-thread Activity Profile\n");
    print("%%Creator:        qp2ap\n");
    print("%%StartTime:      $date\n");
    print("%%CreationDate:   $now\n");
    print("%%Copyright:      1995, 1996 by Hans-Wolfgang Loidl, University of Glasgow\n");
    print("%%EndComments\n");

    print "% " . "-" x 77 . "\n";
    print "% Tunable Parameters:\n";
    print "% The width of a line representing a task\n";
    print "/width $width def\n";
    print "% Scaling factor for the y-axis (usful to enlarge)\n";
    print "/y-scale $y_scale def\n";
    print "% " . "-" x 77 . "\n";

    print "/total-len $tmax def\n";
    print "/show-len $xmax def\n";
    print "/x-offset $xmin def\n";
    print "/y-offset $ymin def\n";
    print "% normalize is the PS version of the formula: \n" .
	  "%   int(($x1/$tmax) * ($xmax-$xmin) + $xmin) \n" .
	  "% in psout.\n";
    print "/normalize { total-len div show-len x-offset sub mul x-offset add floor } def\n";
    print "/x-normalize { exch show-len mul total-len div exch } def\n";
    print "/y-normalize { y-offset sub y-scale mul y-offset add } def\n";
    print "/str-len 12 def\n";
    print "/prt-n { cvi str-len string cvs \n" .
          "         dup stringwidth pop \n" .
	  "         currentpoint pop 780 gt { 10 sub } { 2 div } ifelse \n" .
          "         neg 0 rmoveto \n" . 
          "         show  } def \n" .
	  "        % print top-of-stack integer centered at the current point\n";
    # print "/prt-n { cvi str-len string cvs \n" .
    #       "         dup stringwidth pop 2 div neg 0 rmoveto \n" . 
    #       "         show  } def \n" .
    #	    "        % print top-of-stack integer centered at the current point\n";

    if ( $opt_l ) {
	print ("/runlineto {1.5 setlinewidth lineto} def\n");
	print ("/suspendlineto {0.5 setlinewidth lineto} def\n");
	print ("/fetchlineto {0.2 setlinewidth lineto} def\n");
    } else {
	    if ( $opt_m ) {
		if ( $opt_O ) {
		    print  "/R { 0 } def\n";
		    print  "/G { 0.5 } def\n";
		    print  "/B { 0.2 } def\n";
		} else {
		    print  "/red { 0 } def\n";
		    print  "/green { 0.5 } def\n";
		    print  "/blue { 0.2 } def\n";
		}
		print  "/set-bg { setgray } def\n";
	    } else {
		if ( $opt_O ) {
		    print  "/R { 0.8 0 0 } def\n";
		    print  "/G { 0 0.9 0.1 } def\n";
		    print  "/B { 0 0.1 0.9 } def\n";
		    print  "/set-bg { setrgbcolor } def\n";
		} else {
		    print  "/red { 0.8 0 0 } def\n";
		    print  "/green { 0 0.9 0.1 } def\n";
		    print  "/blue { 0 0.1 0.9 } def\n";
		    print  "/set-bg { setrgbcolor } def\n";
		}
	    }

	    if ( $opt_O ) {
		print "% RL: runlineto; draws a horizontal line in given color\n";
		print "% Operands: x-from x-to y color\n";
		print "/RL { set-bg   % set color \n" .
		      "      newpath y-normalize  % mangle y val\n" .
		      "      2 index 1 index moveto width setlinewidth \n" .
		      "      lineto pop stroke} def\n";
		print "% SL: suspendlineto; draws a horizontal line in given color (thinner)\n";
		print "% Operands: x-from x-to y color\n";
		print "/SL { set-bg   % set color \n" .
		      "      newpath y-normalize  % mangle y val\n" .
		      "      2 index 1 index moveto width 2 div setlinewidth \n" .
		      "      lineto pop stroke} def\n";
		print "% FL: fetchlineto; draws a horizontal line in given color (thinner)\n";
		print "% Operands: x-from x-to y color\n";
		print "/FL { set-bg   % set color \n" .
		      "      newpath y-normalize  % mangle y val\n" .
		      "      2 index 1 index moveto width " . 
			  ( $opt_m ? " 4 " : " 2 ") . 
		      " div setlinewidth \n" .
		      "      lineto pop stroke} def\n";
	    } else {
		print "/run { set-bg newpath 50 sub y-scale mul 50 add moveto width " .
		    "setlinewidth 50 sub y-scale mul 50 add lineto stroke} def\n";
		print "/suspend { set-bg newpath 50 sub y-scale mul 50 add moveto width " .
		    "2 div setlinewidth 50 sub y-scale mul 50 add lineto stroke} def\n";
		print "/fetch { set-bg newpath 50 sub y-scale mul 50 add moveto width " .
		    ( $opt_m ? " 4 " : " 2 ") .
			"div setlinewidth 50 sub y-scale mul 50 add lineto stroke} def\n";
		#print ("/run { newpath moveto 1.5 setlinewidth lineto stroke} def\n");
		#print ("/suspend { newpath moveto 0.5 setlinewidth lineto stroke} def\n");
	    }
	}

    print  "/printText { 0 0 moveto (GrAnSim) show } def\n";      
    print "/asciilogo { 5 sub moveto HB16 setfont (GrAnSim) show } def\n";
    if ( $opt_m ) {
	print "/logo { asciilogo } def\n";
    } else {
	print "/logo { gsave \n" .
	    "        translate \n" .
		"        .95 -.05 0\n" .
		    "          { dup 1 exch sub 0 exch setrgbcolor printText 1 -.5 translate } for \n" . 
			"        1 0 0 setrgbcolor printText\n" . 
			    "        grestore} def\n";
    }
    print "% For debugging PS uncomment this line and add the file behandler.ps\n";
    print "% $brkpage begin printonly endprint \n";

    print("/HE10 /Helvetica findfont 10 scalefont def\n");
    print("/HE12 /Helvetica findfont 12 scalefont def\n");
    print("/HE14 /Helvetica findfont 14 scalefont def\n");
    print("/HB16 /Helvetica-Bold findfont 16 scalefont def\n");
    print "% " . "-" x 77 . "\n";
    print("newpath\n");

    print("-90 rotate\n");
    print("-785 30 translate\n");
    print("0 8.000000 moveto\n");
    print("0 525.000000 760.000000 525.000000 8.000000 arcto\n");
    print("4 {pop} repeat\n");
    print("760.000000 525.000000 760.000000 0 8.000000 arcto\n");
    print("4 {pop} repeat\n");
    print("760.000000 0 0 0 8.000000 arcto\n");
    print("4 {pop} repeat\n");
    print("0 0 0 525.000000 8.000000 arcto\n");
    print("4 {pop} repeat\n");
    print("0.500000 setlinewidth\n");
    print("stroke\n");
    print("newpath\n");
    print("4.000000 505.000000 moveto\n");
    print("4.000000 521.000000 752.000000 521.000000 4.000000 arcto\n");
    print("4 {pop} repeat\n");
    print("752.000000 521.000000 752.000000 501.000000 4.000000 arcto\n");
    print("4 {pop} repeat\n");
    print("752.000000 501.000000 4.000000 501.000000 4.000000 arcto\n");
    print("4 {pop} repeat\n");
    print("4.000000 501.000000 4.000000 521.000000 4.000000 arcto\n");
    print("4 {pop} repeat\n");
    print("0.500000 setlinewidth\n");
    print("stroke\n");

    print("HE14 setfont\n");
    print("100 505 moveto\n");
    print("($pname ) show\n");
    
    print("($date) dup stringwidth pop 750 exch sub 505.000000 moveto show\n");
    
    # print "/total-len $tmax def\n";
    print("-40 -40 translate\n");

    print "% " . "-" x 77 . "\n";
    print "% Print x-axis:\n";
    print "/y-val $ymin def % { y-offset 40 sub 2 div y-offset add } def\n";
    print "0.5 setlinewidth\n";
    print "x-offset y-val moveto total-len normalize x-offset sub 0 rlineto stroke\n";
    print "0 total-len 10 div total-len\n" .
          " { dup normalize dup y-val moveto 0 -2 rlineto stroke  % tic\n" .
          "   y-val 10 sub moveto HE10 setfont round prt-n  % print label \n" .
	  " } for \n";
    print "1 setlinewidth\n";
    print "% " . "-" x 77 . "\n";

}

# -----------------------------------------------------------------------------

sub print_y_axis {
    local ($i);
    local ($y, $smax,$majormax, $majorint);

# Y-axis label

    print "% " . ("-" x 75) . "\n";
    print "% Y-Axis:\n";
    print "% " . ("-" x 75) . "\n";

    if ( $opt_m ) {
	print "0 setgray\n";
    } else {
	print "0 0 0 setrgbcolor\n";
    }

    print("gsave\n");
    print("HE12 setfont\n");
    print("(tasks)\n");
    print("dup stringwidth pop\n");
    print("$ymax\n");
    print("exch sub\n");
    print("$labelx exch\n");
    print("translate\n");
    print("90 rotate\n");
    print("0 0 moveto\n");
    print("show\n");
    print("grestore\n");

# Scale

    if ($pmax < $majorticks) {
	$majorticks = $pmax;
    }

    print "0.5 setlinewidth\n";

    print("HE12 setfont\n$scalex $ymin moveto\n$scalex $ymax lineto\n");
    print("% Total number of tasks: $pmax\n");
    print("% Number of ticks: $majorticks\n");

    $y = $ymax; # (($pmax - $ymin)/$majorticks) * ($majorticks-$i) + $ymin;
    print("$scalex $y moveto\n$major $y lineto\n");
    print("$markx $y moveto\n($pmax) show\n");

    $majormax = int($pmax/$majorticks)*$majorticks;
    $smax = $majormax*(($ymax-$ymin)/$pmax)+$ymin;
    $majorint = $majormax/$majorticks;

    for($i=0; $i <= $majorticks; ++$i) {
	$y = (($smax - $ymin)/$majorticks) * ($majorticks-$i) + $ymin;
	$majorval = int($majorint * ($majormax/$majorint-$i));
	print("$scalex $y moveto\n$major $y lineto\n");
	print("$markx $y moveto\n($majorval) show\n");
    }

    # print("$xmin $ymax moveto\n10 0 rlineto\n10 0 rmoveto\n($pmax) show\n");
    print " stroke\n";
    print "1 setlinewidth\n";
    print "% " . ("-" x 75) . "\n";
}

# ---------------------------------------------------------------------------

sub print_verbose_message {

    print "Prg Name: $pname  Date: $date\n";
    print "Input: stdin  Output: stdout\n";
}

# ----------------------------------------------------------------------------

sub process_options {

     if ( $opt_h ) {                      
	open(ME,$0) || die "Can't open myself ($0): $!\n";
	$n = 0;
	while (<ME>) {
	    last if $_ =~ /^$/;
	    print $_;
	    $n++;
	}
	close(ME);
	exit ;
    }
    
     if ( $opt_s ) {                      
	 $y_scale = $opt_s;
     } else {
	 $y_scale = 1; 
     }

    if ( $#ARGV != 3 ) {
	print "Usage: $0 [options] <max x value> <max y value> <prg name> <date> \n";
	print "Use -h option to get details\n";
	exit 1;
    }

    $tmax = $ARGV[0];
    $pmax = $ARGV[1];
    # GUM uses the absolute path (with '=' instead of '/') of the executed file
    # (for PVM reasons); if you want to have the full path in the generated
    # graph, too, eliminate the substitution below
    ($pname = $ARGV[2]) =~ s/.*=//;
    $date = $ARGV[3];

     if ( $opt_w ) {
	 $width = $opt_w;
     } else {
	 $width = 0;
     }

}
# -----------------------------------------------------------------------------