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 "logging/log.hpp"
33 #include "runtime/os.inline.hpp"
34 #include "runtime/thread.inline.hpp"
35 
JfrChunkState()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   _last_checkpoint_offset(0) {}
43 
~JfrChunkState()44 JfrChunkState::~JfrChunkState() {
45   reset();
46 }
47 
reset()48 void JfrChunkState::reset() {
49   if (_path != NULL) {
50     JfrCHeapObj::free(_path, strlen(_path) + 1);
51     _path = NULL;
52   }
53   set_last_checkpoint_offset(0);
54 }
55 
set_last_checkpoint_offset(int64_t offset)56 void JfrChunkState::set_last_checkpoint_offset(int64_t offset) {
57   _last_checkpoint_offset = offset;
58 }
59 
last_checkpoint_offset() const60 int64_t JfrChunkState::last_checkpoint_offset() const {
61   return _last_checkpoint_offset;
62 }
63 
previous_start_ticks() const64 int64_t JfrChunkState::previous_start_ticks() const {
65   return _previous_start_ticks;
66 }
67 
previous_start_nanos() const68 int64_t JfrChunkState::previous_start_nanos() const {
69   return _previous_start_nanos;
70 }
71 
update_start_ticks()72 void JfrChunkState::update_start_ticks() {
73   _start_ticks = JfrTicks::now();
74 }
75 
update_start_nanos()76 void JfrChunkState::update_start_nanos() {
77   _start_nanos = os::javaTimeMillis() * JfrTimeConverter::NANOS_PER_MILLISEC;
78 }
79 
save_current_and_update_start_ticks()80 void JfrChunkState::save_current_and_update_start_ticks() {
81   _previous_start_ticks = _start_ticks;
82   update_start_ticks();
83 }
84 
save_current_and_update_start_nanos()85 void JfrChunkState::save_current_and_update_start_nanos() {
86   _previous_start_nanos = _start_nanos;
87   update_start_nanos();
88 }
89 
update_time_to_now()90 void JfrChunkState::update_time_to_now() {
91   save_current_and_update_start_nanos();
92   save_current_and_update_start_ticks();
93 }
94 
last_chunk_duration() const95 int64_t JfrChunkState::last_chunk_duration() const {
96   return _start_nanos - _previous_start_nanos;
97 }
98 
copy_path(const char * path)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 + 1);
104   return new_path;
105 }
106 
set_path(const char * path)107 void JfrChunkState::set_path(const char* path) {
108   assert(JfrStream_lock->owned_by_self(), "invariant");
109   if (_path != NULL) {
110     JfrCHeapObj::free(_path, strlen(_path) + 1);
111     _path = NULL;
112   }
113   if (path != NULL) {
114     _path = copy_path(path);
115   }
116 }
117 
path() const118 const char* JfrChunkState::path() const {
119   return _path;
120 }
121