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