src/hotspot/share/jfr/recorder/repository/jfrChunk.cpp
changeset 58863 c16ac7a2eba4
parent 58132 caa25ab47aca
child 59226 a0f39cc47387
equal deleted inserted replaced
58861:2c3cc4b01880 58863:c16ac7a2eba4
       
     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/repository/jfrChunk.hpp"
       
    27 #include "jfr/recorder/service/jfrOptionSet.hpp"
       
    28 #include "jfr/utilities/jfrTime.hpp"
       
    29 #include "jfr/utilities/jfrTimeConverter.hpp"
       
    30 #include "jfr/utilities/jfrTypes.hpp"
       
    31 #include "runtime/os.inline.hpp"
       
    32 
       
    33 static const char* const MAGIC = "FLR";
       
    34 static const u2 JFR_VERSION_MAJOR = 2;
       
    35 static const u2 JFR_VERSION_MINOR = 1;
       
    36 
       
    37 // strictly monotone
       
    38 static jlong nanos_now() {
       
    39   static jlong last = 0;
       
    40   const jlong now = os::javaTimeMillis() * JfrTimeConverter::NANOS_PER_MILLISEC;
       
    41   if (now > last) {
       
    42     last = now;
       
    43   } else {
       
    44     ++last;
       
    45   }
       
    46   return last;
       
    47 }
       
    48 
       
    49 static jlong ticks_now() {
       
    50   return JfrTicks::now();
       
    51 }
       
    52 
       
    53 JfrChunk::JfrChunk() :
       
    54   _path(NULL),
       
    55   _start_ticks(0),
       
    56   _previous_start_ticks(invalid_time),
       
    57   _start_nanos(0),
       
    58   _previous_start_nanos(invalid_time),
       
    59   _last_update_nanos(0),
       
    60   _last_checkpoint_offset(0),
       
    61   _last_metadata_offset(0),
       
    62   _generation(1) {}
       
    63 
       
    64 JfrChunk::~JfrChunk() {
       
    65   reset();
       
    66 }
       
    67 
       
    68 void JfrChunk::reset() {
       
    69   if (_path != NULL) {
       
    70     JfrCHeapObj::free(_path, strlen(_path) + 1);
       
    71     _path = NULL;
       
    72   }
       
    73   _last_checkpoint_offset = _last_metadata_offset = 0;
       
    74   _generation = 1;
       
    75 }
       
    76 
       
    77 const char* JfrChunk::magic() const {
       
    78   return MAGIC;
       
    79 }
       
    80 
       
    81 u2 JfrChunk::major_version() const {
       
    82   return JFR_VERSION_MAJOR;
       
    83 }
       
    84 
       
    85 u2 JfrChunk::minor_version() const {
       
    86   return JFR_VERSION_MINOR;
       
    87 }
       
    88 
       
    89 u2 JfrChunk::capabilities() const {
       
    90   // chunk capabilities, CompressedIntegers etc
       
    91   static bool compressed_integers = JfrOptionSet::compressed_integers();
       
    92   return compressed_integers;
       
    93 }
       
    94 
       
    95 int64_t JfrChunk::cpu_frequency() const {
       
    96   static const jlong frequency = JfrTime::frequency();
       
    97   return frequency;
       
    98 }
       
    99 
       
   100 void JfrChunk::set_last_checkpoint_offset(int64_t offset) {
       
   101   _last_checkpoint_offset = offset;
       
   102 }
       
   103 
       
   104 int64_t JfrChunk::last_checkpoint_offset() const {
       
   105   return _last_checkpoint_offset;
       
   106 }
       
   107 
       
   108 int64_t JfrChunk::start_ticks() const {
       
   109   assert(_start_ticks != 0, "invariant");
       
   110   return _start_ticks;
       
   111 }
       
   112 
       
   113 int64_t JfrChunk::start_nanos() const {
       
   114   assert(_start_nanos != 0, "invariant");
       
   115   return _start_nanos;
       
   116 }
       
   117 
       
   118 int64_t JfrChunk::previous_start_ticks() const {
       
   119   assert(_previous_start_ticks != invalid_time, "invariant");
       
   120   return _previous_start_ticks;
       
   121 }
       
   122 
       
   123 int64_t JfrChunk::previous_start_nanos() const {
       
   124   assert(_previous_start_nanos != invalid_time, "invariant");
       
   125   return _previous_start_nanos;
       
   126 }
       
   127 
       
   128 void JfrChunk::update_start_ticks() {
       
   129   _start_ticks = ticks_now();
       
   130 }
       
   131 
       
   132 void JfrChunk::update_start_nanos() {
       
   133   const jlong now = nanos_now();
       
   134   assert(now > _start_nanos, "invariant");
       
   135   assert(now > _last_update_nanos, "invariant");
       
   136   _start_nanos = _last_update_nanos = now;
       
   137 }
       
   138 
       
   139 void JfrChunk::update_current_nanos() {
       
   140   const jlong now = nanos_now();
       
   141   assert(now > _last_update_nanos, "invariant");
       
   142   _last_update_nanos = now;
       
   143 }
       
   144 
       
   145 void JfrChunk::save_current_and_update_start_ticks() {
       
   146   _previous_start_ticks = _start_ticks;
       
   147   update_start_ticks();
       
   148 }
       
   149 
       
   150 void JfrChunk::save_current_and_update_start_nanos() {
       
   151   _previous_start_nanos = _start_nanos;
       
   152   update_start_nanos();
       
   153 }
       
   154 
       
   155 void JfrChunk::set_time_stamp() {
       
   156   save_current_and_update_start_nanos();
       
   157   save_current_and_update_start_ticks();
       
   158 }
       
   159 
       
   160 int64_t JfrChunk::last_chunk_duration() const {
       
   161   assert(_previous_start_nanos != invalid_time, "invariant");
       
   162   return _start_nanos - _previous_start_nanos;
       
   163 }
       
   164 
       
   165 static char* copy_path(const char* path) {
       
   166   assert(path != NULL, "invariant");
       
   167   const size_t path_len = strlen(path);
       
   168   char* new_path = JfrCHeapObj::new_array<char>(path_len + 1);
       
   169   strncpy(new_path, path, path_len + 1);
       
   170   return new_path;
       
   171 }
       
   172 
       
   173 void JfrChunk::set_path(const char* path) {
       
   174   if (_path != NULL) {
       
   175     JfrCHeapObj::free(_path, strlen(_path) + 1);
       
   176     _path = NULL;
       
   177   }
       
   178   if (path != NULL) {
       
   179     _path = copy_path(path);
       
   180   }
       
   181 }
       
   182 
       
   183 const char* JfrChunk::path() const {
       
   184   return _path;
       
   185 }
       
   186 
       
   187 bool JfrChunk::is_started() const {
       
   188   return _start_nanos != 0;
       
   189 }
       
   190 
       
   191 bool JfrChunk::is_finished() const {
       
   192   return 0 == _generation;
       
   193 }
       
   194 
       
   195 int64_t JfrChunk::duration() const {
       
   196   assert(_last_update_nanos >= _start_nanos, "invariant");
       
   197   return _last_update_nanos - _start_nanos;
       
   198 }
       
   199 
       
   200 int64_t JfrChunk::last_metadata_offset() const {
       
   201   return _last_metadata_offset;
       
   202 }
       
   203 
       
   204 void JfrChunk::set_last_metadata_offset(int64_t offset) {
       
   205   assert(offset > _last_metadata_offset, "invariant");
       
   206   _last_metadata_offset = offset;
       
   207 }
       
   208 
       
   209 bool JfrChunk::has_metadata() const {
       
   210   return 0 != _last_metadata_offset;
       
   211 }
       
   212 
       
   213 u1 JfrChunk::generation() const {
       
   214   assert(_generation > 0, "invariant");
       
   215   const u1 this_generation = _generation++;
       
   216   if (GUARD == _generation) {
       
   217     _generation = 1;
       
   218   }
       
   219   return this_generation;
       
   220 }
       
   221 
       
   222 u1 JfrChunk::next_generation() const {
       
   223   assert(_generation > 0, "invariant");
       
   224   const u1 next_gen = _generation;
       
   225   return GUARD == next_gen ? 1 : next_gen;
       
   226 }