1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #ifndef ECMASCRIPT_MEM_MEM_CONTROLLER_H
17 #define ECMASCRIPT_MEM_MEM_CONTROLLER_H
18
19 #include <chrono>
20
21 #include "ecmascript/base/gc_ring_buffer.h"
22 #include "ecmascript/mem/heap.h"
23 #include "ecmascript/mem/mem.h"
24
25 namespace panda::ecmascript {
26 constexpr static int MILLISECONDS_PER_SECOND = 1000;
27
28 using BytesAndDuration = std::pair<uint64_t, double>;
29
MakeBytesAndDuration(uint64_t bytes,double duration)30 inline BytesAndDuration MakeBytesAndDuration(uint64_t bytes, double duration)
31 {
32 return std::make_pair(bytes, duration);
33 }
34
35 class MemController {
36 public:
37 explicit MemController(Heap* heap);
38 MemController() = default;
39 ~MemController() = default;
40 NO_COPY_SEMANTIC(MemController);
41 NO_MOVE_SEMANTIC(MemController);
42
GetSystemTimeInMs()43 static double GetSystemTimeInMs()
44 {
45 double currentTime =
46 std::chrono::duration<double>(std::chrono::high_resolution_clock::now().time_since_epoch()).count();
47 return currentTime * MILLISECOND_PER_SECOND;
48 }
49
50 double CalculateAllocLimit(size_t currentSize, size_t minSize, size_t maxSize, size_t newSpaceCapacity,
51 double factor) const;
52
53 double CalculateGrowingFactor(double gcSpeed, double mutatorSpeed);
54
55 void StartCalculationBeforeGC();
56 void StopCalculationAfterGC(TriggerGCType gcType);
57
58 void RecordAfterConcurrentMark(const bool isFull, const ConcurrentMarker *marker);
59
60 double CalculateMarkCompactSpeedPerMS();
61 double GetCurrentOldSpaceAllocationThroughtputPerMS(double timeMs = THROUGHPUT_TIME_FRAME_MS) const;
62 double GetNewSpaceAllocationThroughtPerMS() const;
63 double GetOldSpaceAllocationThroughtPerMS() const;
64 double GetNewSpaceConcurrentMarkSpeedPerMS() const;
65 double GetFullSpaceConcurrentMarkSpeedPerMS() const;
66
GetAllocTimeMs()67 double GetAllocTimeMs() const
68 {
69 return allocTimeMs_;
70 }
71
GetOldSpaceAllocAccumulatorSize()72 size_t GetOldSpaceAllocAccumulatorSize() const
73 {
74 return oldSpaceAllocAccumulatorSize_;
75 }
76
GetNonMovableSpaceAllocAccumulatorSize()77 size_t GetNonMovableSpaceAllocAccumulatorSize() const
78 {
79 return nonMovableSpaceAllocAccumulatorSize_;
80 }
81
GetCodeSpaceAllocAccumulatorSize()82 size_t GetCodeSpaceAllocAccumulatorSize() const
83 {
84 return codeSpaceAllocAccumulatorSize_;
85 }
86
GetAllocDurationSinceGc()87 double GetAllocDurationSinceGc() const
88 {
89 return allocDurationSinceGc_;
90 }
91
GetNewSpaceAllocSizeSinceGC()92 size_t GetNewSpaceAllocSizeSinceGC() const
93 {
94 return newSpaceAllocSizeSinceGC_;
95 }
96
GetOldSpaceAllocSizeSinceGC()97 size_t GetOldSpaceAllocSizeSinceGC() const
98 {
99 return oldSpaceAllocSizeSinceGC_;
100 }
101
GetNonMovableSpaceAllocSizeSinceGC()102 size_t GetNonMovableSpaceAllocSizeSinceGC() const
103 {
104 return nonMovableSpaceAllocSizeSinceGC_;
105 }
106
GetCodeSpaceAllocSizeSinceGC()107 size_t GetCodeSpaceAllocSizeSinceGC() const
108 {
109 return codeSpaceAllocSizeSinceGC_;
110 }
111
GetHugeObjectAllocSizeSinceGC()112 size_t GetHugeObjectAllocSizeSinceGC() const
113 {
114 return hugeObjectAllocSizeSinceGC_;
115 }
116
AddSurvivalRate(double rate)117 void AddSurvivalRate(double rate)
118 {
119 recordedSurvivalRates_.Push(rate);
120 }
121
GetAverageSurvivalRate()122 double GetAverageSurvivalRate() const
123 {
124 int count = recordedSurvivalRates_.Count();
125 if (count == 0) {
126 return 0;
127 }
128 double result = recordedSurvivalRates_.Sum([](double x, double y) { return x + y;}, 0.0);
129 return result / count;
130 }
131 private:
132 static constexpr int LENGTH = 10;
133 static double CalculateAverageSpeed(const base::GCRingBuffer<BytesAndDuration, LENGTH> &buffer);
134 static double CalculateAverageSpeed(const base::GCRingBuffer<BytesAndDuration, LENGTH> &buffer,
135 const BytesAndDuration &initial, const double timeMs);
136
137 Heap* heap_;
138
139 double gcStartTime_ {0.0};
140 double gcEndTime_ {0.0};
141
142 // Time and allocation accumulators.
143 double allocTimeMs_ {0.0};
144 size_t oldSpaceAllocAccumulatorSize_ {0};
145 size_t nonMovableSpaceAllocAccumulatorSize_ {0};
146 size_t codeSpaceAllocAccumulatorSize_ {0};
147
148 // Duration and allocation size in last gc.
149 double allocDurationSinceGc_ {0.0};
150 size_t newSpaceAllocSizeSinceGC_ {0};
151 size_t oldSpaceAllocSizeSinceGC_ {0};
152 size_t nonMovableSpaceAllocSizeSinceGC_ {0};
153 size_t codeSpaceAllocSizeSinceGC_ {0};
154 size_t hugeObjectAllocSizeSinceGC_{0};
155
156 int startCounter_ {0};
157 double markCompactSpeedCache_ {0.0};
158
159 base::GCRingBuffer<BytesAndDuration, LENGTH> recordedMarkCompacts_;
160 base::GCRingBuffer<BytesAndDuration, LENGTH> recordedNewSpaceAllocations_;
161 base::GCRingBuffer<BytesAndDuration, LENGTH> recordedOldSpaceAllocations_;
162 base::GCRingBuffer<BytesAndDuration, LENGTH> recordedNonmovableSpaceAllocations_;
163 base::GCRingBuffer<BytesAndDuration, LENGTH> recordedCodeSpaceAllocations_;
164
165 base::GCRingBuffer<BytesAndDuration, LENGTH> recordedConcurrentMarks_;
166 base::GCRingBuffer<BytesAndDuration, LENGTH> recordedSemiConcurrentMarks_;
167 base::GCRingBuffer<double, LENGTH> recordedSurvivalRates_;
168
169 static constexpr double THROUGHPUT_TIME_FRAME_MS = 5000;
170 static constexpr int MILLISECOND_PER_SECOND = 1000;
171 };
172
173 MemController *CreateMemController(Heap *heap, std::string_view gcTriggerType);
174 } // namespace panda::ecmascript
175 #endif // ECMASCRIPT_MEM_MEM_CONTROLLER_H
176