1 /*
   2  * Copyright (c) 2015, 2020, 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 #ifndef SHARE_GC_Z_ZPAGEALLOCATOR_HPP
  25 #define SHARE_GC_Z_ZPAGEALLOCATOR_HPP
  26 
  27 #include "gc/z/zAllocationFlags.hpp"
  28 #include "gc/z/zList.hpp"
  29 #include "gc/z/zLock.hpp"
  30 #include "gc/z/zPageCache.hpp"
  31 #include "gc/z/zPhysicalMemory.hpp"
  32 #include "gc/z/zSafeDelete.hpp"
  33 #include "gc/z/zVirtualMemory.hpp"
  34 
  35 class ZPageAllocation;
  36 class ZWorkers;
  37 
  38 class ZPageAllocator {
  39   friend class VMStructs;
  40 
  41 private:
  42   ZLock                      _lock;
  43   ZPageCache                 _cache;
  44   ZVirtualMemoryManager      _virtual;
  45   ZPhysicalMemoryManager     _physical;
  46   const size_t               _min_capacity;
  47   const size_t               _max_capacity;
  48   const size_t               _max_reserve;
  49   volatile size_t            _current_max_capacity;
  50   volatile size_t            _capacity;
  51   volatile size_t            _used;
  52   size_t                     _used_high;
  53   size_t                     _used_low;
  54   size_t                     _allocated;
  55   ssize_t                    _reclaimed;
  56   ZList<ZPageAllocation>     _stalled;
  57   ZList<ZPageAllocation>     _satisfied;
  58   mutable ZSafeDelete<ZPage> _safe_delete;
  59   volatile bool              _uncommit;
  60   bool                       _initialized;
  61 
  62   bool prime_cache(ZWorkers* workers, size_t size);
  63 
  64   size_t increase_capacity(size_t size);
  65   void decrease_capacity(size_t size, bool set_max_capacity);
  66 
  67   void increase_used(size_t size, bool allocation, bool relocation);
  68   void decrease_used(size_t size, bool free, bool reclaimed);
  69 
  70   bool commit_page(ZPage* page);
  71   void uncommit_page(ZPage* page);
  72 
  73   bool map_page(const ZPage* page) const;
  74   void unmap_page(const ZPage* page) const;
  75 
  76   void destroy_page(ZPage* page);
  77 
  78   bool is_alloc_allowed(size_t size, bool no_reserve) const;
  79   bool is_alloc_allowed_from_cache(size_t size, bool no_reserve) const;
  80 
  81   bool alloc_page_common_inner(uint8_t type, size_t size, bool no_reserve, ZList<ZPage>* pages);
  82   bool alloc_page_common(ZPageAllocation* allocation);
  83   bool alloc_page_stall(ZPageAllocation* allocation);
  84   bool alloc_page_stage0(ZPageAllocation* allocation);
  85   ZPage* alloc_page_create(ZPageAllocation* allocation);
  86   ZPage* alloc_page_stage1(ZPageAllocation* allocation);
  87   void alloc_page_failed(ZPageAllocation* allocation);
  88 
  89   void satisfy_stalled();
  90 
  91   size_t uncommit_inner(uint64_t delay, uint64_t* timeout);
  92 
  93 public:
  94   ZPageAllocator(ZWorkers* workers,
  95                  size_t min_capacity,
  96                  size_t initial_capacity,
  97                  size_t max_capacity,
  98                  size_t max_reserve);
  99 
 100   bool is_initialized() const;
 101 
 102   size_t min_capacity() const;
 103   size_t max_capacity() const;
 104   size_t soft_max_capacity() const;
 105   size_t capacity() const;
 106   size_t max_reserve() const;
 107   size_t used_high() const;
 108   size_t used_low() const;
 109   size_t used() const;
 110   size_t unused() const;
 111   size_t allocated() const;
 112   size_t reclaimed() const;
 113 
 114   void reset_statistics();
 115 
 116   ZPage* alloc_page(uint8_t type, size_t size, ZAllocationFlags flags);
 117   void free_page(ZPage* page, bool reclaimed);
 118 
 119   uint64_t uncommit();
 120   void uncommit_cancel();
 121 
 122   void enable_deferred_delete() const;
 123   void disable_deferred_delete() const;
 124 
 125   void debug_map_page(const ZPage* page) const;
 126   void debug_unmap_page(const ZPage* page) const;
 127 
 128   bool is_alloc_stalled() const;
 129   void check_out_of_memory();
 130 
 131   void pages_do(ZPageClosure* cl) const;
 132 };
 133 
 134 #endif // SHARE_GC_Z_ZPAGEALLOCATOR_HPP