1 /* 2 * Copyright (c) 2013, 2015, 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 25 #include "precompiled.hpp" 26 #include "classfile/metadataOnStackMark.hpp" 27 #include "code/codeCache.hpp" 28 #include "compiler/compileBroker.hpp" 29 #include "oops/metadata.hpp" 30 #include "prims/jvmtiImpl.hpp" 31 #include "runtime/synchronizer.hpp" 32 #include "runtime/thread.hpp" 33 #include "services/threadService.hpp" 34 #include "utilities/chunkedList.hpp" 35 36 MetadataOnStackBuffer* MetadataOnStackMark::_used_buffers = NULL; 37 MetadataOnStackBuffer* MetadataOnStackMark::_free_buffers = NULL; 38 39 MetadataOnStackBuffer* MetadataOnStackMark::_current_buffer = NULL; 40 NOT_PRODUCT(bool MetadataOnStackMark::_is_active = false;) 41 42 // Walk metadata on the stack and mark it so that redefinition doesn't delete 43 // it. Class unloading only deletes in-error class files, methods created by 44 // the relocator and dummy constant pools. None of these appear anywhere except 45 // in metadata Handles. 46 MetadataOnStackMark::MetadataOnStackMark(bool redefinition_walk) { 47 assert(SafepointSynchronize::is_at_safepoint(), "sanity check"); 48 assert(_used_buffers == NULL, "sanity check"); 49 assert(!_is_active, "MetadataOnStackMarks do not nest"); 50 NOT_PRODUCT(_is_active = true;) 51 52 Threads::metadata_handles_do(Metadata::mark_on_stack); 53 54 if (redefinition_walk) { 55 Threads::metadata_do(Metadata::mark_on_stack); 56 CodeCache::alive_nmethods_do(nmethod::mark_on_stack); 57 CompileBroker::mark_on_stack(); 58 JvmtiCurrentBreakpoints::metadata_do(Metadata::mark_on_stack); 59 ThreadService::metadata_do(Metadata::mark_on_stack); 60 } 61 } 62 63 MetadataOnStackMark::~MetadataOnStackMark() { 64 assert(SafepointSynchronize::is_at_safepoint(), "sanity check"); 65 // Unmark everything that was marked. Can't do the same walk because 66 // redefine classes messes up the code cache so the set of methods 67 // might not be the same. 68 retire_current_buffer(); 69 70 MetadataOnStackBuffer* buffer = _used_buffers; 71 while (buffer != NULL) { 72 // Clear on stack state for all metadata. 73 size_t size = buffer->size(); 74 for (size_t i = 0; i < size; i++) { 75 Metadata* md = buffer->at(i); 76 md->set_on_stack(false); 77 } 78 79 MetadataOnStackBuffer* next = buffer->next_used(); 80 81 // Move the buffer to the free list. 82 buffer->clear(); 83 buffer->set_next_used(NULL); 84 buffer->set_next_free(_free_buffers); 85 _free_buffers = buffer; 86 87 // Step to next used buffer. 88 buffer = next; 89 } 90 91 _used_buffers = NULL; 92 93 NOT_PRODUCT(_is_active = false;) 94 } 95 96 void MetadataOnStackMark::retire_buffer(MetadataOnStackBuffer* buffer) { 97 if (buffer == NULL) { 98 return; 99 } 100 buffer->set_next_used(_used_buffers); 101 _used_buffers = buffer; 102 } 103 104 // Current buffer is full or we're ready to walk them, add it to the used list. 105 void MetadataOnStackMark::retire_current_buffer() { 106 retire_buffer(_current_buffer); 107 _current_buffer = NULL; 108 } 109 110 // Get buffer off free list. 111 MetadataOnStackBuffer* MetadataOnStackMark::allocate_buffer() { 112 MetadataOnStackBuffer* allocated = _free_buffers; 113 114 if (allocated != NULL) { 115 _free_buffers = allocated->next_free(); 116 } 117 118 if (allocated == NULL) { 119 allocated = new MetadataOnStackBuffer(); 120 } 121 122 assert(!allocated->is_full(), "Should not be full: " PTR_FORMAT, p2i(allocated)); 123 124 return allocated; 125 } 126 127 // Record which objects are marked so we can unmark the same objects. 128 void MetadataOnStackMark::record(Metadata* m) { 129 assert(_is_active, "metadata on stack marking is active"); 130 131 MetadataOnStackBuffer* buffer = _current_buffer; 132 133 if (buffer != NULL && buffer->is_full()) { 134 retire_buffer(buffer); 135 buffer = NULL; 136 } 137 138 if (buffer == NULL) { 139 buffer = allocate_buffer(); 140 _current_buffer = buffer; 141 } 142 143 buffer->push(m); 144 }