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