• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     size_t 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 GetCurrentOldSpaceAllocationThroughputPerMS(double timeMs = THROUGHPUT_TIME_FRAME_MS) const;
62     double GetNewSpaceAllocationThroughputPerMS() const;
63     double GetOldSpaceAllocationThroughputPerMS() const;
64     double GetNewSpaceConcurrentMarkSpeedPerMS() const;
65     double GetFullSpaceConcurrentMarkSpeedPerMS() const;
66 
GetAllocTimeMs()67     double GetAllocTimeMs() const
68     {
69         return allocTimeMs_;
70     }
71 
GetOldSpaceAllocAccumulatedSize()72     size_t GetOldSpaceAllocAccumulatedSize() const
73     {
74         return oldSpaceAllocAccumulatedSize_;
75     }
76 
GetNonMovableSpaceAllocAccumulatedSize()77     size_t GetNonMovableSpaceAllocAccumulatedSize() const
78     {
79         return nonMovableSpaceAllocAccumulatedSize_;
80     }
81 
GetCodeSpaceAllocAccumulatedSize()82     size_t GetCodeSpaceAllocAccumulatedSize() const
83     {
84         return codeSpaceAllocAccumulatedSize_;
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 
ResetRecordedSurvivalRates()132     void ResetRecordedSurvivalRates()
133     {
134         recordedSurvivalRates_.Reset();
135     }
136 
137 private:
138     static constexpr int LENGTH = 10;
139     static double CalculateAverageSpeed(const base::GCRingBuffer<BytesAndDuration, LENGTH> &buffer);
140     static double CalculateAverageSpeed(const base::GCRingBuffer<BytesAndDuration, LENGTH> &buffer,
141                                         const BytesAndDuration &initial, const double timeMs);
142 
143     Heap* heap_;
144     size_t minAllocLimitGrowingStep_ {0};
145 
146     double gcStartTime_ {0.0};
147     double gcEndTime_ {0.0};
148 
149     // Time and allocation accumulators.
150     double allocTimeMs_ {0.0};
151     size_t oldSpaceAllocAccumulatedSize_ {0};
152     size_t nonMovableSpaceAllocAccumulatedSize_ {0};
153     size_t codeSpaceAllocAccumulatedSize_ {0};
154 
155     // Duration and allocation size in last gc.
156     double allocDurationSinceGc_ {0.0};
157     size_t newSpaceAllocSizeSinceGC_ {0};
158     size_t oldSpaceAllocSizeSinceGC_ {0};
159     size_t nonMovableSpaceAllocSizeSinceGC_ {0};
160     size_t codeSpaceAllocSizeSinceGC_ {0};
161     size_t hugeObjectAllocSizeSinceGC_{0};
162 
163     int startCounter_ {0};
164     double markCompactSpeedCache_ {0.0};
165 
166     base::GCRingBuffer<BytesAndDuration, LENGTH> recordedMarkCompacts_;
167     base::GCRingBuffer<BytesAndDuration, LENGTH> recordedNewSpaceAllocations_;
168     base::GCRingBuffer<BytesAndDuration, LENGTH> recordedOldSpaceAllocations_;
169     base::GCRingBuffer<BytesAndDuration, LENGTH> recordedNonmovableSpaceAllocations_;
170     base::GCRingBuffer<BytesAndDuration, LENGTH> recordedCodeSpaceAllocations_;
171 
172     base::GCRingBuffer<BytesAndDuration, LENGTH> recordedConcurrentMarks_;
173     base::GCRingBuffer<BytesAndDuration, LENGTH> recordedSemiConcurrentMarks_;
174     base::GCRingBuffer<double, LENGTH> recordedSurvivalRates_;
175 
176     static constexpr double THROUGHPUT_TIME_FRAME_MS = 5000;
177     static constexpr int MILLISECOND_PER_SECOND = 1000;
178 };
179 
180 MemController *CreateMemController(Heap *heap, std::string_view gcTriggerType);
181 }  // namespace panda::ecmascript
182 #endif  // ECMASCRIPT_MEM_MEM_CONTROLLER_H
183