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/dcmd/jfrDcmds.hpp"
  27 #include "jfr/recorder/jfrRecorder.hpp"
  28 #include "jfr/recorder/repository/jfrChunkState.hpp"
  29 #include "jfr/recorder/repository/jfrChunkWriter.hpp"
  30 #include "jfr/utilities/jfrTime.hpp"
  31 #include "jfr/utilities/jfrTimeConverter.hpp"
  32 #include "runtime/os.hpp"
  33 #include "runtime/thread.inline.hpp"
  34 
  35 JfrChunkState::JfrChunkState() :
  36   _path(NULL),
  37   _start_ticks(0),
  38   _start_nanos(0),
  39   _previous_start_ticks(0),
  40   _previous_start_nanos(0),
  41   _previous_checkpoint_offset(0) {}
  42 
  43 JfrChunkState::~JfrChunkState() {
  44   reset();
  45 }
  46 
  47 void JfrChunkState::reset() {
  48   if (_path != NULL) {
  49     JfrCHeapObj::free(_path, strlen(_path) + 1);
  50     _path = NULL;
  51   }
  52   set_previous_checkpoint_offset(0);
  53 }
  54 
  55 void JfrChunkState::set_previous_checkpoint_offset(jlong offset) {
  56   _previous_checkpoint_offset = offset;
  57 }
  58 
  59 jlong JfrChunkState::previous_checkpoint_offset() const {
  60   return _previous_checkpoint_offset;
  61 }
  62 
  63 jlong JfrChunkState::previous_start_ticks() const {
  64   return _previous_start_ticks;
  65 }
  66 
  67 jlong JfrChunkState::previous_start_nanos() const {
  68   return _previous_start_nanos;
  69 }
  70 
  71 void JfrChunkState::update_start_ticks() {
  72   _start_ticks = JfrTicks::now();
  73 }
  74 
  75 void JfrChunkState::update_start_nanos() {
  76   _start_nanos = (jlong)(os::javaTimeMillis() * JfrTimeConverter::NANOS_PER_MILLISEC);
  77 }
  78 
  79 void JfrChunkState::save_current_and_update_start_ticks() {
  80   _previous_start_ticks = _start_ticks;
  81   update_start_ticks();
  82 }
  83 
  84 void JfrChunkState::save_current_and_update_start_nanos() {
  85   _previous_start_nanos = _start_nanos;
  86   update_start_nanos();
  87 }
  88 
  89 void JfrChunkState::update_time_to_now() {
  90   save_current_and_update_start_nanos();
  91   save_current_and_update_start_ticks();
  92 }
  93 
  94 jlong JfrChunkState::last_chunk_duration() const {
  95   return _start_nanos - _previous_start_nanos;
  96 }
  97 
  98 static char* copy_path(const char* path) {
  99   assert(path != NULL, "invariant");
 100   const size_t path_len = strlen(path);
 101   char* new_path = JfrCHeapObj::new_array<char>(path_len + 1);
 102   strncpy(new_path, path, path_len + 1);
 103   return new_path;
 104 }
 105 
 106 void JfrChunkState::set_path(const char* path) {
 107   assert(JfrStream_lock->owned_by_self(), "invariant");
 108   if (_path != NULL) {
 109     JfrCHeapObj::free(_path, strlen(_path) + 1);
 110     _path = NULL;
 111   }
 112   if (path != NULL) {
 113     _path = copy_path(path);
 114   }
 115 }
 116 
 117 const char* JfrChunkState::path() const {
 118   return _path;
 119 }