summaryrefslogtreecommitdiff
path: root/utils/prof/ghcprof.prl
blob: bc3b344228b866018647aaf8015a479ac73dd55d (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
# -----------------------------------------------------------------------------
# $Id: ghcprof.prl,v 1.5 2005/04/22 08:41:00 simonmar Exp $
#
# (c) The GHC Team 2000
#
# needs: FPTOOLS_TOP_ABS, INSTALLING, DEFAULT_TMPDIR, TARGETPLATFORM, libexecdir
#

if ($ENV{'UDG_HOME'}) {
    $udrawgraphhome = $ENV{'UDG_HOME'};
    $udrawgraph     = $udrawgraphhome . "/bin/uDrawGraph";
} else {
    print STDERR "ghcprof: UDG_HOME environment variable not set\n";
    exit(1);
}

$machname      = ${TARGETPLATFORM};
$bsp_s         = 10.0;
$bsp_l         = 12;
$bsp_g         = 13;
$MaxNoNodes    = 1900;

$icondir    = ( $INSTALLING ? "$libexecdir/icons" 
	                    : "$FPTOOLS_TOP_ABS/ghc/utils/prof/icons" );
 
$xmlparser  = ( $INSTALLING ? "$libexecdir/xmlparser"
	                    : "$FPTOOLS_TOP_ABS/ghc/utils/prof/xmlparser/xmlparser" );

$cgprof_dir = ( $INSTALLING ? "$libexecdir"
	                    : "$FPTOOLS_TOP_ABS/ghc/utils/prof/cgprof" );

# where to make tmp file names?
if ( $ENV{'TMPDIR'} ) {
    $Tmp_prefix = $ENV{'TMPDIR'} . "/ghcprof";
} else {
    $Tmp_prefix ="${DEFAULT_TMPDIR}/ghcprof";
    $ENV{'TMPDIR'} = "${DEFAULT_TMPDIR}"; # set the env var as well
}

# Create a new temporary filename.
$i = $$;
$tempfile = "";
while (-e ($tempfile = "$Tmp_prefix" . "$i")) {
    $i++;
};

# Create a second temporary filename.
$i = $$;
$tempfile2 = "";
while (-e ($tempfile2 = "$Tmp_prefix" . "$i" . ".sh")) {
    $i++;
};

# Delete temp. file if script is halted.
sub quit_upon_signal { 
    if ($tempfile ne "" && -e $tempfile) {
	print STDERR "Deleting $tempfile .. \n" if $Verbose; 
	unlink "$tempfile"; 
    };
    if ($tempfile2 ne "" && -e $tempfile2) {
	print STDERR "Deleting $tempfile2 .. \n" if $Verbose; 
	unlink "$tempfile2"; 
    }
}

$SIG{'INT'}  = 'quit_upon_signal';
$SIG{'QUIT'} = 'quit_upon_signal';

sub tidy_up_and_die { 
    local($msg) = @_;
    
    print STDERR "$Pgm: $msg\n";
    quit_upon_signal;
    exit(1);
}

select(STDERR); $| = 1; select(STDOUT); # no STDERR buffering, please.
($Pgm = $0) =~ s|.*/||;
$Version        = "v2.1 10-3-2000";
$bug_reports_to = 'stephen.jarvis@dcs.warwick.ac.uk';

$ShortUsage = "\n$Pgm usage: for basic information, try the `-help' option\n";

$Usage = <<EOF
Usage: $Pgm [option...] filename.prof

Options:
    -v          Verbose
    -hide       (???)
    -nologo     Omit the logo
    -grey       Greyscale only
    -color      Enable color (default)
    -normalise  (???)
EOF
    ;

$Verbose       = 0;
$InputFile     = "";
$date          = "";
$nprocs        = 0;
$hide          = 0.01;
$Logo          = 1;
$Colour        = 1;
$DeltaNormalise= 2;

 arg: while ($_ = $ARGV[0]) {
     shift(@ARGV);
     #--------HELP------------------------------------------------
     /^-help$/   && do { print STDERR $Usage; exit(0); };
     
     /^-v$/      && do {$Verbose = 1; next arg;};
     
     /^-hide$/   && do {$hide= &grab_next_arg("-hide");
			if (($hide =~ /^(\d+.\d+)$/) || ($hide =~ /^(\d+)$/)) {
			    $hide = $1/100.0;
			} else {
			    print STDERR "$Pgm: -hide requires a percentage as ",
			    "an argument\n";
			    $Status++;
			}
			next arg;};
     
     /^-nologo$/    && do {$Logo  =0; next arg;};
     /^-gr(e|a)y$/  && do {$Colour=0; next arg;};
     /^-colou?r$/   && do {$Colour=1; next arg;};
     /^-normalise$/ && do {$DeltaNormalise = &grab_next_arg("-normalise");
			   if ($DeltaNormalise =~ /^(\d+)$/) {
			       $DeltaNormalise = int($DeltaNormalise);
			   } else {
			       print STDERR "$Pgm: -normalise requires an integer ",
			       "an argument\n";
			       $Status++;
			   }
			   next arg;};
     
     /^-/           && do { print STDERR "$Pgm: unrecognised option \"",$_,"\"\n"; 
			    $Status++;
			};
     
     if ($InputFile eq "") {
	 $InputFile = $_; next arg; 
     } else {
	 $Status++;
     };
 }

if ($InputFile eq "") {
    print STDERR "$Pgm: no input file given\n";
    $Status++;
}  
if ($Status>0) {
    print STDERR $ShortUsage;
    exit(1);
}
print STDERR "$Pgm: ($Version)\n" if $Verbose;

# -----------------------------------------------------------------------------
# Parse the XML

# ToDo: use the real xmlparser
# system("$xmlparser < $InputFile > $tempfile");
# if ($? != 0) { tidy_up_and_die("xmlparser failed"); }

# Stehpen's hacky replacement for xmlparser:

$cc_write  = 1; 
$ccs_write = 1;
$scc_write = 1;

open(INPUT, "<$InputFile") || tidy_up_and_die("can't open `$InputFile'");
open(TEMPFILE, ">$tempfile") || tidy_up_and_die("can't create `$tempfile'");

while (<INPUT>) { 
    if (/^1 (\d+) (.*)$/)
    {
	if ($cc_write) { 
	    print TEMPFILE ">>cost_centre\n"; 
	    $cc_write = 0; 
	}
	$cc_id		= $1;
	$name		= $2;
	$module		= $3;
	print TEMPFILE "$cc_id $name $module\n"; 
    }	
    if (/^2 (\d+) (\d+) (\d+)$/)
    {
	if ($ccs_write) {
	    print TEMPFILE ">>cost_centre_stack\n";
	    $ccs_write = 0;
	}
	$ccs_id         = $1;
	$ccptr          = $2;
	$ccsptr         = $3;
	print TEMPFILE "$ccs_id $ccptr $ccsptr\n";
    } 
    elsif (/^2 (\d+) (\d+) (\d+) (\d+)$/)
    {
	if ($ccs_write) {
	    print TEMPFILE ">>cost_centre_stack\n";
	    $ccs_write = 0;
	}
	$ccs_id         = $1;
	$type           = $2;
	$ccptr          = $3;
	$ccsptr		= $4;
	print TEMPFILE "$ccs_id $type $ccptr $ccsptr\n";
    } 
    if (/^5 (\d+) (.*)$/)
    {
	if ($scc_write) {
	    print TEMPFILE ">>scc_sample\n";
	    $scc_write = 0;
	}
	$_		= $2;
	while (/^1 (\d+) (\d+) (\d+) (\d+) (.*)$/) 
	{
	    $rg1		= $1;
	    $rg2		= $2;
	    $rg3		= $3;
	    $rg4		= $4;
	    print TEMPFILE "$rg1 $rg2 $rg3 $rg4\n";
	    $_		= $5;
	}	
    }
}
print TEMPFILE ">>\n";

close(INPUT);
close(TEMPFILE);

&readProfileHeader();
open(TEMPFILE2, ">$tempfile2") 
                || tidy_up_and_die("can't create `$tempfile2'");

$shcmd = sprintf("%s/cgprof %s %d \"%s\" " .
	       "\"%s\" %.1f %.1f %.1f %.1f %d %d %d %d %d",
	       $cgprof_dir,$tempfile,$nprocs,$machname,$date,
	       $bsp_s,$bsp_l,$bsp_g,$hide,$Verbose,$Logo,$Colour,
	       $DeltaNormalise,$MaxNoNodes);
print TEMPFILE2 "#!/bin/sh\n";
print TEMPFILE2 "$shcmd\n";
close(TEMPFILE2);

chmod 0755, $tempfile2;
$cmd = "env UDG_ICONDIR=$icondir UDG_HOME=$udrawgraphhome " . 
         $udrawgraph . " -startappl . $tempfile2";
print STDERR "$Pgm: exec $cmd\n" if $Verbose;
exec $cmd;
exit(0);

sub readProfileHeader {
    local($found);
    
    open(PROFILE,$tempfile) || tidy_up_and_die("can't open `$tempfile'");
    $found=0;
    
    while(<PROFILE>) {
	if (/^F/) {
	    if (/-prof/ && /-flibrary-level\s+(\d+)/) {
		$libtype = "P$1";
	    } elsif (/-flibrary-level\s+(\d+)/) {
		$libtype = "O$1";
	    }
	    $found++;
	    
	} elsif (/^P\s*/) {
	    $nprocs = int($');
	    $found++;
	    
	} elsif (/^D\s*/) {
	    chop($date = $');
	    $found++;
	    
	} elsif (/^X\s*/) {
	    chop($device = $');
	}
	last if ($found>=3);
    }
    close(PROFILE);
}