comparison src/share/vm/gc_implementation/g1/concurrentZFThread.cpp @ 342:37f87013dfd8

6711316: Open source the Garbage-First garbage collector Summary: First mercurial integration of the code for the Garbage-First garbage collector. Reviewed-by: apetrusenko, iveresov, jmasa, sgoldman, tonyp, ysr
author ysr
date Thu, 05 Jun 2008 15:57:56 -0700
parents
children e1fdf4fd34dc
comparison
equal deleted inserted replaced
189:0b27f3512f9e 342:37f87013dfd8
1 /*
2 * Copyright 2001-2007 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 *
23 */
24
25 #include "incls/_precompiled.incl"
26 #include "incls/_concurrentZFThread.cpp.incl"
27
28 // ======= Concurrent Zero-Fill Thread ========
29
30 // The CM thread is created when the G1 garbage collector is used
31
32 int ConcurrentZFThread::_region_allocs = 0;
33 int ConcurrentZFThread::_sync_zfs = 0;
34 int ConcurrentZFThread::_zf_waits = 0;
35 int ConcurrentZFThread::_regions_filled = 0;
36
37 ConcurrentZFThread::ConcurrentZFThread() :
38 ConcurrentGCThread(),
39 _co_tracker(G1ZFGroup)
40 {
41 create_and_start();
42 }
43
44 void ConcurrentZFThread::wait_for_ZF_completed(HeapRegion* hr) {
45 assert(ZF_mon->owned_by_self(), "Precondition.");
46 note_zf_wait();
47 while (hr->zero_fill_state() == HeapRegion::ZeroFilling) {
48 ZF_mon->wait(Mutex::_no_safepoint_check_flag);
49 }
50 }
51
52 void ConcurrentZFThread::processHeapRegion(HeapRegion* hr) {
53 assert(!Universe::heap()->is_gc_active(),
54 "This should not happen during GC.");
55 assert(hr != NULL, "Precondition");
56 // These are unlocked reads, but if this test is successful, then no
57 // other thread will attempt this zero filling. Only a GC thread can
58 // modify the ZF state of a region whose state is zero-filling, and this
59 // should only happen while the ZF thread is locking out GC.
60 if (hr->zero_fill_state() == HeapRegion::ZeroFilling
61 && hr->zero_filler() == Thread::current()) {
62 assert(hr->top() == hr->bottom(), "better be empty!");
63 assert(!hr->isHumongous(), "Only free regions on unclean list.");
64 Copy::fill_to_words(hr->bottom(), hr->capacity()/HeapWordSize);
65 note_region_filled();
66 }
67 }
68
69 void ConcurrentZFThread::run() {
70 initialize_in_thread();
71 Thread* thr_self = Thread::current();
72 _vtime_start = os::elapsedVTime();
73 wait_for_universe_init();
74 _co_tracker.enable();
75 _co_tracker.start();
76
77 G1CollectedHeap* g1 = G1CollectedHeap::heap();
78 _sts.join();
79 while (!_should_terminate) {
80 _sts.leave();
81
82 {
83 MutexLockerEx x(ZF_mon, Mutex::_no_safepoint_check_flag);
84
85 // This local variable will hold a region being zero-filled. This
86 // region will neither be on the unclean or zero-filled lists, and
87 // will not be available for allocation; thus, we might have an
88 // allocation fail, causing a full GC, because of this, but this is a
89 // price we will pay. (In future, we might want to make the fact
90 // that there's a region being zero-filled apparent to the G1 heap,
91 // which could then wait for it in this extreme case...)
92 HeapRegion* to_fill;
93
94 while (!g1->should_zf()
95 || (to_fill = g1->pop_unclean_region_list_locked()) == NULL)
96 ZF_mon->wait(Mutex::_no_safepoint_check_flag);
97 while (to_fill->zero_fill_state() == HeapRegion::ZeroFilling)
98 ZF_mon->wait(Mutex::_no_safepoint_check_flag);
99
100 // So now to_fill is non-NULL and is not ZeroFilling. It might be
101 // Allocated or ZeroFilled. (The latter could happen if this thread
102 // starts the zero-filling of a region, but a GC intervenes and
103 // pushes new regions needing on the front of the filling on the
104 // front of the list.)
105
106 switch (to_fill->zero_fill_state()) {
107 case HeapRegion::Allocated:
108 to_fill = NULL;
109 break;
110
111 case HeapRegion::NotZeroFilled:
112 to_fill->set_zero_fill_in_progress(thr_self);
113
114 ZF_mon->unlock();
115 _sts.join();
116 processHeapRegion(to_fill);
117 _sts.leave();
118 ZF_mon->lock_without_safepoint_check();
119
120 if (to_fill->zero_fill_state() == HeapRegion::ZeroFilling
121 && to_fill->zero_filler() == thr_self) {
122 to_fill->set_zero_fill_complete();
123 (void)g1->put_free_region_on_list_locked(to_fill);
124 }
125 break;
126
127 case HeapRegion::ZeroFilled:
128 (void)g1->put_free_region_on_list_locked(to_fill);
129 break;
130
131 case HeapRegion::ZeroFilling:
132 ShouldNotReachHere();
133 break;
134 }
135 }
136 _vtime_accum = (os::elapsedVTime() - _vtime_start);
137 _sts.join();
138
139 _co_tracker.update();
140 }
141 _co_tracker.update(false);
142 _sts.leave();
143
144 assert(_should_terminate, "just checking");
145 terminate();
146 }
147
148 bool ConcurrentZFThread::offer_yield() {
149 if (_sts.should_yield()) {
150 _sts.yield("Concurrent ZF");
151 return true;
152 } else {
153 return false;
154 }
155 }
156
157 void ConcurrentZFThread::stop() {
158 // it is ok to take late safepoints here, if needed
159 MutexLockerEx mu(Terminator_lock);
160 _should_terminate = true;
161 while (!_has_terminated) {
162 Terminator_lock->wait();
163 }
164 }
165
166 void ConcurrentZFThread::print() {
167 gclog_or_tty->print("\"Concurrent ZF Thread\" ");
168 Thread::print();
169 gclog_or_tty->cr();
170 }
171
172
173 double ConcurrentZFThread::_vtime_accum;
174
175 void ConcurrentZFThread::print_summary_info() {
176 gclog_or_tty->print("\nConcurrent Zero-Filling:\n");
177 gclog_or_tty->print(" Filled %d regions, used %5.2fs.\n",
178 _regions_filled,
179 vtime_accum());
180 gclog_or_tty->print(" Of %d region allocs, %d (%5.2f%%) required sync ZF,\n",
181 _region_allocs, _sync_zfs,
182 (_region_allocs > 0 ?
183 (float)_sync_zfs/(float)_region_allocs*100.0 :
184 0.0));
185 gclog_or_tty->print(" and %d (%5.2f%%) required a ZF wait.\n",
186 _zf_waits,
187 (_region_allocs > 0 ?
188 (float)_zf_waits/(float)_region_allocs*100.0 :
189 0.0));
190
191 }