adaptiveSizePolicy.hpp revision 9727:f944761a3ce3
1/*
2 * Copyright (c) 2004, 2015, 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 */
24
25#ifndef SHARE_VM_GC_SHARED_ADAPTIVESIZEPOLICY_HPP
26#define SHARE_VM_GC_SHARED_ADAPTIVESIZEPOLICY_HPP
27
28#include "gc/shared/collectedHeap.hpp"
29#include "gc/shared/gcCause.hpp"
30#include "gc/shared/gcUtil.hpp"
31#include "logging/log.hpp"
32#include "memory/allocation.hpp"
33#include "memory/universe.hpp"
34
35// This class keeps statistical information and computes the
36// size of the heap.
37
38// Forward decls
39class elapsedTimer;
40class CollectorPolicy;
41
42class AdaptiveSizePolicy : public CHeapObj<mtGC> {
43 friend class GCAdaptivePolicyCounters;
44 friend class PSGCAdaptivePolicyCounters;
45 friend class CMSGCAdaptivePolicyCounters;
46 protected:
47
48  enum GCPolicyKind {
49    _gc_adaptive_size_policy,
50    _gc_ps_adaptive_size_policy,
51    _gc_cms_adaptive_size_policy
52  };
53  virtual GCPolicyKind kind() const { return _gc_adaptive_size_policy; }
54
55  enum SizePolicyTrueValues {
56    decrease_old_gen_for_throughput_true = -7,
57    decrease_young_gen_for_througput_true = -6,
58
59    increase_old_gen_for_min_pauses_true = -5,
60    decrease_old_gen_for_min_pauses_true = -4,
61    decrease_young_gen_for_maj_pauses_true = -3,
62    increase_young_gen_for_min_pauses_true = -2,
63    increase_old_gen_for_maj_pauses_true = -1,
64
65    decrease_young_gen_for_min_pauses_true = 1,
66    decrease_old_gen_for_maj_pauses_true = 2,
67    increase_young_gen_for_maj_pauses_true = 3,
68
69    increase_old_gen_for_throughput_true = 4,
70    increase_young_gen_for_througput_true = 5,
71
72    decrease_young_gen_for_footprint_true = 6,
73    decrease_old_gen_for_footprint_true = 7,
74    decide_at_full_gc_true = 8
75  };
76
77  // Goal for the fraction of the total time during which application
78  // threads run
79  const double _throughput_goal;
80
81  // Last calculated sizes, in bytes, and aligned
82  size_t _eden_size;        // calculated eden free space in bytes
83  size_t _promo_size;       // calculated cms gen free space in bytes
84
85  size_t _survivor_size;    // calculated survivor size in bytes
86
87  // This is a hint for the heap:  we've detected that GC times
88  // are taking longer than GCTimeLimit allows.
89  bool _gc_overhead_limit_exceeded;
90  // Use for diagnostics only.  If UseGCOverheadLimit is false,
91  // this variable is still set.
92  bool _print_gc_overhead_limit_would_be_exceeded;
93  // Count of consecutive GC that have exceeded the
94  // GC time limit criterion
95  uint _gc_overhead_limit_count;
96  // This flag signals that GCTimeLimit is being exceeded
97  // but may not have done so for the required number of consecutive
98  // collections
99
100  // Minor collection timers used to determine both
101  // pause and interval times for collections
102  static elapsedTimer _minor_timer;
103
104  // Major collection timers, used to determine both
105  // pause and interval times for collections
106  static elapsedTimer _major_timer;
107
108  // Time statistics
109  AdaptivePaddedAverage*   _avg_minor_pause;
110  AdaptiveWeightedAverage* _avg_minor_interval;
111  AdaptiveWeightedAverage* _avg_minor_gc_cost;
112
113  AdaptiveWeightedAverage* _avg_major_interval;
114  AdaptiveWeightedAverage* _avg_major_gc_cost;
115
116  // Footprint statistics
117  AdaptiveWeightedAverage* _avg_young_live;
118  AdaptiveWeightedAverage* _avg_eden_live;
119  AdaptiveWeightedAverage* _avg_old_live;
120
121  // Statistics for survivor space calculation for young generation
122  AdaptivePaddedAverage*   _avg_survived;
123
124  // Objects that have been directly allocated in the old generation
125  AdaptivePaddedNoZeroDevAverage*   _avg_pretenured;
126
127  // Variable for estimating the major and minor pause times.
128  // These variables represent linear least-squares fits of
129  // the data.
130  //   minor pause time vs. old gen size
131  LinearLeastSquareFit* _minor_pause_old_estimator;
132  //   minor pause time vs. young gen size
133  LinearLeastSquareFit* _minor_pause_young_estimator;
134
135  // Variables for estimating the major and minor collection costs
136  //   minor collection time vs. young gen size
137  LinearLeastSquareFit* _minor_collection_estimator;
138  //   major collection time vs. cms gen size
139  LinearLeastSquareFit* _major_collection_estimator;
140
141  // These record the most recent collection times.  They
142  // are available as an alternative to using the averages
143  // for making ergonomic decisions.
144  double _latest_minor_mutator_interval_seconds;
145
146  // Allowed difference between major and minor GC times, used
147  // for computing tenuring_threshold
148  const double _threshold_tolerance_percent;
149
150  const double _gc_pause_goal_sec; // Goal for maximum GC pause
151
152  // Flag indicating that the adaptive policy is ready to use
153  bool _young_gen_policy_is_ready;
154
155  // Decrease/increase the young generation for minor pause time
156  int _change_young_gen_for_min_pauses;
157
158  // Decrease/increase the old generation for major pause time
159  int _change_old_gen_for_maj_pauses;
160
161  //   change old generation for throughput
162  int _change_old_gen_for_throughput;
163
164  //   change young generation for throughput
165  int _change_young_gen_for_throughput;
166
167  // Flag indicating that the policy would
168  //   increase the tenuring threshold because of the total major GC cost
169  //   is greater than the total minor GC cost
170  bool _increment_tenuring_threshold_for_gc_cost;
171  //   decrease the tenuring threshold because of the the total minor GC
172  //   cost is greater than the total major GC cost
173  bool _decrement_tenuring_threshold_for_gc_cost;
174  //   decrease due to survivor size limit
175  bool _decrement_tenuring_threshold_for_survivor_limit;
176
177  //   decrease generation sizes for footprint
178  int _decrease_for_footprint;
179
180  // Set if the ergonomic decisions were made at a full GC.
181  int _decide_at_full_gc;
182
183  // Changing the generation sizing depends on the data that is
184  // gathered about the effects of changes on the pause times and
185  // throughput.  These variable count the number of data points
186  // gathered.  The policy may use these counters as a threshold
187  // for reliable data.
188  julong _young_gen_change_for_minor_throughput;
189  julong _old_gen_change_for_major_throughput;
190
191  static const uint GCWorkersPerJavaThread  = 2;
192
193  // Accessors
194
195  double gc_pause_goal_sec() const { return _gc_pause_goal_sec; }
196  // The value returned is unitless:  it's the proportion of time
197  // spent in a particular collection type.
198  // An interval time will be 0.0 if a collection type hasn't occurred yet.
199  // The 1.4.2 implementation put a floor on the values of major_gc_cost
200  // and minor_gc_cost.  This was useful because of the way major_gc_cost
201  // and minor_gc_cost was used in calculating the sizes of the generations.
202  // Do not use a floor in this implementation because any finite value
203  // will put a limit on the throughput that can be achieved and any
204  // throughput goal above that limit will drive the generations sizes
205  // to extremes.
206  double major_gc_cost() const {
207    return MAX2(0.0F, _avg_major_gc_cost->average());
208  }
209
210  // The value returned is unitless:  it's the proportion of time
211  // spent in a particular collection type.
212  // An interval time will be 0.0 if a collection type hasn't occurred yet.
213  // The 1.4.2 implementation put a floor on the values of major_gc_cost
214  // and minor_gc_cost.  This was useful because of the way major_gc_cost
215  // and minor_gc_cost was used in calculating the sizes of the generations.
216  // Do not use a floor in this implementation because any finite value
217  // will put a limit on the throughput that can be achieved and any
218  // throughput goal above that limit will drive the generations sizes
219  // to extremes.
220
221  double minor_gc_cost() const {
222    return MAX2(0.0F, _avg_minor_gc_cost->average());
223  }
224
225  // Because we're dealing with averages, gc_cost() can be
226  // larger than 1.0 if just the sum of the minor cost the
227  // the major cost is used.  Worse than that is the
228  // fact that the minor cost and the major cost each
229  // tend toward 1.0 in the extreme of high GC costs.
230  // Limit the value of gc_cost to 1.0 so that the mutator
231  // cost stays non-negative.
232  virtual double gc_cost() const {
233    double result = MIN2(1.0, minor_gc_cost() + major_gc_cost());
234    assert(result >= 0.0, "Both minor and major costs are non-negative");
235    return result;
236  }
237
238  // Elapsed time since the last major collection.
239  virtual double time_since_major_gc() const;
240
241  // Average interval between major collections to be used
242  // in calculating the decaying major GC cost.  An overestimate
243  // of this time would be a conservative estimate because
244  // this time is used to decide if the major GC cost
245  // should be decayed (i.e., if the time since the last
246  // major GC is long compared to the time returned here,
247  // then the major GC cost will be decayed).  See the
248  // implementations for the specifics.
249  virtual double major_gc_interval_average_for_decay() const {
250    return _avg_major_interval->average();
251  }
252
253  // Return the cost of the GC where the major GC cost
254  // has been decayed based on the time since the last
255  // major collection.
256  double decaying_gc_cost() const;
257
258  // Decay the major GC cost.  Use this only for decisions on
259  // whether to adjust, not to determine by how much to adjust.
260  // This approximation is crude and may not be good enough for the
261  // latter.
262  double decaying_major_gc_cost() const;
263
264  // Return the mutator cost using the decayed
265  // GC cost.
266  double adjusted_mutator_cost() const {
267    double result = 1.0 - decaying_gc_cost();
268    assert(result >= 0.0, "adjusted mutator cost calculation is incorrect");
269    return result;
270  }
271
272  virtual double mutator_cost() const {
273    double result = 1.0 - gc_cost();
274    assert(result >= 0.0, "mutator cost calculation is incorrect");
275    return result;
276  }
277
278
279  bool young_gen_policy_is_ready() { return _young_gen_policy_is_ready; }
280
281  void update_minor_pause_young_estimator(double minor_pause_in_ms);
282  virtual void update_minor_pause_old_estimator(double minor_pause_in_ms) {
283    // This is not meaningful for all policies but needs to be present
284    // to use minor_collection_end() in its current form.
285  }
286
287  virtual size_t eden_increment(size_t cur_eden);
288  virtual size_t eden_increment(size_t cur_eden, uint percent_change);
289  virtual size_t eden_decrement(size_t cur_eden);
290  virtual size_t promo_increment(size_t cur_eden);
291  virtual size_t promo_increment(size_t cur_eden, uint percent_change);
292  virtual size_t promo_decrement(size_t cur_eden);
293
294  virtual void clear_generation_free_space_flags();
295
296  int change_old_gen_for_throughput() const {
297    return _change_old_gen_for_throughput;
298  }
299  void set_change_old_gen_for_throughput(int v) {
300    _change_old_gen_for_throughput = v;
301  }
302  int change_young_gen_for_throughput() const {
303    return _change_young_gen_for_throughput;
304  }
305  void set_change_young_gen_for_throughput(int v) {
306    _change_young_gen_for_throughput = v;
307  }
308
309  int change_old_gen_for_maj_pauses() const {
310    return _change_old_gen_for_maj_pauses;
311  }
312  void set_change_old_gen_for_maj_pauses(int v) {
313    _change_old_gen_for_maj_pauses = v;
314  }
315
316  bool decrement_tenuring_threshold_for_gc_cost() const {
317    return _decrement_tenuring_threshold_for_gc_cost;
318  }
319  void set_decrement_tenuring_threshold_for_gc_cost(bool v) {
320    _decrement_tenuring_threshold_for_gc_cost = v;
321  }
322  bool increment_tenuring_threshold_for_gc_cost() const {
323    return _increment_tenuring_threshold_for_gc_cost;
324  }
325  void set_increment_tenuring_threshold_for_gc_cost(bool v) {
326    _increment_tenuring_threshold_for_gc_cost = v;
327  }
328  bool decrement_tenuring_threshold_for_survivor_limit() const {
329    return _decrement_tenuring_threshold_for_survivor_limit;
330  }
331  void set_decrement_tenuring_threshold_for_survivor_limit(bool v) {
332    _decrement_tenuring_threshold_for_survivor_limit = v;
333  }
334  // Return true if the policy suggested a change.
335  bool tenuring_threshold_change() const;
336
337  static bool _debug_perturbation;
338
339 public:
340  AdaptiveSizePolicy(size_t init_eden_size,
341                     size_t init_promo_size,
342                     size_t init_survivor_size,
343                     double gc_pause_goal_sec,
344                     uint gc_cost_ratio);
345
346  // Return number default  GC threads to use in the next GC.
347  static uint calc_default_active_workers(uintx total_workers,
348                                          const uintx min_workers,
349                                          uintx active_workers,
350                                          uintx application_workers);
351
352  // Return number of GC threads to use in the next GC.
353  // This is called sparingly so as not to change the
354  // number of GC workers gratuitously.
355  //   For ParNew collections
356  //   For PS scavenge and ParOld collections
357  //   For G1 evacuation pauses (subject to update)
358  // Other collection phases inherit the number of
359  // GC workers from the calls above.  For example,
360  // a CMS parallel remark uses the same number of GC
361  // workers as the most recent ParNew collection.
362  static uint calc_active_workers(uintx total_workers,
363                                  uintx active_workers,
364                                  uintx application_workers);
365
366  // Return number of GC threads to use in the next concurrent GC phase.
367  static uint calc_active_conc_workers(uintx total_workers,
368                                       uintx active_workers,
369                                       uintx application_workers);
370
371  bool is_gc_cms_adaptive_size_policy() {
372    return kind() == _gc_cms_adaptive_size_policy;
373  }
374  bool is_gc_ps_adaptive_size_policy() {
375    return kind() == _gc_ps_adaptive_size_policy;
376  }
377
378  AdaptivePaddedAverage*   avg_minor_pause() const { return _avg_minor_pause; }
379  AdaptiveWeightedAverage* avg_minor_interval() const {
380    return _avg_minor_interval;
381  }
382  AdaptiveWeightedAverage* avg_minor_gc_cost() const {
383    return _avg_minor_gc_cost;
384  }
385
386  AdaptiveWeightedAverage* avg_major_gc_cost() const {
387    return _avg_major_gc_cost;
388  }
389
390  AdaptiveWeightedAverage* avg_young_live() const { return _avg_young_live; }
391  AdaptiveWeightedAverage* avg_eden_live() const { return _avg_eden_live; }
392  AdaptiveWeightedAverage* avg_old_live() const { return _avg_old_live; }
393
394  AdaptivePaddedAverage*  avg_survived() const { return _avg_survived; }
395  AdaptivePaddedNoZeroDevAverage*  avg_pretenured() { return _avg_pretenured; }
396
397  // Methods indicating events of interest to the adaptive size policy,
398  // called by GC algorithms. It is the responsibility of users of this
399  // policy to call these methods at the correct times!
400  virtual void minor_collection_begin();
401  virtual void minor_collection_end(GCCause::Cause gc_cause);
402  virtual LinearLeastSquareFit* minor_pause_old_estimator() const {
403    return _minor_pause_old_estimator;
404  }
405
406  LinearLeastSquareFit* minor_pause_young_estimator() {
407    return _minor_pause_young_estimator;
408  }
409  LinearLeastSquareFit* minor_collection_estimator() {
410    return _minor_collection_estimator;
411  }
412
413  LinearLeastSquareFit* major_collection_estimator() {
414    return _major_collection_estimator;
415  }
416
417  float minor_pause_young_slope() {
418    return _minor_pause_young_estimator->slope();
419  }
420
421  float minor_collection_slope() { return _minor_collection_estimator->slope();}
422  float major_collection_slope() { return _major_collection_estimator->slope();}
423
424  float minor_pause_old_slope() {
425    return _minor_pause_old_estimator->slope();
426  }
427
428  void set_eden_size(size_t new_size) {
429    _eden_size = new_size;
430  }
431  void set_survivor_size(size_t new_size) {
432    _survivor_size = new_size;
433  }
434
435  size_t calculated_eden_size_in_bytes() const {
436    return _eden_size;
437  }
438
439  size_t calculated_promo_size_in_bytes() const {
440    return _promo_size;
441  }
442
443  size_t calculated_survivor_size_in_bytes() const {
444    return _survivor_size;
445  }
446
447  // This is a hint for the heap:  we've detected that gc times
448  // are taking longer than GCTimeLimit allows.
449  // Most heaps will choose to throw an OutOfMemoryError when
450  // this occurs but it is up to the heap to request this information
451  // of the policy
452  bool gc_overhead_limit_exceeded() {
453    return _gc_overhead_limit_exceeded;
454  }
455  void set_gc_overhead_limit_exceeded(bool v) {
456    _gc_overhead_limit_exceeded = v;
457  }
458
459  // Tests conditions indicate the GC overhead limit is being approached.
460  bool gc_overhead_limit_near() {
461    return gc_overhead_limit_count() >=
462        (AdaptiveSizePolicyGCTimeLimitThreshold - 1);
463  }
464  uint gc_overhead_limit_count() { return _gc_overhead_limit_count; }
465  void reset_gc_overhead_limit_count() { _gc_overhead_limit_count = 0; }
466  void inc_gc_overhead_limit_count() { _gc_overhead_limit_count++; }
467  // accessors for flags recording the decisions to resize the
468  // generations to meet the pause goal.
469
470  int change_young_gen_for_min_pauses() const {
471    return _change_young_gen_for_min_pauses;
472  }
473  void set_change_young_gen_for_min_pauses(int v) {
474    _change_young_gen_for_min_pauses = v;
475  }
476  void set_decrease_for_footprint(int v) { _decrease_for_footprint = v; }
477  int decrease_for_footprint() const { return _decrease_for_footprint; }
478  int decide_at_full_gc() { return _decide_at_full_gc; }
479  void set_decide_at_full_gc(int v) { _decide_at_full_gc = v; }
480
481  // Check the conditions for an out-of-memory due to excessive GC time.
482  // Set _gc_overhead_limit_exceeded if all the conditions have been met.
483  void check_gc_overhead_limit(size_t young_live,
484                               size_t eden_live,
485                               size_t max_old_gen_size,
486                               size_t max_eden_size,
487                               bool   is_full_gc,
488                               GCCause::Cause gc_cause,
489                               CollectorPolicy* collector_policy);
490
491  static bool should_update_promo_stats(GCCause::Cause cause) {
492    return ((GCCause::is_user_requested_gc(cause)  &&
493               UseAdaptiveSizePolicyWithSystemGC) ||
494            GCCause::is_tenured_allocation_failure_gc(cause));
495  }
496
497  static bool should_update_eden_stats(GCCause::Cause cause) {
498    return ((GCCause::is_user_requested_gc(cause)  &&
499               UseAdaptiveSizePolicyWithSystemGC) ||
500            GCCause::is_allocation_failure_gc(cause));
501  }
502
503  // Printing support
504  virtual bool print() const;
505  void print_tenuring_threshold(uint new_tenuring_threshold) const;
506};
507
508// Class that can be used to print information about the
509// adaptive size policy at intervals specified by
510// AdaptiveSizePolicyOutputInterval.  Only print information
511// if an adaptive size policy is in use.
512class AdaptiveSizePolicyOutput : StackObj {
513  static bool enabled() {
514    return UseParallelGC &&
515           UseAdaptiveSizePolicy &&
516           log_is_enabled(Debug, gc, ergo);
517  }
518 public:
519  static void print() {
520    if (enabled()) {
521      Universe::heap()->size_policy()->print();
522    }
523  }
524
525  static void print(AdaptiveSizePolicy* size_policy, uint count) {
526    bool do_print =
527        enabled() &&
528        (AdaptiveSizePolicyOutputInterval > 0) &&
529        (count % AdaptiveSizePolicyOutputInterval) == 0;
530
531    if (do_print) {
532      size_policy->print();
533    }
534  }
535};
536
537#endif // SHARE_VM_GC_SHARED_ADAPTIVESIZEPOLICY_HPP
538