1 #ifdef USE_PRAGMA_IDENT_HDR
2 #pragma ident "@(#)markSweep.hpp 1.67 07/05/17 15:52:55 JVM"
3 #endif
4 /*
5 * Copyright 1997-2007 Sun Microsystems, Inc. All Rights Reserved.
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7 *
8 * This code is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License version 2 only, as
10 * published by the Free Software Foundation.
11 *
12 * This code is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * version 2 for more details (a copy is included in the LICENSE file that
16 * accompanied this code).
17 *
18 * You should have received a copy of the GNU General Public License version
19 * 2 along with this work; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
24 * have any questions.
25 *
32 // generations are assumed to support marking; those that can also support
33 // compaction.
34 //
35 // Class unloading will only occur when a full gc is invoked.
36
37 // If VALIDATE_MARK_SWEEP is defined, the -XX:+ValidateMarkSweep flag will
38 // be operational, and will provide slow but comprehensive self-checks within
39 // the GC. This is not enabled by default in product or release builds,
40 // since the extra call to track_adjusted_pointer() in _adjust_pointer()
41 // would be too much overhead, and would disturb performance measurement.
42 // However, debug builds are sometimes way too slow to run GC tests!
43 #ifdef ASSERT
44 #define VALIDATE_MARK_SWEEP 1
45 #endif
46 #ifdef VALIDATE_MARK_SWEEP
47 #define VALIDATE_MARK_SWEEP_ONLY(code) code
48 #else
49 #define VALIDATE_MARK_SWEEP_ONLY(code)
50 #endif
51
52
53 // declared at end
54 class PreservedMark;
55
56 class MarkSweep : AllStatic {
57 //
58 // In line closure decls
59 //
60
61 class FollowRootClosure: public OopsInGenClosure{
62 public:
63 void do_oop(oop* p) { follow_root(p); }
64 virtual const bool do_nmethods() const { return true; }
65 };
66
67 class MarkAndPushClosure: public OopClosure {
68 public:
69 void do_oop(oop* p) { mark_and_push(p); }
70 virtual const bool do_nmethods() const { return true; }
71 };
72
73 class FollowStackClosure: public VoidClosure {
74 public:
75 void do_void() { follow_stack(); }
76 };
77
78 class AdjustPointerClosure: public OopsInGenClosure {
79 bool _is_root;
80 public:
81 AdjustPointerClosure(bool is_root) : _is_root(is_root) {}
82 void do_oop(oop* p) { _adjust_pointer(p, _is_root); }
83 };
84
85 // Used for java/lang/ref handling
86 class IsAliveClosure: public BoolObjectClosure {
87 public:
88 void do_object(oop p) { assert(false, "don't call"); }
89 bool do_object_b(oop p) { return p->is_gc_marked(); }
90 };
91
92 class KeepAliveClosure: public OopClosure {
93 public:
94 void do_oop(oop* p);
95 };
96
97 //
98 // Friend decls
99 //
100
101 friend class AdjustPointerClosure;
102 friend class KeepAliveClosure;
103 friend class VM_MarkSweep;
104 friend void marksweep_init();
105
106 //
107 // Vars
108 //
109 protected:
110 // Traversal stack used during phase1
111 static GrowableArray<oop>* _marking_stack;
112 // Stack for live klasses to revisit at end of marking phase
113 static GrowableArray<Klass*>* _revisit_klass_stack;
114
115 // Space for storing/restoring mark word
116 static GrowableArray<markOop>* _preserved_mark_stack;
117 static GrowableArray<oop>* _preserved_oop_stack;
118 static size_t _preserved_count;
119 static size_t _preserved_count_max;
120 static PreservedMark* _preserved_marks;
121
122 // Reference processing (used in ...follow_contents)
123 static ReferenceProcessor* _ref_processor;
124
125 #ifdef VALIDATE_MARK_SWEEP
126 static GrowableArray<oop*>* _root_refs_stack;
127 static GrowableArray<oop> * _live_oops;
128 static GrowableArray<oop> * _live_oops_moved_to;
129 static GrowableArray<size_t>* _live_oops_size;
130 static size_t _live_oops_index;
131 static size_t _live_oops_index_at_perm;
132 static GrowableArray<oop*>* _other_refs_stack;
133 static GrowableArray<oop*>* _adjusted_pointers;
134 static bool _pointer_tracking;
135 static bool _root_tracking;
136
137 // The following arrays are saved since the time of the last GC and
138 // assist in tracking down problems where someone has done an errant
139 // store into the heap, usually to an oop that wasn't properly
140 // handleized across a GC. If we crash or otherwise fail before the
141 // next GC, we can query these arrays to find out the object we had
142 // intended to do the store to (assuming it is still alive) and the
143 // offset within that object. Covered under RecordMarkSweepCompaction.
144 static GrowableArray<HeapWord*> * _cur_gc_live_oops;
145 static GrowableArray<HeapWord*> * _cur_gc_live_oops_moved_to;
146 static GrowableArray<size_t>* _cur_gc_live_oops_size;
147 static GrowableArray<HeapWord*> * _last_gc_live_oops;
148 static GrowableArray<HeapWord*> * _last_gc_live_oops_moved_to;
149 static GrowableArray<size_t>* _last_gc_live_oops_size;
150 #endif
151
152
153 // Non public closures
154 static IsAliveClosure is_alive;
155 static KeepAliveClosure keep_alive;
156
157 // Class unloading. Update subklass/sibling/implementor links at end of marking phase.
158 static void follow_weak_klass_links();
159
160 // Debugging
161 static void trace(const char* msg) PRODUCT_RETURN;
162
163 public:
164 // Public closures
165 static FollowRootClosure follow_root_closure;
166 static MarkAndPushClosure mark_and_push_closure;
167 static FollowStackClosure follow_stack_closure;
168 static AdjustPointerClosure adjust_root_pointer_closure;
169 static AdjustPointerClosure adjust_pointer_closure;
170
171 // Reference Processing
172 static ReferenceProcessor* const ref_processor() { return _ref_processor; }
173
174 // Call backs for marking
175 static void mark_object(oop obj);
176 static void follow_root(oop* p); // Mark pointer and follow contents. Empty marking
177
178 // stack afterwards.
179
180 static void mark_and_follow(oop* p); // Mark pointer and follow contents.
181 static void _mark_and_push(oop* p); // Mark pointer and push obj on
182 // marking stack.
183
184
185 static void mark_and_push(oop* p) { // Check mark and maybe push on
186 // marking stack
187 // assert(Universe::is_reserved_heap((oop)p), "we should only be traversing objects here");
188 oop m = *p;
189 if (m != NULL && !m->mark()->is_marked()) {
190 _mark_and_push(p);
191 }
192 }
193
194 static void follow_stack(); // Empty marking stack.
195
196
197 static void preserve_mark(oop p, markOop mark); // Save the mark word so it can be restored later
198 static void adjust_marks(); // Adjust the pointers in the preserved marks table
199 static void restore_marks(); // Restore the marks that we saved in preserve_mark
200
201 static void _adjust_pointer(oop* p, bool isroot);
202
203 static void adjust_root_pointer(oop* p) { _adjust_pointer(p, true); }
204 static void adjust_pointer(oop* p) { _adjust_pointer(p, false); }
205
206 #ifdef VALIDATE_MARK_SWEEP
207 static void track_adjusted_pointer(oop* p, oop newobj, bool isroot);
208 static void check_adjust_pointer(oop* p); // Adjust this pointer
209 static void track_interior_pointers(oop obj);
210 static void check_interior_pointers();
211
212 static void reset_live_oop_tracking(bool at_perm);
213 static void register_live_oop(oop p, size_t size);
214 static void validate_live_oop(oop p, size_t size);
215 static void live_oop_moved_to(HeapWord* q, size_t size, HeapWord* compaction_top);
216 static void compaction_complete();
217
218 // Querying operation of RecordMarkSweepCompaction results.
219 // Finds and prints the current base oop and offset for a word
220 // within an oop that was live during the last GC. Helpful for
221 // tracking down heap stomps.
222 static void print_new_location_of_heap_address(HeapWord* q);
223 #endif
224
225 // Call backs for class unloading
226 static void revisit_weak_klass_link(Klass* k); // Update subklass/sibling/implementor links at end of marking.
227 };
228
229
230 class PreservedMark VALUE_OBJ_CLASS_SPEC {
231 private:
232 oop _obj;
233 markOop _mark;
234
235 public:
236 void init(oop obj, markOop mark) {
237 _obj = obj;
238 _mark = mark;
239 }
240
241 void adjust_pointer() {
242 MarkSweep::adjust_pointer(&_obj);
243 }
244
245 void restore() {
246 _obj->set_mark(_mark);
247 }
248 };
|
1 #ifdef USE_PRAGMA_IDENT_HDR
2 #pragma ident "@(#)markSweep.hpp 1.67 07/05/17 15:52:55 JVM"
3 #endif
4 /*
5 * Copyright 1997-2008 Sun Microsystems, Inc. All Rights Reserved.
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7 *
8 * This code is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License version 2 only, as
10 * published by the Free Software Foundation.
11 *
12 * This code is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * version 2 for more details (a copy is included in the LICENSE file that
16 * accompanied this code).
17 *
18 * You should have received a copy of the GNU General Public License version
19 * 2 along with this work; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
24 * have any questions.
25 *
32 // generations are assumed to support marking; those that can also support
33 // compaction.
34 //
35 // Class unloading will only occur when a full gc is invoked.
36
37 // If VALIDATE_MARK_SWEEP is defined, the -XX:+ValidateMarkSweep flag will
38 // be operational, and will provide slow but comprehensive self-checks within
39 // the GC. This is not enabled by default in product or release builds,
40 // since the extra call to track_adjusted_pointer() in _adjust_pointer()
41 // would be too much overhead, and would disturb performance measurement.
42 // However, debug builds are sometimes way too slow to run GC tests!
43 #ifdef ASSERT
44 #define VALIDATE_MARK_SWEEP 1
45 #endif
46 #ifdef VALIDATE_MARK_SWEEP
47 #define VALIDATE_MARK_SWEEP_ONLY(code) code
48 #else
49 #define VALIDATE_MARK_SWEEP_ONLY(code)
50 #endif
51
52 // declared at end
53 class PreservedMark;
54
55 class MarkSweep : AllStatic {
56 //
57 // Inline closure decls
58 //
59 class FollowRootClosure: public OopsInGenClosure {
60 public:
61 virtual void do_oop(oop* p);
62 virtual void do_oop(narrowOop* p);
63 virtual const bool do_nmethods() const { return true; }
64 };
65
66 class MarkAndPushClosure: public OopClosure {
67 public:
68 virtual void do_oop(oop* p);
69 virtual void do_oop(narrowOop* p);
70 virtual const bool do_nmethods() const { return true; }
71 };
72
73 class FollowStackClosure: public VoidClosure {
74 public:
75 virtual void do_void();
76 };
77
78 class AdjustPointerClosure: public OopsInGenClosure {
79 private:
80 bool _is_root;
81 public:
82 AdjustPointerClosure(bool is_root) : _is_root(is_root) {}
83 virtual void do_oop(oop* p);
84 virtual void do_oop(narrowOop* p);
85 };
86
87 // Used for java/lang/ref handling
88 class IsAliveClosure: public BoolObjectClosure {
89 public:
90 virtual void do_object(oop p);
91 virtual bool do_object_b(oop p);
92 };
93
94 class KeepAliveClosure: public OopClosure {
95 protected:
96 template <class T> void do_oop_work(T* p);
97 public:
98 virtual void do_oop(oop* p);
99 virtual void do_oop(narrowOop* p);
100 };
101
102 //
103 // Friend decls
104 //
105 friend class AdjustPointerClosure;
106 friend class KeepAliveClosure;
107 friend class VM_MarkSweep;
108 friend void marksweep_init();
109
110 //
111 // Vars
112 //
113 protected:
114 // Traversal stack used during phase1
115 static GrowableArray<oop>* _marking_stack;
116 // Stack for live klasses to revisit at end of marking phase
117 static GrowableArray<Klass*>* _revisit_klass_stack;
118
119 // Space for storing/restoring mark word
120 static GrowableArray<markOop>* _preserved_mark_stack;
121 static GrowableArray<oop>* _preserved_oop_stack;
122 static size_t _preserved_count;
123 static size_t _preserved_count_max;
124 static PreservedMark* _preserved_marks;
125
126 // Reference processing (used in ...follow_contents)
127 static ReferenceProcessor* _ref_processor;
128
129 #ifdef VALIDATE_MARK_SWEEP
130 static GrowableArray<void*>* _root_refs_stack;
131 static GrowableArray<oop> * _live_oops;
132 static GrowableArray<oop> * _live_oops_moved_to;
133 static GrowableArray<size_t>* _live_oops_size;
134 static size_t _live_oops_index;
135 static size_t _live_oops_index_at_perm;
136 static GrowableArray<void*>* _other_refs_stack;
137 static GrowableArray<void*>* _adjusted_pointers;
138 static bool _pointer_tracking;
139 static bool _root_tracking;
140
141 // The following arrays are saved since the time of the last GC and
142 // assist in tracking down problems where someone has done an errant
143 // store into the heap, usually to an oop that wasn't properly
144 // handleized across a GC. If we crash or otherwise fail before the
145 // next GC, we can query these arrays to find out the object we had
146 // intended to do the store to (assuming it is still alive) and the
147 // offset within that object. Covered under RecordMarkSweepCompaction.
148 static GrowableArray<HeapWord*> * _cur_gc_live_oops;
149 static GrowableArray<HeapWord*> * _cur_gc_live_oops_moved_to;
150 static GrowableArray<size_t>* _cur_gc_live_oops_size;
151 static GrowableArray<HeapWord*> * _last_gc_live_oops;
152 static GrowableArray<HeapWord*> * _last_gc_live_oops_moved_to;
153 static GrowableArray<size_t>* _last_gc_live_oops_size;
154 #endif
155
156 // Non public closures
157 static IsAliveClosure is_alive;
158 static KeepAliveClosure keep_alive;
159
160 // Class unloading. Update subklass/sibling/implementor links at end of marking phase.
161 static void follow_weak_klass_links();
162
163 // Debugging
164 static void trace(const char* msg) PRODUCT_RETURN;
165
166 public:
167 // Public closures
168 static FollowRootClosure follow_root_closure;
169 static MarkAndPushClosure mark_and_push_closure;
170 static FollowStackClosure follow_stack_closure;
171 static AdjustPointerClosure adjust_root_pointer_closure;
172 static AdjustPointerClosure adjust_pointer_closure;
173
174 // Reference Processing
175 static ReferenceProcessor* const ref_processor() { return _ref_processor; }
176
177 // Call backs for marking
178 static void mark_object(oop obj);
179 // Mark pointer and follow contents. Empty marking stack afterwards.
180 template <class T> static inline void follow_root(T* p);
181 // Mark pointer and follow contents.
182 template <class T> static inline void mark_and_follow(T* p);
183 // Check mark and maybe push on marking stack
184 template <class T> static inline void mark_and_push(T* p);
185
186 static void follow_stack(); // Empty marking stack.
187
188 static void preserve_mark(oop p, markOop mark);
189 // Save the mark word so it can be restored later
190 static void adjust_marks(); // Adjust the pointers in the preserved marks table
191 static void restore_marks(); // Restore the marks that we saved in preserve_mark
192
193 template <class T> static inline void adjust_pointer(T* p, bool isroot);
194
195 static void adjust_root_pointer(oop* p) { adjust_pointer(p, true); }
196 static void adjust_pointer(oop* p) { adjust_pointer(p, false); }
197 static void adjust_pointer(narrowOop* p) { adjust_pointer(p, false); }
198
199 #ifdef VALIDATE_MARK_SWEEP
200 static void track_adjusted_pointer(void* p, bool isroot);
201 static void check_adjust_pointer(void* p);
202 static void track_interior_pointers(oop obj);
203 static void check_interior_pointers();
204
205 static void reset_live_oop_tracking(bool at_perm);
206 static void register_live_oop(oop p, size_t size);
207 static void validate_live_oop(oop p, size_t size);
208 static void live_oop_moved_to(HeapWord* q, size_t size, HeapWord* compaction_top);
209 static void compaction_complete();
210
211 // Querying operation of RecordMarkSweepCompaction results.
212 // Finds and prints the current base oop and offset for a word
213 // within an oop that was live during the last GC. Helpful for
214 // tracking down heap stomps.
215 static void print_new_location_of_heap_address(HeapWord* q);
216 #endif
217
218 // Call backs for class unloading
219 static void revisit_weak_klass_link(Klass* k); // Update subklass/sibling/implementor links at end of marking.
220 };
221
222 class PreservedMark VALUE_OBJ_CLASS_SPEC {
223 private:
224 oop _obj;
225 markOop _mark;
226
227 public:
228 void init(oop obj, markOop mark) {
229 _obj = obj;
230 _mark = mark;
231 }
232
233 void adjust_pointer() {
234 MarkSweep::adjust_pointer(&_obj);
235 }
236
237 void restore() {
238 _obj->set_mark(_mark);
239 }
240 };
|