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