1 /*
   2  * Copyright (c) 2016, 2019, 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_VM_JFR_RECORDER_STORAGE_JFRMEMORYSPACE_HPP
  25 #define SHARE_VM_JFR_RECORDER_STORAGE_JFRMEMORYSPACE_HPP
  26 
  27 #include "jfr/utilities/jfrAllocation.hpp"
  28 #include "jfr/utilities/jfrDoublyLinkedList.hpp"
  29 #include "jfr/utilities/jfrIterator.hpp"
  30 #include "jfr/utilities/jfrTypes.hpp"
  31 #include "runtime/os.hpp"
  32 #include "utilities/globalDefinitions.hpp"
  33 #include "utilities/macros.hpp"
  34 
  35 template <typename T, template <typename> class RetrievalType, typename Callback>
  36 class JfrMemorySpace : public JfrCHeapObj {
  37  public:
  38   typedef T Type;
  39   typedef RetrievalType<JfrMemorySpace<T, RetrievalType, Callback> > Retrieval;
  40   typedef JfrDoublyLinkedList<Type> List;
  41   typedef StopOnNullIterator<List> Iterator;
  42  private:
  43   List _free;
  44   List _full;
  45   size_t _min_elem_size;
  46   size_t _limit_size;
  47   size_t _cache_count;
  48   Callback* _callback;
  49 
  50   bool should_populate_cache() const { return _free.count() < _cache_count; }
  51 
  52  public:
  53   JfrMemorySpace(size_t min_elem_size, size_t limit_size, size_t cache_count, Callback* callback);
  54   ~JfrMemorySpace();
  55   bool initialize();
  56 
  57   size_t min_elem_size() const { return _min_elem_size; }
  58   size_t limit_size() const { return _limit_size; }
  59 
  60   bool has_full() const { return _full.head() != NULL; }
  61   bool has_free() const { return _free.head() != NULL; }
  62   bool is_full_empty() const { return !has_full(); }
  63   bool is_free_empty() const { return !has_free(); }
  64 
  65   size_t full_count() const { return _full.count(); }
  66   size_t free_count() const { return _free.count(); }
  67 
  68   List& full() { return _full; }
  69   const List& full() const { return _full; }
  70   List& free() { return _free; }
  71   const List& free() const { return _free; }
  72 
  73   Type* full_head() { return _full.head(); }
  74   Type* full_tail() { return _full.tail(); }
  75   Type* free_head() { return _free.head(); }
  76   Type* free_tail() { return _free.tail(); }
  77 
  78   void insert_free_head(Type* t) { _free.prepend(t); }
  79   void insert_free_tail(Type* t) { _free.append(t); }
  80   void insert_free_tail(Type* t, Type* tail, size_t count) { _free.append_list(t, tail, count); }
  81   void insert_full_head(Type* t) { _full.prepend(t); }
  82   void insert_full_tail(Type* t) { _full.append(t); }
  83   void insert_full_tail(Type* t, Type* tail, size_t count) { _full.append_list(t, tail, count); }
  84 
  85   Type* remove_free(Type* t) { return _free.remove(t); }
  86   Type* remove_full(Type* t) { return _full.remove(t); }
  87   Type* remove_free_tail() { _free.remove(_free.tail()); }
  88   Type* remove_full_tail() { return _full.remove(_full.tail()); }
  89   Type* clear_full(bool return_tail = false) { return _full.clear(return_tail); }
  90   Type* clear_free(bool return_tail = false) { return _free.clear(return_tail); }
  91   void release(Type* t);
  92 
  93   void register_full(Type* t, Thread* thread) { _callback->register_full(t, thread); }
  94   void lock() { _callback->lock(); }
  95   void unlock() { _callback->unlock(); }
  96 
  97   Type* allocate(size_t size);
  98   void deallocate(Type* t);
  99   Type* get(size_t size, Thread* thread) { return Retrieval::get(size, this, thread); }
 100 
 101   template <typename IteratorCallback, typename IteratorType>
 102   void iterate(IteratorCallback& callback, bool full = true, jfr_iter_direction direction = forward);
 103 
 104   debug_only(bool in_full_list(const Type* t) const { return _full.in_list(t); })
 105   debug_only(bool in_free_list(const Type* t) const { return _free.in_list(t); })
 106 };
 107 
 108 // allocations are even multiples of the mspace min size
 109 inline u8 align_allocation_size(u8 requested_size, size_t min_elem_size) {
 110   assert((int)min_elem_size % os::vm_page_size() == 0, "invariant");
 111   u8 alloc_size_bytes = min_elem_size;
 112   while (requested_size > alloc_size_bytes) {
 113     alloc_size_bytes <<= 1;
 114   }
 115   assert((int)alloc_size_bytes % os::vm_page_size() == 0, "invariant");
 116   return alloc_size_bytes;
 117 }
 118 
 119 template <typename T, template <typename> class RetrievalType, typename Callback>
 120 T* JfrMemorySpace<T, RetrievalType, Callback>::allocate(size_t size) {
 121   const u8 aligned_size_bytes = align_allocation_size(size, _min_elem_size);
 122   void* const allocation = JfrCHeapObj::new_array<u1>(aligned_size_bytes + sizeof(T));
 123   if (allocation == NULL) {
 124     return NULL;
 125   }
 126   T* const t = new (allocation) T;
 127   assert(t != NULL, "invariant");
 128   if (!t->initialize(sizeof(T), aligned_size_bytes)) {
 129     JfrCHeapObj::free(t, aligned_size_bytes + sizeof(T));
 130     return NULL;
 131   }
 132   return t;
 133 }
 134 
 135 template <typename T, template <typename> class RetrievalType, typename Callback>
 136 void JfrMemorySpace<T, RetrievalType, Callback>::deallocate(T* t) {
 137   assert(t != NULL, "invariant");
 138   assert(!_free.in_list(t), "invariant");
 139   assert(!_full.in_list(t), "invariant");
 140   assert(t != NULL, "invariant");
 141   JfrCHeapObj::free(t, t->total_size());
 142 }
 143 
 144 template <typename Mspace>
 145 class MspaceLock {
 146  private:
 147   Mspace* _mspace;
 148  public:
 149   MspaceLock(Mspace* mspace) : _mspace(mspace) { _mspace->lock(); }
 150   ~MspaceLock() { _mspace->unlock(); }
 151 };
 152 
 153 template <typename Mspace>
 154 class ReleaseOp : public StackObj {
 155  private:
 156   Mspace* _mspace;
 157  public:
 158   typedef typename Mspace::Type Type;
 159   ReleaseOp(Mspace* mspace) : _mspace(mspace) {}
 160   bool process(Type* t);
 161   size_t processed() const { return 0; }
 162 };
 163 
 164 template <typename Mspace>
 165 class MutexedReleaseOp : public ReleaseOp<Mspace> {
 166  private:
 167   Thread* _thread;
 168  public:
 169   typedef typename Mspace::Type Type;
 170   MutexedReleaseOp(Mspace* mspace, Thread* thread) : ReleaseOp<Mspace>(mspace), _thread(thread) {}
 171   bool process(Type* t);
 172   size_t processed() const { return ReleaseOp<Mspace>::processed(); }
 173 };
 174 
 175 template <typename Mspace>
 176 class ThreadLocalReleaseOp : public StackObj {
 177  private:
 178   Mspace* _mspace;
 179  public:
 180   typedef typename Mspace::Type Type;
 181   ThreadLocalReleaseOp(Mspace* mspace) : _mspace(mspace) {}
 182   bool process(Type* t);
 183   size_t processed() const { return 0; }
 184 };
 185 
 186 #endif // SHARE_VM_JFR_RECORDER_STORAGE_JFRMEMORYSPACE_HPP