1 /*
  2  * Copyright (c) 2016, 2021, 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_JFR_RECORDER_CHECKPOINT_JFRCHECKPOINTMANAGER_HPP
 26 #define SHARE_JFR_RECORDER_CHECKPOINT_JFRCHECKPOINTMANAGER_HPP
 27 
 28 #include "jfr/recorder/storage/jfrBuffer.hpp"
 29 #include "jfr/recorder/storage/jfrEpochStorage.hpp"
 30 #include "jfr/recorder/storage/jfrMemorySpace.hpp"
 31 #include "jfr/recorder/storage/jfrMemorySpaceRetrieval.hpp"
 32 #include "jfr/utilities/jfrBlob.hpp"
 33 #include "jfr/utilities/jfrLinkedList.hpp"
 34 #include "jfr/utilities/jfrTypes.hpp"
 35 #include "runtime/thread.hpp"
 36 
 37 class JfrCheckpointManager;
 38 class JfrChunkWriter;
 39 class Thread;
 40 
 41 struct JfrCheckpointEntry {
 42   jlong size;
 43   jlong start_time;
 44   jlong duration;
 45   juint flushpoint;
 46   juint nof_segments;
 47 };
 48 
 49 typedef JfrMemorySpace<JfrCheckpointManager, JfrMspaceRetrieval, JfrLinkedList<JfrBuffer>, JfrLinkedList<JfrBuffer>, true > JfrCheckpointMspace;
 50 typedef JfrEpochStorageHost<JfrBuffer, JfrMspaceRemoveRetrieval, true /* reclaim buffers eagerly*/ > JfrThreadLocalCheckpointMspace;
 51 
 52 //
 53 // Responsible for maintaining checkpoints and by implication types.
 54 // A checkpoint is an event that has a payload consisting of constant types.
 55 // A constant type is a binary relation, a set of key-value pairs.
 56 //
 57 class JfrCheckpointManager : public JfrCHeapObj {
 58  public:
 59   typedef JfrCheckpointMspace::Node Buffer;
 60   typedef JfrCheckpointMspace::NodePtr BufferPtr;
 61  private:
 62   JfrCheckpointMspace* _global_mspace;
 63   JfrThreadLocalCheckpointMspace* _java_thread_local_mspace;
 64   JfrChunkWriter& _chunkwriter;
 65 
 66   JfrCheckpointManager(JfrChunkWriter& cw);
 67   ~JfrCheckpointManager();
 68   static JfrCheckpointManager& instance();
 69   static JfrCheckpointManager* create(JfrChunkWriter& cw);
 70   bool initialize();
 71   static void destroy();
 72 
 73   static BufferPtr get_thread_local(Thread* thread);
 74   static void set_thread_local(Thread* thread, BufferPtr buffer);
 75   static BufferPtr acquire_thread_local(size_t size, Thread* thread);
 76 
 77   static BufferPtr lease(Thread* thread, bool previous_epoch = false, size_t size = 0);
 78   static BufferPtr lease(BufferPtr old, Thread* thread, size_t size);
 79   static BufferPtr lease_thread_local(Thread* thread, size_t size = 0);
 80 
 81   static BufferPtr flush(BufferPtr old, size_t used, size_t requested, Thread* thread);
 82 
 83   size_t clear();
 84   size_t write();
 85   void notify_threads();
 86 
 87   size_t write_static_type_set(Thread* thread);
 88   size_t write_threads(JavaThread* thread);
 89   size_t write_static_type_set_and_threads();
 90   void clear_type_set();
 91   void write_type_set();
 92 
 93   void begin_epoch_shift();
 94   void end_epoch_shift();
 95 
 96   static void on_unloading_classes();
 97   void on_rotation();
 98 
 99   // mspace callback
100   void register_full(BufferPtr buffer, Thread* thread);
101 
102  public:
103   static JfrBlobHandle create_thread_blob(JavaThread* jt, traceid tid = 0, oop vthread = NULL);
104   static void write_checkpoint(Thread* t, traceid tid = 0, oop vthread = NULL);
105   size_t flush_type_set();
106 
107   friend class Jfr;
108   friend class JfrRecorder;
109   friend class JfrRecorderService;
110   friend class JfrCheckpointFlush;
111   friend class JfrCheckpointWriter;
112   friend class JfrSerializer;
113   template <typename, template <typename> class, typename, typename, bool>
114   friend class JfrMemorySpace;
115 };
116 
117 #endif // SHARE_JFR_RECORDER_CHECKPOINT_JFRCHECKPOINTMANAGER_HPP