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