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/jfrLinkedList.hpp"
 33 
 34 class JfrCheckpointManager;
 35 class JfrChunkWriter;
 36 class Thread;
 37 
 38 struct JfrCheckpointEntry {
 39   jlong size;
 40   jlong start_time;
 41   jlong duration;
 42   juint flushpoint;
 43   juint nof_segments;
 44 };
 45 
 46 typedef JfrMemorySpace<JfrCheckpointManager, JfrMspaceRetrieval, JfrLinkedList<JfrBuffer>, JfrLinkedList<JfrBuffer>, true > JfrCheckpointMspace;
 47 typedef JfrEpochStorageHost<JfrBuffer, JfrMspaceRemoveRetrieval, true /* reclaim buffers eagerly*/ > JfrThreadLocalCheckpointMspace;
 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::Node Buffer;
 57   typedef JfrCheckpointMspace::NodePtr BufferPtr;
 58  private:
 59   JfrCheckpointMspace* _global_mspace;
 60   JfrThreadLocalCheckpointMspace* _thread_local_mspace;
 61   JfrChunkWriter& _chunkwriter;
 62 
 63   JfrCheckpointManager(JfrChunkWriter& cw);
 64   ~JfrCheckpointManager();
 65   static JfrCheckpointManager& instance();
 66   static JfrCheckpointManager* create(JfrChunkWriter& cw);
 67   bool initialize();
 68   static void destroy();
 69 
 70   static BufferPtr lease(Thread* thread, bool previous_epoch = false, size_t size = 0);
 71   static BufferPtr lease(BufferPtr old, Thread* thread, size_t size);
 72 
 73   static BufferPtr acquire_thread_local(size_t size, Thread* thread);
 74   static BufferPtr lease_thread_local(Thread* thread, size_t size = 0);
 75 
 76   static BufferPtr flush(BufferPtr old, size_t used, size_t requested, Thread* thread);
 77 
 78   size_t clear();
 79   size_t write();
 80   void notify_threads();
 81 
 82   size_t write_static_type_set(Thread* thread);
 83   size_t write_threads(JavaThread* thread);
 84   size_t write_static_type_set_and_threads();
 85   void clear_type_set();
 86   void write_type_set();
 87 
 88   void begin_epoch_shift();
 89   void end_epoch_shift();
 90 
 91   static void on_unloading_classes();
 92   void on_rotation();
 93 
 94   // mspace callback
 95   void register_full(BufferPtr buffer, Thread* thread);
 96 
 97  public:
 98   size_t flush_type_set();
 99   static void create_thread_blob(Thread* thread);
100   static void write_thread_checkpoint(Thread* thread);
101 
102   friend class Jfr;
103   friend class JfrRecorder;
104   friend class JfrRecorderService;
105   friend class JfrCheckpointFlush;
106   friend class JfrCheckpointWriter;
107   friend class JfrSerializer;
108   template <typename, template <typename> class, typename, typename, bool>
109   friend class JfrMemorySpace;
110 };
111 
112 #endif // SHARE_JFR_RECORDER_CHECKPOINT_JFRCHECKPOINTMANAGER_HPP