comparison src/share/vm/memory/referenceProcessor.cpp @ 4712:e7dead7e90af

7117303: VM uses non-monotonic time source and complains that it is non-monotonic Summary: Replaces calls to os::javaTimeMillis(), which does not (and cannot) guarantee monotonicity, in GC code to an equivalent expression that uses os::javaTimeNanos(). os::javaTimeNanos is guaranteed monotonically non-decreasing if the underlying platform provides a monotonic time source. Changes in OS files are to make use of the newly defined constants in globalDefinitions.hpp. Reviewed-by: dholmes, ysr
author johnc
date Mon, 19 Dec 2011 10:02:05 -0800
parents bf2d2b8b1726
children 441e946dc1af
comparison
equal deleted inserted replaced
4711:adedfbbf0360 4712:e7dead7e90af
41 void referenceProcessor_init() { 41 void referenceProcessor_init() {
42 ReferenceProcessor::init_statics(); 42 ReferenceProcessor::init_statics();
43 } 43 }
44 44
45 void ReferenceProcessor::init_statics() { 45 void ReferenceProcessor::init_statics() {
46 jlong now = os::javaTimeMillis(); 46 // We need a monotonically non-deccreasing time in ms but
47 // os::javaTimeMillis() does not guarantee monotonicity.
48 jlong now = os::javaTimeNanos() / NANOSECS_PER_MILLISEC;
47 49
48 // Initialize the soft ref timestamp clock. 50 // Initialize the soft ref timestamp clock.
49 _soft_ref_timestamp_clock = now; 51 _soft_ref_timestamp_clock = now;
50 // Also update the soft ref clock in j.l.r.SoftReference 52 // Also update the soft ref clock in j.l.r.SoftReference
51 java_lang_ref_SoftReference::set_clock(_soft_ref_timestamp_clock); 53 java_lang_ref_SoftReference::set_clock(_soft_ref_timestamp_clock);
149 } 151 }
150 152
151 void ReferenceProcessor::update_soft_ref_master_clock() { 153 void ReferenceProcessor::update_soft_ref_master_clock() {
152 // Update (advance) the soft ref master clock field. This must be done 154 // Update (advance) the soft ref master clock field. This must be done
153 // after processing the soft ref list. 155 // after processing the soft ref list.
154 jlong now = os::javaTimeMillis(); 156
157 // We need a monotonically non-deccreasing time in ms but
158 // os::javaTimeMillis() does not guarantee monotonicity.
159 jlong now = os::javaTimeNanos() / NANOSECS_PER_MILLISEC;
155 jlong soft_ref_clock = java_lang_ref_SoftReference::clock(); 160 jlong soft_ref_clock = java_lang_ref_SoftReference::clock();
156 assert(soft_ref_clock == _soft_ref_timestamp_clock, "soft ref clocks out of sync"); 161 assert(soft_ref_clock == _soft_ref_timestamp_clock, "soft ref clocks out of sync");
157 162
158 NOT_PRODUCT( 163 NOT_PRODUCT(
159 if (now < _soft_ref_timestamp_clock) { 164 if (now < _soft_ref_timestamp_clock) {
160 warning("time warp: "INT64_FORMAT" to "INT64_FORMAT, 165 warning("time warp: "INT64_FORMAT" to "INT64_FORMAT,
161 _soft_ref_timestamp_clock, now); 166 _soft_ref_timestamp_clock, now);
162 } 167 }
163 ) 168 )
164 // In product mode, protect ourselves from system time being adjusted 169 // The values of now and _soft_ref_timestamp_clock are set using
165 // externally and going backward; see note in the implementation of 170 // javaTimeNanos(), which is guaranteed to be monotonically
166 // GenCollectedHeap::time_since_last_gc() for the right way to fix 171 // non-decreasing provided the underlying platform provides such
167 // this uniformly throughout the VM; see bug-id 4741166. XXX 172 // a time source (and it is bug free).
173 // In product mode, however, protect ourselves from non-monotonicty.
168 if (now > _soft_ref_timestamp_clock) { 174 if (now > _soft_ref_timestamp_clock) {
169 _soft_ref_timestamp_clock = now; 175 _soft_ref_timestamp_clock = now;
170 java_lang_ref_SoftReference::set_clock(now); 176 java_lang_ref_SoftReference::set_clock(now);
171 } 177 }
172 // Else leave clock stalled at its old value until time progresses 178 // Else leave clock stalled at its old value until time progresses