• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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_HEAP_REGION_ALLOCATOR_H
17 #define ECMASCRIPT_MEM_HEAP_REGION_ALLOCATOR_H
18 
19 #include <atomic>
20 
21 #include "ecmascript/mem/mem.h"
22 
23 namespace panda::ecmascript {
24 class BaseHeap;
25 class Region;
26 class Space;
27 
28 class HeapRegionAllocator {
29 public:
30     // For `Runtime::heapRegionAllocator_`, since it's for SharedHeap, so do not need enable PageTag threadId.
31     HeapRegionAllocator() = default;
32     HeapRegionAllocator(JSRuntimeOptions &option);
33     virtual ~HeapRegionAllocator() = default;
34 
35     Region *AllocateAlignedRegion(Space *space, size_t capacity, JSThread* thread, BaseHeap *heap,
36                                   bool isFresh = false);
37     void FreeRegion(Region *region, size_t cachedSize, bool skipCache = false);
38 
IncreaseAnnoMemoryUsage(size_t bytes)39     void IncreaseAnnoMemoryUsage(size_t bytes)
40     {
41         size_t current = annoMemoryUsage_.fetch_add(bytes, std::memory_order_relaxed) + bytes;
42         size_t max = maxAnnoMemoryUsage_.load(std::memory_order_relaxed);
43         while (current > max && !maxAnnoMemoryUsage_.compare_exchange_weak(max, current, std::memory_order_relaxed)) {
44         }
45     }
46 
DecreaseAnnoMemoryUsage(size_t bytes)47     void DecreaseAnnoMemoryUsage(size_t bytes)
48     {
49         annoMemoryUsage_.fetch_sub(bytes, std::memory_order_relaxed);
50     }
51 
GetAnnoMemoryUsage()52     size_t GetAnnoMemoryUsage() const
53     {
54         return annoMemoryUsage_.load(std::memory_order_relaxed);
55     }
56 
GetMaxAnnoMemoryUsage()57     size_t GetMaxAnnoMemoryUsage() const
58     {
59         return maxAnnoMemoryUsage_.load(std::memory_order_relaxed);
60     }
61 
62 private:
63     NO_COPY_SEMANTIC(HeapRegionAllocator);
64     NO_MOVE_SEMANTIC(HeapRegionAllocator);
65 
66     // Can not throw OOM during GC, so just make MemMapAllocator infinite to make allocating region always
67     // success to complete this GC, and then do HeapDump and Fatal.
68     // This will temporarily lead that all JSThread could always AllcationRegion success,
69     // breaking the global region limit, but thread calling this will soon complete GC and then fatal.
70     void TemporarilyEnsureAllocateionAlwaysSuccess(BaseHeap *heap);
71 
72     bool AllocateRegionShouldPageTag(Space *space) const;
73     bool FreeRegionShouldPageTag(Region *region) const;
74 
75     std::atomic<size_t> annoMemoryUsage_ {0};
76     std::atomic<size_t> maxAnnoMemoryUsage_ {0};
77     bool enablePageTagThreadId_ {false};
78 };
79 }  // namespace panda::ecmascript
80 
81 #endif  // ECMASCRIPT_MEM_HEAP_REGION_ALLOCATOR_H
82