1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/services/mallocTracker.cpp Wed Aug 27 08:19:12 2014 -0400 1.3 @@ -0,0 +1,200 @@ 1.4 +/* 1.5 + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + * 1.26 + */ 1.27 +#include "precompiled.hpp" 1.28 + 1.29 +#include "runtime/atomic.hpp" 1.30 +#include "runtime/atomic.inline.hpp" 1.31 +#include "services/mallocSiteTable.hpp" 1.32 +#include "services/mallocTracker.hpp" 1.33 +#include "services/mallocTracker.inline.hpp" 1.34 +#include "services/memTracker.hpp" 1.35 + 1.36 +size_t MallocMemorySummary::_snapshot[CALC_OBJ_SIZE_IN_TYPE(MallocMemorySnapshot, size_t)]; 1.37 + 1.38 +// Total malloc'd memory amount 1.39 +size_t MallocMemorySnapshot::total() const { 1.40 + size_t amount = 0; 1.41 + for (int index = 0; index < mt_number_of_types; index ++) { 1.42 + amount += _malloc[index].malloc_size(); 1.43 + } 1.44 + amount += _tracking_header.size() + total_arena(); 1.45 + return amount; 1.46 +} 1.47 + 1.48 +// Total malloc'd memory used by arenas 1.49 +size_t MallocMemorySnapshot::total_arena() const { 1.50 + size_t amount = 0; 1.51 + for (int index = 0; index < mt_number_of_types; index ++) { 1.52 + amount += _malloc[index].arena_size(); 1.53 + } 1.54 + return amount; 1.55 +} 1.56 + 1.57 + 1.58 +void MallocMemorySnapshot::reset() { 1.59 + _tracking_header.reset(); 1.60 + for (int index = 0; index < mt_number_of_types; index ++) { 1.61 + _malloc[index].reset(); 1.62 + } 1.63 +} 1.64 + 1.65 +// Make adjustment by subtracting chunks used by arenas 1.66 +// from total chunks to get total free chunck size 1.67 +void MallocMemorySnapshot::make_adjustment() { 1.68 + size_t arena_size = total_arena(); 1.69 + int chunk_idx = NMTUtil::flag_to_index(mtChunk); 1.70 + _malloc[chunk_idx].record_free(arena_size); 1.71 +} 1.72 + 1.73 + 1.74 +void MallocMemorySummary::initialize() { 1.75 + assert(sizeof(_snapshot) >= sizeof(MallocMemorySnapshot), "Sanity Check"); 1.76 + // Uses placement new operator to initialize static area. 1.77 + ::new ((void*)_snapshot)MallocMemorySnapshot(); 1.78 +} 1.79 + 1.80 +void MallocHeader::release() const { 1.81 + // Tracking already shutdown, no housekeeping is needed anymore 1.82 + if (MemTracker::tracking_level() <= NMT_minimal) return; 1.83 + 1.84 + MallocMemorySummary::record_free(size(), flags()); 1.85 + MallocMemorySummary::record_free_malloc_header(sizeof(MallocHeader)); 1.86 + if (tracking_level() == NMT_detail) { 1.87 + MallocSiteTable::deallocation_at(size(), _bucket_idx, _pos_idx); 1.88 + } 1.89 +} 1.90 + 1.91 +bool MallocHeader::record_malloc_site(const NativeCallStack& stack, size_t size, 1.92 + size_t* bucket_idx, size_t* pos_idx) const { 1.93 + bool ret = MallocSiteTable::allocation_at(stack, size, bucket_idx, pos_idx); 1.94 + 1.95 + // Something went wrong, could be OOM or overflow malloc site table. 1.96 + // We want to keep tracking data under OOM circumstance, so transition to 1.97 + // summary tracking. 1.98 + if (!ret) { 1.99 + MemTracker::transition_to(NMT_summary); 1.100 + } 1.101 + return ret; 1.102 +} 1.103 + 1.104 +bool MallocHeader::get_stack(NativeCallStack& stack) const { 1.105 + return MallocSiteTable::access_stack(stack, _bucket_idx, _pos_idx); 1.106 +} 1.107 + 1.108 +bool MallocTracker::initialize(NMT_TrackingLevel level) { 1.109 + if (level >= NMT_summary) { 1.110 + MallocMemorySummary::initialize(); 1.111 + } 1.112 + 1.113 + if (level == NMT_detail) { 1.114 + return MallocSiteTable::initialize(); 1.115 + } 1.116 + return true; 1.117 +} 1.118 + 1.119 +bool MallocTracker::transition(NMT_TrackingLevel from, NMT_TrackingLevel to) { 1.120 + assert(from != NMT_off, "Can not transition from off state"); 1.121 + assert(to != NMT_off, "Can not transition to off state"); 1.122 + if (from == NMT_minimal) { 1.123 + MallocMemorySummary::reset(); 1.124 + } 1.125 + 1.126 + if (to == NMT_detail) { 1.127 + assert(from == NMT_minimal || from == NMT_summary, "Just check"); 1.128 + return MallocSiteTable::initialize(); 1.129 + } else if (from == NMT_detail) { 1.130 + assert(to == NMT_minimal || to == NMT_summary, "Just check"); 1.131 + MallocSiteTable::shutdown(); 1.132 + } 1.133 + return true; 1.134 +} 1.135 + 1.136 +// Record a malloc memory allocation 1.137 +void* MallocTracker::record_malloc(void* malloc_base, size_t size, MEMFLAGS flags, 1.138 + const NativeCallStack& stack, NMT_TrackingLevel level) { 1.139 + void* memblock; // the address for user data 1.140 + MallocHeader* header = NULL; 1.141 + 1.142 + if (malloc_base == NULL) { 1.143 + return NULL; 1.144 + } 1.145 + 1.146 + // Check malloc size, size has to <= MAX_MALLOC_SIZE. This is only possible on 32-bit 1.147 + // systems, when malloc size >= 1GB, but is is safe to assume it won't happen. 1.148 + if (size > MAX_MALLOC_SIZE) { 1.149 + fatal("Should not use malloc for big memory block, use virtual memory instead"); 1.150 + } 1.151 + // Uses placement global new operator to initialize malloc header 1.152 + switch(level) { 1.153 + case NMT_off: 1.154 + return malloc_base; 1.155 + case NMT_minimal: { 1.156 + MallocHeader* hdr = ::new (malloc_base) MallocHeader(); 1.157 + break; 1.158 + } 1.159 + case NMT_summary: { 1.160 + header = ::new (malloc_base) MallocHeader(size, flags); 1.161 + break; 1.162 + } 1.163 + case NMT_detail: { 1.164 + header = ::new (malloc_base) MallocHeader(size, flags, stack); 1.165 + break; 1.166 + } 1.167 + default: 1.168 + ShouldNotReachHere(); 1.169 + } 1.170 + memblock = (void*)((char*)malloc_base + sizeof(MallocHeader)); 1.171 + 1.172 + // The alignment check: 8 bytes alignment for 32 bit systems. 1.173 + // 16 bytes alignment for 64-bit systems. 1.174 + assert(((size_t)memblock & (sizeof(size_t) * 2 - 1)) == 0, "Alignment check"); 1.175 + 1.176 + // Sanity check 1.177 + assert(get_memory_tracking_level(memblock) == level, 1.178 + "Wrong tracking level"); 1.179 + 1.180 +#ifdef ASSERT 1.181 + if (level > NMT_minimal) { 1.182 + // Read back 1.183 + assert(get_size(memblock) == size, "Wrong size"); 1.184 + assert(get_flags(memblock) == flags, "Wrong flags"); 1.185 + } 1.186 +#endif 1.187 + 1.188 + return memblock; 1.189 +} 1.190 + 1.191 +void* MallocTracker::record_free(void* memblock) { 1.192 + // Never turned on 1.193 + if (MemTracker::tracking_level() == NMT_off || 1.194 + memblock == NULL) { 1.195 + return memblock; 1.196 + } 1.197 + MallocHeader* header = malloc_header(memblock); 1.198 + header->release(); 1.199 + 1.200 + return (void*)header; 1.201 +} 1.202 + 1.203 +