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#include "ecmascript/mem/heap-inl.h"
17
18namespace panda::ecmascript {
19
20void SharedMemController::RecordAllocationForIdle()
21{
22    double currentTimeInMs = MemControllerUtils::GetSystemTimeInMs();
23    size_t currentSize = sheap_->GetHeapObjectSize();
24    if (sharedSpaceAllocSizeSinceIdle_ == 0 || currentSize < sharedSpaceAllocSizeSinceIdle_) {
25        sharedSpaceAllocSizeSinceIdle_ = currentSize;
26        allocTimeMsSharedIdle_ = currentTimeInMs;
27        return;
28    }
29    recordedSharedSpaceAllocations_.Push(std::make_pair(currentSize - sharedSpaceAllocSizeSinceIdle_,
30        currentTimeInMs - allocTimeMsSharedIdle_));
31    sharedSpaceAllocSizeSinceIdle_ = currentSize;
32    allocTimeMsSharedIdle_ = currentTimeInMs;
33}
34
35bool SharedMemController::CheckLowAllocationUsageState() const
36{
37    return GetIdleSharedSpaceAllocationThroughputPerMS() < LOW_ALLOCATION_RATE_PER_MS;
38}
39
40void SharedMemController::UpdateAllocationAfterGC()
41{
42    sharedSpaceAllocSizeSinceIdle_ = sheap_->GetHeapObjectSize();
43    allocTimeMsSharedIdle_ = MemControllerUtils::GetSystemTimeInMs();
44}
45
46}  // namespace panda::ecmascript
47