src/hotspot/share/jfr/recorder/stringpool/jfrStringPool.cpp
author mgronlun
Wed, 30 Oct 2019 19:43:52 +0100
changeset 58863 c16ac7a2eba4
parent 54964 ec7d6d8effc7
child 59247 56bf71d64d51
permissions -rw-r--r--
8226511: Implement JFR Event Streaming Reviewed-by: egahlin, mseledtsov, mgronlun Contributed-by: erik.gahlin@oracle.com, mikhailo.seledtsov@oracle.com, markus.gronlund@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     1
/*
54964
ec7d6d8effc7 8220293: Deadlock in JFR string pool
mgronlun
parents: 50714
diff changeset
     2
 * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
50113
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 "classfile/javaClasses.inline.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    27
#include "jfr/recorder/checkpoint/types/traceid/jfrTraceIdEpoch.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    28
#include "jfr/recorder/service/jfrOptionSet.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    29
#include "jfr/recorder/storage/jfrMemorySpace.inline.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    30
#include "jfr/recorder/repository/jfrChunkWriter.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    31
#include "jfr/recorder/storage/jfrStorageUtils.inline.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    32
#include "jfr/recorder/stringpool/jfrStringPool.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    33
#include "jfr/recorder/stringpool/jfrStringPoolWriter.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    34
#include "jfr/utilities/jfrTypes.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    35
#include "logging/log.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    36
#include "runtime/mutexLocker.hpp"
50429
83aec1d357d4 8204301: Make OrderAccess functions available to hpp rather than inline.hpp files
coleenp
parents: 50113
diff changeset
    37
#include "runtime/orderAccess.hpp"
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    38
#include "runtime/safepoint.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    39
#include "runtime/thread.inline.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    40
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    41
typedef JfrStringPool::Buffer* BufferPtr;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    42
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    43
static JfrStringPool* _instance = NULL;
58863
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 54964
diff changeset
    44
static uint64_t store_generation = 0;
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 54964
diff changeset
    45
static uint64_t serialized_generation = 0;
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 54964
diff changeset
    46
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 54964
diff changeset
    47
inline void set_generation(uint64_t value, uint64_t* const dest) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 54964
diff changeset
    48
  assert(dest != NULL, "invariant");
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 54964
diff changeset
    49
  OrderAccess::release_store(dest, value);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 54964
diff changeset
    50
}
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 54964
diff changeset
    51
static void increment_store_generation() {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 54964
diff changeset
    52
  const uint64_t current_serialized = OrderAccess::load_acquire(&serialized_generation);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 54964
diff changeset
    53
  const uint64_t current_stored = OrderAccess::load_acquire(&store_generation);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 54964
diff changeset
    54
  if (current_serialized == current_stored) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 54964
diff changeset
    55
    set_generation(current_serialized + 1, &store_generation);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 54964
diff changeset
    56
  }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 54964
diff changeset
    57
}
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 54964
diff changeset
    58
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 54964
diff changeset
    59
static bool increment_serialized_generation() {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 54964
diff changeset
    60
  const uint64_t current_stored = OrderAccess::load_acquire(&store_generation);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 54964
diff changeset
    61
  const uint64_t current_serialized = OrderAccess::load_acquire(&serialized_generation);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 54964
diff changeset
    62
  if (current_stored != current_serialized) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 54964
diff changeset
    63
    set_generation(current_stored, &serialized_generation);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 54964
diff changeset
    64
    return true;
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 54964
diff changeset
    65
  }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 54964
diff changeset
    66
  return false;
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 54964
diff changeset
    67
}
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 54964
diff changeset
    68
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 54964
diff changeset
    69
bool JfrStringPool::is_modified() {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 54964
diff changeset
    70
  return increment_serialized_generation();
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 54964
diff changeset
    71
}
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    72
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    73
JfrStringPool& JfrStringPool::instance() {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    74
  return *_instance;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    75
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    76
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    77
JfrStringPool* JfrStringPool::create(JfrChunkWriter& cw) {
58863
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 54964
diff changeset
    78
  store_generation = 0;
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 54964
diff changeset
    79
  serialized_generation = 0;
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    80
  assert(_instance == NULL, "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    81
  _instance = new JfrStringPool(cw);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    82
  return _instance;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    83
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    84
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    85
void JfrStringPool::destroy() {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    86
  assert(_instance != NULL, "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    87
  delete _instance;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    88
  _instance = NULL;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    89
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    90
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    91
JfrStringPool::JfrStringPool(JfrChunkWriter& cw) : _free_list_mspace(NULL), _lock(NULL), _chunkwriter(cw) {}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    92
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    93
JfrStringPool::~JfrStringPool() {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    94
  if (_free_list_mspace != NULL) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    95
    delete _free_list_mspace;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    96
  }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    97
  if (_lock != NULL) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    98
    delete _lock;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    99
  }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   100
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   101
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   102
static const size_t unlimited_mspace_size = 0;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   103
static const size_t string_pool_cache_count = 2;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   104
static const size_t string_pool_buffer_size = 512 * K;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   105
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   106
bool JfrStringPool::initialize() {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   107
  assert(_free_list_mspace == NULL, "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   108
  _free_list_mspace = new JfrStringPoolMspace(string_pool_buffer_size, unlimited_mspace_size, string_pool_cache_count, this);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   109
  if (_free_list_mspace == NULL || !_free_list_mspace->initialize()) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   110
    return false;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   111
  }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   112
  assert(_lock == NULL, "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   113
  _lock = new Mutex(Monitor::leaf - 1, "Checkpoint mutex", Mutex::_allow_vm_block_flag, Monitor::_safepoint_check_never);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   114
  return _lock != NULL;
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
/*
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   118
* If the buffer was a "lease" from the global system, release back.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   119
*
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   120
* The buffer is effectively invalidated for the thread post-return,
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   121
* and the caller should take means to ensure that it is not referenced any longer.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   122
*/
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   123
static void release(BufferPtr buffer, Thread* thread) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   124
  assert(buffer != NULL, "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   125
  assert(buffer->lease(), "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   126
  assert(buffer->acquired_by_self(), "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   127
  buffer->clear_lease();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   128
  buffer->release();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   129
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   130
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   131
BufferPtr JfrStringPool::flush(BufferPtr old, size_t used, size_t requested, Thread* thread) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   132
  assert(old != NULL, "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   133
  assert(old->lease(), "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   134
  if (0 == requested) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   135
    // indicates a lease is being returned
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   136
    release(old, thread);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   137
    return NULL;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   138
  }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   139
  // migration of in-flight information
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   140
  BufferPtr const new_buffer = lease_buffer(thread, used + requested);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   141
  if (new_buffer != NULL) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   142
    migrate_outstanding_writes(old, new_buffer, used, requested);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   143
  }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   144
  release(old, thread);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   145
  return new_buffer; // might be NULL
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
static const size_t lease_retry = 10;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   149
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   150
BufferPtr JfrStringPool::lease_buffer(Thread* thread, size_t size /* 0 */) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   151
  BufferPtr buffer = mspace_get_free_lease_with_retry(size, instance()._free_list_mspace, lease_retry, thread);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   152
  if (buffer == NULL) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   153
    buffer = mspace_allocate_transient_lease_to_free(size,  instance()._free_list_mspace, thread);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   154
  }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   155
  assert(buffer->acquired_by_self(), "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   156
  assert(buffer->lease(), "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   157
  return buffer;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   158
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   159
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   160
bool JfrStringPool::add(bool epoch, jlong id, jstring string, JavaThread* jt) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   161
  assert(jt != NULL, "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   162
  const bool current_epoch = JfrTraceIdEpoch::epoch();
58863
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 54964
diff changeset
   163
  if (current_epoch != epoch) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 54964
diff changeset
   164
    return current_epoch;
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 54964
diff changeset
   165
  }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 54964
diff changeset
   166
  {
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   167
    JfrStringPoolWriter writer(jt);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   168
    writer.write(id);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   169
    writer.write(string);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   170
    writer.inc_nof_strings();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   171
  }
58863
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 54964
diff changeset
   172
  increment_store_generation();
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   173
  return current_epoch;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   174
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   175
54964
ec7d6d8effc7 8220293: Deadlock in JFR string pool
mgronlun
parents: 50714
diff changeset
   176
template <template <typename> class Operation>
ec7d6d8effc7 8220293: Deadlock in JFR string pool
mgronlun
parents: 50714
diff changeset
   177
class StringPoolOp {
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   178
 public:
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   179
  typedef JfrStringPoolBuffer Type;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   180
 private:
54964
ec7d6d8effc7 8220293: Deadlock in JFR string pool
mgronlun
parents: 50714
diff changeset
   181
  Operation<Type> _op;
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   182
  Thread* _thread;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   183
  size_t _strings_processed;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   184
 public:
54964
ec7d6d8effc7 8220293: Deadlock in JFR string pool
mgronlun
parents: 50714
diff changeset
   185
  StringPoolOp() : _op(), _thread(Thread::current()), _strings_processed(0) {}
ec7d6d8effc7 8220293: Deadlock in JFR string pool
mgronlun
parents: 50714
diff changeset
   186
  StringPoolOp(JfrChunkWriter& writer, Thread* thread) : _op(writer), _thread(thread), _strings_processed(0) {}
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   187
  bool write(Type* buffer, const u1* data, size_t size) {
54964
ec7d6d8effc7 8220293: Deadlock in JFR string pool
mgronlun
parents: 50714
diff changeset
   188
    assert(buffer->acquired_by(_thread) || buffer->retired(), "invariant");
50714
2230bb152a9f 8203299: StringPoolBuffer access covered by exclusive access invariant, remove (problematic) cas operations
mgronlun
parents: 50429
diff changeset
   189
    const uint64_t nof_strings_used = buffer->string_count();
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   190
    assert(nof_strings_used > 0, "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   191
    buffer->set_string_top(buffer->string_top() + nof_strings_used);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   192
    // "size processed" for string pool buffers is the number of processed string elements
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   193
    _strings_processed += nof_strings_used;
54964
ec7d6d8effc7 8220293: Deadlock in JFR string pool
mgronlun
parents: 50714
diff changeset
   194
    return _op.write(buffer, data, size);
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   195
  }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   196
  size_t processed() { return _strings_processed; }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   197
};
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   198
58863
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 54964
diff changeset
   199
template <typename T>
54964
ec7d6d8effc7 8220293: Deadlock in JFR string pool
mgronlun
parents: 50714
diff changeset
   200
class StringPoolDiscarderStub {
ec7d6d8effc7 8220293: Deadlock in JFR string pool
mgronlun
parents: 50714
diff changeset
   201
 public:
58863
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 54964
diff changeset
   202
  typedef T Type;
54964
ec7d6d8effc7 8220293: Deadlock in JFR string pool
mgronlun
parents: 50714
diff changeset
   203
  bool write(Type* buffer, const u1* data, size_t size) {
ec7d6d8effc7 8220293: Deadlock in JFR string pool
mgronlun
parents: 50714
diff changeset
   204
    // stub only, discard happens at higher level
ec7d6d8effc7 8220293: Deadlock in JFR string pool
mgronlun
parents: 50714
diff changeset
   205
    return true;
ec7d6d8effc7 8220293: Deadlock in JFR string pool
mgronlun
parents: 50714
diff changeset
   206
  }
ec7d6d8effc7 8220293: Deadlock in JFR string pool
mgronlun
parents: 50714
diff changeset
   207
};
ec7d6d8effc7 8220293: Deadlock in JFR string pool
mgronlun
parents: 50714
diff changeset
   208
ec7d6d8effc7 8220293: Deadlock in JFR string pool
mgronlun
parents: 50714
diff changeset
   209
typedef StringPoolOp<UnBufferedWriteToChunk> WriteOperation;
ec7d6d8effc7 8220293: Deadlock in JFR string pool
mgronlun
parents: 50714
diff changeset
   210
typedef StringPoolOp<StringPoolDiscarderStub> DiscardOperation;
ec7d6d8effc7 8220293: Deadlock in JFR string pool
mgronlun
parents: 50714
diff changeset
   211
typedef ExclusiveOp<WriteOperation> ExclusiveWriteOperation;
ec7d6d8effc7 8220293: Deadlock in JFR string pool
mgronlun
parents: 50714
diff changeset
   212
typedef ExclusiveOp<DiscardOperation> ExclusiveDiscardOperation;
ec7d6d8effc7 8220293: Deadlock in JFR string pool
mgronlun
parents: 50714
diff changeset
   213
typedef ReleaseOp<JfrStringPoolMspace> StringPoolReleaseOperation;
ec7d6d8effc7 8220293: Deadlock in JFR string pool
mgronlun
parents: 50714
diff changeset
   214
typedef CompositeOperation<ExclusiveWriteOperation, StringPoolReleaseOperation> StringPoolWriteOperation;
ec7d6d8effc7 8220293: Deadlock in JFR string pool
mgronlun
parents: 50714
diff changeset
   215
typedef CompositeOperation<ExclusiveDiscardOperation, StringPoolReleaseOperation> StringPoolDiscardOperation;
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   216
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   217
size_t JfrStringPool::write() {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   218
  Thread* const thread = Thread::current();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   219
  WriteOperation wo(_chunkwriter, thread);
54964
ec7d6d8effc7 8220293: Deadlock in JFR string pool
mgronlun
parents: 50714
diff changeset
   220
  ExclusiveWriteOperation ewo(wo);
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   221
  StringPoolReleaseOperation spro(_free_list_mspace, thread, false);
54964
ec7d6d8effc7 8220293: Deadlock in JFR string pool
mgronlun
parents: 50714
diff changeset
   222
  StringPoolWriteOperation spwo(&ewo, &spro);
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   223
  assert(_free_list_mspace->is_full_empty(), "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   224
  process_free_list(spwo, _free_list_mspace);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   225
  return wo.processed();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   226
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   227
54964
ec7d6d8effc7 8220293: Deadlock in JFR string pool
mgronlun
parents: 50714
diff changeset
   228
size_t JfrStringPool::write_at_safepoint() {
ec7d6d8effc7 8220293: Deadlock in JFR string pool
mgronlun
parents: 50714
diff changeset
   229
  assert(SafepointSynchronize::is_at_safepoint(), "invariant");
ec7d6d8effc7 8220293: Deadlock in JFR string pool
mgronlun
parents: 50714
diff changeset
   230
  return write();
ec7d6d8effc7 8220293: Deadlock in JFR string pool
mgronlun
parents: 50714
diff changeset
   231
}
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   232
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   233
size_t JfrStringPool::clear() {
58863
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 54964
diff changeset
   234
  increment_serialized_generation();
54964
ec7d6d8effc7 8220293: Deadlock in JFR string pool
mgronlun
parents: 50714
diff changeset
   235
  DiscardOperation discard_operation;
ec7d6d8effc7 8220293: Deadlock in JFR string pool
mgronlun
parents: 50714
diff changeset
   236
  ExclusiveDiscardOperation edo(discard_operation);
ec7d6d8effc7 8220293: Deadlock in JFR string pool
mgronlun
parents: 50714
diff changeset
   237
  StringPoolReleaseOperation spro(_free_list_mspace, Thread::current(), false);
ec7d6d8effc7 8220293: Deadlock in JFR string pool
mgronlun
parents: 50714
diff changeset
   238
  StringPoolDiscardOperation spdo(&edo, &spro);
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   239
  assert(_free_list_mspace->is_full_empty(), "invariant");
54964
ec7d6d8effc7 8220293: Deadlock in JFR string pool
mgronlun
parents: 50714
diff changeset
   240
  process_free_list(spdo, _free_list_mspace);
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   241
  return discard_operation.processed();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   242
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   243
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   244
void JfrStringPool::register_full(BufferPtr t, Thread* thread) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   245
  // nothing here at the moment
54964
ec7d6d8effc7 8220293: Deadlock in JFR string pool
mgronlun
parents: 50714
diff changeset
   246
  assert(t != NULL, "invariant");
ec7d6d8effc7 8220293: Deadlock in JFR string pool
mgronlun
parents: 50714
diff changeset
   247
  assert(t->acquired_by(thread), "invariant");
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   248
  assert(t->retired(), "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   249
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   250
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   251
void JfrStringPool::lock() {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   252
  assert(!_lock->owned_by_self(), "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   253
  _lock->lock_without_safepoint_check();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   254
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   255
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   256
void JfrStringPool::unlock() {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   257
  _lock->unlock();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   258
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   259
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   260
#ifdef ASSERT
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   261
bool JfrStringPool::is_locked() const {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   262
  return _lock->owned_by_self();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   263
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   264
#endif