1 /*
   2  * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2020 SAP SE. 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_MEMORY_METASPACE_COMMITLIMITER_HPP
  27 #define SHARE_MEMORY_METASPACE_COMMITLIMITER_HPP
  28 
  29 #include "memory/allocation.hpp"
  30 #include "memory/metaspace/counter.hpp"
  31 
  32 namespace metaspace {
  33 
  34 // The CommitLimiter encapsulates a limit we may want to impose on how much
  35 //  memory can be committed. This is a matter of separation of concerns:
  36 //
  37 // In metaspace, we have two limits to committing memory: the absolute limit,
  38 //  MaxMetaspaceSize; and the GC threshold. In both cases an allocation should
  39 //  fail if it would require committing memory and hit one of these limits.
  40 //
  41 // However, the actual Metaspace allocator is a generic one and this
  42 //  GC- and classloading specific logic should be kept separate. Therefore
  43 //  it is hidden inside this interface.
  44 //
  45 // This allows us to:
  46 //  - more easily write tests for metaspace, by providing a different implementation
  47 //    of the commit limiter, thus keeping test logic separate from VM state.
  48 //  - (potentially) use the metaspace for things other than class metadata,
  49 //    where different commit rules would apply.
  50 //
  51 class CommitLimiter : public CHeapObj<mtMetaspace> {
  52 
  53   // Counts total words committed for metaspace
  54   SizeCounter _cnt;
  55 
  56   // Purely for testing purposes: cap, in words.
  57   const size_t _cap;
  58 
  59 public:
  60 
  61   // Create a commit limiter. This is only useful for testing, with a cap != 0,
  62   // since normal code should use the global commit limiter.
  63   // If cap != 0 (word size), the cap replaces the internal logic of limiting.
  64   CommitLimiter(size_t cap = 0) : _cnt(), _cap(cap) {}
  65 
  66   // Returns the size, in words, by which we may expand the metaspace committed area without:
  67   // - _cap == 0: hitting GC threshold or the MaxMetaspaceSize
  68   // - _cap > 0: hitting cap (this is just for testing purposes)
  69   size_t possible_expansion_words() const;
  70 
  71   void increase_committed(size_t word_size)   { _cnt.increment_by(word_size); }
  72   void decrease_committed(size_t word_size)   { _cnt.decrement_by(word_size); }
  73 
  74   size_t committed_words() const              { return _cnt.get(); }
  75   size_t cap() const                          { return _cap; }
  76 
  77   // Returns the global metaspace commit counter
  78   static CommitLimiter* globalLimiter();
  79 
  80 };
  81 
  82 } // namespace metaspace
  83 
  84 #endif // SHARE_MEMORY_METASPACE_COMMITLIMITER_HPP