1 PROGRAM prohst(input,output);
2 {$debug- $line- $symtab+}
4 {**********************************************************************}
8 {* This program produces a histogram from the profile file produced *}
9 {* by the MS-DOS profile utility. It optionally reads the map file *}
10 {* generated when the program being profiled was linked, and writes *}
11 {* either the module address or, if available, the line number as *}
12 {* a prefix to the line of the graph which describes a particular *}
15 {* After using filbm (derived from the Pascal and Fortran front end *}
16 {* command scanner) to parse its parameters, prohst opens the map *}
17 {* file if specified, searches for the heading line, and then reads *}
18 {* the lines giving the names and positions of the modules. It builds *}
19 {* a linked list of module names and start addresses. *}
21 {* It then reads the bucket file header and and bucket array elements *}
22 {* into a variable created on the heap. It simultaneously calculates *}
23 {* a normalization factor. It writes the profile listing header and *}
24 {* starts to write the profile lines. For each bucket, the address *}
25 {* is calculated. The first entry in the address/name linked list *}
26 {* is the lowest addressed module. This is initially the 'current' *}
27 {* module. The bucket address is compared with the current module *}
28 {* address. When it becomes the greater, the module name is written *}
29 {* to the listing and the next entry in the address/name list becomes *}
30 {* the current module. If line numbers are available, the bucket *}
31 {* address is also compared to the current line/address. This is *}
32 {* read and calculated directly from the file. Since there may be *}
33 {* more than one line per bucket, several entries may be read until *}
34 {* the addresses compare within the span of addresses encompassed by *}
35 {* a bucket (its 'width'). Note that the idiosyncracies of Pascal i/o *}
36 {* make it necessary to continually check for the end of the map file *}
37 {* and the complexity of this code is mainly due to an attempt to *}
38 {* make it reasonably resilient to changes in the format of the map *}
41 {**********************************************************************}
49 filenam = LSTRING (max_file);
51 address_pointer = ^address_record;
52 address_record = RECORD
53 next: address_pointer;
70 this_address: address_pointer;
81 line_nos_avail: BOOLEAN;
110 line_no_part: LSTRING (17);
113 buckets: ^SUPER ARRAY [1 .. *] OF REAL;
120 PROCEDURE filbm (VAR prffil, hstfil, mapfil: filenam;
121 VAR switches: sets); EXTERN;
123 FUNCTION realword (w: WORD): REAL;
125 IF ORD (w) < 0 THEN BEGIN
126 realword := FLOAT (maxint) + FLOAT (ORD (w - maxint));
129 realword := FLOAT (ORD(w));
135 PROCEDURE skip_spaces;
137 WHILE NOT eof(map) AND THEN map^ = ' ' DO BEGIN
143 FUNCTION hex_char (ch: CHAR): WORD;
145 IF ch >= '0' AND THEN ch <= '9' THEN BEGIN
146 hex_char := WRD (ch) - WRD ('0');
148 ELSE IF ch >= 'A' AND THEN ch <= 'F' THEN BEGIN
149 hex_char := WRD (ch) - WRD ('A') + 10;
152 WRITELN ('Invalid hex character');
158 FUNCTION read_hex (i :WORD): WORD;
164 WHILE NOT eof (map) AND THEN i <> 0 DO BEGIN
165 hex_val := hex_val * 16 + hex_char (map^);
172 FUNCTION read_h: WORD;
174 read_h := read_hex (4);
179 FUNCTION read_word: WORD;
184 IF NOT EOF (map) THEN BEGIN
185 READ (map, int_value);
187 read_word := int_value;
191 FUNCTION map_digit: BOOLEAN;
193 map_digit := (map^ >= '0') OR (map^ <= '9');
197 writeln (output, ' Profile Histogram Utility - Version 1.0');
199 writeln (output, ' Copyright - Microsoft, 1983');
203 filbm (bucket_name, hist_name, map_name, switches);
205 IF 31 IN switches THEN BEGIN
206 ABORT ('Map file must not be terminal', 0, 0);
209 IF NOT (28 IN switches) THEN BEGIN
210 ABORT ('No histogram file specified', 0, 0);
213 ASSIGN (bucket, bucket_name);
215 ASSIGN (hist, hist_name);
218 map_avail := 29 IN switches;
219 line_nos_avail := FALSE;
221 IF map_avail THEN BEGIN
222 ASSIGN (map, map_name);
225 WHILE NOT EOF (map) AND THEN start <> ' Start' DO BEGIN
232 WHILE NOT EOF(map) DO BEGIN
234 IF line.len < 6 OR ELSE line [2] < '0' OR ELSE
235 line [2] > '9' THEN BEGIN
239 IF this_address <> NIL THEN BEGIN
240 NEW (this_address^.next);
241 this_address := this_address^.next;
244 this_address := first_address;
246 this_address^.next := NIL;
248 this_address^.address := (hex_char (line [2]) * 4096) +
249 (hex_char (line [3]) * 256) +
250 (hex_char (line [4]) * 16) +
253 FOR i := 1 TO 15 DO BEGIN
254 this_address^.name [i] := line [22 + i];
259 WHILE NOT EOF (map) DO BEGIN
260 READLN (map, line_no_part);
261 IF line_no_part = 'Line numbers for ' THEN BEGIN
262 line_nos_avail := TRUE;
269 read (bucket, clock_grain, bucket_num, bucket_size,
270 prog_low_pa, prog_high_pa, dos_pa, hit_io, hit_dos, hit_high);
272 NEW (buckets,ORD (bucket_num));
277 FOR i := 1 TO ORD (bucket_num) DO BEGIN
278 read (bucket, this_bucket);
279 real_bucket := realword (this_bucket);
281 IF real_bucket > norm_bucket THEN BEGIN
282 norm_bucket := real_bucket;
285 norm := norm + real_bucket;
286 buckets^[i] := real_bucket;
288 norm_bucket := 45.0/norm_bucket;
291 WRITELN (hist, 'Microsoft Profiler Output Listing');
294 WRITELN (hist, ORD (bucket_num):6, bucket_size:4,'-byte buckets.');
297 WRITELN (hist, 'Profile taken between ', prog_low_pa*16::16,
298 ' and ', prog_high_pa*16::16, '.');
301 WRITELN (hist, 'DOS program address:', dos_pa::16);
304 WRITELN (hist, 'Number of hits in DOS: ', hit_dos:5,
305 ' or ', realword (hit_dos) * norm:4:1, '%.');
306 WRITELN (hist, 'Number of hits in I/O: ', hit_io:5,
307 ' or ', realword (hit_io) * norm:4:1, '%.');
308 WRITELN (hist, 'Number of hits high : ', hit_high:5,
309 ' or ', realword (hit_high) * norm:4:1, '%.');
311 WRITELN (hist, ' Hits Addr. Line/ Cumul. % 0.0 ',
315 WRITELN (hist, ' Offset +----------------',
316 '----------------------------');
317 WRITELN (hist, name);
325 WHILE i < ORD (bucket_num) DO BEGIN
327 IF buckets^[i] < 0.9 THEN BEGIN
331 UNTIL (i = ORD (bucket_num)) OR ELSE buckets^[i] > 0.0;
334 address := bucket_size * (WRD (i) - 1);
336 WHILE map_avail AND THEN
337 address >= first_address^.address DO BEGIN
338 WRITELN (hist, ' ', first_address^.name);
339 current_base := first_address^.address;
340 first_address := first_address^.next;
343 WHILE line_nos_avail AND THEN NOT eof (map) AND THEN
344 address >= parcel DO BEGIN
346 WHILE (map^ < '0') OR (map^ > '9') DO BEGIN
348 IF EOF (map) THEN BEGIN
356 line_no := new_line_no;
357 new_line_no := read_word;
359 IF EOF (map) THEN BEGIN
362 IF map^ <> ':' THEN BEGIN
363 WRITELN ('Invalid map file');
366 IF EOF (map) THEN BEGIN
369 offset := read_hex (3) + WRD (hex_char (map^) > 0);
371 IF map^ <> 'H' THEN BEGIN
372 WRITELN ('Invalid map file');
374 IF EOF (map) THEN BEGIN
378 parcel := seg + offset;
380 1: real_per_cent := buckets^[i] * norm;
381 cum_per_cent := cum_per_cent + real_per_cent;
382 per_cent := ROUND ( buckets^[i] * norm_bucket);
384 WRITE (hist, buckets^ [i]:6:0, ' ',
386 IF line_no <> 0 THEN BEGIN
387 WRITE (hist, line_no:6);
390 ELSE IF map_avail AND THEN first_address <> NIL THEN BEGIN
391 WRITE (hist, ' #', address - first_address^.address:4:16);
397 WRITELN (hist, ' ', cum_per_cent:5:1, ' ', real_per_cent:4:1, ' |',
400 WRITELN (hist, ' +-----------------',
401 '------------------');