1 /*
   2  * Copyright (c) 2016, 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 
  25 #ifndef SHARE_VM_JFR_RECORDER_CHECKPOINT_JFRCHECKPOINTMANAGER_HPP
  26 #define SHARE_VM_JFR_RECORDER_CHECKPOINT_JFRCHECKPOINTMANAGER_HPP
  27 
  28 #include "jfr/recorder/storage/jfrBuffer.hpp"
  29 #include "jfr/recorder/storage/jfrMemorySpace.hpp"
  30 #include "jfr/recorder/storage/jfrMemorySpaceRetrieval.hpp"
  31 
  32 class JfrCheckpointManager;
  33 class JfrChunkWriter;
  34 class JfrSerializer;
  35 class JfrTypeManager;
  36 class Mutex;
  37 class Thread;
  38 
  39 struct JfrCheckpointEntry {
  40   jlong size;
  41   jlong start_time;
  42   jlong duration;
  43   juint flushpoint;
  44   juint nof_segments;
  45 };
  46 
  47 typedef JfrMemorySpace<JfrBuffer, JfrMspaceSequentialRetrieval, JfrCheckpointManager> JfrCheckpointMspace;
  48 
  49 //
  50 // Responsible for maintaining checkpoints and by implication types.
  51 // A checkpoint is an event that has a payload consisting of constant types.
  52 // A constant type is a binary relation, a set of key-value pairs.
  53 //
  54 class JfrCheckpointManager : public JfrCHeapObj {
  55  public:
  56   typedef JfrCheckpointMspace::Type Buffer;
  57  private:
  58   JfrCheckpointMspace* _free_list_mspace;
  59   JfrCheckpointMspace* _epoch_transition_mspace;
  60   Mutex* _lock;
  61   const Thread* _service_thread;
  62   JfrChunkWriter& _chunkwriter;
  63   u1 _checkpoint_epoch_state;
  64 
  65   // mspace callback
  66   void register_full(Buffer* t, Thread* thread);
  67   void lock();
  68   void unlock();
  69   DEBUG_ONLY(bool is_locked() const;)
  70 
  71   static Buffer* lease_buffer(Thread* t, size_t size = 0);
  72   static Buffer* flush(Buffer* old, size_t used, size_t requested, Thread* t);
  73 
  74   size_t clear();
  75   size_t write();
  76   size_t write_epoch_transition_mspace();
  77   size_t write_types();
  78   size_t write_safepoint_types();
  79   void write_type_set();
  80   void shift_epoch();
  81   void synchronize_epoch();
  82   bool use_epoch_transition_mspace(const Thread* t) const;
  83 
  84   JfrCheckpointManager(JfrChunkWriter& cw);
  85   ~JfrCheckpointManager();
  86 
  87   static JfrCheckpointManager& instance();
  88   static JfrCheckpointManager* create(JfrChunkWriter& cw);
  89   bool initialize();
  90   static void destroy();
  91 
  92  public:
  93   void register_service_thread(const Thread* t);
  94   static void write_type_set_for_unloaded_classes();
  95   static void create_thread_checkpoint(JavaThread* jt);
  96   static void write_thread_checkpoint(JavaThread* jt);
  97 
  98   friend class JfrRecorder;
  99   friend class JfrRecorderService;
 100   friend class JfrCheckpointFlush;
 101   friend class JfrCheckpointWriter;
 102   friend class JfrSerializer;
 103   friend class JfrStackTraceRepository;
 104   template <typename, template <typename> class, typename>
 105   friend class JfrMemorySpace;
 106 };
 107 
 108 #endif //SHARE_VM_JFR_RECORDER_CHECKPOINT_JFRCHECKPOINTMANAGER_HPP