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