1 /*
   2  * Copyright (c) 2012, 2018, 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_JFRSTORAGE_HPP
  25 #define SHARE_VM_JFR_RECORDER_STORAGE_JFRSTORAGE_HPP
  26 
  27 #include "jfr/recorder/storage/jfrBuffer.hpp"
  28 #include "jfr/recorder/storage/jfrMemorySpace.hpp"
  29 #include "jfr/recorder/storage/jfrMemorySpaceRetrieval.hpp"
  30 
  31 class JfrChunkWriter;
  32 class JfrPostBox;
  33 class JfrStorage;
  34 class JfrStorageControl;
  35 
  36 typedef JfrMemorySpace<JfrBuffer, JfrMspaceAlternatingRetrieval, JfrStorage> JfrStorageMspace;
  37 typedef JfrMemorySpace<JfrBuffer, JfrThreadLocalRetrieval, JfrStorage> JfrThreadLocalMspace;
  38 typedef JfrMemorySpace<JfrAgeNode, JfrMspaceSequentialRetrieval, JfrStorage> JfrStorageAgeMspace;
  39 
  40 //
  41 // Responsible for providing backing storage for writing events.
  42 //
  43 class JfrStorage : public JfrCHeapObj {
  44  public:
  45   typedef JfrStorageMspace::Type Buffer;
  46  private:
  47   JfrStorageControl* _control;
  48   JfrStorageMspace* _global_mspace;
  49   JfrThreadLocalMspace* _thread_local_mspace;
  50   JfrStorageMspace* _transient_mspace;
  51   JfrStorageAgeMspace* _age_mspace;
  52   JfrChunkWriter& _chunkwriter;
  53   JfrPostBox& _post_box;
  54 
  55   // mspace callbacks
  56   void register_full(Buffer* t, Thread* thread);
  57   void lock();
  58   void unlock();
  59   DEBUG_ONLY(bool is_locked() const;)
  60 
  61   Buffer* acquire_large(size_t size, Thread* t);
  62   Buffer* acquire_transient(size_t size, Thread* thread);
  63   bool flush_regular_buffer(Buffer* const buffer, Thread* t);
  64   Buffer* flush_regular(Buffer* cur, const u1* cur_pos, size_t used, size_t req, bool native, Thread* t);
  65   Buffer* flush_large(Buffer* cur, const u1* cur_pos, size_t used, size_t req, bool native, Thread* t);
  66   Buffer* provision_large(Buffer* cur, const u1* cur_pos, size_t used, size_t req, bool native, Thread* t);
  67   void release(Buffer* buffer, Thread* t);
  68 
  69   size_t clear();
  70   size_t clear_full();
  71   size_t write();
  72   size_t write_full();
  73   size_t write_at_safepoint();
  74   size_t scavenge();
  75 
  76   JfrStorage(JfrChunkWriter& cw, JfrPostBox& post_box);
  77   ~JfrStorage();
  78 
  79   static JfrStorage& instance();
  80   static JfrStorage* create(JfrChunkWriter& chunkwriter, JfrPostBox& post_box);
  81   bool initialize();
  82   static void destroy();
  83 
  84  public:
  85   static Buffer* acquire_thread_local(Thread* t, size_t size = 0);
  86   static void release_thread_local(Buffer* buffer, Thread* t);
  87   void release_large(Buffer* const buffer, Thread* t);
  88   static Buffer* flush(Buffer* cur, size_t used, size_t req, bool native, Thread* t);
  89   void discard_oldest(Thread* t);
  90   static JfrStorageControl& control();
  91 
  92   friend class JfrRecorder;
  93   friend class JfrRecorderService;
  94   template <typename, template <typename> class, typename>
  95   friend class JfrMemorySpace;
  96 };
  97 
  98 #endif // SHARE_VM_JFR_RECORDER_STORAGE_JFRSTORAGE_HPP