1 /*
   2  * Copyright (c) 2012, 2019, 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 "jfr/recorder/access/jfrOptionSet.hpp"
  27 #include "jfr/recorder/repository/jfrChunkState.hpp"
  28 #include "jfr/recorder/repository/jfrChunkWriter.hpp"
  29 #include "jfr/utilities/jfrTraceTime.hpp"
  30 #include "jfr/utilities/jfrTypes.hpp"
  31 #include "runtime/mutexLocker.hpp"
  32 #include "runtime/os.hpp"
  33 
  34 const u2 JFR_VERSION_MAJOR = 1;
  35 const u2 JFR_VERSION_MINOR = 0;
  36 
  37 static const size_t MAGIC_LEN = 4;
  38 static const size_t FILEHEADER_SLOT_SIZE = 8;
  39 static const size_t CHUNK_SIZE_OFFSET = 8;
  40 
  41 JfrChunkWriter::JfrChunkWriter() : JfrChunkWriterBase(NULL), _chunkstate(NULL) {}
  42 
  43 bool JfrChunkWriter::initialize() {
  44   assert(_chunkstate == NULL, "invariant");
  45   _chunkstate = new JfrChunkState();
  46   return _chunkstate != NULL;
  47 }
  48 
  49 static fio_fd open_existing(const char* path) {
  50   return os::open(path, O_RDWR, S_IREAD | S_IWRITE);
  51 }
  52 
  53 static fio_fd open_chunk(const char* path) {
  54   assert(JfrStream_lock->owned_by_self(), "invariant");
  55   return path != NULL ? open_existing(path) : invalid_fd;
  56 }
  57 
  58 bool JfrChunkWriter::open() {
  59   assert(_chunkstate != NULL, "invariant");
  60   JfrChunkWriterBase::reset(open_chunk(_chunkstate->path()));
  61   const bool is_open = this->has_valid_fd();
  62   if (is_open) {
  63     this->bytes("FLR", MAGIC_LEN);
  64     this->be_write((u2)JFR_VERSION_MAJOR);
  65     this->be_write((u2)JFR_VERSION_MINOR);
  66     this->reserve(6 * FILEHEADER_SLOT_SIZE);
  67     // u8 chunk_size
  68     // u8 initial checkpoint offset
  69     // u8 metadata section offset
  70     // u8 chunk start nanos
  71     // u8 chunk duration nanos
  72     // u8 chunk start ticks
  73     this->be_write(JfrTraceTime::frequency());
  74     // chunk capabilities, CompressedIntegers etc
  75     this->be_write((u4)JfrOptionSet::compressed_integers() ? 1 : 0);
  76     _chunkstate->reset();
  77   }
  78   return is_open;
  79 }
  80 
  81 size_t JfrChunkWriter::close(intptr_t metadata_offset) {
  82   write_header(metadata_offset);
  83   this->flush();
  84   this->close_fd();
  85   return size_written();
  86 }
  87 
  88 void JfrChunkWriter::write_header(intptr_t metadata_offset) {
  89   assert(this->is_valid(), "invariant");
  90   // Chunk size
  91   this->write_be_at_offset(size_written(), CHUNK_SIZE_OFFSET);
  92   // initial checkpoint event offset
  93   this->write_be_at_offset(_chunkstate->previous_checkpoint_offset(), CHUNK_SIZE_OFFSET + (1 * FILEHEADER_SLOT_SIZE));
  94   // metadata event offset
  95   this->write_be_at_offset(metadata_offset, CHUNK_SIZE_OFFSET + (2 * FILEHEADER_SLOT_SIZE));
  96   // start of chunk in nanos since epoch
  97   this->write_be_at_offset(_chunkstate->previous_start_nanos(), CHUNK_SIZE_OFFSET + (3 * FILEHEADER_SLOT_SIZE));
  98   // duration of chunk in nanos
  99   this->write_be_at_offset(_chunkstate->last_chunk_duration(), CHUNK_SIZE_OFFSET + (4 * FILEHEADER_SLOT_SIZE));
 100   // start of chunk in ticks
 101   this->write_be_at_offset(_chunkstate->previous_start_ticks(), CHUNK_SIZE_OFFSET + (5 * FILEHEADER_SLOT_SIZE));
 102 }
 103 
 104 void JfrChunkWriter::set_chunk_path(const char* chunk_path) {
 105   _chunkstate->set_path(chunk_path);
 106 }
 107 
 108 intptr_t JfrChunkWriter::size_written() const {
 109   return this->is_valid() ? this->current_offset() : 0;
 110 }
 111 
 112 intptr_t JfrChunkWriter::previous_checkpoint_offset() const {
 113   return _chunkstate->previous_checkpoint_offset();
 114 }
 115 
 116 void JfrChunkWriter::set_previous_checkpoint_offset(intptr_t offset) {
 117   _chunkstate->set_previous_checkpoint_offset(offset);
 118 }
 119 
 120 void JfrChunkWriter::time_stamp_chunk_now() {
 121   _chunkstate->update_time_to_now();
 122 }