src/share/vm/memory/iterator.hpp

Mon, 07 Jul 2014 10:12:40 +0200

author
stefank
date
Mon, 07 Jul 2014 10:12:40 +0200
changeset 6992
2c6ef90f030a
parent 6982
4c1b88a53c74
child 7535
7ae4e26cb1e0
child 9661
379a59bf685d
permissions
-rw-r--r--

8049421: G1 Class Unloading after completing a concurrent mark cycle
Reviewed-by: tschatzl, ehelin, brutisso, coleenp, roland, iveresov
Contributed-by: stefan.karlsson@oracle.com, mikael.gerdin@oracle.com

duke@435 1 /*
mikael@6198 2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
duke@435 22 *
duke@435 23 */
duke@435 24
stefank@2314 25 #ifndef SHARE_VM_MEMORY_ITERATOR_HPP
stefank@2314 26 #define SHARE_VM_MEMORY_ITERATOR_HPP
stefank@2314 27
stefank@2314 28 #include "memory/allocation.hpp"
stefank@2314 29 #include "memory/memRegion.hpp"
stefank@2314 30 #include "runtime/prefetch.hpp"
stefank@2314 31 #include "utilities/top.hpp"
stefank@2314 32
duke@435 33 // The following classes are C++ `closures` for iterating over objects, roots and spaces
duke@435 34
jrose@1424 35 class CodeBlob;
jrose@1429 36 class nmethod;
duke@435 37 class ReferenceProcessor;
ysr@1376 38 class DataLayout;
coleenp@4037 39 class KlassClosure;
coleenp@4037 40 class ClassLoaderData;
duke@435 41
ysr@777 42 // Closure provides abortability.
ysr@777 43
ysr@777 44 class Closure : public StackObj {
ysr@777 45 protected:
ysr@777 46 bool _abort;
ysr@777 47 void set_abort() { _abort = true; }
ysr@777 48 public:
ysr@777 49 Closure() : _abort(false) {}
ysr@777 50 // A subtype can use this mechanism to indicate to some iterator mapping
ysr@777 51 // functions that the iteration should cease.
ysr@777 52 bool abort() { return _abort; }
ysr@777 53 void clear_abort() { _abort = false; }
ysr@777 54 };
ysr@777 55
coleenp@4037 56 // OopClosure is used for iterating through references to Java objects.
duke@435 57
ysr@777 58 class OopClosure : public Closure {
duke@435 59 public:
duke@435 60 virtual void do_oop(oop* o) = 0;
duke@435 61 virtual void do_oop_v(oop* o) { do_oop(o); }
coleenp@548 62 virtual void do_oop(narrowOop* o) = 0;
coleenp@548 63 virtual void do_oop_v(narrowOop* o) { do_oop(o); }
coleenp@4037 64 };
duke@435 65
coleenp@4037 66 // ExtendedOopClosure adds extra code to be run during oop iterations.
coleenp@4037 67 // This is needed by the GC and is extracted to a separate type to not
coleenp@4037 68 // pollute the OopClosure interface.
coleenp@4037 69 class ExtendedOopClosure : public OopClosure {
coleenp@4037 70 public:
coleenp@4037 71 ReferenceProcessor* _ref_processor;
coleenp@4037 72 ExtendedOopClosure(ReferenceProcessor* rp) : _ref_processor(rp) { }
coleenp@4037 73 ExtendedOopClosure() : OopClosure(), _ref_processor(NULL) { }
jmasa@1370 74
coleenp@4037 75 // If the do_metadata functions return "true",
coleenp@4037 76 // we invoke the following when running oop_iterate():
coleenp@4037 77 //
coleenp@4037 78 // 1) do_klass on the header klass pointer.
coleenp@4037 79 // 2) do_klass on the klass pointer in the mirrors.
coleenp@4037 80 // 3) do_class_loader_data on the class loader data in class loaders.
coleenp@4037 81 //
coleenp@4037 82 // The virtual (without suffix) and the non-virtual (with _nv suffix) need
coleenp@4037 83 // to be updated together, or else the devirtualization will break.
coleenp@4037 84 //
coleenp@4037 85 // Providing default implementations of the _nv functions unfortunately
coleenp@4037 86 // removes the compile-time safeness, but reduces the clutter for the
stefank@6992 87 // ExtendedOopClosures that don't need to walk the metadata.
stefank@6992 88 // Currently, only CMS and G1 need these.
jmasa@1370 89
coleenp@4037 90 virtual bool do_metadata() { return do_metadata_nv(); }
coleenp@4037 91 bool do_metadata_v() { return do_metadata(); }
coleenp@4037 92 bool do_metadata_nv() { return false; }
duke@435 93
coleenp@4037 94 virtual void do_klass(Klass* k) { do_klass_nv(k); }
coleenp@4037 95 void do_klass_v(Klass* k) { do_klass(k); }
coleenp@4037 96 void do_klass_nv(Klass* k) { ShouldNotReachHere(); }
ysr@1376 97
coleenp@4037 98 virtual void do_class_loader_data(ClassLoaderData* cld) { ShouldNotReachHere(); }
duke@435 99
duke@435 100 // Controls how prefetching is done for invocations of this closure.
duke@435 101 Prefetch::style prefetch_style() { // Note that this is non-virtual.
duke@435 102 return Prefetch::do_none;
duke@435 103 }
ysr@777 104
ysr@777 105 // True iff this closure may be safely applied more than once to an oop
ysr@777 106 // location without an intervening "major reset" (like the end of a GC).
ysr@777 107 virtual bool idempotent() { return false; }
ysr@777 108 virtual bool apply_to_weak_ref_discovered_field() { return false; }
coleenp@4037 109 };
jmasa@1370 110
coleenp@4037 111 // Wrapper closure only used to implement oop_iterate_no_header().
coleenp@4037 112 class NoHeaderExtendedOopClosure : public ExtendedOopClosure {
coleenp@4037 113 OopClosure* _wrapped_closure;
coleenp@4037 114 public:
coleenp@4037 115 NoHeaderExtendedOopClosure(OopClosure* cl) : _wrapped_closure(cl) {}
coleenp@4037 116 // Warning: this calls the virtual version do_oop in the the wrapped closure.
coleenp@4037 117 void do_oop_nv(oop* p) { _wrapped_closure->do_oop(p); }
coleenp@4037 118 void do_oop_nv(narrowOop* p) { _wrapped_closure->do_oop(p); }
coleenp@4037 119
coleenp@4037 120 void do_oop(oop* p) { assert(false, "Only the _nv versions should be used");
coleenp@4037 121 _wrapped_closure->do_oop(p); }
coleenp@4037 122 void do_oop(narrowOop* p) { assert(false, "Only the _nv versions should be used");
coleenp@4037 123 _wrapped_closure->do_oop(p);}
coleenp@4037 124 };
coleenp@4037 125
coleenp@4037 126 class KlassClosure : public Closure {
coleenp@4037 127 public:
coleenp@4037 128 virtual void do_klass(Klass* k) = 0;
duke@435 129 };
duke@435 130
stefank@6973 131 class CLDClosure : public Closure {
stefank@6973 132 public:
stefank@6973 133 virtual void do_cld(ClassLoaderData* cld) = 0;
stefank@6973 134 };
stefank@6973 135
stefank@4050 136 class KlassToOopClosure : public KlassClosure {
stefank@6982 137 friend class MetadataAwareOopClosure;
stefank@6982 138 friend class MetadataAwareOopsInGenClosure;
stefank@6982 139
stefank@4050 140 OopClosure* _oop_closure;
stefank@6982 141
stefank@6982 142 // Used when _oop_closure couldn't be set in an initialization list.
stefank@6982 143 void initialize(OopClosure* oop_closure) {
stefank@6982 144 assert(_oop_closure == NULL, "Should only be called once");
stefank@6982 145 _oop_closure = oop_closure;
stefank@6982 146 }
stefank@6982 147
stefank@6992 148 public:
stefank@6982 149 KlassToOopClosure(OopClosure* oop_closure = NULL) : _oop_closure(oop_closure) {}
stefank@6992 150
stefank@4050 151 virtual void do_klass(Klass* k);
stefank@4050 152 };
stefank@4050 153
stefank@6973 154 class CLDToOopClosure : public CLDClosure {
stefank@6992 155 OopClosure* _oop_closure;
stefank@4298 156 KlassToOopClosure _klass_closure;
stefank@6992 157 bool _must_claim_cld;
stefank@4298 158
stefank@4298 159 public:
stefank@4298 160 CLDToOopClosure(OopClosure* oop_closure, bool must_claim_cld = true) :
stefank@4298 161 _oop_closure(oop_closure),
stefank@4298 162 _klass_closure(oop_closure),
stefank@4298 163 _must_claim_cld(must_claim_cld) {}
stefank@4298 164
stefank@4298 165 void do_cld(ClassLoaderData* cld);
stefank@4298 166 };
stefank@4298 167
stefank@6992 168 class CLDToKlassAndOopClosure : public CLDClosure {
stefank@6992 169 friend class SharedHeap;
stefank@6992 170 friend class G1CollectedHeap;
stefank@6992 171 protected:
stefank@6992 172 OopClosure* _oop_closure;
stefank@6992 173 KlassClosure* _klass_closure;
stefank@6992 174 bool _must_claim_cld;
stefank@6992 175 public:
stefank@6992 176 CLDToKlassAndOopClosure(KlassClosure* klass_closure,
stefank@6992 177 OopClosure* oop_closure,
stefank@6992 178 bool must_claim_cld) :
stefank@6992 179 _oop_closure(oop_closure),
stefank@6992 180 _klass_closure(klass_closure),
stefank@6992 181 _must_claim_cld(must_claim_cld) {}
stefank@6992 182 void do_cld(ClassLoaderData* cld);
stefank@6992 183 };
stefank@6992 184
stefank@6982 185 // The base class for all concurrent marking closures,
stefank@6982 186 // that participates in class unloading.
stefank@6982 187 // It's used to proxy through the metadata to the oops defined in them.
stefank@6982 188 class MetadataAwareOopClosure: public ExtendedOopClosure {
stefank@6982 189 KlassToOopClosure _klass_closure;
stefank@6982 190
stefank@6982 191 public:
stefank@6982 192 MetadataAwareOopClosure() : ExtendedOopClosure() {
stefank@6982 193 _klass_closure.initialize(this);
stefank@6982 194 }
stefank@6982 195 MetadataAwareOopClosure(ReferenceProcessor* rp) : ExtendedOopClosure(rp) {
stefank@6982 196 _klass_closure.initialize(this);
stefank@6982 197 }
stefank@6982 198
stefank@6982 199 virtual bool do_metadata() { return do_metadata_nv(); }
stefank@6982 200 inline bool do_metadata_nv() { return true; }
stefank@6982 201
stefank@6982 202 virtual void do_klass(Klass* k);
stefank@6982 203 void do_klass_nv(Klass* k);
stefank@6982 204
stefank@6982 205 virtual void do_class_loader_data(ClassLoaderData* cld);
stefank@6982 206 };
stefank@6982 207
duke@435 208 // ObjectClosure is used for iterating through an object space
duke@435 209
ysr@777 210 class ObjectClosure : public Closure {
duke@435 211 public:
duke@435 212 // Called for each object.
duke@435 213 virtual void do_object(oop obj) = 0;
duke@435 214 };
duke@435 215
duke@435 216
ehelin@5159 217 class BoolObjectClosure : public Closure {
duke@435 218 public:
duke@435 219 virtual bool do_object_b(oop obj) = 0;
duke@435 220 };
duke@435 221
duke@435 222 // Applies an oop closure to all ref fields in objects iterated over in an
duke@435 223 // object iteration.
duke@435 224 class ObjectToOopClosure: public ObjectClosure {
coleenp@4037 225 ExtendedOopClosure* _cl;
duke@435 226 public:
duke@435 227 void do_object(oop obj);
coleenp@4037 228 ObjectToOopClosure(ExtendedOopClosure* cl) : _cl(cl) {}
duke@435 229 };
duke@435 230
duke@435 231 // A version of ObjectClosure that is expected to be robust
duke@435 232 // in the face of possibly uninitialized objects.
duke@435 233 class ObjectClosureCareful : public ObjectClosure {
duke@435 234 public:
duke@435 235 virtual size_t do_object_careful_m(oop p, MemRegion mr) = 0;
duke@435 236 virtual size_t do_object_careful(oop p) = 0;
duke@435 237 };
duke@435 238
duke@435 239 // The following are used in CompactibleFreeListSpace and
duke@435 240 // ConcurrentMarkSweepGeneration.
duke@435 241
duke@435 242 // Blk closure (abstract class)
duke@435 243 class BlkClosure : public StackObj {
duke@435 244 public:
duke@435 245 virtual size_t do_blk(HeapWord* addr) = 0;
duke@435 246 };
duke@435 247
duke@435 248 // A version of BlkClosure that is expected to be robust
duke@435 249 // in the face of possibly uninitialized objects.
duke@435 250 class BlkClosureCareful : public BlkClosure {
duke@435 251 public:
duke@435 252 size_t do_blk(HeapWord* addr) {
duke@435 253 guarantee(false, "call do_blk_careful instead");
duke@435 254 return 0;
duke@435 255 }
duke@435 256 virtual size_t do_blk_careful(HeapWord* addr) = 0;
duke@435 257 };
duke@435 258
duke@435 259 // SpaceClosure is used for iterating over spaces
duke@435 260
duke@435 261 class Space;
duke@435 262 class CompactibleSpace;
duke@435 263
duke@435 264 class SpaceClosure : public StackObj {
duke@435 265 public:
duke@435 266 // Called for each space
duke@435 267 virtual void do_space(Space* s) = 0;
duke@435 268 };
duke@435 269
duke@435 270 class CompactibleSpaceClosure : public StackObj {
duke@435 271 public:
duke@435 272 // Called for each compactible space
duke@435 273 virtual void do_space(CompactibleSpace* s) = 0;
duke@435 274 };
duke@435 275
duke@435 276
jrose@1424 277 // CodeBlobClosure is used for iterating through code blobs
jrose@1424 278 // in the code cache or on thread stacks
jrose@1424 279
jrose@1424 280 class CodeBlobClosure : public Closure {
jrose@1424 281 public:
jrose@1424 282 // Called for each code blob.
jrose@1424 283 virtual void do_code_blob(CodeBlob* cb) = 0;
jrose@1424 284 };
jrose@1424 285
stefank@6992 286 // Applies an oop closure to all ref fields in code blobs
stefank@6992 287 // iterated over in an object iteration.
stefank@6992 288 class CodeBlobToOopClosure : public CodeBlobClosure {
stefank@6992 289 OopClosure* _cl;
stefank@6992 290 bool _fix_relocations;
stefank@6992 291 protected:
stefank@6992 292 void do_nmethod(nmethod* nm);
stefank@6992 293 public:
stefank@6992 294 CodeBlobToOopClosure(OopClosure* cl, bool fix_relocations) : _cl(cl), _fix_relocations(fix_relocations) {}
stefank@6992 295 virtual void do_code_blob(CodeBlob* cb);
jrose@1424 296
stefank@6992 297 const static bool FixRelocations = true;
stefank@6992 298 };
stefank@6992 299
stefank@6992 300 class MarkingCodeBlobClosure : public CodeBlobToOopClosure {
jrose@1424 301 public:
stefank@6992 302 MarkingCodeBlobClosure(OopClosure* cl, bool fix_relocations) : CodeBlobToOopClosure(cl, fix_relocations) {}
jrose@1424 303 // Called for each code blob, but at most once per unique blob.
jrose@1424 304
jrose@1424 305 virtual void do_code_blob(CodeBlob* cb);
jrose@1424 306
jrose@1424 307 class MarkScope : public StackObj {
jrose@1424 308 protected:
jrose@1424 309 bool _active;
jrose@1424 310 public:
jrose@1424 311 MarkScope(bool activate = true);
jrose@1424 312 // = { if (active) nmethod::oops_do_marking_prologue(); }
jrose@1424 313 ~MarkScope();
jrose@1424 314 // = { if (active) nmethod::oops_do_marking_epilogue(); }
jrose@1424 315 };
jrose@1424 316 };
jrose@1424 317
duke@435 318 // MonitorClosure is used for iterating over monitors in the monitors cache
duke@435 319
duke@435 320 class ObjectMonitor;
duke@435 321
duke@435 322 class MonitorClosure : public StackObj {
duke@435 323 public:
duke@435 324 // called for each monitor in cache
duke@435 325 virtual void do_monitor(ObjectMonitor* m) = 0;
duke@435 326 };
duke@435 327
duke@435 328 // A closure that is applied without any arguments.
duke@435 329 class VoidClosure : public StackObj {
duke@435 330 public:
duke@435 331 // I would have liked to declare this a pure virtual, but that breaks
duke@435 332 // in mysterious ways, for unknown reasons.
duke@435 333 virtual void do_void();
duke@435 334 };
duke@435 335
duke@435 336
duke@435 337 // YieldClosure is intended for use by iteration loops
duke@435 338 // to incrementalize their work, allowing interleaving
duke@435 339 // of an interruptable task so as to allow other
duke@435 340 // threads to run (which may not otherwise be able to access
duke@435 341 // exclusive resources, for instance). Additionally, the
duke@435 342 // closure also allows for aborting an ongoing iteration
duke@435 343 // by means of checking the return value from the polling
duke@435 344 // call.
duke@435 345 class YieldClosure : public StackObj {
duke@435 346 public:
duke@435 347 virtual bool should_return() = 0;
duke@435 348 };
duke@435 349
duke@435 350 // Abstract closure for serializing data (read or write).
duke@435 351
coleenp@4037 352 class SerializeClosure : public Closure {
duke@435 353 public:
duke@435 354 // Return bool indicating whether closure implements read or write.
duke@435 355 virtual bool reading() const = 0;
duke@435 356
duke@435 357 // Read/write the void pointer pointed to by p.
duke@435 358 virtual void do_ptr(void** p) = 0;
duke@435 359
duke@435 360 // Read/write the region specified.
duke@435 361 virtual void do_region(u_char* start, size_t size) = 0;
duke@435 362
duke@435 363 // Check/write the tag. If reading, then compare the tag against
duke@435 364 // the passed in value and fail is they don't match. This allows
duke@435 365 // for verification that sections of the serialized data are of the
duke@435 366 // correct length.
duke@435 367 virtual void do_tag(int tag) = 0;
duke@435 368 };
jmasa@1370 369
coleenp@2497 370 class SymbolClosure : public StackObj {
coleenp@2497 371 public:
coleenp@2497 372 virtual void do_symbol(Symbol**) = 0;
coleenp@2497 373
coleenp@2497 374 // Clear LSB in symbol address; it can be set by CPSlot.
coleenp@2497 375 static Symbol* load_symbol(Symbol** p) {
coleenp@2497 376 return (Symbol*)(intptr_t(*p) & ~1);
coleenp@2497 377 }
coleenp@2497 378
coleenp@2497 379 // Store symbol, adjusting new pointer if the original pointer was adjusted
coleenp@2497 380 // (symbol references in constant pool slots have their LSB set to 1).
coleenp@2497 381 static void store_symbol(Symbol** p, Symbol* sym) {
coleenp@2497 382 *p = (Symbol*)(intptr_t(sym) | (intptr_t(*p) & 1));
coleenp@2497 383 }
coleenp@2497 384 };
coleenp@2497 385
stefank@6982 386
stefank@6982 387 // Helper defines for ExtendOopClosure
stefank@6982 388
stefank@6982 389 #define if_do_metadata_checked(closure, nv_suffix) \
stefank@6982 390 /* Make sure the non-virtual and the virtual versions match. */ \
stefank@6982 391 assert(closure->do_metadata##nv_suffix() == closure->do_metadata(), \
stefank@6982 392 "Inconsistency in do_metadata"); \
stefank@6982 393 if (closure->do_metadata##nv_suffix())
stefank@6982 394
stefank@6982 395 #define assert_should_ignore_metadata(closure, nv_suffix) \
stefank@6982 396 assert(!closure->do_metadata##nv_suffix(), "Code to handle metadata is not implemented")
stefank@6982 397
stefank@2314 398 #endif // SHARE_VM_MEMORY_ITERATOR_HPP

mercurial