1/* 2 * Copyright (c) 2024 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_SHARED_MEM_CONTROLLER_H 17#define ECMASCRIPT_MEM_SHARED_MEM_CONTROLLER_H 18 19#include <chrono> 20#include <cmath> 21#include <limits> 22 23#include "ecmascript/base/gc_ring_buffer.h" 24#include "ecmascript/mem/mem_controller_utils.h" 25#include "ecmascript/mem/mem.h" 26 27namespace panda::ecmascript { 28class SharedHeap; 29 30using Clock = std::chrono::high_resolution_clock; 31using BytesAndDuration = std::pair<uint64_t, double>; 32 33class SharedMemController { 34public: 35 explicit SharedMemController(const SharedHeap* sheap):sheap_(sheap){}; 36 SharedMemController() = default; 37 ~SharedMemController() = default; 38 NO_COPY_SEMANTIC(SharedMemController); 39 NO_MOVE_SEMANTIC(SharedMemController); 40 41 void RecordAllocationForIdle(); 42 bool CheckLowAllocationUsageState() const; 43 void UpdateAllocationAfterGC(); 44 45 double GetIdleSharedSpaceAllocationThroughputPerMS() const 46 { 47 return MemControllerUtils::CalculateAverageSpeed(recordedSharedSpaceAllocations_); 48 } 49 50 double GetLastGCOldSpaceObjectUsageRate() const 51 { 52 return lastGCOldSpaceObjectUsageRate_; 53 } 54 55 double GetLastGCObjectUsageRate() const 56 { 57 return lastGCObjectUsageRate_; 58 } 59 60private: 61 const SharedHeap *sheap_ {nullptr}; 62 63 double allocTimeMsSharedIdle_ {0.0}; 64 size_t sharedSpaceAllocSizeSinceIdle_ {0}; 65 double lastGCObjectUsageRate_ {1.0f}; 66 double lastGCOldSpaceObjectUsageRate_ {1.0f}; 67 base::GCRingBuffer<BytesAndDuration, MemControllerUtils::LENGTH> recordedSharedSpaceAllocations_; 68}; 69 70} // namespace panda::ecmascript 71#endif // ECMASCRIPT_MEM_SHARED_MEM_CONTROLLER_H 72