1 /*
2  * Copyright (c) 2012, 2020, 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_JFR_RECORDER_STORAGE_JFRSTORAGE_HPP
25 #define SHARE_JFR_RECORDER_STORAGE_JFRSTORAGE_HPP
26 
27 #include "jfr/recorder/storage/jfrBuffer.hpp"
28 #include "jfr/recorder/storage/jfrFullStorage.hpp"
29 #include "jfr/recorder/storage/jfrMemorySpace.hpp"
30 #include "jfr/recorder/storage/jfrMemorySpaceRetrieval.hpp"
31 #include "jfr/utilities/jfrConcurrentQueue.hpp"
32 #include "jfr/utilities/jfrLinkedList.hpp"
33 #include "jfr/utilities/jfrNode.hpp"
34 #include "jfr/utilities/jfrRelation.hpp"
35 
36 class JfrChunkWriter;
37 class JfrPostBox;
38 class JfrStorage;
39 class JfrStorageControl;
40 
41 typedef JfrMemorySpace<JfrStorage, JfrMspaceRetrieval, JfrLinkedList<JfrBuffer> > JfrStorageMspace;
42 typedef JfrMemorySpace<JfrStorage, JfrMspaceRemoveRetrieval, JfrConcurrentQueue<JfrBuffer>, JfrLinkedList<JfrBuffer> > JfrThreadLocalMspace;
43 typedef JfrFullStorage<JfrBuffer*, JfrValueNode> JfrFullList;
44 
45 //
46 // Responsible for providing backing storage for writing events.
47 //
48 class JfrStorage : public JfrCHeapObj {
49  public:
50   typedef JfrStorageMspace::Node    Buffer;
51   typedef JfrStorageMspace::NodePtr BufferPtr;
52 
53  private:
54   JfrStorageControl* _control;
55   JfrStorageMspace* _global_mspace;
56   JfrThreadLocalMspace* _thread_local_mspace;
57   JfrFullList* _full_list;
58   JfrChunkWriter& _chunkwriter;
59   JfrPostBox& _post_box;
60 
61   BufferPtr acquire_large(size_t size, Thread* thread);
62   BufferPtr acquire_transient(size_t size, Thread* thread);
63   bool flush_regular_buffer(BufferPtr buffer, Thread* thread);
64   BufferPtr flush_regular(BufferPtr cur, const u1* cur_pos, size_t used, size_t req, bool native, Thread* thread);
65   BufferPtr flush_large(BufferPtr cur, const u1* cur_pos, size_t used, size_t req, bool native, Thread* thread);
66   BufferPtr provision_large(BufferPtr cur, const u1* cur_pos, size_t used, size_t req, bool native, Thread* thread);
67   void release(BufferPtr buffer, Thread* thread);
68 
69   size_t clear();
70   size_t clear_full();
71   size_t write_full();
72   size_t write_at_safepoint();
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   // mspace callback
83   void register_full(BufferPtr buffer, Thread* thread);
84 
85  public:
86   static BufferPtr acquire_thread_local(Thread* thread, size_t size = 0);
87   static void release_thread_local(BufferPtr buffer, Thread* thread);
88   void release_large(BufferPtr buffer, Thread* thread);
89   static BufferPtr flush(BufferPtr cur, size_t used, size_t req, bool native, Thread* thread);
90   void discard_oldest(Thread* thread);
91   static JfrStorageControl& control();
92   size_t write();
93 
94   friend class JfrRecorder;
95   friend class JfrRecorderService;
96   template <typename, template <typename> class, typename, typename, bool>
97   friend class JfrMemorySpace;
98 };
99 
100 #endif // SHARE_JFR_RECORDER_STORAGE_JFRSTORAGE_HPP
101