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