[project @ 2000-06-13 16:07:20 by simonmar]
[ghc-hetmet.git] / ghc / driver / stats / ghc-stats.lprl
1 %************************************************************************
2 %*                                                                      *
3 \section[Driver-stats]{Collecting garbage collection statistics for a ghc run}
4 %*                                                                      *
5 %************************************************************************
6
7 \begin{code}
8 $StatsFile = $ARGV[0];
9 &process_ghc_timings();
10
11 sub process_ghc_timings {
12     local($SysSpecificTiming) = 'ghc';
13
14     open(STATS, $StatsFile) || die "Failed when opening $StatsFile\n";
15     local($max_live)    = 0; 
16     local($tot_live)    = 0; # for calculating residency stuff
17     local($tot_samples) = 0;
18
19     while (<STATS>) {
20         if (! /Gen:\s+0/ && ! /Minor/ && /^\s*\d+\s+\d+\s+(\d+)\s+\d+\.\d+/ ) {
21                 $max_live = $1 if $max_live < $1;
22                 $tot_live += $1;
23                 $tot_samples += 1;
24         }
25         $BytesAlloc = $1 if /^\s*([0-9,]+) bytes allocated in the heap/;
26
27         if ( /^\s*([0-9,]+) bytes maximum residency .* (\d+) sample/ ) {
28             $MaxResidency = $1; $ResidencySamples = $2;
29         }
30
31         $GCs = $1 if /^\s*([0-9,]+) (collections? in generation 0|garbage collections? performed)/;
32
33         if ( /^\s+([0-9]+)\s+Mb total memory/ ) {
34             $TotMem = $1;
35         }
36
37         # The presence of -? in the following pattern is only there to
38         # accommodate 0.29 && <= 2.05 RTS'
39         if ( /^\s*INIT\s+time\s*(\d+\.\d\d)s\s*\(\s*-?(\d+\.\d\d)s elapsed\)/ ) {
40             $InitTime = $1; $InitElapsed = $2;
41         } elsif ( /^\s*MUT\s+time\s*(\d+\.\d\d)s\s*\(\s*(\d+\.\d\d)s elapsed\)/ ) {
42             $MutTime = $1; $MutElapsed = $2;
43         } elsif ( /^\s*GC\s+time\s*(\d+\.\d\d)s\s*\(\s*(\d+\.\d\d)s elapsed\)/ ) {
44             $GcTime = $1; $GcElapsed = $2;
45         }
46     }
47     close(STATS) || die "Failed when closing $StatsFile\n";
48     if ( $tot_samples > 0 ) {
49         $ResidencySamples = $tot_samples;
50         $MaxResidency = $max_live;
51         $AvgResidency = int ($tot_live / $tot_samples) ;
52     }
53
54     # warn about what we didn't find
55     print STDERR "Warning: BytesAlloc not found in stats file\n" unless defined($BytesAlloc);
56     print STDERR "Warning: GCs not found in stats file\n" unless defined($GCs);
57     print STDERR "Warning: InitTime not found in stats file\n" unless defined($InitTime);
58     print STDERR "Warning: InitElapsed not found in stats file\n" unless defined($InitElapsed);
59     print STDERR "Warning: MutTime not found in stats file\n" unless defined($MutTime);
60     print STDERR "Warning: MutElapsed not found in stats file\n" unless defined($MutElapsed);
61     print STDERR "Warning: GcTime inot found in stats file\n" unless defined($GcTime);
62     print STDERR "Warning: GcElapsed not found in stats file\n" unless defined($GcElapsed);
63
64     # things we didn't necessarily expect to find
65     $MaxResidency     = 0 unless defined($MaxResidency);
66     $AvgResidency     = 0 unless defined($AvgResidency);
67     $ResidencySamples = 0 unless defined($ResidencySamples);
68
69     # a bit of tidying
70     $BytesAlloc =~ s/,//g;
71     $MaxResidency =~ s/,//g;
72     $GCs =~ s/,//g;
73     $InitTime =~ s/,//g;
74     $InitElapsed =~ s/,//g;
75     $MutTime =~ s/,//g;
76     $MutElapsed =~ s/,//g;
77     $GcTime =~ s/,//g;
78     $GcElapsed =~ s/,//g;
79
80     # print out what we found
81     print STDERR "<<$SysSpecificTiming: ",
82         "$BytesAlloc bytes, $GCs GCs, $AvgResidency/$MaxResidency avg/max bytes residency ($ResidencySamples samples), ${TotMem}M in use, $InitTime INIT ($InitElapsed elapsed), $MutTime MUT ($MutElapsed elapsed), $GcTime GC ($GcElapsed elapsed)",
83         " :$SysSpecificTiming>>\n";
84 }
85 \end{code}