2727#include " memory/metaspaceUtils.hpp"
2828#include " nmt/memBaseline.hpp"
2929#include " nmt/memTracker.hpp"
30- #include " runtime/javaThread.hpp"
31- #include " runtime/safepoint.hpp"
30+ #include " nmt/regionsTree.inline.hpp"
3231
3332/*
3433 * Sizes are sorted in descenting order for reporting
@@ -104,38 +103,6 @@ class MallocAllocationSiteWalker : public MallocSiteWalker {
104103 }
105104};
106105
107- // Walk all virtual memory regions for baselining
108- class VirtualMemoryAllocationWalker : public VirtualMemoryWalker {
109- private:
110- typedef LinkedListImpl<ReservedMemoryRegion, AnyObj::C_HEAP, mtNMT,
111- AllocFailStrategy::RETURN_NULL> EntryList;
112- EntryList _virtual_memory_regions;
113- DEBUG_ONLY (address _last_base;)
114- public:
115- VirtualMemoryAllocationWalker () {
116- DEBUG_ONLY (_last_base = nullptr );
117- }
118-
119- bool do_allocation_site (const ReservedMemoryRegion* rgn) {
120- assert (rgn->base () >= _last_base, " region unordered?" );
121- DEBUG_ONLY (_last_base = rgn->base ());
122- if (rgn->size () > 0 ) {
123- if (_virtual_memory_regions.add (*rgn) != nullptr ) {
124- return true ;
125- } else {
126- return false ;
127- }
128- } else {
129- // Ignore empty sites.
130- return true ;
131- }
132- }
133-
134- LinkedList<ReservedMemoryRegion>* virtual_memory_allocations () {
135- return &_virtual_memory_regions;
136- }
137- };
138-
139106void MemBaseline::baseline_summary () {
140107 MallocMemorySummary::snapshot (&_malloc_memory_snapshot);
141108 {
@@ -158,14 +125,15 @@ bool MemBaseline::baseline_allocation_sites() {
158125 // The malloc sites are collected in size order
159126 _malloc_sites_order = by_size;
160127
161- // Virtual memory allocation sites
162- VirtualMemoryAllocationWalker virtual_memory_walker;
163- if (!MemTracker::walk_virtual_memory (&virtual_memory_walker)) {
164- return false ;
165- }
128+ assert (_vma_allocations == nullptr , " must" );
166129
167- // Virtual memory allocations are collected in call stack order
168- _virtual_memory_allocations.move (virtual_memory_walker.virtual_memory_allocations ());
130+ {
131+ MemTracker::NmtVirtualMemoryLocker locker;
132+ _vma_allocations = new (mtNMT, std::nothrow) RegionsTree (*VirtualMemoryTracker::Instance::tree ());
133+ if (_vma_allocations == nullptr ) {
134+ return false ;
135+ }
136+ }
169137
170138 if (!aggregate_virtual_memory_allocation_sites ()) {
171139 return false ;
@@ -202,20 +170,28 @@ int compare_allocation_site(const VirtualMemoryAllocationSite& s1,
202170bool MemBaseline::aggregate_virtual_memory_allocation_sites () {
203171 SortedLinkedList<VirtualMemoryAllocationSite, compare_allocation_site> allocation_sites;
204172
205- VirtualMemoryAllocationIterator itr = virtual_memory_allocations ();
206- const ReservedMemoryRegion* rgn;
207173 VirtualMemoryAllocationSite* site;
208- while ((rgn = itr.next ()) != nullptr ) {
209- VirtualMemoryAllocationSite tmp (*rgn->call_stack (), rgn->mem_tag ());
174+ bool failed_oom = false ;
175+ _vma_allocations->visit_reserved_regions ([&](ReservedMemoryRegion& rgn) {
176+ VirtualMemoryAllocationSite tmp (*rgn.call_stack (), rgn.mem_tag ());
210177 site = allocation_sites.find (tmp);
211178 if (site == nullptr ) {
212179 LinkedListNode<VirtualMemoryAllocationSite>* node =
213180 allocation_sites.add (tmp);
214- if (node == nullptr ) return false ;
181+ if (node == nullptr ) {
182+ failed_oom = true ;
183+ return false ;
184+ }
215185 site = node->data ();
216186 }
217- site->reserve_memory (rgn->size ());
218- site->commit_memory (VirtualMemoryTracker::Instance::committed_size (rgn));
187+ site->reserve_memory (rgn.size ());
188+
189+ site->commit_memory (_vma_allocations->committed_size (rgn));
190+ return true ;
191+ });
192+
193+ if (failed_oom) {
194+ return false ;
219195 }
220196
221197 _virtual_memory_sites.move (&allocation_sites);
0 commit comments