author | egahlin |
Fri, 17 May 2019 16:02:27 +0200 | |
branch | JEP-349-branch |
changeset 57360 | 5d043a159d5c |
parent 54263 | 3cabb47758c9 |
child 57870 | 00860d9caf4d |
permissions | -rw-r--r-- |
50113 | 1 |
/* |
54263
3cabb47758c9
8217362: Emergency dump does not work when disk=false is set
mgronlun
parents:
53897
diff
changeset
|
2 |
* Copyright (c) 2012, 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" |
|
57360 | 26 |
#include "jfr/recorder/repository/jfrChunk.hpp" |
50113 | 27 |
#include "jfr/recorder/repository/jfrChunkWriter.hpp" |
28 |
#include "jfr/recorder/service/jfrOptionSet.hpp" |
|
29 |
#include "jfr/utilities/jfrTime.hpp" |
|
57360 | 30 |
#include "jfr/utilities/jfrTimeConverter.hpp" |
50113 | 31 |
#include "jfr/utilities/jfrTypes.hpp" |
32 |
#include "runtime/mutexLocker.hpp" |
|
33 |
#include "runtime/os.hpp" |
|
50117
fb66b2959eaf
8203251: Non-PCH build failed after JDK-8199712 (Flight Recorder)
shade
parents:
50113
diff
changeset
|
34 |
#include "runtime/os.inline.hpp" |
50113 | 35 |
|
53897
0abec72a3ac2
8217647: JFR: recordings on 32-bit systems unreadable
bulasevich
parents:
50117
diff
changeset
|
36 |
static const u2 JFR_VERSION_MAJOR = 2; |
0abec72a3ac2
8217647: JFR: recordings on 32-bit systems unreadable
bulasevich
parents:
50117
diff
changeset
|
37 |
static const u2 JFR_VERSION_MINOR = 0; |
50113 | 38 |
|
57360 | 39 |
static const int64_t MAGIC_OFFSET = 0; |
40 |
static const int64_t MAGIC_LEN = 4; |
|
41 |
static const int64_t VERSION_OFFSET = MAGIC_LEN; |
|
42 |
static const int64_t SIZE_OFFSET = 8; |
|
43 |
static const int64_t SLOT_SIZE = 8; |
|
44 |
static const int64_t CHECKPOINT_OFFSET = SIZE_OFFSET + SLOT_SIZE; |
|
45 |
static const int64_t METADATA_OFFSET = CHECKPOINT_OFFSET + SLOT_SIZE; |
|
46 |
static const int64_t START_NANOS_OFFSET = METADATA_OFFSET + SLOT_SIZE; |
|
47 |
static const int64_t DURATION_NANOS_OFFSET = START_NANOS_OFFSET + SLOT_SIZE; |
|
48 |
static const int64_t START_TICKS_OFFSET = DURATION_NANOS_OFFSET + SLOT_SIZE; |
|
49 |
static const int64_t CPU_FREQUENCY_OFFSET = START_TICKS_OFFSET + SLOT_SIZE; |
|
50 |
static const int64_t GENERATION_OFFSET = CPU_FREQUENCY_OFFSET + SLOT_SIZE; |
|
51 |
static const int64_t CAPABILITY_OFFSET = GENERATION_OFFSET + 2; |
|
52 |
static const int64_t HEADER_SIZE = CAPABILITY_OFFSET + 2; |
|
53 |
static const int64_t RESERVE_SIZE = GENERATION_OFFSET - (4 * SIZE_OFFSET); |
|
54 |
static const int64_t VOLATILE_FIELD_SIZE = SLOT_SIZE * 2; |
|
55 |
||
56 |
static const u1 COMPLETE = 0; |
|
57 |
static const u1 GUARD = 0xff; |
|
58 |
static const u1 PAD = 0; |
|
59 |
static const size_t GENERATION_SIZE = sizeof(u2); |
|
60 |
static const size_t HEAD_BUFFER_SIZE = HEADER_SIZE + SLOT_SIZE; |
|
61 |
||
62 |
typedef NoOwnershipAdapter JfrHeadBuffer; // stack local array as buffer |
|
63 |
typedef StreamWriterHost<JfrHeadBuffer, StackObj> JfrBufferedHeadWriter; |
|
64 |
typedef WriterHost<BigEndianEncoder, BigEndianEncoder, JfrBufferedHeadWriter> JfrHeadWriterBase; |
|
65 |
||
66 |
static uint8_t head_buffer[HEAD_BUFFER_SIZE] = {0}; |
|
50113 | 67 |
|
68 |
static fio_fd open_chunk(const char* path) { |
|
54263
3cabb47758c9
8217362: Emergency dump does not work when disk=false is set
mgronlun
parents:
53897
diff
changeset
|
69 |
return path != NULL ? os::open(path, O_CREAT | O_RDWR, S_IREAD | S_IWRITE) : invalid_fd; |
50113 | 70 |
} |
71 |
||
57360 | 72 |
class JfrChunkHeadWriter : public StackObj { |
73 |
friend class JfrChunkWriter; |
|
74 |
private: |
|
75 |
JfrChunkWriter* _writer; |
|
76 |
JfrChunk* _chunk; |
|
77 |
||
78 |
void write_magic() { |
|
79 |
assert(MAGIC_OFFSET == _writer->current_offset(), "invariant"); |
|
80 |
_writer->bytes("FLR", MAGIC_LEN); |
|
81 |
} |
|
82 |
||
83 |
void write_version() { |
|
84 |
assert(VERSION_OFFSET == _writer->current_offset(), "invariant"); |
|
85 |
_writer->be_write((u2)JFR_VERSION_MAJOR); |
|
86 |
_writer->be_write((u2)JFR_VERSION_MINOR); |
|
87 |
} |
|
88 |
||
89 |
void write_size(int64_t size) { |
|
90 |
assert(SIZE_OFFSET == _writer->current_offset(), "invariant"); |
|
91 |
_writer->be_write(size); |
|
92 |
} |
|
93 |
||
94 |
void write_checkpoint() { |
|
95 |
assert(CHECKPOINT_OFFSET == _writer->current_offset(), "invariant"); |
|
96 |
_writer->be_write(_chunk->last_checkpoint_offset()); |
|
97 |
} |
|
98 |
||
99 |
void write_metadata() { |
|
100 |
assert(METADATA_OFFSET == _writer->current_offset(), "invariant"); |
|
101 |
_writer->be_write(_chunk->last_metadata_offset()); |
|
102 |
} |
|
103 |
||
104 |
void write_time(bool finalize) { |
|
105 |
assert(_writer->is_valid(), "invariant"); |
|
106 |
assert(_chunk != NULL, "invariant"); |
|
107 |
assert(START_NANOS_OFFSET == _writer->current_offset(), "invariant"); |
|
108 |
if (finalize) { |
|
109 |
_writer->be_write(_chunk->previous_start_nanos()); |
|
110 |
_writer->be_write(_chunk->last_chunk_duration()); |
|
111 |
_writer->be_write(_chunk->previous_start_ticks()); |
|
112 |
return; |
|
113 |
} |
|
114 |
_writer->be_write(_chunk->start_nanos()); |
|
115 |
_writer->be_write(_chunk->duration()); |
|
116 |
_writer->be_write(_chunk->start_ticks()); |
|
117 |
} |
|
118 |
||
119 |
void write_cpu_frequency() { |
|
120 |
assert(CPU_FREQUENCY_OFFSET == _writer->current_offset(), "invariant"); |
|
121 |
static const jlong frequency = JfrTime::frequency(); |
|
122 |
_writer->be_write(frequency); |
|
123 |
} |
|
124 |
||
125 |
void write_capabilities() { |
|
126 |
assert(CAPABILITY_OFFSET == _writer->current_offset(), "invariant"); |
|
50113 | 127 |
// chunk capabilities, CompressedIntegers etc |
57360 | 128 |
static bool compressed_integers = JfrOptionSet::compressed_integers(); |
129 |
_writer->be_write(compressed_integers ? (u2)1 : (u2)0); |
|
130 |
} |
|
131 |
||
132 |
void write_generation(bool finalize) { |
|
133 |
assert(GENERATION_OFFSET == _writer->current_offset(), "invariant"); |
|
134 |
_writer->be_write(finalize ? COMPLETE : _chunk->generation()); |
|
135 |
_writer->be_write(PAD); |
|
136 |
} |
|
137 |
||
138 |
void write_guard() { |
|
139 |
assert(GENERATION_OFFSET == _writer->current_offset(), "invariant"); |
|
140 |
_writer->be_write(GUARD); |
|
141 |
_writer->be_write(PAD); |
|
142 |
} |
|
143 |
||
144 |
void write_guard_flush() { |
|
145 |
assert(GENERATION_OFFSET == _writer->current_offset(), "invariant"); |
|
146 |
write_guard(); |
|
147 |
_writer->flush(); |
|
50113 | 148 |
} |
57360 | 149 |
|
150 |
void initialize() { |
|
151 |
assert(_writer->is_valid(), "invariant"); |
|
152 |
assert(_chunk != NULL, "invariant"); |
|
153 |
assert(0 == _writer->current_offset(), "invariant"); |
|
154 |
write_magic(); |
|
155 |
write_version(); |
|
156 |
write_size(HEADER_SIZE); |
|
157 |
write_checkpoint(); |
|
158 |
write_metadata(); |
|
159 |
write_time(false); |
|
160 |
write_cpu_frequency(); |
|
161 |
write_generation(false); |
|
162 |
write_capabilities(); |
|
163 |
assert(HEADER_SIZE == _writer->current_offset(), "invariant"); |
|
164 |
_writer->flush(); |
|
165 |
} |
|
166 |
||
167 |
void flush(int64_t size, bool finalize) { |
|
168 |
assert(_writer->is_valid(), "invariant"); |
|
169 |
assert(_chunk != NULL, "invariant"); |
|
170 |
assert(SIZE_OFFSET == _writer->current_offset(), "invariant"); |
|
171 |
write_size(size); |
|
172 |
write_checkpoint(); |
|
173 |
write_metadata(); |
|
174 |
write_time(finalize); |
|
175 |
write_cpu_frequency(); |
|
176 |
write_generation(finalize); |
|
177 |
// no need to write capabilities |
|
178 |
_writer->seek(size); // implicit flush |
|
179 |
} |
|
180 |
||
181 |
JfrChunkHeadWriter(JfrChunkWriter* writer, int64_t offset) : _writer(writer), _chunk(writer->_chunk) { |
|
182 |
assert(_writer != NULL, "invariant"); |
|
183 |
assert(_writer->is_valid(), "invariant"); |
|
184 |
assert(_chunk != NULL, "invariant"); |
|
185 |
if (0 == _writer->current_offset()) { |
|
186 |
assert(HEADER_SIZE == offset, "invariant"); |
|
187 |
initialize(); |
|
188 |
} else { |
|
189 |
_writer->seek(GENERATION_OFFSET); |
|
190 |
write_guard(); |
|
191 |
_writer->seek(offset); |
|
192 |
} |
|
193 |
assert(offset == _writer->current_offset(), "invariant"); |
|
194 |
} |
|
195 |
}; |
|
196 |
||
197 |
JfrChunkWriter::JfrChunkWriter() : JfrChunkWriterBase(NULL), _chunk(new JfrChunk()) {} |
|
198 |
||
199 |
JfrChunkWriter::~JfrChunkWriter() { |
|
200 |
assert(_chunk != NULL, "invariant"); |
|
201 |
delete _chunk; |
|
50113 | 202 |
} |
203 |
||
57360 | 204 |
void JfrChunkWriter::set_path(const char* path) { |
205 |
assert(_chunk != NULL, "invariant"); |
|
206 |
_chunk->set_path(path); |
|
207 |
} |
|
208 |
||
209 |
void JfrChunkWriter::time_stamp_chunk_now() { |
|
210 |
assert(_chunk != NULL, "invariant"); |
|
211 |
_chunk->update_time_to_now(); |
|
50113 | 212 |
} |
213 |
||
57360 | 214 |
int64_t JfrChunkWriter::flushpoint(bool finalize) { |
215 |
assert(_chunk != NULL, "invariant"); |
|
216 |
const int64_t sz_written = size_written(); |
|
217 |
if (!finalize) { |
|
218 |
_chunk->update(); |
|
219 |
} |
|
220 |
JfrChunkHeadWriter head(this, SIZE_OFFSET); |
|
221 |
head.flush(sz_written, finalize); |
|
222 |
return sz_written; |
|
50113 | 223 |
} |
224 |
||
53897
0abec72a3ac2
8217647: JFR: recordings on 32-bit systems unreadable
bulasevich
parents:
50117
diff
changeset
|
225 |
int64_t JfrChunkWriter::size_written() const { |
50113 | 226 |
return this->is_valid() ? this->current_offset() : 0; |
227 |
} |
|
228 |
||
57360 | 229 |
int64_t JfrChunkWriter::last_checkpoint_offset() const { |
230 |
assert(_chunk != NULL, "invariant"); |
|
231 |
return _chunk->last_checkpoint_offset(); |
|
232 |
} |
|
233 |
||
234 |
int64_t JfrChunkWriter::current_chunk_start_nanos() const { |
|
235 |
assert(_chunk != NULL, "invariant"); |
|
236 |
return this->is_valid() ? _chunk->start_nanos() : invalid_time; |
|
237 |
} |
|
238 |
||
239 |
void JfrChunkWriter::set_last_checkpoint_offset(int64_t offset) { |
|
240 |
assert(_chunk != NULL, "invariant"); |
|
241 |
_chunk->set_last_checkpoint_offset(offset); |
|
242 |
} |
|
243 |
||
244 |
bool JfrChunkWriter::is_initial_flushpoint_for_chunk() const { |
|
245 |
assert(_chunk != NULL, "invariant"); |
|
246 |
assert(_chunk->is_started(), "invariant"); |
|
247 |
assert(!_chunk->is_finished(), "invariant"); |
|
248 |
return _chunk->is_initial_flush(); |
|
50113 | 249 |
} |
250 |
||
57360 | 251 |
void JfrChunkWriter::set_last_metadata_offset(int64_t offset) { |
252 |
assert(_chunk != NULL, "invariant"); |
|
253 |
_chunk->set_last_metadata_offset(offset); |
|
254 |
} |
|
255 |
||
256 |
bool JfrChunkWriter::has_metadata() const { |
|
257 |
assert(_chunk != NULL, "invariant"); |
|
258 |
return _chunk->has_metadata(); |
|
50113 | 259 |
} |
260 |
||
57360 | 261 |
bool JfrChunkWriter::open() { |
262 |
assert(_chunk != NULL, "invariant"); |
|
263 |
JfrChunkWriterBase::reset(open_chunk(_chunk->path())); |
|
264 |
const bool is_open = this->has_valid_fd(); |
|
265 |
if (is_open) { |
|
266 |
assert(0 == this->current_offset(), "invariant"); |
|
267 |
_chunk->reset(); |
|
268 |
JfrChunkHeadWriter head(this, HEADER_SIZE); |
|
269 |
} |
|
270 |
return is_open; |
|
50113 | 271 |
} |
57360 | 272 |
|
273 |
int64_t JfrChunkWriter::close() { |
|
274 |
assert(this->has_valid_fd(), "invariant"); |
|
275 |
const int64_t size_written = flushpoint(true); |
|
276 |
this->close_fd(); |
|
277 |
assert(!this->is_valid(), "invariant"); |
|
278 |
return size_written; |
|
279 |
} |