Thu, 11 Apr 2013 09:08:15 -0700
8011972: Field can be erroneously marked as contended when @Contended annotation isn't present
Reviewed-by: kvn, kmo, shade
1 /*
2 * Copyright (c) 2012, 2013, 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 */
25 #ifndef SHARE_VM_CLASSFILE_CLASSLOADERDATA_HPP
26 #define SHARE_VM_CLASSFILE_CLASSLOADERDATA_HPP
28 #include "memory/allocation.hpp"
29 #include "memory/memRegion.hpp"
30 #include "memory/metaspace.hpp"
31 #include "memory/metaspaceCounters.hpp"
32 #include "runtime/mutex.hpp"
33 #include "utilities/growableArray.hpp"
35 //
36 // A class loader represents a linkset. Conceptually, a linkset identifies
37 // the complete transitive closure of resolved links that a dynamic linker can
38 // produce.
39 //
40 // A ClassLoaderData also encapsulates the allocation space, called a metaspace,
41 // used by the dynamic linker to allocate the runtime representation of all
42 // the types it defines.
43 //
44 // ClassLoaderData are stored in the runtime representation of classes and the
45 // system dictionary, are roots of garbage collection, and provides iterators
46 // for root tracing and other GC operations.
48 class ClassLoaderData;
49 class JNIMethodBlock;
50 class JNIHandleBlock;
51 class Metadebug;
52 // GC root for walking class loader data created
54 class ClassLoaderDataGraph : public AllStatic {
55 friend class ClassLoaderData;
56 friend class ClassLoaderDataGraphMetaspaceIterator;
57 friend class VMStructs;
58 private:
59 // All CLDs (except the null CLD) can be reached by walking _head->_next->...
60 static ClassLoaderData* _head;
61 static ClassLoaderData* _unloading;
62 // CMS support.
63 static ClassLoaderData* _saved_head;
65 static ClassLoaderData* add(ClassLoaderData** loader_data_addr, Handle class_loader, TRAPS);
66 public:
67 static ClassLoaderData* find_or_create(Handle class_loader, TRAPS);
68 static void purge();
69 static void clear_claimed_marks();
70 static void oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim);
71 static void always_strong_oops_do(OopClosure* blk, KlassClosure* klass_closure, bool must_claim);
72 static void keep_alive_oops_do(OopClosure* blk, KlassClosure* klass_closure, bool must_claim);
73 static void classes_do(KlassClosure* klass_closure);
74 static bool do_unloading(BoolObjectClosure* is_alive);
76 // CMS support.
77 static void remember_new_clds(bool remember) { _saved_head = (remember ? _head : NULL); }
78 static GrowableArray<ClassLoaderData*>* new_clds();
80 static void dump_on(outputStream * const out) PRODUCT_RETURN;
81 static void dump() { dump_on(tty); }
82 static void verify();
84 #ifndef PRODUCT
85 // expensive test for pointer in metaspace for debugging
86 static bool contains(address x);
87 static bool contains_loader_data(ClassLoaderData* loader_data);
88 #endif
89 };
91 // ClassLoaderData class
93 class ClassLoaderData : public CHeapObj<mtClass> {
94 friend class VMStructs;
95 private:
96 friend class ClassLoaderDataGraph;
97 friend class ClassLoaderDataGraphMetaspaceIterator;
98 friend class MetaDataFactory;
99 friend class Method;
101 static ClassLoaderData * _the_null_class_loader_data;
103 oop _class_loader; // oop used to uniquely identify a class loader
104 // class loader or a canonical class path
105 oop _dependencies; // oop to hold dependencies from this class loader
106 // data to others.
107 Metaspace * _metaspace; // Meta-space where meta-data defined by the
108 // classes in the class loader are allocated.
109 Mutex* _metaspace_lock; // Locks the metaspace for allocations and setup.
110 bool _unloading; // true if this class loader goes away
111 bool _keep_alive; // if this CLD can be unloaded for anonymous loaders
112 bool _is_anonymous; // if this CLD is for an anonymous class
113 volatile int _claimed; // true if claimed, for example during GC traces.
114 // To avoid applying oop closure more than once.
115 // Has to be an int because we cas it.
116 Klass* _klasses; // The classes defined by the class loader.
118 JNIHandleBlock* _handles; // Handles to constant pool arrays
120 // These method IDs are created for the class loader and set to NULL when the
121 // class loader is unloaded. They are rarely freed, only for redefine classes
122 // and if they lose a data race in InstanceKlass.
123 JNIMethodBlock* _jmethod_ids;
125 // Metadata to be deallocated when it's safe at class unloading, when
126 // this class loader isn't unloaded itself.
127 GrowableArray<Metadata*>* _deallocate_list;
129 // Support for walking class loader data objects
130 ClassLoaderData* _next; /// Next loader_datas created
132 // ReadOnly and ReadWrite metaspaces (static because only on the null
133 // class loader for now).
134 static Metaspace* _ro_metaspace;
135 static Metaspace* _rw_metaspace;
137 void add_dependency(Handle dependency, TRAPS);
138 void locked_add_dependency(objArrayHandle last, objArrayHandle new_dependency);
140 void set_next(ClassLoaderData* next) { _next = next; }
141 ClassLoaderData* next() const { return _next; }
143 ClassLoaderData(Handle h_class_loader, bool is_anonymous);
144 ~ClassLoaderData();
146 void set_metaspace(Metaspace* m) { _metaspace = m; }
148 JNIHandleBlock* handles() const;
149 void set_handles(JNIHandleBlock* handles);
151 Mutex* metaspace_lock() const { return _metaspace_lock; }
153 // GC interface.
154 void clear_claimed() { _claimed = 0; }
155 bool claimed() const { return _claimed == 1; }
156 bool claim();
158 void unload();
159 bool keep_alive() const { return _keep_alive; }
160 bool is_alive(BoolObjectClosure* is_alive_closure) const;
162 void classes_do(void f(InstanceKlass*));
164 // Deallocate free list during class unloading.
165 void free_deallocate_list();
167 // Allocate out of this class loader data
168 MetaWord* allocate(size_t size);
170 public:
171 // Accessors
172 Metaspace* metaspace_or_null() const { return _metaspace; }
174 static ClassLoaderData* the_null_class_loader_data() {
175 return _the_null_class_loader_data;
176 }
178 bool is_anonymous() const { return _is_anonymous; }
180 static void init_null_class_loader_data() {
181 assert(_the_null_class_loader_data == NULL, "cannot initialize twice");
182 assert(ClassLoaderDataGraph::_head == NULL, "cannot initialize twice");
183 _the_null_class_loader_data = new ClassLoaderData((oop)NULL, false);
184 ClassLoaderDataGraph::_head = _the_null_class_loader_data;
185 assert(_the_null_class_loader_data->is_the_null_class_loader_data(), "Must be");
186 if (DumpSharedSpaces) {
187 _the_null_class_loader_data->initialize_shared_metaspaces();
188 }
189 }
191 bool is_the_null_class_loader_data() const {
192 return this == _the_null_class_loader_data;
193 }
194 bool is_ext_class_loader_data() const;
196 // The Metaspace is created lazily so may be NULL. This
197 // method will allocate a Metaspace if needed.
198 Metaspace* metaspace_non_null();
200 oop class_loader() const { return _class_loader; }
202 // Returns true if this class loader data is for a loader going away.
203 bool is_unloading() const {
204 assert(!(is_the_null_class_loader_data() && _unloading), "The null class loader can never be unloaded");
205 return _unloading;
206 }
207 // Anonymous class loader data doesn't have anything to keep them from
208 // being unloaded during parsing the anonymous class.
209 void set_keep_alive(bool value) { _keep_alive = value; }
211 unsigned int identity_hash() {
212 return _class_loader == NULL ? 0 : _class_loader->identity_hash();
213 }
215 // Used when tracing from klasses.
216 void oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim);
218 void classes_do(KlassClosure* klass_closure);
220 JNIMethodBlock* jmethod_ids() const { return _jmethod_ids; }
221 void set_jmethod_ids(JNIMethodBlock* new_block) { _jmethod_ids = new_block; }
223 void print_value() { print_value_on(tty); }
224 void print_value_on(outputStream* out) const;
225 void dump(outputStream * const out) PRODUCT_RETURN;
226 void verify();
227 const char* loader_name();
229 jobject add_handle(Handle h);
230 void add_class(Klass* k);
231 void remove_class(Klass* k);
232 void record_dependency(Klass* to, TRAPS);
233 void init_dependencies(TRAPS);
235 void add_to_deallocate_list(Metadata* m);
237 static ClassLoaderData* class_loader_data(oop loader);
238 static ClassLoaderData* class_loader_data_or_null(oop loader);
239 static ClassLoaderData* anonymous_class_loader_data(oop loader, TRAPS);
240 static void print_loader(ClassLoaderData *loader_data, outputStream *out);
242 // CDS support
243 Metaspace* ro_metaspace();
244 Metaspace* rw_metaspace();
245 void initialize_shared_metaspaces();
246 };
248 class ClassLoaderDataGraphMetaspaceIterator : public StackObj {
249 ClassLoaderData* _data;
250 public:
251 ClassLoaderDataGraphMetaspaceIterator();
252 ~ClassLoaderDataGraphMetaspaceIterator();
253 bool repeat() { return _data != NULL; }
254 Metaspace* get_next() {
255 assert(_data != NULL, "Should not be NULL in call to the iterator");
256 Metaspace* result = _data->metaspace_or_null();
257 _data = _data->next();
258 // This result might be NULL for class loaders without metaspace
259 // yet. It would be nice to return only non-null results but
260 // there is no guarantee that there will be a non-null result
261 // down the list so the caller is going to have to check.
262 return result;
263 }
264 };
265 #endif // SHARE_VM_CLASSFILE_CLASSLOADERDATA_HPP