src/hotspot/share/jfr/recorder/checkpoint/jfrCheckpointWriter.cpp
author iignatyev
Mon, 10 Dec 2018 11:04:55 -0800
changeset 52934 8deeb7bba516
parent 50113 caf115bb98ad
child 53897 0abec72a3ac2
permissions -rw-r--r--
8214917: CTW testlibrary shouldn't ignore errors raised by the library itself Reviewed-by: kvn, roland
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     1
/*
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     2
 * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     4
 *
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     7
 * published by the Free Software Foundation.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     8
 *
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    13
 * accompanied this code).
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    14
 *
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    18
 *
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    21
 * questions.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    22
 *
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    23
 */
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    24
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    25
#include "precompiled.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    26
#include "jfr/recorder/checkpoint/jfrCheckpointManager.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    27
#include "jfr/recorder/checkpoint/jfrCheckpointWriter.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    28
#include "jfr/writers/jfrBigEndianWriter.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    29
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    30
JfrCheckpointFlush::JfrCheckpointFlush(Type* old, size_t used, size_t requested, Thread* t) :
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    31
  _result(JfrCheckpointManager::flush(old, used, requested, t)) {}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    32
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    33
JfrCheckpointWriter::JfrCheckpointWriter(bool flushpoint, bool header, Thread* thread) :
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    34
  JfrCheckpointWriterBase(JfrCheckpointManager::lease_buffer(thread), thread),
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    35
  _time(JfrTicks::now()),
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    36
  _offset(0),
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    37
  _count(0),
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    38
  _flushpoint(flushpoint),
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    39
  _header(header) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    40
  assert(this->is_acquired(), "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    41
  assert(0 == this->current_offset(), "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    42
  if (_header) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    43
    reserve(sizeof(JfrCheckpointEntry));
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    44
  }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    45
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    46
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    47
static void write_checkpoint_header(u1* pos, jlong size, jlong time, bool flushpoint, juint type_count) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    48
  assert(pos != NULL, "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    49
  JfrBigEndianWriter be_writer(pos, sizeof(JfrCheckpointEntry));
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    50
  be_writer.write(size);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    51
  be_writer.write(time);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    52
  be_writer.write(JfrTicks::now().value() - time);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    53
  be_writer.write(flushpoint ? (juint)1 : (juint)0);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    54
  be_writer.write(type_count);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    55
  assert(be_writer.is_valid(), "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    56
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    57
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    58
JfrCheckpointWriter::~JfrCheckpointWriter() {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    59
  assert(this->is_acquired(), "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    60
  if (!this->is_valid() || !_header) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    61
    release();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    62
    return;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    63
  }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    64
  if (0 == count()) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    65
    assert(this->used_size() == sizeof(JfrCheckpointEntry), "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    66
    this->seek(_offset);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    67
    release();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    68
    return;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    69
  }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    70
  assert(_header, "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    71
  assert(this->is_valid(), "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    72
  assert(count() > 0, "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    73
  assert(this->used_size() > sizeof(JfrCheckpointEntry), "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    74
  const jlong size = this->current_offset();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    75
  assert(size + this->start_pos() == this->current_pos(), "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    76
  write_checkpoint_header(const_cast<u1*>(this->start_pos()), size, _time, is_flushpoint(), count());
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    77
  release();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    78
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    79
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    80
void JfrCheckpointWriter::set_flushpoint(bool flushpoint) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    81
  _flushpoint = flushpoint;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    82
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    83
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    84
bool JfrCheckpointWriter::is_flushpoint() const {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    85
  return _flushpoint;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    86
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    87
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    88
juint JfrCheckpointWriter::count() const {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    89
  return _count;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    90
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    91
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    92
void JfrCheckpointWriter::set_count(juint count) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    93
  _count = count;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    94
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    95
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    96
void JfrCheckpointWriter::release() {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    97
  assert(this->is_acquired(), "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    98
  if (!this->is_valid() || this->used_size() == 0) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    99
    return;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   100
  }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   101
  assert(this->used_size() > 0, "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   102
  // write through to backing storage
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   103
  this->commit();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   104
  assert(0 == this->current_offset(), "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   105
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   106
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   107
void JfrCheckpointWriter::write_type(JfrTypeId type_id) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   108
  assert(type_id < TYPES_END, "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   109
  write<u8>(type_id);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   110
  increment();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   111
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   112
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   113
void JfrCheckpointWriter::write_key(u8 key) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   114
  write<u8>(key);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   115
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   116
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   117
void JfrCheckpointWriter::increment() {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   118
  ++_count;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   119
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   120
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   121
void JfrCheckpointWriter::write_count(u4 nof_entries) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   122
  write<u4>((u4)nof_entries);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   123
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   124
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   125
void JfrCheckpointWriter::write_count(u4 nof_entries, jlong offset) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   126
  write_padded_at_offset(nof_entries, offset);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   127
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   128
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   129
const u1* JfrCheckpointWriter::session_data(size_t* size, const JfrCheckpointContext* ctx /* 0 */) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   130
  assert(this->is_acquired(), "wrong state!");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   131
  if (!this->is_valid()) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   132
    *size = 0;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   133
    return NULL;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   134
  }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   135
  if (ctx != NULL) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   136
    const u1* session_start_pos = this->start_pos() + ctx->offset;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   137
    *size = this->current_pos() - session_start_pos;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   138
    return session_start_pos;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   139
  }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   140
  *size = this->used_size();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   141
  assert(this->start_pos() + *size == this->current_pos(), "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   142
  write_checkpoint_header(const_cast<u1*>(this->start_pos()), this->used_offset(), _time, is_flushpoint(), count());
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   143
  this->seek(_offset + (_header ? sizeof(JfrCheckpointEntry) : 0));
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   144
  set_count(0);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   145
  return this->start_pos();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   146
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   147
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   148
const JfrCheckpointContext JfrCheckpointWriter::context() const {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   149
  JfrCheckpointContext ctx;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   150
  ctx.offset = this->current_offset();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   151
  ctx.count = this->count();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   152
  return ctx;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   153
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   154
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   155
void JfrCheckpointWriter::set_context(const JfrCheckpointContext ctx) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   156
  this->seek(ctx.offset);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   157
  set_count(ctx.count);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   158
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   159
bool JfrCheckpointWriter::has_data() const {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   160
  return this->used_size() > sizeof(JfrCheckpointEntry);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   161
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   162
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   163
JfrCheckpointBlobHandle JfrCheckpointWriter::checkpoint_blob() {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   164
  size_t size = 0;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   165
  const u1* data = session_data(&size);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   166
  return JfrCheckpointBlob::make(data, size);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   167
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   168
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   169
JfrCheckpointBlobHandle JfrCheckpointWriter::copy(const JfrCheckpointContext* ctx /* 0 */) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   170
  if (ctx == NULL) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   171
    return checkpoint_blob();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   172
  }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   173
  size_t size = 0;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   174
  const u1* data = session_data(&size, ctx);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   175
  return JfrCheckpointBlob::make(data, size);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   176
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   177
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   178
JfrCheckpointBlobHandle JfrCheckpointWriter::move(const JfrCheckpointContext* ctx /* 0 */) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   179
  JfrCheckpointBlobHandle data = copy(ctx);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   180
  if (ctx != NULL) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   181
    const_cast<JfrCheckpointContext*>(ctx)->count = 0;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   182
    set_context(*ctx);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   183
  }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   184
  return data;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   185
}