src/share/vm/jfr/recorder/repository/jfrChunkWriter.cpp

changeset 9858
b985cbb00e68
child 9947
db357034b763
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/vm/jfr/recorder/repository/jfrChunkWriter.cpp	Mon Aug 12 18:30:40 2019 +0300
     1.3 @@ -0,0 +1,123 @@
     1.4 +/*
     1.5 + * Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +#include "precompiled.hpp"
    1.29 +#include "jfr/recorder/repository/jfrChunkState.hpp"
    1.30 +#include "jfr/recorder/repository/jfrChunkWriter.hpp"
    1.31 +#include "jfr/recorder/service/jfrOptionSet.hpp"
    1.32 +#include "jfr/utilities/jfrTime.hpp"
    1.33 +#include "jfr/utilities/jfrTypes.hpp"
    1.34 +#include "runtime/mutexLocker.hpp"
    1.35 +#include "runtime/os.hpp"
    1.36 +#include "runtime/os.hpp"
    1.37 +
    1.38 +const u2 JFR_VERSION_MAJOR = 2;
    1.39 +const u2 JFR_VERSION_MINOR = 0;
    1.40 +
    1.41 +static const size_t MAGIC_LEN = 4;
    1.42 +static const size_t FILEHEADER_SLOT_SIZE = 8;
    1.43 +static const size_t CHUNK_SIZE_OFFSET = 8;
    1.44 +
    1.45 +JfrChunkWriter::JfrChunkWriter() : JfrChunkWriterBase(NULL), _chunkstate(NULL) {}
    1.46 +
    1.47 +bool JfrChunkWriter::initialize() {
    1.48 +  assert(_chunkstate == NULL, "invariant");
    1.49 +  _chunkstate = new JfrChunkState();
    1.50 +  return _chunkstate != NULL;
    1.51 +}
    1.52 +
    1.53 +static fio_fd open_existing(const char* path) {
    1.54 +  return os::open(path, O_RDWR, S_IREAD | S_IWRITE);
    1.55 +}
    1.56 +
    1.57 +static fio_fd open_chunk(const char* path) {
    1.58 +  assert(JfrStream_lock->owned_by_self(), "invariant");
    1.59 +  return path != NULL ? open_existing(path) : invalid_fd;
    1.60 +}
    1.61 +
    1.62 +bool JfrChunkWriter::open() {
    1.63 +  assert(_chunkstate != NULL, "invariant");
    1.64 +  JfrChunkWriterBase::reset(open_chunk(_chunkstate->path()));
    1.65 +  const bool is_open = this->has_valid_fd();
    1.66 +  if (is_open) {
    1.67 +    this->bytes("FLR", MAGIC_LEN);
    1.68 +    this->be_write((u2)JFR_VERSION_MAJOR);
    1.69 +    this->be_write((u2)JFR_VERSION_MINOR);
    1.70 +    this->reserve(6 * FILEHEADER_SLOT_SIZE);
    1.71 +    // u8 chunk_size
    1.72 +    // u8 initial checkpoint offset
    1.73 +    // u8 metadata section offset
    1.74 +    // u8 chunk start nanos
    1.75 +    // u8 chunk duration nanos
    1.76 +    // u8 chunk start ticks
    1.77 +    this->be_write(JfrTime::frequency());
    1.78 +    // chunk capabilities, CompressedIntegers etc
    1.79 +    this->be_write((u4)JfrOptionSet::compressed_integers() ? 1 : 0);
    1.80 +    _chunkstate->reset();
    1.81 +  }
    1.82 +  return is_open;
    1.83 +}
    1.84 +
    1.85 +size_t JfrChunkWriter::close(intptr_t metadata_offset) {
    1.86 +  write_header(metadata_offset);
    1.87 +  this->flush();
    1.88 +  this->close_fd();
    1.89 +  return size_written();
    1.90 +}
    1.91 +
    1.92 +void JfrChunkWriter::write_header(intptr_t metadata_offset) {
    1.93 +  assert(this->is_valid(), "invariant");
    1.94 +  // Chunk size
    1.95 +  this->write_be_at_offset((jlong)size_written(), CHUNK_SIZE_OFFSET);
    1.96 +  // initial checkpoint event offset
    1.97 +  this->write_be_at_offset(_chunkstate->previous_checkpoint_offset(), CHUNK_SIZE_OFFSET + (1 * FILEHEADER_SLOT_SIZE));
    1.98 +  // metadata event offset
    1.99 +  this->write_be_at_offset((jlong)metadata_offset, CHUNK_SIZE_OFFSET + (2 * FILEHEADER_SLOT_SIZE));
   1.100 +  // start of chunk in nanos since epoch
   1.101 +  this->write_be_at_offset(_chunkstate->previous_start_nanos(), CHUNK_SIZE_OFFSET + (3 * FILEHEADER_SLOT_SIZE));
   1.102 +  // duration of chunk in nanos
   1.103 +  this->write_be_at_offset(_chunkstate->last_chunk_duration(), CHUNK_SIZE_OFFSET + (4 * FILEHEADER_SLOT_SIZE));
   1.104 +  // start of chunk in ticks
   1.105 +  this->write_be_at_offset(_chunkstate->previous_start_ticks(), CHUNK_SIZE_OFFSET + (5 * FILEHEADER_SLOT_SIZE));
   1.106 +}
   1.107 +
   1.108 +void JfrChunkWriter::set_chunk_path(const char* chunk_path) {
   1.109 +  _chunkstate->set_path(chunk_path);
   1.110 +}
   1.111 +
   1.112 +intptr_t JfrChunkWriter::size_written() const {
   1.113 +  return this->is_valid() ? this->current_offset() : 0;
   1.114 +}
   1.115 +
   1.116 +intptr_t JfrChunkWriter::previous_checkpoint_offset() const {
   1.117 +  return _chunkstate->previous_checkpoint_offset();
   1.118 +}
   1.119 +
   1.120 +void JfrChunkWriter::set_previous_checkpoint_offset(intptr_t offset) {
   1.121 +  _chunkstate->set_previous_checkpoint_offset(offset);
   1.122 +}
   1.123 +
   1.124 +void JfrChunkWriter::time_stamp_chunk_now() {
   1.125 +  _chunkstate->update_time_to_now();
   1.126 +}

mercurial