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