apetushkov@9858: /* apetushkov@9858: * Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved. apetushkov@9858: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. apetushkov@9858: * apetushkov@9858: * This code is free software; you can redistribute it and/or modify it apetushkov@9858: * under the terms of the GNU General Public License version 2 only, as apetushkov@9858: * published by the Free Software Foundation. apetushkov@9858: * apetushkov@9858: * This code is distributed in the hope that it will be useful, but WITHOUT apetushkov@9858: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or apetushkov@9858: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License apetushkov@9858: * version 2 for more details (a copy is included in the LICENSE file that apetushkov@9858: * accompanied this code). apetushkov@9858: * apetushkov@9858: * You should have received a copy of the GNU General Public License version apetushkov@9858: * 2 along with this work; if not, write to the Free Software Foundation, apetushkov@9858: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. apetushkov@9858: * apetushkov@9858: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA apetushkov@9858: * or visit www.oracle.com if you need additional information or have any apetushkov@9858: * questions. apetushkov@9858: * apetushkov@9858: */ apetushkov@9858: apetushkov@9858: #include "precompiled.hpp" apetushkov@9858: #include "jfr/recorder/repository/jfrChunkState.hpp" apetushkov@9858: #include "jfr/recorder/repository/jfrChunkWriter.hpp" apetushkov@9858: #include "jfr/recorder/service/jfrOptionSet.hpp" apetushkov@9858: #include "jfr/utilities/jfrTime.hpp" apetushkov@9858: #include "jfr/utilities/jfrTypes.hpp" apetushkov@9858: #include "runtime/mutexLocker.hpp" apetushkov@9858: #include "runtime/os.hpp" apetushkov@9858: #include "runtime/os.hpp" apetushkov@9858: bulasevich@9947: static const u2 JFR_VERSION_MAJOR = 2; bulasevich@9947: static const u2 JFR_VERSION_MINOR = 0; apetushkov@9858: static const size_t MAGIC_LEN = 4; apetushkov@9858: static const size_t FILEHEADER_SLOT_SIZE = 8; apetushkov@9858: static const size_t CHUNK_SIZE_OFFSET = 8; apetushkov@9858: apetushkov@9858: JfrChunkWriter::JfrChunkWriter() : JfrChunkWriterBase(NULL), _chunkstate(NULL) {} apetushkov@9858: apetushkov@9858: bool JfrChunkWriter::initialize() { apetushkov@9858: assert(_chunkstate == NULL, "invariant"); apetushkov@9858: _chunkstate = new JfrChunkState(); apetushkov@9858: return _chunkstate != NULL; apetushkov@9858: } apetushkov@9858: apetushkov@9858: static fio_fd open_existing(const char* path) { apetushkov@9858: return os::open(path, O_RDWR, S_IREAD | S_IWRITE); apetushkov@9858: } apetushkov@9858: apetushkov@9858: static fio_fd open_chunk(const char* path) { apetushkov@9858: assert(JfrStream_lock->owned_by_self(), "invariant"); apetushkov@9858: return path != NULL ? open_existing(path) : invalid_fd; apetushkov@9858: } apetushkov@9858: apetushkov@9858: bool JfrChunkWriter::open() { apetushkov@9858: assert(_chunkstate != NULL, "invariant"); apetushkov@9858: JfrChunkWriterBase::reset(open_chunk(_chunkstate->path())); apetushkov@9858: const bool is_open = this->has_valid_fd(); apetushkov@9858: if (is_open) { apetushkov@9858: this->bytes("FLR", MAGIC_LEN); apetushkov@9858: this->be_write((u2)JFR_VERSION_MAJOR); apetushkov@9858: this->be_write((u2)JFR_VERSION_MINOR); apetushkov@9858: this->reserve(6 * FILEHEADER_SLOT_SIZE); apetushkov@9858: // u8 chunk_size apetushkov@9858: // u8 initial checkpoint offset apetushkov@9858: // u8 metadata section offset apetushkov@9858: // u8 chunk start nanos apetushkov@9858: // u8 chunk duration nanos apetushkov@9858: // u8 chunk start ticks apetushkov@9858: this->be_write(JfrTime::frequency()); apetushkov@9858: // chunk capabilities, CompressedIntegers etc apetushkov@9858: this->be_write((u4)JfrOptionSet::compressed_integers() ? 1 : 0); apetushkov@9858: _chunkstate->reset(); apetushkov@9858: } apetushkov@9858: return is_open; apetushkov@9858: } apetushkov@9858: bulasevich@9947: size_t JfrChunkWriter::close(int64_t metadata_offset) { apetushkov@9858: write_header(metadata_offset); apetushkov@9858: this->flush(); apetushkov@9858: this->close_fd(); bulasevich@9947: return (size_t)size_written(); apetushkov@9858: } apetushkov@9858: bulasevich@9947: void JfrChunkWriter::write_header(int64_t metadata_offset) { apetushkov@9858: assert(this->is_valid(), "invariant"); apetushkov@9858: // Chunk size apetushkov@9858: this->write_be_at_offset((jlong)size_written(), CHUNK_SIZE_OFFSET); apetushkov@9858: // initial checkpoint event offset apetushkov@9858: this->write_be_at_offset(_chunkstate->previous_checkpoint_offset(), CHUNK_SIZE_OFFSET + (1 * FILEHEADER_SLOT_SIZE)); apetushkov@9858: // metadata event offset apetushkov@9858: this->write_be_at_offset((jlong)metadata_offset, CHUNK_SIZE_OFFSET + (2 * FILEHEADER_SLOT_SIZE)); apetushkov@9858: // start of chunk in nanos since epoch apetushkov@9858: this->write_be_at_offset(_chunkstate->previous_start_nanos(), CHUNK_SIZE_OFFSET + (3 * FILEHEADER_SLOT_SIZE)); apetushkov@9858: // duration of chunk in nanos apetushkov@9858: this->write_be_at_offset(_chunkstate->last_chunk_duration(), CHUNK_SIZE_OFFSET + (4 * FILEHEADER_SLOT_SIZE)); apetushkov@9858: // start of chunk in ticks apetushkov@9858: this->write_be_at_offset(_chunkstate->previous_start_ticks(), CHUNK_SIZE_OFFSET + (5 * FILEHEADER_SLOT_SIZE)); apetushkov@9858: } apetushkov@9858: apetushkov@9858: void JfrChunkWriter::set_chunk_path(const char* chunk_path) { apetushkov@9858: _chunkstate->set_path(chunk_path); apetushkov@9858: } apetushkov@9858: bulasevich@9947: int64_t JfrChunkWriter::size_written() const { apetushkov@9858: return this->is_valid() ? this->current_offset() : 0; apetushkov@9858: } apetushkov@9858: bulasevich@9947: int64_t JfrChunkWriter::previous_checkpoint_offset() const { apetushkov@9858: return _chunkstate->previous_checkpoint_offset(); apetushkov@9858: } apetushkov@9858: bulasevich@9947: void JfrChunkWriter::set_previous_checkpoint_offset(int64_t offset) { apetushkov@9858: _chunkstate->set_previous_checkpoint_offset(offset); apetushkov@9858: } apetushkov@9858: apetushkov@9858: void JfrChunkWriter::time_stamp_chunk_now() { apetushkov@9858: _chunkstate->update_time_to_now(); apetushkov@9858: }