1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/gc_implementation/shared/adaptiveSizePolicy.hpp Sat Dec 01 00:00:00 2007 +0000 1.3 @@ -0,0 +1,492 @@ 1.4 +/* 1.5 + * Copyright 2004-2006 Sun Microsystems, Inc. All Rights Reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 1.24 + * have any questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +// This class keeps statistical information and computes the 1.29 +// size of the heap. 1.30 + 1.31 +// Forward decls 1.32 +class elapsedTimer; 1.33 + 1.34 +class AdaptiveSizePolicy : public CHeapObj { 1.35 + friend class GCAdaptivePolicyCounters; 1.36 + friend class PSGCAdaptivePolicyCounters; 1.37 + friend class CMSGCAdaptivePolicyCounters; 1.38 + protected: 1.39 + 1.40 + enum GCPolicyKind { 1.41 + _gc_adaptive_size_policy, 1.42 + _gc_ps_adaptive_size_policy, 1.43 + _gc_cms_adaptive_size_policy 1.44 + }; 1.45 + virtual GCPolicyKind kind() const { return _gc_adaptive_size_policy; } 1.46 + 1.47 + enum SizePolicyTrueValues { 1.48 + decrease_old_gen_for_throughput_true = -7, 1.49 + decrease_young_gen_for_througput_true = -6, 1.50 + 1.51 + increase_old_gen_for_min_pauses_true = -5, 1.52 + decrease_old_gen_for_min_pauses_true = -4, 1.53 + decrease_young_gen_for_maj_pauses_true = -3, 1.54 + increase_young_gen_for_min_pauses_true = -2, 1.55 + increase_old_gen_for_maj_pauses_true = -1, 1.56 + 1.57 + decrease_young_gen_for_min_pauses_true = 1, 1.58 + decrease_old_gen_for_maj_pauses_true = 2, 1.59 + increase_young_gen_for_maj_pauses_true = 3, 1.60 + 1.61 + increase_old_gen_for_throughput_true = 4, 1.62 + increase_young_gen_for_througput_true = 5, 1.63 + 1.64 + decrease_young_gen_for_footprint_true = 6, 1.65 + decrease_old_gen_for_footprint_true = 7, 1.66 + decide_at_full_gc_true = 8 1.67 + }; 1.68 + 1.69 + // Goal for the fraction of the total time during which application 1.70 + // threads run. 1.71 + const double _throughput_goal; 1.72 + 1.73 + // Last calculated sizes, in bytes, and aligned 1.74 + size_t _eden_size; // calculated eden free space in bytes 1.75 + size_t _promo_size; // calculated cms gen free space in bytes 1.76 + 1.77 + size_t _survivor_size; // calculated survivor size in bytes 1.78 + 1.79 + // This is a hint for the heap: we've detected that gc times 1.80 + // are taking longer than GCTimeLimit allows. 1.81 + bool _gc_time_limit_exceeded; 1.82 + // Use for diagnostics only. If UseGCTimeLimit is false, 1.83 + // this variable is still set. 1.84 + bool _print_gc_time_limit_would_be_exceeded; 1.85 + // Count of consecutive GC that have exceeded the 1.86 + // GC time limit criterion. 1.87 + uint _gc_time_limit_count; 1.88 + 1.89 + // Minor collection timers used to determine both 1.90 + // pause and interval times for collections. 1.91 + static elapsedTimer _minor_timer; 1.92 + 1.93 + // Major collection timers, used to determine both 1.94 + // pause and interval times for collections 1.95 + static elapsedTimer _major_timer; 1.96 + 1.97 + // Time statistics 1.98 + AdaptivePaddedAverage* _avg_minor_pause; 1.99 + AdaptiveWeightedAverage* _avg_minor_interval; 1.100 + AdaptiveWeightedAverage* _avg_minor_gc_cost; 1.101 + 1.102 + AdaptiveWeightedAverage* _avg_major_interval; 1.103 + AdaptiveWeightedAverage* _avg_major_gc_cost; 1.104 + 1.105 + // Footprint statistics 1.106 + AdaptiveWeightedAverage* _avg_young_live; 1.107 + AdaptiveWeightedAverage* _avg_eden_live; 1.108 + AdaptiveWeightedAverage* _avg_old_live; 1.109 + 1.110 + // Statistics for survivor space calculation for young generation 1.111 + AdaptivePaddedAverage* _avg_survived; 1.112 + 1.113 + // Objects that have been directly allocated in the old generation. 1.114 + AdaptivePaddedNoZeroDevAverage* _avg_pretenured; 1.115 + 1.116 + // Variable for estimating the major and minor pause times. 1.117 + // These variables represent linear least-squares fits of 1.118 + // the data. 1.119 + // minor pause time vs. old gen size 1.120 + LinearLeastSquareFit* _minor_pause_old_estimator; 1.121 + // minor pause time vs. young gen size 1.122 + LinearLeastSquareFit* _minor_pause_young_estimator; 1.123 + 1.124 + // Variables for estimating the major and minor collection costs 1.125 + // minor collection time vs. young gen size 1.126 + LinearLeastSquareFit* _minor_collection_estimator; 1.127 + // major collection time vs. cms gen size 1.128 + LinearLeastSquareFit* _major_collection_estimator; 1.129 + 1.130 + // These record the most recent collection times. They 1.131 + // are available as an alternative to using the averages 1.132 + // for making ergonomic decisions. 1.133 + double _latest_minor_mutator_interval_seconds; 1.134 + 1.135 + // Allowed difference between major and minor gc times, used 1.136 + // for computing tenuring_threshold. 1.137 + const double _threshold_tolerance_percent; 1.138 + 1.139 + const double _gc_pause_goal_sec; // goal for maximum gc pause 1.140 + 1.141 + // Flag indicating that the adaptive policy is ready to use 1.142 + bool _young_gen_policy_is_ready; 1.143 + 1.144 + // decrease/increase the young generation for minor pause time 1.145 + int _change_young_gen_for_min_pauses; 1.146 + 1.147 + // decrease/increase the old generation for major pause time 1.148 + int _change_old_gen_for_maj_pauses; 1.149 + 1.150 + // change old geneneration for throughput 1.151 + int _change_old_gen_for_throughput; 1.152 + 1.153 + // change young generation for throughput 1.154 + int _change_young_gen_for_throughput; 1.155 + 1.156 + // Flag indicating that the policy would 1.157 + // increase the tenuring threshold because of the total major gc cost 1.158 + // is greater than the total minor gc cost 1.159 + bool _increment_tenuring_threshold_for_gc_cost; 1.160 + // decrease the tenuring threshold because of the the total minor gc 1.161 + // cost is greater than the total major gc cost 1.162 + bool _decrement_tenuring_threshold_for_gc_cost; 1.163 + // decrease due to survivor size limit 1.164 + bool _decrement_tenuring_threshold_for_survivor_limit; 1.165 + 1.166 + // decrease generation sizes for footprint 1.167 + int _decrease_for_footprint; 1.168 + 1.169 + // Set if the ergonomic decisions were made at a full GC. 1.170 + int _decide_at_full_gc; 1.171 + 1.172 + // Changing the generation sizing depends on the data that is 1.173 + // gathered about the effects of changes on the pause times and 1.174 + // throughput. These variable count the number of data points 1.175 + // gathered. The policy may use these counters as a threshhold 1.176 + // for reliable data. 1.177 + julong _young_gen_change_for_minor_throughput; 1.178 + julong _old_gen_change_for_major_throughput; 1.179 + 1.180 + // Accessors 1.181 + 1.182 + double gc_pause_goal_sec() const { return _gc_pause_goal_sec; } 1.183 + // The value returned is unitless: it's the proportion of time 1.184 + // spent in a particular collection type. 1.185 + // An interval time will be 0.0 if a collection type hasn't occurred yet. 1.186 + // The 1.4.2 implementation put a floor on the values of major_gc_cost 1.187 + // and minor_gc_cost. This was useful because of the way major_gc_cost 1.188 + // and minor_gc_cost was used in calculating the sizes of the generations. 1.189 + // Do not use a floor in this implementation because any finite value 1.190 + // will put a limit on the throughput that can be achieved and any 1.191 + // throughput goal above that limit will drive the generations sizes 1.192 + // to extremes. 1.193 + double major_gc_cost() const { 1.194 + return MAX2(0.0F, _avg_major_gc_cost->average()); 1.195 + } 1.196 + 1.197 + // The value returned is unitless: it's the proportion of time 1.198 + // spent in a particular collection type. 1.199 + // An interval time will be 0.0 if a collection type hasn't occurred yet. 1.200 + // The 1.4.2 implementation put a floor on the values of major_gc_cost 1.201 + // and minor_gc_cost. This was useful because of the way major_gc_cost 1.202 + // and minor_gc_cost was used in calculating the sizes of the generations. 1.203 + // Do not use a floor in this implementation because any finite value 1.204 + // will put a limit on the throughput that can be achieved and any 1.205 + // throughput goal above that limit will drive the generations sizes 1.206 + // to extremes. 1.207 + 1.208 + double minor_gc_cost() const { 1.209 + return MAX2(0.0F, _avg_minor_gc_cost->average()); 1.210 + } 1.211 + 1.212 + // Because we're dealing with averages, gc_cost() can be 1.213 + // larger than 1.0 if just the sum of the minor cost the 1.214 + // the major cost is used. Worse than that is the 1.215 + // fact that the minor cost and the major cost each 1.216 + // tend toward 1.0 in the extreme of high gc costs. 1.217 + // Limit the value of gc_cost to 1.0 so that the mutator 1.218 + // cost stays non-negative. 1.219 + virtual double gc_cost() const { 1.220 + double result = MIN2(1.0, minor_gc_cost() + major_gc_cost()); 1.221 + assert(result >= 0.0, "Both minor and major costs are non-negative"); 1.222 + return result; 1.223 + } 1.224 + 1.225 + // Elapsed time since the last major collection. 1.226 + virtual double time_since_major_gc() const; 1.227 + 1.228 + // Average interval between major collections to be used 1.229 + // in calculating the decaying major gc cost. An overestimate 1.230 + // of this time would be a conservative estimate because 1.231 + // this time is used to decide if the major GC cost 1.232 + // should be decayed (i.e., if the time since the last 1.233 + // major gc is long compared to the time returned here, 1.234 + // then the major GC cost will be decayed). See the 1.235 + // implementations for the specifics. 1.236 + virtual double major_gc_interval_average_for_decay() const { 1.237 + return _avg_major_interval->average(); 1.238 + } 1.239 + 1.240 + // Return the cost of the GC where the major gc cost 1.241 + // has been decayed based on the time since the last 1.242 + // major collection. 1.243 + double decaying_gc_cost() const; 1.244 + 1.245 + // Decay the major gc cost. Use this only for decisions on 1.246 + // whether to adjust, not to determine by how much to adjust. 1.247 + // This approximation is crude and may not be good enough for the 1.248 + // latter. 1.249 + double decaying_major_gc_cost() const; 1.250 + 1.251 + // Return the mutator cost using the decayed 1.252 + // GC cost. 1.253 + double adjusted_mutator_cost() const { 1.254 + double result = 1.0 - decaying_gc_cost(); 1.255 + assert(result >= 0.0, "adjusted mutator cost calculation is incorrect"); 1.256 + return result; 1.257 + } 1.258 + 1.259 + virtual double mutator_cost() const { 1.260 + double result = 1.0 - gc_cost(); 1.261 + assert(result >= 0.0, "mutator cost calculation is incorrect"); 1.262 + return result; 1.263 + } 1.264 + 1.265 + 1.266 + bool young_gen_policy_is_ready() { return _young_gen_policy_is_ready; } 1.267 + 1.268 + void update_minor_pause_young_estimator(double minor_pause_in_ms); 1.269 + virtual void update_minor_pause_old_estimator(double minor_pause_in_ms) { 1.270 + // This is not meaningful for all policies but needs to be present 1.271 + // to use minor_collection_end() in its current form. 1.272 + } 1.273 + 1.274 + virtual size_t eden_increment(size_t cur_eden); 1.275 + virtual size_t eden_increment(size_t cur_eden, uint percent_change); 1.276 + virtual size_t eden_decrement(size_t cur_eden); 1.277 + virtual size_t promo_increment(size_t cur_eden); 1.278 + virtual size_t promo_increment(size_t cur_eden, uint percent_change); 1.279 + virtual size_t promo_decrement(size_t cur_eden); 1.280 + 1.281 + virtual void clear_generation_free_space_flags(); 1.282 + 1.283 + int change_old_gen_for_throughput() const { 1.284 + return _change_old_gen_for_throughput; 1.285 + } 1.286 + void set_change_old_gen_for_throughput(int v) { 1.287 + _change_old_gen_for_throughput = v; 1.288 + } 1.289 + int change_young_gen_for_throughput() const { 1.290 + return _change_young_gen_for_throughput; 1.291 + } 1.292 + void set_change_young_gen_for_throughput(int v) { 1.293 + _change_young_gen_for_throughput = v; 1.294 + } 1.295 + 1.296 + int change_old_gen_for_maj_pauses() const { 1.297 + return _change_old_gen_for_maj_pauses; 1.298 + } 1.299 + void set_change_old_gen_for_maj_pauses(int v) { 1.300 + _change_old_gen_for_maj_pauses = v; 1.301 + } 1.302 + 1.303 + bool decrement_tenuring_threshold_for_gc_cost() const { 1.304 + return _decrement_tenuring_threshold_for_gc_cost; 1.305 + } 1.306 + void set_decrement_tenuring_threshold_for_gc_cost(bool v) { 1.307 + _decrement_tenuring_threshold_for_gc_cost = v; 1.308 + } 1.309 + bool increment_tenuring_threshold_for_gc_cost() const { 1.310 + return _increment_tenuring_threshold_for_gc_cost; 1.311 + } 1.312 + void set_increment_tenuring_threshold_for_gc_cost(bool v) { 1.313 + _increment_tenuring_threshold_for_gc_cost = v; 1.314 + } 1.315 + bool decrement_tenuring_threshold_for_survivor_limit() const { 1.316 + return _decrement_tenuring_threshold_for_survivor_limit; 1.317 + } 1.318 + void set_decrement_tenuring_threshold_for_survivor_limit(bool v) { 1.319 + _decrement_tenuring_threshold_for_survivor_limit = v; 1.320 + } 1.321 + // Return true if the policy suggested a change. 1.322 + bool tenuring_threshold_change() const; 1.323 + 1.324 + public: 1.325 + AdaptiveSizePolicy(size_t init_eden_size, 1.326 + size_t init_promo_size, 1.327 + size_t init_survivor_size, 1.328 + double gc_pause_goal_sec, 1.329 + uint gc_cost_ratio); 1.330 + 1.331 + bool is_gc_cms_adaptive_size_policy() { 1.332 + return kind() == _gc_cms_adaptive_size_policy; 1.333 + } 1.334 + bool is_gc_ps_adaptive_size_policy() { 1.335 + return kind() == _gc_ps_adaptive_size_policy; 1.336 + } 1.337 + 1.338 + AdaptivePaddedAverage* avg_minor_pause() const { return _avg_minor_pause; } 1.339 + AdaptiveWeightedAverage* avg_minor_interval() const { 1.340 + return _avg_minor_interval; 1.341 + } 1.342 + AdaptiveWeightedAverage* avg_minor_gc_cost() const { 1.343 + return _avg_minor_gc_cost; 1.344 + } 1.345 + 1.346 + AdaptiveWeightedAverage* avg_major_gc_cost() const { 1.347 + return _avg_major_gc_cost; 1.348 + } 1.349 + 1.350 + AdaptiveWeightedAverage* avg_young_live() const { return _avg_young_live; } 1.351 + AdaptiveWeightedAverage* avg_eden_live() const { return _avg_eden_live; } 1.352 + AdaptiveWeightedAverage* avg_old_live() const { return _avg_old_live; } 1.353 + 1.354 + AdaptivePaddedAverage* avg_survived() const { return _avg_survived; } 1.355 + AdaptivePaddedNoZeroDevAverage* avg_pretenured() { return _avg_pretenured; } 1.356 + 1.357 + // Methods indicating events of interest to the adaptive size policy, 1.358 + // called by GC algorithms. It is the responsibility of users of this 1.359 + // policy to call these methods at the correct times! 1.360 + virtual void minor_collection_begin(); 1.361 + virtual void minor_collection_end(GCCause::Cause gc_cause); 1.362 + virtual LinearLeastSquareFit* minor_pause_old_estimator() const { 1.363 + return _minor_pause_old_estimator; 1.364 + } 1.365 + 1.366 + LinearLeastSquareFit* minor_pause_young_estimator() { 1.367 + return _minor_pause_young_estimator; 1.368 + } 1.369 + LinearLeastSquareFit* minor_collection_estimator() { 1.370 + return _minor_collection_estimator; 1.371 + } 1.372 + 1.373 + LinearLeastSquareFit* major_collection_estimator() { 1.374 + return _major_collection_estimator; 1.375 + } 1.376 + 1.377 + float minor_pause_young_slope() { 1.378 + return _minor_pause_young_estimator->slope(); 1.379 + } 1.380 + 1.381 + float minor_collection_slope() { return _minor_collection_estimator->slope();} 1.382 + float major_collection_slope() { return _major_collection_estimator->slope();} 1.383 + 1.384 + float minor_pause_old_slope() { 1.385 + return _minor_pause_old_estimator->slope(); 1.386 + } 1.387 + 1.388 + void set_eden_size(size_t new_size) { 1.389 + _eden_size = new_size; 1.390 + } 1.391 + void set_survivor_size(size_t new_size) { 1.392 + _survivor_size = new_size; 1.393 + } 1.394 + 1.395 + size_t calculated_eden_size_in_bytes() const { 1.396 + return _eden_size; 1.397 + } 1.398 + 1.399 + size_t calculated_promo_size_in_bytes() const { 1.400 + return _promo_size; 1.401 + } 1.402 + 1.403 + size_t calculated_survivor_size_in_bytes() const { 1.404 + return _survivor_size; 1.405 + } 1.406 + 1.407 + // This is a hint for the heap: we've detected that gc times 1.408 + // are taking longer than GCTimeLimit allows. 1.409 + // Most heaps will choose to throw an OutOfMemoryError when 1.410 + // this occurs but it is up to the heap to request this information 1.411 + // of the policy 1.412 + bool gc_time_limit_exceeded() { 1.413 + return _gc_time_limit_exceeded; 1.414 + } 1.415 + void set_gc_time_limit_exceeded(bool v) { 1.416 + _gc_time_limit_exceeded = v; 1.417 + } 1.418 + bool print_gc_time_limit_would_be_exceeded() { 1.419 + return _print_gc_time_limit_would_be_exceeded; 1.420 + } 1.421 + void set_print_gc_time_limit_would_be_exceeded(bool v) { 1.422 + _print_gc_time_limit_would_be_exceeded = v; 1.423 + } 1.424 + 1.425 + uint gc_time_limit_count() { return _gc_time_limit_count; } 1.426 + void reset_gc_time_limit_count() { _gc_time_limit_count = 0; } 1.427 + void inc_gc_time_limit_count() { _gc_time_limit_count++; } 1.428 + // accessors for flags recording the decisions to resize the 1.429 + // generations to meet the pause goal. 1.430 + 1.431 + int change_young_gen_for_min_pauses() const { 1.432 + return _change_young_gen_for_min_pauses; 1.433 + } 1.434 + void set_change_young_gen_for_min_pauses(int v) { 1.435 + _change_young_gen_for_min_pauses = v; 1.436 + } 1.437 + void set_decrease_for_footprint(int v) { _decrease_for_footprint = v; } 1.438 + int decrease_for_footprint() const { return _decrease_for_footprint; } 1.439 + int decide_at_full_gc() { return _decide_at_full_gc; } 1.440 + void set_decide_at_full_gc(int v) { _decide_at_full_gc = v; } 1.441 + 1.442 + // Printing support 1.443 + virtual bool print_adaptive_size_policy_on(outputStream* st) const; 1.444 + bool print_adaptive_size_policy_on(outputStream* st, int 1.445 + tenuring_threshold) const; 1.446 +}; 1.447 + 1.448 +// Class that can be used to print information about the 1.449 +// adaptive size policy at intervals specified by 1.450 +// AdaptiveSizePolicyOutputInterval. Only print information 1.451 +// if an adaptive size policy is in use. 1.452 +class AdaptiveSizePolicyOutput : StackObj { 1.453 + AdaptiveSizePolicy* _size_policy; 1.454 + bool _do_print; 1.455 + bool print_test(uint count) { 1.456 + // A count of zero is a special value that indicates that the 1.457 + // interval test should be ignored. An interval is of zero is 1.458 + // a special value that indicates that the interval test should 1.459 + // always fail (never do the print based on the interval test). 1.460 + return PrintGCDetails && 1.461 + UseAdaptiveSizePolicy && 1.462 + (UseParallelGC || UseConcMarkSweepGC) && 1.463 + (AdaptiveSizePolicyOutputInterval > 0) && 1.464 + ((count == 0) || 1.465 + ((count % AdaptiveSizePolicyOutputInterval) == 0)); 1.466 + } 1.467 + public: 1.468 + // The special value of a zero count can be used to ignore 1.469 + // the count test. 1.470 + AdaptiveSizePolicyOutput(uint count) { 1.471 + if (UseAdaptiveSizePolicy && (AdaptiveSizePolicyOutputInterval > 0)) { 1.472 + CollectedHeap* heap = Universe::heap(); 1.473 + _size_policy = heap->size_policy(); 1.474 + _do_print = print_test(count); 1.475 + } else { 1.476 + _size_policy = NULL; 1.477 + _do_print = false; 1.478 + } 1.479 + } 1.480 + AdaptiveSizePolicyOutput(AdaptiveSizePolicy* size_policy, 1.481 + uint count) : 1.482 + _size_policy(size_policy) { 1.483 + if (UseAdaptiveSizePolicy && (AdaptiveSizePolicyOutputInterval > 0)) { 1.484 + _do_print = print_test(count); 1.485 + } else { 1.486 + _do_print = false; 1.487 + } 1.488 + } 1.489 + ~AdaptiveSizePolicyOutput() { 1.490 + if (_do_print) { 1.491 + assert(UseAdaptiveSizePolicy, "Should not be in use"); 1.492 + _size_policy->print_adaptive_size_policy_on(gclog_or_tty); 1.493 + } 1.494 + } 1.495 +};