1/*
2 * Copyright (c) 2014, 2017, 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 "runtime/atomic.hpp"
27#include "services/mallocSiteTable.hpp"
28#include "services/mallocTracker.hpp"
29#include "services/mallocTracker.inline.hpp"
30#include "services/memTracker.hpp"
31
32size_t MallocMemorySummary::_snapshot[CALC_OBJ_SIZE_IN_TYPE(MallocMemorySnapshot, size_t)];
33
34// Total malloc'd memory amount
35size_t MallocMemorySnapshot::total() const {
36 size_t amount = 0;
37 for (int index = 0; index < mt_number_of_types; index ++) {
38 amount += _malloc[index].malloc_size();
39 }
40 amount += _tracking_header.size() + total_arena();
41 return amount;
42}
43
44// Total malloc'd memory used by arenas
45size_t MallocMemorySnapshot::total_arena() const {
46 size_t amount = 0;
47 for (int index = 0; index < mt_number_of_types; index ++) {
48 amount += _malloc[index].arena_size();
49 }
50 return amount;
51}
52
53// Make adjustment by subtracting chunks used by arenas
54// from total chunks to get total free chunk size
55void MallocMemorySnapshot::make_adjustment() {
56 size_t arena_size = total_arena();
57 int chunk_idx = NMTUtil::flag_to_index(mtChunk);
58 _malloc[chunk_idx].record_free(arena_size);
59}
60
61
62void MallocMemorySummary::initialize() {
63 assert(sizeof(_snapshot) >= sizeof(MallocMemorySnapshot), "Sanity Check");
64 // Uses placement new operator to initialize static area.
65 ::new ((void*)_snapshot)MallocMemorySnapshot();
66}
67
68void MallocHeader::release() const {
69 // Tracking already shutdown, no housekeeping is needed anymore
70 if (MemTracker::tracking_level() <= NMT_minimal) return;
71
72 MallocMemorySummary::record_free(size(), flags());
73 MallocMemorySummary::record_free_malloc_header(sizeof(MallocHeader));
74 if (MemTracker::tracking_level() == NMT_detail) {
75 MallocSiteTable::deallocation_at(size(), _bucket_idx, _pos_idx);
76 }
77}
78
79bool MallocHeader::record_malloc_site(const NativeCallStack& stack, size_t size,
80 size_t* bucket_idx, size_t* pos_idx, MEMFLAGS flags) const {
81 bool ret = MallocSiteTable::allocation_at(stack, size, bucket_idx, pos_idx, flags);
82
83 // Something went wrong, could be OOM or overflow malloc site table.
84 // We want to keep tracking data under OOM circumstance, so transition to
85 // summary tracking.
86 if (!ret) {
87 MemTracker::transition_to(NMT_summary);
88 }
89 return ret;
90}
91
92bool MallocHeader::get_stack(NativeCallStack& stack) const {
93 return MallocSiteTable::access_stack(stack, _bucket_idx, _pos_idx);
94}
95
96bool MallocTracker::initialize(NMT_TrackingLevel level) {
97 if (level >= NMT_summary) {
98 MallocMemorySummary::initialize();
99 }
100
101 if (level == NMT_detail) {
102 return MallocSiteTable::initialize();
103 }
104 return true;
105}
106
107bool MallocTracker::transition(NMT_TrackingLevel from, NMT_TrackingLevel to) {
108 assert(from != NMT_off, "Can not transition from off state");
109 assert(to != NMT_off, "Can not transition to off state");
110 assert (from != NMT_minimal, "cannot transition from minimal state");
111
112 if (from == NMT_detail) {
113 assert(to == NMT_minimal || to == NMT_summary, "Just check");
114 MallocSiteTable::shutdown();
115 }
116 return true;
117}
118
119// Record a malloc memory allocation
120void* MallocTracker::record_malloc(void* malloc_base, size_t size, MEMFLAGS flags,
121 const NativeCallStack& stack, NMT_TrackingLevel level) {
122 void* memblock; // the address for user data
123 MallocHeader* header = NULL;
124
125 if (malloc_base == NULL) {
126 return NULL;
127 }
128
129 // Uses placement global new operator to initialize malloc header
130
131 if (level == NMT_off) {
132 return malloc_base;
133 }
134
135 header = ::new (malloc_base)MallocHeader(size, flags, stack, level);
136 memblock = (void*)((char*)malloc_base + sizeof(MallocHeader));
137
138 // The alignment check: 8 bytes alignment for 32 bit systems.
139 // 16 bytes alignment for 64-bit systems.
140 assert(((size_t)memblock & (sizeof(size_t) * 2 - 1)) == 0, "Alignment check");
141
142#ifdef ASSERT
143 if (level > NMT_minimal) {
144 // Read back
145 assert(get_size(memblock) == size, "Wrong size");
146 assert(get_flags(memblock) == flags, "Wrong flags");
147 }
148#endif
149
150 return memblock;
151}
152
153void* MallocTracker::record_free(void* memblock) {
154 // Never turned on
155 if (MemTracker::tracking_level() == NMT_off ||
156 memblock == NULL) {
157 return memblock;
158 }
159 MallocHeader* header = malloc_header(memblock);
160 header->release();
161
162 return (void*)header;
163}
164
165
166