50113
|
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 |
#include "precompiled.hpp"
|
|
26 |
#include "classfile/javaClasses.inline.hpp"
|
|
27 |
#include "jfr/recorder/checkpoint/types/traceid/jfrTraceIdEpoch.hpp"
|
|
28 |
#include "jfr/recorder/service/jfrOptionSet.hpp"
|
|
29 |
#include "jfr/recorder/storage/jfrMemorySpace.inline.hpp"
|
|
30 |
#include "jfr/recorder/repository/jfrChunkWriter.hpp"
|
|
31 |
#include "jfr/recorder/storage/jfrStorageUtils.inline.hpp"
|
|
32 |
#include "jfr/recorder/stringpool/jfrStringPool.hpp"
|
|
33 |
#include "jfr/recorder/stringpool/jfrStringPoolWriter.hpp"
|
|
34 |
#include "jfr/utilities/jfrTypes.hpp"
|
|
35 |
#include "logging/log.hpp"
|
|
36 |
#include "runtime/atomic.hpp"
|
|
37 |
#include "runtime/mutexLocker.hpp"
|
|
38 |
#include "runtime/orderAccess.inline.hpp"
|
|
39 |
#include "runtime/safepoint.hpp"
|
|
40 |
#include "runtime/thread.inline.hpp"
|
|
41 |
|
|
42 |
typedef JfrStringPool::Buffer* BufferPtr;
|
|
43 |
|
|
44 |
static JfrStringPool* _instance = NULL;
|
|
45 |
|
|
46 |
JfrStringPool& JfrStringPool::instance() {
|
|
47 |
return *_instance;
|
|
48 |
}
|
|
49 |
|
|
50 |
JfrStringPool* JfrStringPool::create(JfrChunkWriter& cw) {
|
|
51 |
assert(_instance == NULL, "invariant");
|
|
52 |
_instance = new JfrStringPool(cw);
|
|
53 |
return _instance;
|
|
54 |
}
|
|
55 |
|
|
56 |
void JfrStringPool::destroy() {
|
|
57 |
assert(_instance != NULL, "invariant");
|
|
58 |
delete _instance;
|
|
59 |
_instance = NULL;
|
|
60 |
}
|
|
61 |
|
|
62 |
JfrStringPool::JfrStringPool(JfrChunkWriter& cw) : _free_list_mspace(NULL), _lock(NULL), _chunkwriter(cw) {}
|
|
63 |
|
|
64 |
JfrStringPool::~JfrStringPool() {
|
|
65 |
if (_free_list_mspace != NULL) {
|
|
66 |
delete _free_list_mspace;
|
|
67 |
}
|
|
68 |
if (_lock != NULL) {
|
|
69 |
delete _lock;
|
|
70 |
}
|
|
71 |
}
|
|
72 |
|
|
73 |
static const size_t unlimited_mspace_size = 0;
|
|
74 |
static const size_t string_pool_cache_count = 2;
|
|
75 |
static const size_t string_pool_buffer_size = 512 * K;
|
|
76 |
|
|
77 |
bool JfrStringPool::initialize() {
|
|
78 |
assert(_free_list_mspace == NULL, "invariant");
|
|
79 |
_free_list_mspace = new JfrStringPoolMspace(string_pool_buffer_size, unlimited_mspace_size, string_pool_cache_count, this);
|
|
80 |
if (_free_list_mspace == NULL || !_free_list_mspace->initialize()) {
|
|
81 |
return false;
|
|
82 |
}
|
|
83 |
assert(_lock == NULL, "invariant");
|
|
84 |
_lock = new Mutex(Monitor::leaf - 1, "Checkpoint mutex", Mutex::_allow_vm_block_flag, Monitor::_safepoint_check_never);
|
|
85 |
return _lock != NULL;
|
|
86 |
}
|
|
87 |
|
|
88 |
/*
|
|
89 |
* If the buffer was a "lease" from the global system, release back.
|
|
90 |
*
|
|
91 |
* The buffer is effectively invalidated for the thread post-return,
|
|
92 |
* and the caller should take means to ensure that it is not referenced any longer.
|
|
93 |
*/
|
|
94 |
static void release(BufferPtr buffer, Thread* thread) {
|
|
95 |
assert(buffer != NULL, "invariant");
|
|
96 |
assert(buffer->lease(), "invariant");
|
|
97 |
assert(buffer->acquired_by_self(), "invariant");
|
|
98 |
buffer->clear_lease();
|
|
99 |
buffer->release();
|
|
100 |
}
|
|
101 |
|
|
102 |
BufferPtr JfrStringPool::flush(BufferPtr old, size_t used, size_t requested, Thread* thread) {
|
|
103 |
assert(old != NULL, "invariant");
|
|
104 |
assert(old->lease(), "invariant");
|
|
105 |
if (0 == requested) {
|
|
106 |
// indicates a lease is being returned
|
|
107 |
release(old, thread);
|
|
108 |
return NULL;
|
|
109 |
}
|
|
110 |
// migration of in-flight information
|
|
111 |
BufferPtr const new_buffer = lease_buffer(thread, used + requested);
|
|
112 |
if (new_buffer != NULL) {
|
|
113 |
migrate_outstanding_writes(old, new_buffer, used, requested);
|
|
114 |
}
|
|
115 |
release(old, thread);
|
|
116 |
return new_buffer; // might be NULL
|
|
117 |
}
|
|
118 |
|
|
119 |
static const size_t lease_retry = 10;
|
|
120 |
|
|
121 |
BufferPtr JfrStringPool::lease_buffer(Thread* thread, size_t size /* 0 */) {
|
|
122 |
BufferPtr buffer = mspace_get_free_lease_with_retry(size, instance()._free_list_mspace, lease_retry, thread);
|
|
123 |
if (buffer == NULL) {
|
|
124 |
buffer = mspace_allocate_transient_lease_to_free(size, instance()._free_list_mspace, thread);
|
|
125 |
}
|
|
126 |
assert(buffer->acquired_by_self(), "invariant");
|
|
127 |
assert(buffer->lease(), "invariant");
|
|
128 |
return buffer;
|
|
129 |
}
|
|
130 |
|
|
131 |
bool JfrStringPool::add(bool epoch, jlong id, jstring string, JavaThread* jt) {
|
|
132 |
assert(jt != NULL, "invariant");
|
|
133 |
const bool current_epoch = JfrTraceIdEpoch::epoch();
|
|
134 |
if (current_epoch == epoch) {
|
|
135 |
JfrStringPoolWriter writer(jt);
|
|
136 |
writer.write(id);
|
|
137 |
writer.write(string);
|
|
138 |
writer.inc_nof_strings();
|
|
139 |
}
|
|
140 |
return current_epoch;
|
|
141 |
}
|
|
142 |
|
|
143 |
class StringPoolWriteOp {
|
|
144 |
public:
|
|
145 |
typedef JfrStringPoolBuffer Type;
|
|
146 |
private:
|
|
147 |
UnBufferedWriteToChunk<Type> _writer;
|
|
148 |
Thread* _thread;
|
|
149 |
size_t _strings_processed;
|
|
150 |
public:
|
|
151 |
StringPoolWriteOp(JfrChunkWriter& writer, Thread* thread) : _writer(writer), _thread(thread), _strings_processed(0) {}
|
|
152 |
bool write(Type* buffer, const u1* data, size_t size) {
|
|
153 |
buffer->acquire(_thread); // blocking
|
|
154 |
const u4 nof_strings_used = (const u4)buffer->string_count();
|
|
155 |
assert(nof_strings_used > 0, "invariant");
|
|
156 |
buffer->set_string_top(buffer->string_top() + nof_strings_used);
|
|
157 |
// "size processed" for string pool buffers is the number of processed string elements
|
|
158 |
_strings_processed += nof_strings_used;
|
|
159 |
const bool ret = _writer.write(buffer, data, size);
|
|
160 |
buffer->release();
|
|
161 |
return ret;
|
|
162 |
}
|
|
163 |
size_t processed() { return _strings_processed; }
|
|
164 |
};
|
|
165 |
|
|
166 |
typedef StringPoolWriteOp WriteOperation;
|
|
167 |
typedef ConcurrentWriteOp<WriteOperation> ConcurrentWriteOperation;
|
|
168 |
|
|
169 |
size_t JfrStringPool::write() {
|
|
170 |
Thread* const thread = Thread::current();
|
|
171 |
WriteOperation wo(_chunkwriter, thread);
|
|
172 |
ConcurrentWriteOperation cwo(wo);
|
|
173 |
assert(_free_list_mspace->is_full_empty(), "invariant");
|
|
174 |
process_free_list(cwo, _free_list_mspace);
|
|
175 |
return wo.processed();
|
|
176 |
}
|
|
177 |
|
|
178 |
typedef MutexedWriteOp<WriteOperation> MutexedWriteOperation;
|
|
179 |
typedef ReleaseOp<JfrStringPoolMspace> StringPoolReleaseOperation;
|
|
180 |
typedef CompositeOperation<MutexedWriteOperation, StringPoolReleaseOperation> StringPoolWriteOperation;
|
|
181 |
|
|
182 |
size_t JfrStringPool::write_at_safepoint() {
|
|
183 |
assert(SafepointSynchronize::is_at_safepoint(), "invariant");
|
|
184 |
Thread* const thread = Thread::current();
|
|
185 |
WriteOperation wo(_chunkwriter, thread);
|
|
186 |
MutexedWriteOperation mwo(wo);
|
|
187 |
StringPoolReleaseOperation spro(_free_list_mspace, thread, false);
|
|
188 |
StringPoolWriteOperation spwo(&mwo, &spro);
|
|
189 |
assert(_free_list_mspace->is_full_empty(), "invariant");
|
|
190 |
process_free_list(spwo, _free_list_mspace);
|
|
191 |
return wo.processed();
|
|
192 |
}
|
|
193 |
|
|
194 |
class StringPoolBufferDiscarder {
|
|
195 |
private:
|
|
196 |
Thread* _thread;
|
|
197 |
size_t _processed;
|
|
198 |
public:
|
|
199 |
typedef JfrStringPoolBuffer Type;
|
|
200 |
StringPoolBufferDiscarder() : _thread(Thread::current()), _processed(0) {}
|
|
201 |
bool process(Type* buffer) {
|
|
202 |
buffer->acquire(_thread); // serialized access
|
|
203 |
const u1* const current_top = buffer->top();
|
|
204 |
const size_t unflushed_size = buffer->pos() - current_top;
|
|
205 |
if (unflushed_size == 0) {
|
|
206 |
assert(buffer->string_count() == 0, "invariant");
|
|
207 |
buffer->release();
|
|
208 |
return true;
|
|
209 |
}
|
|
210 |
buffer->set_top(current_top + unflushed_size);
|
|
211 |
const u4 nof_strings_used = buffer->string_count();
|
|
212 |
buffer->set_string_top(buffer->string_top() + nof_strings_used);
|
|
213 |
// "size processed" for string pool buffers is the number of string elements
|
|
214 |
_processed += nof_strings_used;
|
|
215 |
buffer->release();
|
|
216 |
return true;
|
|
217 |
}
|
|
218 |
size_t processed() const { return _processed; }
|
|
219 |
};
|
|
220 |
|
|
221 |
size_t JfrStringPool::clear() {
|
|
222 |
StringPoolBufferDiscarder discard_operation;
|
|
223 |
assert(_free_list_mspace->is_full_empty(), "invariant");
|
|
224 |
process_free_list(discard_operation, _free_list_mspace);
|
|
225 |
return discard_operation.processed();
|
|
226 |
}
|
|
227 |
|
|
228 |
void JfrStringPool::register_full(BufferPtr t, Thread* thread) {
|
|
229 |
// nothing here at the moment
|
|
230 |
assert(t->retired(), "invariant");
|
|
231 |
}
|
|
232 |
|
|
233 |
void JfrStringPool::lock() {
|
|
234 |
assert(!_lock->owned_by_self(), "invariant");
|
|
235 |
_lock->lock_without_safepoint_check();
|
|
236 |
}
|
|
237 |
|
|
238 |
void JfrStringPool::unlock() {
|
|
239 |
_lock->unlock();
|
|
240 |
}
|
|
241 |
|
|
242 |
#ifdef ASSERT
|
|
243 |
bool JfrStringPool::is_locked() const {
|
|
244 |
return _lock->owned_by_self();
|
|
245 |
}
|
|
246 |
#endif
|