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 
  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 #include "tracefiles/traceTypes.hpp"
  32 
  33 class JfrCheckpointManager;
  34 class JfrChunkWriter;
  35 class JfrConstantManager;
  36 class JfrConstantSerializer;
  37 class Mutex;
  38 class Thread;
  39 
  40 struct JfrCheckpointEntry {
  41   jlong size;
  42   jlong start_time;
  43   jlong duration;
  44   juint flushpoint;
  45   juint nof_segments;
  46 };
  47 
  48 typedef JfrMemorySpace<JfrBuffer, JfrMspaceSequentialRetrieval, JfrCheckpointManager> JfrCheckpointMspace;
  49 
  50 //
  51 // Responsible for maintaining checkpoints and by implication constants.
  52 // A checkpoint is an event that has a payload consisting of constant types.
  53 // A constant type is a relation, a set of key-value pairs.
  54 //
  55 class JfrCheckpointManager : public JfrCHeapObj {
  56  public:
  57   typedef JfrCheckpointMspace::Type Buffer;
  58   typedef JfrCheckpointMspace::List List;
  59  private:
  60   List _epoch_transition_list;
  61   JfrCheckpointMspace* _free_list_mspace;
  62   JfrCheckpointMspace* _transient_mspace;
  63   Mutex* _lock;
  64   JfrConstantManager* _constant_manager;
  65   const Thread* _service_thread;
  66   JfrChunkWriter& _chunkwriter;
  67   bool _checkpoint_epoch_state;
  68 
  69   // mspace callback
  70   void register_full(Buffer* t, Thread* thread);
  71   void lock();
  72   void unlock();
  73 
  74   static Buffer* lease_buffer(Thread* t, size_t size = 0);
  75   Buffer* lease_transient(size_t size, Thread* t);
  76   static Buffer* flush(Buffer* old, size_t used, size_t requested, Thread* t);
  77 
  78   size_t clear();
  79   size_t write();
  80   size_t write_epoch_transition_list();
  81   size_t write_constant_types();
  82   size_t write_safepoint_constant_types();
  83   void write_constant_tag_set();
  84   void shift_epoch();
  85   void synchronize_epoch();
  86   bool use_epoch_transition_list(const Thread* t) const;
  87 
  88   JfrCheckpointManager(JfrChunkWriter& cw);
  89   ~JfrCheckpointManager();
  90 
  91   static JfrCheckpointManager &instance();
  92   static JfrCheckpointManager* create(JfrChunkWriter& cw);
  93   bool initialize();
  94   static void destroy();
  95 
  96   static bool register_serializer(JfrConstantTypeId id, bool require_safepoint, bool permit_cache, JfrConstantSerializer* serializer);
  97 
  98  public:
  99   void register_service_thread(const Thread* t);
 100   static void write_constant_tag_set_for_unloaded_classes();
 101   static void create_thread_checkpoint(JavaThread* jt);
 102   static void write_thread_checkpoint(JavaThread* jt);
 103 
 104   friend class JfrRecorder;
 105   friend class JfrRecorderService;
 106   friend class JfrCheckpointFlush;
 107   friend class JfrCheckpointWriter;
 108   friend class JfrConstantSerializer;
 109   friend class JfrStackTraceRepository;
 110   template <typename, template <typename> class, typename>
 111   friend class JfrMemorySpace;
 112 };
 113 
 114 #endif //SHARE_VM_JFR_RECORDER_CHECKPOINT_JFRCHECKPOINTMANAGER_HPP