src/hotspot/share/jfr/recorder/repository/jfrChunk.cpp
branchJEP-349-branch
changeset 57361 53dccc90a5be
parent 53897 0abec72a3ac2
child 57870 00860d9caf4d
equal deleted inserted replaced
57360:5d043a159d5c 57361:53dccc90a5be
       
     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/jfrChunk.hpp"
       
    29 #include "jfr/recorder/repository/jfrChunkWriter.hpp"
       
    30 #include "jfr/utilities/jfrTimeConverter.hpp"
       
    31 #include "logging/log.hpp"
       
    32 #include "runtime/os.inline.hpp"
       
    33 #include "runtime/thread.inline.hpp"
       
    34 
       
    35 static const u1 GUARD = 0xff;
       
    36 
       
    37 static jlong nanos_now() {
       
    38   return os::javaTimeMillis() * JfrTimeConverter::NANOS_PER_MILLISEC;
       
    39 }
       
    40 
       
    41 static jlong ticks_now() {
       
    42   return JfrTicks::now();
       
    43 }
       
    44 
       
    45 JfrChunk::JfrChunk() :
       
    46   _path(NULL),
       
    47   _start_ticks(0),
       
    48   _previous_start_ticks(invalid_time),
       
    49   _start_nanos(0),
       
    50   _previous_start_nanos(invalid_time),
       
    51   _last_update_nanos(0),
       
    52   _last_checkpoint_offset(0),
       
    53   _last_metadata_offset(0),
       
    54   _generation(1) {}
       
    55 
       
    56 JfrChunk::~JfrChunk() {
       
    57   reset();
       
    58 }
       
    59 
       
    60 void JfrChunk::reset() {
       
    61   if (_path != NULL) {
       
    62     JfrCHeapObj::free(_path, strlen(_path) + 1);
       
    63     _path = NULL;
       
    64   }
       
    65   _last_checkpoint_offset = _last_metadata_offset = 0;
       
    66   _generation = 1;
       
    67 }
       
    68 
       
    69 void JfrChunk::set_last_checkpoint_offset(int64_t offset) {
       
    70   _last_checkpoint_offset = offset;
       
    71 }
       
    72 
       
    73 int64_t JfrChunk::last_checkpoint_offset() const {
       
    74   return _last_checkpoint_offset;
       
    75 }
       
    76 
       
    77 int64_t JfrChunk::start_ticks() const {
       
    78   assert(_start_ticks != 0, "invariant");
       
    79   return _start_ticks;
       
    80 }
       
    81 
       
    82 int64_t JfrChunk::start_nanos() const {
       
    83   assert(_start_nanos != 0, "invariant");
       
    84   return _start_nanos;
       
    85 }
       
    86 
       
    87 int64_t JfrChunk::previous_start_ticks() const {
       
    88   assert(_previous_start_ticks != invalid_time, "invariant");
       
    89   return _previous_start_ticks;
       
    90 }
       
    91 
       
    92 int64_t JfrChunk::previous_start_nanos() const {
       
    93   assert(_previous_start_nanos != invalid_time, "invariant");
       
    94   return _previous_start_nanos;
       
    95 }
       
    96 
       
    97 void JfrChunk::update_start_ticks() {
       
    98   _start_ticks = ticks_now();
       
    99 }
       
   100 
       
   101 void JfrChunk::update_start_nanos() {
       
   102   _start_nanos = _last_update_nanos = nanos_now();
       
   103 }
       
   104 
       
   105 void JfrChunk::update() {
       
   106   _last_update_nanos = nanos_now();
       
   107 }
       
   108 
       
   109 void JfrChunk::save_current_and_update_start_ticks() {
       
   110   _previous_start_ticks = _start_ticks;
       
   111   update_start_ticks();
       
   112 }
       
   113 
       
   114 void JfrChunk::save_current_and_update_start_nanos() {
       
   115   _previous_start_nanos = _start_nanos;
       
   116   update_start_nanos();
       
   117 }
       
   118 
       
   119 void JfrChunk::update_time_to_now() {
       
   120   save_current_and_update_start_nanos();
       
   121   save_current_and_update_start_ticks();
       
   122 }
       
   123 
       
   124 int64_t JfrChunk::last_chunk_duration() const {
       
   125   assert(_previous_start_nanos != invalid_time, "invariant");
       
   126   return _start_nanos - _previous_start_nanos;
       
   127 }
       
   128 
       
   129 static char* copy_path(const char* path) {
       
   130   assert(path != NULL, "invariant");
       
   131   const size_t path_len = strlen(path);
       
   132   char* new_path = JfrCHeapObj::new_array<char>(path_len + 1);
       
   133   strncpy(new_path, path, path_len + 1);
       
   134   return new_path;
       
   135 }
       
   136 
       
   137 void JfrChunk::set_path(const char* path) {
       
   138   if (_path != NULL) {
       
   139     JfrCHeapObj::free(_path, strlen(_path) + 1);
       
   140     _path = NULL;
       
   141   }
       
   142   if (path != NULL) {
       
   143     _path = copy_path(path);
       
   144   }
       
   145 }
       
   146 
       
   147 const char* JfrChunk::path() const {
       
   148   return _path;
       
   149 }
       
   150 
       
   151 bool JfrChunk::is_started() const {
       
   152   return _start_nanos != 0;
       
   153 }
       
   154 
       
   155 bool JfrChunk::is_finished() const {
       
   156   return 0 == _generation;
       
   157 }
       
   158 
       
   159 bool JfrChunk::is_initial_flush() const {
       
   160   return 0 == _last_metadata_offset;
       
   161 }
       
   162 
       
   163 int64_t JfrChunk::duration() const {
       
   164   assert(_last_update_nanos >= _start_nanos, "invariant");
       
   165   return _last_update_nanos - _start_nanos;
       
   166 }
       
   167 
       
   168 int64_t JfrChunk::last_metadata_offset() const {
       
   169   return _last_metadata_offset;
       
   170 }
       
   171 
       
   172 void JfrChunk::set_last_metadata_offset(int64_t offset) {
       
   173   if (0 == offset) {
       
   174     return;
       
   175   }
       
   176   assert(offset > _last_metadata_offset, "invariant");
       
   177   _last_metadata_offset = offset;
       
   178 }
       
   179 
       
   180 bool JfrChunk::has_metadata() const {
       
   181   return 0 != _last_metadata_offset;
       
   182 }
       
   183 
       
   184 u1 JfrChunk::generation() const {
       
   185   assert(_generation > 0, "invariant");
       
   186   const u1 this_generation = _generation++;
       
   187   if (GUARD == _generation) {
       
   188     _generation = 1;
       
   189   }
       
   190   return this_generation;
       
   191 }
       
   192