1 /*
   2  * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2019, Google and/or its affiliates. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  25 
  26 #ifndef SHARE_GC_SHARED_GCOVERHEADCHECKER_HPP
  27 #define SHARE_GC_SHARED_GCOVERHEADCHECKER_HPP
  28 
  29 #include "memory/allocation.hpp"
  30 #include "gc/shared/gcCause.hpp"
  31 
  32 class SoftRefPolicy;
  33 
  34 class GCOverheadTester: public StackObj {
  35 public:
  36   virtual bool is_exceeded() = 0;
  37 };
  38 
  39 class GCOverheadChecker: public CHeapObj<mtGC> {
  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   GCOverheadChecker();
  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 >= (GCOverheadLimitThreshold - 1);
  71   }
  72   void reset_gc_overhead_limit_count() {
  73     _gc_overhead_limit_count = 0;
  74   }
  75 
  76   // Check the conditions for an out-of-memory due to excessive GC time.
  77   // Set _gc_overhead_limit_exceeded if all the conditions have been met.
  78   void check_gc_overhead_limit(GCOverheadTester* time_overhead,
  79                                GCOverheadTester* space_overhead,
  80                                bool is_full_gc,
  81                                GCCause::Cause gc_cause,
  82                                SoftRefPolicy* soft_ref_policy);
  83 };
  84 
  85 #endif // SHARE_GC_SHARED_GCOVERHEADCHECKER_HPP