src/hotspot/share/jfr/leakprofiler/chains/edgeUtils.cpp
author egahlin
Tue, 15 May 2018 20:24:34 +0200
changeset 50113 caf115bb98ad
child 55571 49102ba8cf14
child 58678 9cf78a70fa4f
permissions -rw-r--r--
8199712: Flight Recorder Reviewed-by: coleenp, ihse, erikj, dsamersoff, mseledtsov, egahlin, mgronlun Contributed-by: erik.gahlin@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
/*
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     2
 * Copyright (c) 2014, 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 "classfile/javaClasses.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    27
#include "jfr/leakprofiler/chains/edge.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    28
#include "jfr/leakprofiler/chains/edgeStore.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    29
#include "jfr/leakprofiler/chains/edgeUtils.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    30
#include "jfr/leakprofiler/utilities/unifiedOop.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    31
#include "oops/fieldStreams.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    32
#include "oops/instanceKlass.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    33
#include "oops/objArrayOop.inline.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    34
#include "oops/oopsHierarchy.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    35
#include "runtime/handles.inline.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    36
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    37
bool EdgeUtils::is_leak_edge(const Edge& edge) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    38
  return (const Edge*)edge.pointee()->mark() == &edge;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    39
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    40
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    41
bool EdgeUtils::is_root(const Edge& edge) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    42
  return edge.is_root();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    43
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    44
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    45
static int field_offset(const Edge& edge) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    46
  assert(!edge.is_root(), "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    47
  const oop ref_owner = edge.reference_owner();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    48
  assert(ref_owner != NULL, "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    49
  const oop* reference = UnifiedOop::decode(edge.reference());
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    50
  assert(reference != NULL, "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    51
  assert(!UnifiedOop::is_narrow(reference), "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    52
  assert(!ref_owner->is_array(), "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    53
  assert(ref_owner->is_instance(), "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    54
  const int offset = (int)pointer_delta(reference, ref_owner, sizeof(char));
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    55
  assert(offset < (ref_owner->size() * HeapWordSize), "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    56
  return offset;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    57
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    58
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    59
static const InstanceKlass* field_type(const Edge& edge) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    60
  assert(!edge.is_root() || !EdgeUtils::is_array_element(edge), "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    61
  return (const InstanceKlass*)edge.reference_owner_klass();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    62
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    63
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    64
const Symbol* EdgeUtils::field_name_symbol(const Edge& edge) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    65
  assert(!edge.is_root(), "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    66
  assert(!is_array_element(edge), "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    67
  const int offset = field_offset(edge);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    68
  const InstanceKlass* ik = field_type(edge);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    69
  while (ik != NULL) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    70
    JavaFieldStream jfs(ik);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    71
    while (!jfs.done()) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    72
      if (offset == jfs.offset()) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    73
        return jfs.name();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    74
      }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    75
      jfs.next();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    76
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    77
    ik = (InstanceKlass*)ik->super();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    78
  }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    79
  return NULL;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    80
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    81
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    82
jshort EdgeUtils::field_modifiers(const Edge& edge) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    83
  const int offset = field_offset(edge);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    84
  const InstanceKlass* ik = field_type(edge);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    85
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    86
  while (ik != NULL) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    87
    JavaFieldStream jfs(ik);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    88
    while (!jfs.done()) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    89
      if (offset == jfs.offset()) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    90
        return jfs.access_flags().as_short();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    91
      }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    92
      jfs.next();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    93
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    94
    ik = (InstanceKlass*)ik->super();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    95
  }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    96
  return 0;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    97
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    98
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    99
bool EdgeUtils::is_array_element(const Edge& edge) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   100
  assert(!edge.is_root(), "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   101
  const oop ref_owner = edge.reference_owner();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   102
  assert(ref_owner != NULL, "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   103
  return ref_owner->is_objArray();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   104
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   105
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   106
static int array_offset(const Edge& edge) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   107
  assert(!edge.is_root(), "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   108
  const oop ref_owner = edge.reference_owner();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   109
  assert(ref_owner != NULL, "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   110
  const oop* reference = UnifiedOop::decode(edge.reference());
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   111
  assert(reference != NULL, "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   112
  assert(!UnifiedOop::is_narrow(reference), "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   113
  assert(ref_owner->is_array(), "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   114
  const objArrayOop ref_owner_array = static_cast<const objArrayOop>(ref_owner);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   115
  const int offset = (int)pointer_delta(reference, ref_owner_array->base(), heapOopSize);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   116
  assert(offset >= 0 && offset < ref_owner_array->length(), "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   117
  return offset;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   118
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   119
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   120
int EdgeUtils::array_index(const Edge& edge) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   121
  return is_array_element(edge) ? array_offset(edge) : 0;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   122
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   123
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   124
int EdgeUtils::array_size(const Edge& edge) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   125
  if (is_array_element(edge)) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   126
    const oop ref_owner = edge.reference_owner();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   127
    assert(ref_owner != NULL, "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   128
    assert(ref_owner->is_objArray(), "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   129
    return ((objArrayOop)(ref_owner))->length();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   130
  }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   131
  return 0;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   132
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   133
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   134
const Edge* EdgeUtils::root(const Edge& edge) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   135
  const Edge* current = &edge;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   136
  const Edge* parent = current->parent();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   137
  while (parent != NULL) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   138
    current = parent;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   139
    parent = current->parent();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   140
  }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   141
  return current;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   142
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   143
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   144
// The number of references associated with the leak node;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   145
// can be viewed as the leak node "context".
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   146
// Used to provide leak context for a "capped/skipped" reference chain.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   147
static const size_t leak_context = 100;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   148
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   149
// The number of references associated with the root node;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   150
// can be viewed as the root node "context".
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   151
// Used to provide root context for a "capped/skipped" reference chain.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   152
static const size_t root_context = 100;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   153
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   154
// A limit on the reference chain depth to be serialized,
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   155
static const size_t max_ref_chain_depth = leak_context + root_context;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   156
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   157
const RoutableEdge* skip_to(const RoutableEdge& edge, size_t skip_length) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   158
  const RoutableEdge* current = &edge;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   159
  const RoutableEdge* parent = current->physical_parent();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   160
  size_t seek = 0;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   161
  while (parent != NULL && seek != skip_length) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   162
    seek++;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   163
    current = parent;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   164
    parent = parent->physical_parent();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   165
  }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   166
  return current;
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
#ifdef ASSERT
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   170
static void validate_skip_target(const RoutableEdge* skip_target) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   171
  assert(skip_target != NULL, "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   172
  assert(skip_target->distance_to_root() + 1 == root_context, "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   173
  assert(skip_target->is_sentinel(), "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   174
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   175
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   176
static void validate_new_skip_edge(const RoutableEdge* new_skip_edge, const RoutableEdge* last_skip_edge, size_t adjustment) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   177
  assert(new_skip_edge != NULL, "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   178
  assert(new_skip_edge->is_skip_edge(), "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   179
  if (last_skip_edge != NULL) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   180
    const RoutableEdge* const target = skip_to(*new_skip_edge->logical_parent(), adjustment);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   181
    validate_skip_target(target->logical_parent());
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   182
    return;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   183
  }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   184
  assert(last_skip_edge == NULL, "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   185
  // only one level of logical indirection
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   186
  validate_skip_target(new_skip_edge->logical_parent());
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   187
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   188
#endif // ASSERT
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   189
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   190
static void install_logical_route(const RoutableEdge* new_skip_edge, size_t skip_target_distance) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   191
  assert(new_skip_edge != NULL, "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   192
  assert(!new_skip_edge->is_skip_edge(), "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   193
  assert(!new_skip_edge->processed(), "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   194
  const RoutableEdge* const skip_target = skip_to(*new_skip_edge, skip_target_distance);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   195
  assert(skip_target != NULL, "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   196
  new_skip_edge->set_skip_edge(skip_target);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   197
  new_skip_edge->set_skip_length(skip_target_distance);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   198
  assert(new_skip_edge->is_skip_edge(), "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   199
  assert(new_skip_edge->logical_parent() == skip_target, "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   200
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   201
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   202
static const RoutableEdge* find_last_skip_edge(const RoutableEdge& edge, size_t& distance) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   203
  assert(distance == 0, "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   204
  const RoutableEdge* current = &edge;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   205
  while (current != NULL) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   206
    if (current->is_skip_edge() && current->skip_edge()->is_sentinel()) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   207
      return current;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   208
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   209
    current = current->physical_parent();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   210
    ++distance;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   211
  }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   212
  return current;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   213
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   214
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   215
static void collapse_overlapping_chain(const RoutableEdge& edge,
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   216
                                       const RoutableEdge* first_processed_edge,
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   217
                                       size_t first_processed_distance) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   218
  assert(first_processed_edge != NULL, "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   219
  // first_processed_edge is already processed / written
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   220
  assert(first_processed_edge->processed(), "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   221
  assert(first_processed_distance + 1 <= leak_context, "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   222
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   223
  // from this first processed edge, attempt to fetch the last skip edge
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   224
  size_t last_skip_edge_distance = 0;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   225
  const RoutableEdge* const last_skip_edge = find_last_skip_edge(*first_processed_edge, last_skip_edge_distance);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   226
  const size_t distance_discovered = first_processed_distance + last_skip_edge_distance + 1;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   227
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   228
  if (distance_discovered <= leak_context || (last_skip_edge == NULL && distance_discovered <= max_ref_chain_depth)) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   229
    // complete chain can be accommodated without modification
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   230
    return;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   231
  }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   232
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   233
  // backtrack one edge from existing processed edge
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   234
  const RoutableEdge* const new_skip_edge = skip_to(edge, first_processed_distance - 1);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   235
  assert(new_skip_edge != NULL, "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   236
  assert(!new_skip_edge->processed(), "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   237
  assert(new_skip_edge->parent() == first_processed_edge, "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   238
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   239
  size_t adjustment = 0;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   240
  if (last_skip_edge != NULL) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   241
    assert(leak_context - 1 > first_processed_distance - 1, "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   242
    adjustment = leak_context - first_processed_distance - 1;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   243
    assert(last_skip_edge_distance + 1 > adjustment, "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   244
    install_logical_route(new_skip_edge, last_skip_edge_distance + 1 - adjustment);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   245
  } else {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   246
    install_logical_route(new_skip_edge, last_skip_edge_distance + 1 - root_context);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   247
    new_skip_edge->logical_parent()->set_skip_length(1); // sentinel
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   248
  }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   249
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   250
  DEBUG_ONLY(validate_new_skip_edge(new_skip_edge, last_skip_edge, adjustment);)
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   251
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   252
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   253
static void collapse_non_overlapping_chain(const RoutableEdge& edge,
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   254
                                           const RoutableEdge* first_processed_edge,
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   255
                                           size_t first_processed_distance) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   256
  assert(first_processed_edge != NULL, "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   257
  assert(!first_processed_edge->processed(), "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   258
  // this implies that the first "processed" edge is the leak context relative "leaf"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   259
  assert(first_processed_distance + 1 == leak_context, "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   260
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   261
  const size_t distance_to_root = edge.distance_to_root();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   262
  if (distance_to_root + 1 <= max_ref_chain_depth) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   263
    // complete chain can be accommodated without constructing a skip edge
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   264
    return;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   265
  }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   266
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   267
  install_logical_route(first_processed_edge, distance_to_root + 1 - first_processed_distance - root_context);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   268
  first_processed_edge->logical_parent()->set_skip_length(1); // sentinel
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   269
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   270
  DEBUG_ONLY(validate_new_skip_edge(first_processed_edge, NULL, 0);)
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   271
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   272
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   273
static const RoutableEdge* processed_edge(const RoutableEdge& edge, size_t& distance) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   274
  assert(distance == 0, "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   275
  const RoutableEdge* current = &edge;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   276
  while (current != NULL && distance < leak_context - 1) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   277
    if (current->processed()) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   278
      return current;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   279
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   280
    current = current->physical_parent();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   281
    ++distance;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   282
  }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   283
  assert(distance <= leak_context - 1, "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   284
  return current;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   285
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   286
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   287
/*
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   288
 * Some vocabulary:
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   289
 * -----------
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   290
 * "Context" is an interval in the chain, it is associcated with an edge and it signifies a number of connected edges.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   291
 * "Processed / written" means an edge that has already been serialized.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   292
 * "Skip edge" is an edge that contains additional information for logical routing purposes.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   293
 * "Skip target" is an edge used as a destination for a skip edge
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   294
 */
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   295
void EdgeUtils::collapse_chain(const RoutableEdge& edge) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   296
  assert(is_leak_edge(edge), "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   297
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   298
  // attempt to locate an already processed edge inside current leak context (if any)
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   299
  size_t first_processed_distance = 0;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   300
  const RoutableEdge* const first_processed_edge = processed_edge(edge, first_processed_distance);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   301
  if (first_processed_edge == NULL) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   302
    return;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   303
  }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   304
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   305
  if (first_processed_edge->processed()) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   306
    collapse_overlapping_chain(edge, first_processed_edge, first_processed_distance);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   307
  } else {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   308
    collapse_non_overlapping_chain(edge, first_processed_edge, first_processed_distance);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   309
  }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   310
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   311
  assert(edge.logical_distance_to_root() + 1 <= max_ref_chain_depth, "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   312
}