Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/hotspot/share/services/mallocTracker.cpp
41145 views
1
/*
2
* Copyright (c) 2014, 2021, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*
23
*/
24
#include "precompiled.hpp"
25
26
#include "services/mallocSiteTable.hpp"
27
#include "services/mallocTracker.hpp"
28
#include "services/mallocTracker.inline.hpp"
29
#include "services/memTracker.hpp"
30
31
size_t MallocMemorySummary::_snapshot[CALC_OBJ_SIZE_IN_TYPE(MallocMemorySnapshot, size_t)];
32
33
#ifdef ASSERT
34
void MemoryCounter::update_peak_count(size_t count) {
35
size_t peak_cnt = peak_count();
36
while (peak_cnt < count) {
37
size_t old_cnt = Atomic::cmpxchg(&_peak_count, peak_cnt, count, memory_order_relaxed);
38
if (old_cnt != peak_cnt) {
39
peak_cnt = old_cnt;
40
}
41
}
42
}
43
44
void MemoryCounter::update_peak_size(size_t sz) {
45
size_t peak_sz = peak_size();
46
while (peak_sz < sz) {
47
size_t old_sz = Atomic::cmpxchg(&_peak_size, peak_sz, sz, memory_order_relaxed);
48
if (old_sz != peak_sz) {
49
peak_sz = old_sz;
50
}
51
}
52
}
53
54
size_t MemoryCounter::peak_count() const {
55
return Atomic::load(&_peak_count);
56
}
57
58
size_t MemoryCounter::peak_size() const {
59
return Atomic::load(&_peak_size);
60
}
61
#endif
62
63
// Total malloc'd memory amount
64
size_t MallocMemorySnapshot::total() const {
65
size_t amount = 0;
66
for (int index = 0; index < mt_number_of_types; index ++) {
67
amount += _malloc[index].malloc_size();
68
}
69
amount += _tracking_header.size() + total_arena();
70
return amount;
71
}
72
73
// Total malloc'd memory used by arenas
74
size_t MallocMemorySnapshot::total_arena() const {
75
size_t amount = 0;
76
for (int index = 0; index < mt_number_of_types; index ++) {
77
amount += _malloc[index].arena_size();
78
}
79
return amount;
80
}
81
82
// Make adjustment by subtracting chunks used by arenas
83
// from total chunks to get total free chunk size
84
void MallocMemorySnapshot::make_adjustment() {
85
size_t arena_size = total_arena();
86
int chunk_idx = NMTUtil::flag_to_index(mtChunk);
87
_malloc[chunk_idx].record_free(arena_size);
88
}
89
90
91
void MallocMemorySummary::initialize() {
92
assert(sizeof(_snapshot) >= sizeof(MallocMemorySnapshot), "Sanity Check");
93
// Uses placement new operator to initialize static area.
94
::new ((void*)_snapshot)MallocMemorySnapshot();
95
}
96
97
void MallocHeader::release() const {
98
// Tracking already shutdown, no housekeeping is needed anymore
99
if (MemTracker::tracking_level() <= NMT_minimal) return;
100
101
MallocMemorySummary::record_free(size(), flags());
102
MallocMemorySummary::record_free_malloc_header(sizeof(MallocHeader));
103
if (MemTracker::tracking_level() == NMT_detail) {
104
MallocSiteTable::deallocation_at(size(), _bucket_idx, _pos_idx);
105
}
106
}
107
108
bool MallocHeader::record_malloc_site(const NativeCallStack& stack, size_t size,
109
size_t* bucket_idx, size_t* pos_idx, MEMFLAGS flags) const {
110
bool ret = MallocSiteTable::allocation_at(stack, size, bucket_idx, pos_idx, flags);
111
112
// Something went wrong, could be OOM or overflow malloc site table.
113
// We want to keep tracking data under OOM circumstance, so transition to
114
// summary tracking.
115
if (!ret) {
116
MemTracker::transition_to(NMT_summary);
117
}
118
return ret;
119
}
120
121
bool MallocHeader::get_stack(NativeCallStack& stack) const {
122
return MallocSiteTable::access_stack(stack, _bucket_idx, _pos_idx);
123
}
124
125
bool MallocTracker::initialize(NMT_TrackingLevel level) {
126
if (level >= NMT_summary) {
127
MallocMemorySummary::initialize();
128
}
129
130
if (level == NMT_detail) {
131
return MallocSiteTable::initialize();
132
}
133
return true;
134
}
135
136
bool MallocTracker::transition(NMT_TrackingLevel from, NMT_TrackingLevel to) {
137
assert(from != NMT_off, "Can not transition from off state");
138
assert(to != NMT_off, "Can not transition to off state");
139
assert (from != NMT_minimal, "cannot transition from minimal state");
140
141
if (from == NMT_detail) {
142
assert(to == NMT_minimal || to == NMT_summary, "Just check");
143
MallocSiteTable::shutdown();
144
}
145
return true;
146
}
147
148
// Record a malloc memory allocation
149
void* MallocTracker::record_malloc(void* malloc_base, size_t size, MEMFLAGS flags,
150
const NativeCallStack& stack, NMT_TrackingLevel level) {
151
assert(level != NMT_off, "precondition");
152
void* memblock; // the address for user data
153
MallocHeader* header = NULL;
154
155
if (malloc_base == NULL) {
156
return NULL;
157
}
158
159
// Uses placement global new operator to initialize malloc header
160
161
header = ::new (malloc_base)MallocHeader(size, flags, stack, level);
162
memblock = (void*)((char*)malloc_base + sizeof(MallocHeader));
163
164
// The alignment check: 8 bytes alignment for 32 bit systems.
165
// 16 bytes alignment for 64-bit systems.
166
assert(((size_t)memblock & (sizeof(size_t) * 2 - 1)) == 0, "Alignment check");
167
168
#ifdef ASSERT
169
if (level > NMT_minimal) {
170
// Read back
171
assert(get_size(memblock) == size, "Wrong size");
172
assert(get_flags(memblock) == flags, "Wrong flags");
173
}
174
#endif
175
176
return memblock;
177
}
178
179
void* MallocTracker::record_free(void* memblock) {
180
assert(MemTracker::tracking_level() != NMT_off && memblock != NULL, "precondition");
181
MallocHeader* header = malloc_header(memblock);
182
header->release();
183
return (void*)header;
184
}
185
186