author | mgronlun |
Sat, 24 Aug 2019 14:30:27 +0200 | |
branch | JEP-349-branch |
changeset 57870 | 00860d9caf4d |
parent 57360 | 5d043a159d5c |
child 58154 | 060d9d139109 |
permissions | -rw-r--r-- |
50113 | 1 |
/* |
57360 | 2 |
* Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved. |
50113 | 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 "jfr/recorder/storage/jfrBuffer.hpp" |
|
27 |
#include "runtime/atomic.hpp" |
|
50429
83aec1d357d4
8204301: Make OrderAccess functions available to hpp rather than inline.hpp files
coleenp
parents:
50113
diff
changeset
|
28 |
#include "runtime/orderAccess.hpp" |
50113 | 29 |
#include "runtime/thread.inline.hpp" |
30 |
||
31 |
static const u1* const MUTEX_CLAIM = NULL; |
|
32 |
||
33 |
JfrBuffer::JfrBuffer() : _next(NULL), |
|
34 |
_prev(NULL), |
|
35 |
_identity(NULL), |
|
36 |
_pos(NULL), |
|
37 |
_top(NULL), |
|
38 |
_flags(0), |
|
39 |
_header_size(0), |
|
40 |
_size(0) {} |
|
41 |
||
42 |
bool JfrBuffer::initialize(size_t header_size, size_t size, const void* id /* NULL */) { |
|
43 |
_header_size = (u2)header_size; |
|
44 |
_size = (u4)(size / BytesPerWord); |
|
45 |
assert(_identity == NULL, "invariant"); |
|
46 |
_identity = id; |
|
47 |
set_pos(start()); |
|
48 |
set_top(start()); |
|
49 |
assert(_next == NULL, "invariant"); |
|
50 |
assert(free_size() == size, "invariant"); |
|
51 |
assert(!transient(), "invariant"); |
|
52 |
assert(!lease(), "invariant"); |
|
53 |
assert(!retired(), "invariant"); |
|
54 |
return true; |
|
55 |
} |
|
56 |
||
57360 | 57 |
void JfrBuffer::reinitialize(bool exclusion /* false */) { |
50113 | 58 |
assert(!lease(), "invariant"); |
59 |
assert(!transient(), "invariant"); |
|
60 |
set_pos(start()); |
|
57360 | 61 |
if (exclusion != excluded()) { |
62 |
// update |
|
63 |
if (exclusion) { |
|
64 |
set_excluded(); |
|
65 |
} else { |
|
66 |
clear_excluded(); |
|
67 |
} |
|
68 |
} |
|
50113 | 69 |
clear_retired(); |
70 |
set_top(start()); |
|
71 |
} |
|
72 |
||
73 |
void JfrBuffer::concurrent_reinitialization() { |
|
74 |
concurrent_top(); |
|
75 |
assert(!lease(), "invariant"); |
|
76 |
assert(!transient(), "invariant"); |
|
77 |
set_pos(start()); |
|
78 |
set_concurrent_top(start()); |
|
79 |
clear_retired(); |
|
80 |
} |
|
81 |
||
82 |
size_t JfrBuffer::discard() { |
|
83 |
size_t discard_size = unflushed_size(); |
|
84 |
set_top(pos()); |
|
85 |
return discard_size; |
|
86 |
} |
|
87 |
||
88 |
const u1* JfrBuffer::stable_top() const { |
|
89 |
const u1* current_top; |
|
90 |
do { |
|
91 |
current_top = OrderAccess::load_acquire(&_top); |
|
92 |
} while (MUTEX_CLAIM == current_top); |
|
93 |
return current_top; |
|
94 |
} |
|
95 |
||
96 |
const u1* JfrBuffer::top() const { |
|
97 |
return _top; |
|
98 |
} |
|
99 |
||
100 |
void JfrBuffer::set_top(const u1* new_top) { |
|
101 |
_top = new_top; |
|
102 |
} |
|
103 |
||
104 |
const u1* JfrBuffer::concurrent_top() const { |
|
105 |
do { |
|
106 |
const u1* current_top = stable_top(); |
|
107 |
if (Atomic::cmpxchg(MUTEX_CLAIM, &_top, current_top) == current_top) { |
|
108 |
return current_top; |
|
109 |
} |
|
110 |
} while (true); |
|
111 |
} |
|
112 |
||
113 |
void JfrBuffer::set_concurrent_top(const u1* new_top) { |
|
114 |
assert(new_top != MUTEX_CLAIM, "invariant"); |
|
115 |
assert(new_top <= end(), "invariant"); |
|
116 |
assert(new_top >= start(), "invariant"); |
|
117 |
assert(top() == MUTEX_CLAIM, "invariant"); |
|
118 |
OrderAccess::release_store(&_top, new_top); |
|
119 |
} |
|
120 |
||
121 |
size_t JfrBuffer::unflushed_size() const { |
|
122 |
return pos() - stable_top(); |
|
123 |
} |
|
124 |
||
125 |
void JfrBuffer::acquire(const void* id) { |
|
126 |
assert(id != NULL, "invariant"); |
|
127 |
const void* current_id; |
|
128 |
do { |
|
129 |
current_id = OrderAccess::load_acquire(&_identity); |
|
130 |
} while (current_id != NULL || Atomic::cmpxchg(id, &_identity, current_id) != current_id); |
|
131 |
} |
|
132 |
||
133 |
bool JfrBuffer::try_acquire(const void* id) { |
|
134 |
assert(id != NULL, "invariant"); |
|
135 |
const void* const current_id = OrderAccess::load_acquire(&_identity); |
|
136 |
return current_id == NULL && Atomic::cmpxchg(id, &_identity, current_id) == current_id; |
|
137 |
} |
|
138 |
||
139 |
void JfrBuffer::release() { |
|
140 |
OrderAccess::release_store(&_identity, (const void*)NULL); |
|
141 |
} |
|
142 |
||
57360 | 143 |
bool JfrBuffer::acquired_by(const void* id) const { |
144 |
return identity() == id; |
|
145 |
} |
|
146 |
||
147 |
bool JfrBuffer::acquired_by_self() const { |
|
148 |
return acquired_by(Thread::current()); |
|
149 |
} |
|
150 |
||
50113 | 151 |
#ifdef ASSERT |
152 |
static bool validate_to(const JfrBuffer* const to, size_t size) { |
|
153 |
assert(to != NULL, "invariant"); |
|
154 |
assert(to->acquired_by_self(), "invariant"); |
|
155 |
assert(to->free_size() >= size, "invariant"); |
|
156 |
return true; |
|
157 |
} |
|
158 |
||
159 |
static bool validate_concurrent_this(const JfrBuffer* const t, size_t size) { |
|
160 |
assert(t->top() == MUTEX_CLAIM, "invariant"); |
|
161 |
return true; |
|
162 |
} |
|
163 |
||
164 |
static bool validate_this(const JfrBuffer* const t, size_t size) { |
|
165 |
assert(t->top() + size <= t->pos(), "invariant"); |
|
166 |
return true; |
|
167 |
} |
|
168 |
#endif // ASSERT |
|
169 |
||
170 |
void JfrBuffer::move(JfrBuffer* const to, size_t size) { |
|
171 |
assert(validate_to(to, size), "invariant"); |
|
172 |
assert(validate_this(this, size), "invariant"); |
|
173 |
const u1* current_top = top(); |
|
174 |
assert(current_top != NULL, "invariant"); |
|
175 |
memcpy(to->pos(), current_top, size); |
|
176 |
to->set_pos(size); |
|
177 |
to->release(); |
|
178 |
set_top(current_top + size); |
|
179 |
} |
|
180 |
||
181 |
void JfrBuffer::concurrent_move_and_reinitialize(JfrBuffer* const to, size_t size) { |
|
182 |
assert(validate_to(to, size), "invariant"); |
|
183 |
const u1* current_top = concurrent_top(); |
|
184 |
assert(validate_concurrent_this(this, size), "invariant"); |
|
185 |
const size_t actual_size = MIN2(size, (size_t)(pos() - current_top)); |
|
186 |
assert(actual_size <= size, "invariant"); |
|
187 |
memcpy(to->pos(), current_top, actual_size); |
|
188 |
to->set_pos(actual_size); |
|
189 |
set_pos(start()); |
|
190 |
to->release(); |
|
191 |
set_concurrent_top(start()); |
|
192 |
} |
|
193 |
||
194 |
enum FLAG { |
|
195 |
RETIRED = 1, |
|
196 |
TRANSIENT = 2, |
|
57360 | 197 |
LEASE = 4, |
198 |
EXCLUDED = 8 |
|
50113 | 199 |
}; |
200 |
||
201 |
bool JfrBuffer::transient() const { |
|
202 |
return (u1)TRANSIENT == (_flags & (u1)TRANSIENT); |
|
203 |
} |
|
204 |
||
205 |
void JfrBuffer::set_transient() { |
|
206 |
_flags |= (u1)TRANSIENT; |
|
207 |
assert(transient(), "invariant"); |
|
208 |
} |
|
209 |
||
210 |
void JfrBuffer::clear_transient() { |
|
211 |
if (transient()) { |
|
212 |
_flags ^= (u1)TRANSIENT; |
|
213 |
} |
|
214 |
assert(!transient(), "invariant"); |
|
215 |
} |
|
216 |
||
217 |
bool JfrBuffer::lease() const { |
|
218 |
return (u1)LEASE == (_flags & (u1)LEASE); |
|
219 |
} |
|
220 |
||
221 |
void JfrBuffer::set_lease() { |
|
222 |
_flags |= (u1)LEASE; |
|
223 |
assert(lease(), "invariant"); |
|
224 |
} |
|
225 |
||
226 |
void JfrBuffer::clear_lease() { |
|
227 |
if (lease()) { |
|
228 |
_flags ^= (u1)LEASE; |
|
229 |
} |
|
230 |
assert(!lease(), "invariant"); |
|
231 |
} |
|
232 |
||
57360 | 233 |
bool JfrBuffer::excluded() const { |
234 |
return (u1)EXCLUDED == (_flags & (u1)EXCLUDED); |
|
235 |
} |
|
236 |
||
237 |
void JfrBuffer::set_excluded() { |
|
238 |
_flags |= (u1)EXCLUDED; |
|
239 |
assert(excluded(), "invariant"); |
|
240 |
} |
|
241 |
||
242 |
void JfrBuffer::clear_excluded() { |
|
243 |
if (excluded()) { |
|
244 |
_flags ^= (u1)EXCLUDED; |
|
245 |
} |
|
246 |
assert(!excluded(), "invariant"); |
|
247 |
} |
|
248 |
||
50113 | 249 |
static u2 load_acquire_flags(const u2* const flags) { |
250 |
return OrderAccess::load_acquire(flags); |
|
251 |
} |
|
252 |
||
253 |
static void release_store_flags(u2* const flags, u2 new_flags) { |
|
254 |
OrderAccess::release_store(flags, new_flags); |
|
255 |
} |
|
256 |
||
257 |
bool JfrBuffer::retired() const { |
|
258 |
return (u1)RETIRED == (load_acquire_flags(&_flags) & (u1)RETIRED); |
|
259 |
} |
|
260 |
||
261 |
void JfrBuffer::set_retired() { |
|
262 |
const u2 new_flags = load_acquire_flags(&_flags) | (u1)RETIRED; |
|
263 |
release_store_flags(&_flags, new_flags); |
|
264 |
} |
|
265 |
||
266 |
void JfrBuffer::clear_retired() { |
|
267 |
u2 new_flags = load_acquire_flags(&_flags); |
|
268 |
if ((u1)RETIRED == (new_flags & (u1)RETIRED)) { |
|
269 |
new_flags ^= (u1)RETIRED; |
|
270 |
release_store_flags(&_flags, new_flags); |
|
271 |
} |
|
272 |
} |