1 /*
   2  * Copyright (c) 2018, 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_GC_SHARED_OVERHEADCHECKER_HPP
  26 #define SHARE_GC_SHARED_OVERHEADCHECKER_HPP
  27 
  28 #include "memory/allocation.hpp"
  29 #include "gc/shared/gcCause.hpp"
  30 
  31 class SoftRefPolicy;
  32 
  33 class OverheadTester: public StackObj {
  34  public:
  35   virtual bool is_exceeded() = 0;
  36 };
  37 
  38 class OverheadChecker: public CHeapObj<mtGC> {
  39  private:
  40   // This is a hint for the heap:  we've detected that GC times
  41   // are taking longer than GCTimeLimit allows.
  42   bool _gc_overhead_limit_exceeded;
  43   // Use for diagnostics only.  If UseGCOverheadLimit is false,
  44   // this variable is still set.
  45   bool _print_gc_overhead_limit_would_be_exceeded;
  46   // Count of consecutive GC that have exceeded the
  47   // GC time limit criterion
  48   uint _gc_overhead_limit_count;
  49   // This flag signals that GCTimeLimit is being exceeded
  50   // but may not have done so for the required number of consecutive
  51   // collections
  52 
  53  public:
  54   OverheadChecker();
  55 
  56   // This is a hint for the heap:  we've detected that gc times
  57   // are taking longer than GCTimeLimit allows.
  58   // Most heaps will choose to throw an OutOfMemoryError when
  59   // this occurs but it is up to the heap to request this information
  60   // of the policy
  61   bool gc_overhead_limit_exceeded() {
  62     return _gc_overhead_limit_exceeded;
  63   }
  64   void set_gc_overhead_limit_exceeded(bool v) {
  65     _gc_overhead_limit_exceeded = v;
  66   }
  67 
  68   // Tests conditions indicate the GC overhead limit is being approached.
  69   bool gc_overhead_limit_near() {
  70     return _gc_overhead_limit_count >=
  71            (AdaptiveSizePolicyGCTimeLimitThreshold - 1);
  72   }
  73   void reset_gc_overhead_limit_count() {
  74     _gc_overhead_limit_count = 0;
  75   }
  76 
  77   // Check the conditions for an out-of-memory due to excessive GC time.
  78   // Set _gc_overhead_limit_exceeded if all the conditions have been met.
  79   void check_gc_overhead_limit(OverheadTester* time_overhead,
  80                                OverheadTester* space_overhead,
  81                                bool is_full_gc,
  82                                GCCause::Cause gc_cause,
  83                                SoftRefPolicy* soft_ref_policy);
  84 };
  85 
  86 #endif // SHARE_GC_SHARED_OVERHEADCHECKER_HPP