Print this page
Split |
Close |
Expand all |
Collapse all |
--- old/src/share/vm/memory/referenceProcessor.hpp
+++ new/src/share/vm/memory/referenceProcessor.hpp
1 1 /*
2 2 * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
3 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 4 *
5 5 * This code is free software; you can redistribute it and/or modify it
6 6 * under the terms of the GNU General Public License version 2 only, as
7 7 * published by the Free Software Foundation.
8 8 *
9 9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 12 * version 2 for more details (a copy is included in the LICENSE file that
13 13 * accompanied this code).
14 14 *
15 15 * You should have received a copy of the GNU General Public License version
16 16 * 2 along with this work; if not, write to the Free Software Foundation,
17 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 18 *
19 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 20 * or visit www.oracle.com if you need additional information or have any
21 21 * questions.
22 22 *
23 23 */
24 24
25 25 #ifndef SHARE_VM_MEMORY_REFERENCEPROCESSOR_HPP
26 26 #define SHARE_VM_MEMORY_REFERENCEPROCESSOR_HPP
27 27
28 28 #include "memory/referencePolicy.hpp"
29 29 #include "oops/instanceRefKlass.hpp"
30 30
31 31 // ReferenceProcessor class encapsulates the per-"collector" processing
32 32 // of java.lang.Reference objects for GC. The interface is useful for supporting
33 33 // a generational abstraction, in particular when there are multiple
34 34 // generations that are being independently collected -- possibly
35 35 // concurrently and/or incrementally. Note, however, that the
36 36 // ReferenceProcessor class abstracts away from a generational setting
37 37 // by using only a heap interval (called "span" below), thus allowing
38 38 // its use in a straightforward manner in a general, non-generational
39 39 // setting.
40 40 //
41 41 // The basic idea is that each ReferenceProcessor object concerns
42 42 // itself with ("weak") reference processing in a specific "span"
43 43 // of the heap of interest to a specific collector. Currently,
44 44 // the span is a convex interval of the heap, but, efficiency
45 45 // apart, there seems to be no reason it couldn't be extended
46 46 // (with appropriate modifications) to any "non-convex interval".
47 47
48 48 // forward references
49 49 class ReferencePolicy;
50 50 class AbstractRefProcTaskExecutor;
51 51 class DiscoveredList;
52 52
53 53 class ReferenceProcessor : public CHeapObj {
54 54 protected:
55 55 // End of list marker
56 56 static oop _sentinelRef;
57 57 MemRegion _span; // (right-open) interval of heap
58 58 // subject to wkref discovery
59 59 bool _discovering_refs; // true when discovery enabled
60 60 bool _discovery_is_atomic; // if discovery is atomic wrt
61 61 // other collectors in configuration
62 62 bool _discovery_is_mt; // true if reference discovery is MT.
63 63 // If true, setting "next" field of a discovered refs list requires
64 64 // write barrier(s). (Must be true if used in a collector in which
65 65 // elements of a discovered list may be moved during discovery: for
66 66 // example, a collector like Garbage-First that moves objects during a
67 67 // long-term concurrent marking phase that does weak reference
68 68 // discovery.)
69 69 bool _discovered_list_needs_barrier;
70 70 BarrierSet* _bs; // Cached copy of BarrierSet.
71 71 bool _enqueuing_is_done; // true if all weak references enqueued
72 72 bool _processing_is_mt; // true during phases when
73 73 // reference processing is MT.
74 74 int _next_id; // round-robin counter in
75 75 // support of work distribution
76 76
77 77 // For collectors that do not keep GC marking information
78 78 // in the object header, this field holds a closure that
79 79 // helps the reference processor determine the reachability
80 80 // of an oop (the field is currently initialized to NULL for
81 81 // all collectors but the CMS collector).
82 82 BoolObjectClosure* _is_alive_non_header;
83 83
84 84 // Soft ref clearing policies
85 85 // . the default policy
86 86 static ReferencePolicy* _default_soft_ref_policy;
87 87 // . the "clear all" policy
88 88 static ReferencePolicy* _always_clear_soft_ref_policy;
89 89 // . the current policy below is either one of the above
90 90 ReferencePolicy* _current_soft_ref_policy;
91 91
92 92 // The discovered ref lists themselves
93 93
94 94 // The active MT'ness degree of the queues below
95 95 int _num_q;
96 96 // The maximum MT'ness degree of the queues below
97 97 int _max_num_q;
98 98 // Arrays of lists of oops, one per thread
99 99 DiscoveredList* _discoveredSoftRefs;
100 100 DiscoveredList* _discoveredWeakRefs;
101 101 DiscoveredList* _discoveredFinalRefs;
102 102 DiscoveredList* _discoveredPhantomRefs;
103 103
104 104 public:
105 105 int num_q() { return _num_q; }
106 106 void set_mt_degree(int v) { _num_q = v; }
107 107 DiscoveredList* discovered_soft_refs() { return _discoveredSoftRefs; }
108 108 static oop sentinel_ref() { return _sentinelRef; }
109 109 static oop* adr_sentinel_ref() { return &_sentinelRef; }
110 110 ReferencePolicy* setup_policy(bool always_clear) {
111 111 _current_soft_ref_policy = always_clear ?
112 112 _always_clear_soft_ref_policy : _default_soft_ref_policy;
113 113 _current_soft_ref_policy->setup(); // snapshot the policy threshold
114 114 return _current_soft_ref_policy;
115 115 }
116 116
117 117 public:
118 118 // Process references with a certain reachability level.
119 119 void process_discovered_reflist(DiscoveredList refs_lists[],
120 120 ReferencePolicy* policy,
121 121 bool clear_referent,
122 122 BoolObjectClosure* is_alive,
123 123 OopClosure* keep_alive,
124 124 VoidClosure* complete_gc,
125 125 AbstractRefProcTaskExecutor* task_executor);
126 126
127 127 void process_phaseJNI(BoolObjectClosure* is_alive,
128 128 OopClosure* keep_alive,
129 129 VoidClosure* complete_gc);
130 130
131 131 // Work methods used by the method process_discovered_reflist
132 132 // Phase1: keep alive all those referents that are otherwise
133 133 // dead but which must be kept alive by policy (and their closure).
134 134 void process_phase1(DiscoveredList& refs_list,
135 135 ReferencePolicy* policy,
136 136 BoolObjectClosure* is_alive,
137 137 OopClosure* keep_alive,
138 138 VoidClosure* complete_gc);
139 139 // Phase2: remove all those references whose referents are
140 140 // reachable.
141 141 inline void process_phase2(DiscoveredList& refs_list,
142 142 BoolObjectClosure* is_alive,
143 143 OopClosure* keep_alive,
144 144 VoidClosure* complete_gc) {
145 145 if (discovery_is_atomic()) {
146 146 // complete_gc is ignored in this case for this phase
147 147 pp2_work(refs_list, is_alive, keep_alive);
148 148 } else {
149 149 assert(complete_gc != NULL, "Error");
150 150 pp2_work_concurrent_discovery(refs_list, is_alive,
151 151 keep_alive, complete_gc);
152 152 }
153 153 }
154 154 // Work methods in support of process_phase2
155 155 void pp2_work(DiscoveredList& refs_list,
156 156 BoolObjectClosure* is_alive,
157 157 OopClosure* keep_alive);
158 158 void pp2_work_concurrent_discovery(
159 159 DiscoveredList& refs_list,
160 160 BoolObjectClosure* is_alive,
161 161 OopClosure* keep_alive,
162 162 VoidClosure* complete_gc);
163 163 // Phase3: process the referents by either clearing them
164 164 // or keeping them alive (and their closure)
165 165 void process_phase3(DiscoveredList& refs_list,
166 166 bool clear_referent,
167 167 BoolObjectClosure* is_alive,
168 168 OopClosure* keep_alive,
169 169 VoidClosure* complete_gc);
170 170
171 171 // Enqueue references with a certain reachability level
172 172 void enqueue_discovered_reflist(DiscoveredList& refs_list, HeapWord* pending_list_addr);
173 173
174 174 // "Preclean" all the discovered reference lists
175 175 // by removing references with strongly reachable referents.
176 176 // The first argument is a predicate on an oop that indicates
177 177 // its (strong) reachability and the second is a closure that
178 178 // may be used to incrementalize or abort the precleaning process.
179 179 // The caller is responsible for taking care of potential
180 180 // interference with concurrent operations on these lists
181 181 // (or predicates involved) by other threads. Currently
182 182 // only used by the CMS collector. should_unload_classes is
183 183 // used to aid assertion checking when classes are collected.
184 184 void preclean_discovered_references(BoolObjectClosure* is_alive,
185 185 OopClosure* keep_alive,
186 186 VoidClosure* complete_gc,
187 187 YieldClosure* yield,
188 188 bool should_unload_classes);
189 189
190 190 // Delete entries in the discovered lists that have
191 191 // either a null referent or are not active. Such
192 192 // Reference objects can result from the clearing
193 193 // or enqueueing of Reference objects concurrent
194 194 // with their discovery by a (concurrent) collector.
195 195 // For a definition of "active" see java.lang.ref.Reference;
196 196 // Refs are born active, become inactive when enqueued,
197 197 // and never become active again. The state of being
198 198 // active is encoded as follows: A Ref is active
199 199 // if and only if its "next" field is NULL.
200 200 void clean_up_discovered_references();
201 201 void clean_up_discovered_reflist(DiscoveredList& refs_list);
202 202
203 203 // Returns the name of the discovered reference list
204 204 // occupying the i / _num_q slot.
205 205 const char* list_name(int i);
206 206
207 207 void enqueue_discovered_reflists(HeapWord* pending_list_addr, AbstractRefProcTaskExecutor* task_executor);
208 208
209 209 protected:
210 210 // "Preclean" the given discovered reference list
211 211 // by removing references with strongly reachable referents.
212 212 // Currently used in support of CMS only.
213 213 void preclean_discovered_reflist(DiscoveredList& refs_list,
214 214 BoolObjectClosure* is_alive,
215 215 OopClosure* keep_alive,
216 216 VoidClosure* complete_gc,
217 217 YieldClosure* yield);
218 218
219 219 int next_id() {
220 220 int id = _next_id;
221 221 if (++_next_id == _num_q) {
222 222 _next_id = 0;
223 223 }
224 224 return id;
225 225 }
226 226 DiscoveredList* get_discovered_list(ReferenceType rt);
227 227 inline void add_to_discovered_list_mt(DiscoveredList& refs_list, oop obj,
228 228 HeapWord* discovered_addr);
229 229 void verify_ok_to_handle_reflists() PRODUCT_RETURN;
230 230
231 231 void abandon_partial_discovered_list(DiscoveredList& refs_list);
232 232
233 233 // Calculate the number of jni handles.
234 234 unsigned int count_jni_refs();
235 235
236 236 // Balances reference queues.
237 237 void balance_queues(DiscoveredList ref_lists[]);
238 238
239 239 // Update (advance) the soft ref master clock field.
240 240 void update_soft_ref_master_clock();
241 241
242 242 public:
243 243 // constructor
244 244 ReferenceProcessor():
245 245 _span((HeapWord*)NULL, (HeapWord*)NULL),
246 246 _discoveredSoftRefs(NULL), _discoveredWeakRefs(NULL),
247 247 _discoveredFinalRefs(NULL), _discoveredPhantomRefs(NULL),
248 248 _discovering_refs(false),
↓ open down ↓ |
248 lines elided |
↑ open up ↑ |
249 249 _discovery_is_atomic(true),
250 250 _enqueuing_is_done(false),
251 251 _discovery_is_mt(false),
252 252 _discovered_list_needs_barrier(false),
253 253 _bs(NULL),
254 254 _is_alive_non_header(NULL),
255 255 _num_q(0),
256 256 _max_num_q(0),
257 257 _processing_is_mt(false),
258 258 _next_id(0)
259 - {}
259 + { }
260 260
261 - ReferenceProcessor(MemRegion span, bool atomic_discovery,
262 - bool mt_discovery,
263 - int mt_degree = 1,
264 - bool mt_processing = false,
261 + // Default parameters give you a vanilla reference processor.
262 + ReferenceProcessor(MemRegion span,
263 + bool mt_processing = false, int mt_processing_degree = 1,
264 + bool mt_discovery = false, int mt_discovery_degree = 1,
265 + bool atomic_discovery = true,
266 + BoolObjectClosure* is_alive_non_header = NULL,
265 267 bool discovered_list_needs_barrier = false);
266 268
267 - // Allocates and initializes a reference processor.
268 - static ReferenceProcessor* create_ref_processor(
269 - MemRegion span,
270 - bool atomic_discovery,
271 - bool mt_discovery,
272 - BoolObjectClosure* is_alive_non_header = NULL,
273 - int parallel_gc_threads = 1,
274 - bool mt_processing = false,
275 - bool discovered_list_needs_barrier = false);
276 -
277 269 // RefDiscoveryPolicy values
278 270 enum DiscoveryPolicy {
279 271 ReferenceBasedDiscovery = 0,
280 272 ReferentBasedDiscovery = 1,
281 273 DiscoveryPolicyMin = ReferenceBasedDiscovery,
282 274 DiscoveryPolicyMax = ReferentBasedDiscovery
283 275 };
284 276
285 277 static void init_statics();
286 278
287 279 public:
288 280 // get and set "is_alive_non_header" field
289 281 BoolObjectClosure* is_alive_non_header() {
290 282 return _is_alive_non_header;
291 283 }
292 284 void set_is_alive_non_header(BoolObjectClosure* is_alive_non_header) {
293 285 _is_alive_non_header = is_alive_non_header;
294 286 }
295 287
296 288 // get and set span
297 289 MemRegion span() { return _span; }
298 290 void set_span(MemRegion span) { _span = span; }
299 291
300 292 // start and stop weak ref discovery
301 293 void enable_discovery() { _discovering_refs = true; }
302 294 void disable_discovery() { _discovering_refs = false; }
303 295 bool discovery_enabled() { return _discovering_refs; }
304 296
305 297 // whether discovery is atomic wrt other collectors
306 298 bool discovery_is_atomic() const { return _discovery_is_atomic; }
307 299 void set_atomic_discovery(bool atomic) { _discovery_is_atomic = atomic; }
308 300
309 301 // whether discovery is done by multiple threads same-old-timeously
310 302 bool discovery_is_mt() const { return _discovery_is_mt; }
311 303 void set_mt_discovery(bool mt) { _discovery_is_mt = mt; }
312 304
313 305 // Whether we are in a phase when _processing_ is MT.
314 306 bool processing_is_mt() const { return _processing_is_mt; }
315 307 void set_mt_processing(bool mt) { _processing_is_mt = mt; }
316 308
317 309 // whether all enqueuing of weak references is complete
318 310 bool enqueuing_is_done() { return _enqueuing_is_done; }
319 311 void set_enqueuing_is_done(bool v) { _enqueuing_is_done = v; }
320 312
321 313 // iterate over oops
322 314 void weak_oops_do(OopClosure* f); // weak roots
323 315 static void oops_do(OopClosure* f); // strong root(s)
324 316
325 317 // Balance each of the discovered lists.
326 318 void balance_all_queues();
327 319
328 320 // Discover a Reference object, using appropriate discovery criteria
329 321 bool discover_reference(oop obj, ReferenceType rt);
330 322
331 323 // Process references found during GC (called by the garbage collector)
332 324 void process_discovered_references(BoolObjectClosure* is_alive,
333 325 OopClosure* keep_alive,
334 326 VoidClosure* complete_gc,
335 327 AbstractRefProcTaskExecutor* task_executor);
336 328
337 329 public:
338 330 // Enqueue references at end of GC (called by the garbage collector)
339 331 bool enqueue_discovered_references(AbstractRefProcTaskExecutor* task_executor = NULL);
340 332
341 333 // If a discovery is in process that is being superceded, abandon it: all
342 334 // the discovered lists will be empty, and all the objects on them will
343 335 // have NULL discovered fields. Must be called only at a safepoint.
344 336 void abandon_partial_discovery();
345 337
346 338 // debugging
347 339 void verify_no_references_recorded() PRODUCT_RETURN;
348 340 void verify_referent(oop obj) PRODUCT_RETURN;
349 341 static void verify();
350 342
351 343 // clear the discovered lists (unlinking each entry).
352 344 void clear_discovered_references() PRODUCT_RETURN;
353 345 };
354 346
355 347 // A utility class to disable reference discovery in
356 348 // the scope which contains it, for given ReferenceProcessor.
357 349 class NoRefDiscovery: StackObj {
358 350 private:
359 351 ReferenceProcessor* _rp;
360 352 bool _was_discovering_refs;
361 353 public:
362 354 NoRefDiscovery(ReferenceProcessor* rp) : _rp(rp) {
363 355 _was_discovering_refs = _rp->discovery_enabled();
364 356 if (_was_discovering_refs) {
365 357 _rp->disable_discovery();
366 358 }
367 359 }
368 360
369 361 ~NoRefDiscovery() {
370 362 if (_was_discovering_refs) {
371 363 _rp->enable_discovery();
372 364 }
373 365 }
374 366 };
375 367
376 368
377 369 // A utility class to temporarily mutate the span of the
378 370 // given ReferenceProcessor in the scope that contains it.
379 371 class ReferenceProcessorSpanMutator: StackObj {
380 372 private:
381 373 ReferenceProcessor* _rp;
382 374 MemRegion _saved_span;
383 375
384 376 public:
385 377 ReferenceProcessorSpanMutator(ReferenceProcessor* rp,
386 378 MemRegion span):
387 379 _rp(rp) {
388 380 _saved_span = _rp->span();
389 381 _rp->set_span(span);
390 382 }
391 383
392 384 ~ReferenceProcessorSpanMutator() {
393 385 _rp->set_span(_saved_span);
394 386 }
395 387 };
396 388
397 389 // A utility class to temporarily change the MT'ness of
398 390 // reference discovery for the given ReferenceProcessor
399 391 // in the scope that contains it.
400 392 class ReferenceProcessorMTMutator: StackObj {
401 393 private:
402 394 ReferenceProcessor* _rp;
403 395 bool _saved_mt;
404 396
405 397 public:
406 398 ReferenceProcessorMTMutator(ReferenceProcessor* rp,
407 399 bool mt):
408 400 _rp(rp) {
409 401 _saved_mt = _rp->discovery_is_mt();
410 402 _rp->set_mt_discovery(mt);
411 403 }
412 404
413 405 ~ReferenceProcessorMTMutator() {
414 406 _rp->set_mt_discovery(_saved_mt);
415 407 }
416 408 };
417 409
418 410
419 411 // A utility class to temporarily change the disposition
420 412 // of the "is_alive_non_header" closure field of the
421 413 // given ReferenceProcessor in the scope that contains it.
422 414 class ReferenceProcessorIsAliveMutator: StackObj {
423 415 private:
424 416 ReferenceProcessor* _rp;
425 417 BoolObjectClosure* _saved_cl;
426 418
427 419 public:
428 420 ReferenceProcessorIsAliveMutator(ReferenceProcessor* rp,
429 421 BoolObjectClosure* cl):
430 422 _rp(rp) {
431 423 _saved_cl = _rp->is_alive_non_header();
432 424 _rp->set_is_alive_non_header(cl);
433 425 }
434 426
435 427 ~ReferenceProcessorIsAliveMutator() {
436 428 _rp->set_is_alive_non_header(_saved_cl);
437 429 }
438 430 };
439 431
440 432 // A utility class to temporarily change the disposition
441 433 // of the "discovery_is_atomic" field of the
442 434 // given ReferenceProcessor in the scope that contains it.
443 435 class ReferenceProcessorAtomicMutator: StackObj {
444 436 private:
445 437 ReferenceProcessor* _rp;
446 438 bool _saved_atomic_discovery;
447 439
448 440 public:
449 441 ReferenceProcessorAtomicMutator(ReferenceProcessor* rp,
450 442 bool atomic):
451 443 _rp(rp) {
452 444 _saved_atomic_discovery = _rp->discovery_is_atomic();
453 445 _rp->set_atomic_discovery(atomic);
454 446 }
455 447
456 448 ~ReferenceProcessorAtomicMutator() {
457 449 _rp->set_atomic_discovery(_saved_atomic_discovery);
458 450 }
459 451 };
460 452
461 453
462 454 // A utility class to temporarily change the MT processing
463 455 // disposition of the given ReferenceProcessor instance
464 456 // in the scope that contains it.
465 457 class ReferenceProcessorMTProcMutator: StackObj {
466 458 private:
467 459 ReferenceProcessor* _rp;
468 460 bool _saved_mt;
469 461
470 462 public:
471 463 ReferenceProcessorMTProcMutator(ReferenceProcessor* rp,
472 464 bool mt):
473 465 _rp(rp) {
474 466 _saved_mt = _rp->processing_is_mt();
475 467 _rp->set_mt_processing(mt);
476 468 }
477 469
478 470 ~ReferenceProcessorMTProcMutator() {
479 471 _rp->set_mt_processing(_saved_mt);
480 472 }
481 473 };
482 474
483 475
484 476 // This class is an interface used to implement task execution for the
485 477 // reference processing.
486 478 class AbstractRefProcTaskExecutor {
487 479 public:
488 480
489 481 // Abstract tasks to execute.
490 482 class ProcessTask;
491 483 class EnqueueTask;
492 484
493 485 // Executes a task using worker threads.
494 486 virtual void execute(ProcessTask& task) = 0;
495 487 virtual void execute(EnqueueTask& task) = 0;
496 488
497 489 // Switch to single threaded mode.
498 490 virtual void set_single_threaded_mode() { };
499 491 };
500 492
501 493 // Abstract reference processing task to execute.
502 494 class AbstractRefProcTaskExecutor::ProcessTask {
503 495 protected:
504 496 ProcessTask(ReferenceProcessor& ref_processor,
505 497 DiscoveredList refs_lists[],
506 498 bool marks_oops_alive)
507 499 : _ref_processor(ref_processor),
508 500 _refs_lists(refs_lists),
509 501 _marks_oops_alive(marks_oops_alive)
510 502 { }
511 503
512 504 public:
513 505 virtual void work(unsigned int work_id, BoolObjectClosure& is_alive,
514 506 OopClosure& keep_alive,
515 507 VoidClosure& complete_gc) = 0;
516 508
517 509 // Returns true if a task marks some oops as alive.
518 510 bool marks_oops_alive() const
519 511 { return _marks_oops_alive; }
520 512
521 513 protected:
522 514 ReferenceProcessor& _ref_processor;
523 515 DiscoveredList* _refs_lists;
524 516 const bool _marks_oops_alive;
525 517 };
526 518
527 519 // Abstract reference processing task to execute.
528 520 class AbstractRefProcTaskExecutor::EnqueueTask {
529 521 protected:
530 522 EnqueueTask(ReferenceProcessor& ref_processor,
531 523 DiscoveredList refs_lists[],
532 524 HeapWord* pending_list_addr,
533 525 oop sentinel_ref,
534 526 int n_queues)
535 527 : _ref_processor(ref_processor),
536 528 _refs_lists(refs_lists),
537 529 _pending_list_addr(pending_list_addr),
538 530 _sentinel_ref(sentinel_ref),
539 531 _n_queues(n_queues)
540 532 { }
541 533
542 534 public:
543 535 virtual void work(unsigned int work_id) = 0;
544 536
545 537 protected:
546 538 ReferenceProcessor& _ref_processor;
547 539 DiscoveredList* _refs_lists;
548 540 HeapWord* _pending_list_addr;
549 541 oop _sentinel_ref;
550 542 int _n_queues;
551 543 };
552 544
553 545 #endif // SHARE_VM_MEMORY_REFERENCEPROCESSOR_HPP
↓ open down ↓ |
267 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX