1 /*
2     Title:  heapsizing.h - parameters to adjust heap size
3 
4     Copyright (c) Copyright David C.J. Matthews 2012
5 
6     This library is free software; you can redistribute it and/or
7     modify it under the terms of the GNU Lesser General Public
8     License as published by the Free Software Foundation; either
9     version 2.1 of the License, or (at your option) any later version.
10 
11     This library is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14     Lesser General Public License for more details.
15 
16     You should have received a copy of the GNU Lesser General Public
17     License along with this library; if not, write to the Free Software
18     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19 
20 */
21 
22 #ifndef HEAPSIZING_H_INCLUDED
23 #define HEAPSIZING_H_INCLUDED 1
24 
25 #include "timing.h"
26 
27 class LocalMemSpace;
28 
29 class HeapSizeParameters {
30 public:
31     HeapSizeParameters();
32 
33     // Extract timing information for ML.
34     Handle getGCUtime(TaskData *taskData) const;
35     Handle getGCStime(TaskData *taskData) const;
36 
37     void SetHeapParameters(uintptr_t minsize, uintptr_t maxsize, uintptr_t initialsize, unsigned percent);
38 
39     void SetReservation(uintptr_t rsize);
40 
41     // Called in the minor GC if a GC thread needs to grow the heap.
42     // Returns zero if the heap cannot be grown.
43     LocalMemSpace *AddSpaceInMinorGC(uintptr_t space, bool isMutable);
44 
45     // Called in the major GC before the copy phase if the heap is more than
46     // 90% full.  This should improve the efficiency of copying.
47     LocalMemSpace *AddSpaceBeforeCopyPhase(bool isMutable);
48 
PerformSharingPass()49     bool PerformSharingPass() const { return performSharingPass; }
50     void AdjustSizeAfterMajorGC(uintptr_t wordsRequired);
51     bool AdjustSizeAfterMinorGC(uintptr_t spaceAfterGC, uintptr_t spaceBeforeGC);
52 
53     // Returns true if we should run a major GC at this point
54     bool RunMajorGCImmediately();
55 
56     /* Called by the garbage collector at the beginning and
57        end of garbage collection. */
58     typedef enum __gcTime {
59         GCTimeStart,
60         GCTimeIntermediate,
61         GCTimeEnd
62     } gcTime;
63 
64     // These are called by the GC to record information about its progress.
65     void RecordAtStartOfMajorGC();
66     void RecordGCTime(gcTime isEnd, const char *stage = "");
67     void RecordSharingData(POLYUNSIGNED recovery);
68 
69     void resetMinorTimingData(void);
70     void resetMajorTimingData(void);
71 
72     void Init(void);
73     void Final(void);
74 
75 private:
76     // Estimate the GC cost for a given heap size.  The result is the ratio of
77     // GC time to application time.
78     double costFunction(uintptr_t heapSize, bool withSharing, bool withSharingCost);
79 
80     bool getCostAndSize(uintptr_t &heapSize, double &cost, bool withSharing);
81 
82     // Set if we should do a full GC next time instead of a minor GC.
83     bool fullGCNextTime;
84 
85     // Whether a sharing pass should be performed on the next GC
86     bool performSharingPass;
87     // The proportion of the total heap recovered by the sharing pass
88     double sharingRecoveryRate;
89     // The cost of doing the sharing as a proportion of the rest of the GC.
90     double sharingCostFactor;
91     // The actual number of words recovered in the last sharing pass
92     POLYUNSIGNED sharingWordsRecovered;
93     // The saving we would have made by enabling sharing in the past
94     double cumulativeSharingSaving;
95 
96     // Maximum and minimum heap size as given by the user.
97     uintptr_t minHeapSize, maxHeapSize;
98 
99     // Target GC cost requested by the user.
100     double userGCRatio;
101     // Actual ratio for the last major GC
102     double lastMajorGCRatio;
103     // Predicted ratio for the next GC
104     double predictedRatio;
105 
106     uintptr_t lastFreeSpace, currentSpaceUsed;
107     // Set to false if an allocation failed.  Indicates that
108     // we may have reached some virtual memory limit.
109     bool lastAllocationSucceeded;
110     // Set to true if the last major GC may have hit the limit
111     bool allocationFailedBeforeLastMajorGC;
112 
113     // The estimated boundary where the paging will become
114     // a significant factor.
115     uintptr_t pagingLimitSize;
116 
117     // The maximum size the heap has reached so far.
118     uintptr_t highWaterMark;
119 
120     // The heap size at the start of the current GC before any spaces have been deleted.
121     uintptr_t heapSizeAtStart;
122 
123     // The start of the clock.
124     TIMEDATA startTime;
125 
126     // Timing for the last minor or major GC
127     TIMEDATA minorNonGCUserCPU;
128     TIMEDATA minorNonGCSystemCPU;
129     TIMEDATA minorNonGCReal;
130     TIMEDATA minorGCUserCPU;
131     TIMEDATA minorGCSystemCPU;
132     TIMEDATA minorGCReal;
133     long minorGCPageFaults;
134     unsigned minorGCsSinceMajor;
135 
136     // Timing for all the minor GCs and the last major GC.
137     // Reset after each major GC.
138     TIMEDATA majorNonGCUserCPU;
139     TIMEDATA majorNonGCSystemCPU;
140     TIMEDATA majorNonGCReal;
141     TIMEDATA majorGCUserCPU;
142     TIMEDATA majorGCSystemCPU;
143     TIMEDATA majorGCReal;
144     long majorGCPageFaults;
145 
146     // Totals for all GCs
147     TIMEDATA totalGCUserCPU;
148     TIMEDATA totalGCSystemCPU;
149     TIMEDATA totalGCReal;
150 
151     // The cost for the last sharing pass
152     TIMEDATA sharingCPU;
153 
154     TIMEDATA startUsageU, startUsageS, lastUsageU, lastUsageS;
155     TIMEDATA startRTime, lastRTime;
156     long startPF;
157 };
158 
159 extern HeapSizeParameters gHeapSizeParameters;
160 #endif
161