File: heapsizing.h

package info (click to toggle)
polyml 5.7.1-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 40,616 kB
  • sloc: cpp: 44,142; ansic: 26,963; sh: 22,002; asm: 13,486; makefile: 602; exp: 525; python: 253; awk: 91
file content (160 lines) | stat: -rw-r--r-- 5,471 bytes parent folder | download | duplicates (4)
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
/*
    Title:  heapsizing.h - parameters to adjust heap size

    Copyright (c) Copyright David C.J. Matthews 2012

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.
    
    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.
    
    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

*/

#ifndef HEAPSIZING_H_INCLUDED
#define HEAPSIZING_H_INCLUDED 1

#include "timing.h"

class LocalMemSpace;

class HeapSizeParameters {
public:
    HeapSizeParameters();

    // Extract timing information for ML.
    Handle getGCUtime(TaskData *taskData) const;
    Handle getGCStime(TaskData *taskData) const;

    void SetHeapParameters(POLYUNSIGNED minsize, POLYUNSIGNED maxsize, POLYUNSIGNED initialsize, unsigned percent);

    void SetReservation(POLYUNSIGNED rsize);

    // Called in the minor GC if a GC thread needs to grow the heap.
    // Returns zero if the heap cannot be grown.
    LocalMemSpace *AddSpaceInMinorGC(POLYUNSIGNED space, bool isMutable);

    // Called in the major GC before the copy phase if the heap is more than
    // 90% full.  This should improve the efficiency of copying.
    LocalMemSpace *AddSpaceBeforeCopyPhase(bool isMutable);

    bool PerformSharingPass() const { return performSharingPass; }
    void AdjustSizeAfterMajorGC(POLYUNSIGNED wordsRequired);
    bool AdjustSizeAfterMinorGC(POLYUNSIGNED spaceAfterGC, POLYUNSIGNED spaceBeforeGC);

    // Returns true if we should run a major GC at this point
    bool RunMajorGCImmediately();

    /* Called by the garbage collector at the beginning and
       end of garbage collection. */
    typedef enum __gcTime {
        GCTimeStart,
        GCTimeIntermediate,
        GCTimeEnd
    } gcTime;

    // These are called by the GC to record information about its progress.
    void RecordAtStartOfMajorGC();
    void RecordGCTime(gcTime isEnd, const char *stage = "");
    void RecordSharingData(POLYUNSIGNED recovery);
    
    void resetMinorTimingData(void);
    void resetMajorTimingData(void);

    void Init(void);
    void Final(void);

private:
    // Estimate the GC cost for a given heap size.  The result is the ratio of
    // GC time to application time.
    double costFunction(POLYUNSIGNED heapSize, bool withSharing, bool withSharingCost);

    bool getCostAndSize(POLYUNSIGNED &heapSize, double &cost, bool withSharing);

    // Set if we should do a full GC next time instead of a minor GC.
    bool fullGCNextTime;

    // Whether a sharing pass should be performed on the next GC
    bool performSharingPass;
    // The proportion of the total heap recovered by the sharing pass
    double sharingRecoveryRate;
    // The cost of doing the sharing as a proportion of the rest of the GC.
    double sharingCostFactor;
    // The actual number of words recovered in the last sharing pass
    POLYUNSIGNED sharingWordsRecovered;
    // The saving we would have made by enabling sharing in the past
    double cumulativeSharingSaving;

    // Maximum and minimum heap size as given by the user.
    POLYUNSIGNED minHeapSize, maxHeapSize;

    // Target GC cost requested by the user.
    double userGCRatio;
    // Actual ratio for the last major GC
    double lastMajorGCRatio;
    // Predicted ratio for the next GC
    double predictedRatio;

    POLYUNSIGNED lastFreeSpace, currentSpaceUsed;
    // Set to false if an allocation failed.  Indicates that
    // we may have reached some virtual memory limit.
    bool lastAllocationSucceeded;
    // Set to true if the last major GC may have hit the limit
    bool allocationFailedBeforeLastMajorGC;

    // The estimated boundary where the paging will become
    // a significant factor.
    POLYUNSIGNED pagingLimitSize;

    // The maximum size the heap has reached so far. 
    POLYUNSIGNED highWaterMark;

    // The heap size at the start of the current GC before any spaces have been deleted.
    POLYUNSIGNED heapSizeAtStart;

    // The start of the clock.
    TIMEDATA startTime;

    // Timing for the last minor or major GC
    TIMEDATA minorNonGCUserCPU;
    TIMEDATA minorNonGCSystemCPU;
    TIMEDATA minorNonGCReal;
    TIMEDATA minorGCUserCPU;
    TIMEDATA minorGCSystemCPU;
    TIMEDATA minorGCReal;
    long minorGCPageFaults;
    unsigned minorGCsSinceMajor;

    // Timing for all the minor GCs and the last major GC.
    // Reset after each major GC.
    TIMEDATA majorNonGCUserCPU;
    TIMEDATA majorNonGCSystemCPU;
    TIMEDATA majorNonGCReal;
    TIMEDATA majorGCUserCPU;
    TIMEDATA majorGCSystemCPU;
    TIMEDATA majorGCReal;
    long majorGCPageFaults;

    // Totals for all GCs
    TIMEDATA totalGCUserCPU;
    TIMEDATA totalGCSystemCPU;
    TIMEDATA totalGCReal;

    // The cost for the last sharing pass
    TIMEDATA sharingCPU;

    TIMEDATA startUsageU, startUsageS, lastUsageU, lastUsageS;
    TIMEDATA startRTime, lastRTime;
    long startPF;
};

extern HeapSizeParameters gHeapSizeParameters;
#endif