1 /*
  2  * Copyright (c) 2014, 2021, 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 #ifndef SHARE_JFR_LEAKPROFILER_CHAINS_EDGESTORE_HPP
 26 #define SHARE_JFR_LEAKPROFILER_CHAINS_EDGESTORE_HPP
 27 
 28 #include "jfr/leakprofiler/chains/edge.hpp"
 29 #include "jfr/leakprofiler/utilities/unifiedOopRef.hpp"
 30 #include "jfr/utilities/jfrHashtable.hpp"
 31 #include "memory/allocation.hpp"
 32 
 33 typedef u8 traceid;
 34 class ObjectSample;
 35 
 36 class StoredEdge : public Edge {
 37  private:
 38   mutable traceid _gc_root_id;
 39   size_t _skip_length;
 40 
 41  public:
 42   StoredEdge();
 43   StoredEdge(const Edge* parent, UnifiedOopRef reference);
 44   StoredEdge(const Edge& edge);
 45   StoredEdge(const StoredEdge& edge);
 46 
 47   traceid gc_root_id() const { return _gc_root_id; }
 48   void set_gc_root_id(traceid root_id) const { _gc_root_id = root_id; }
 49 
 50   bool is_skip_edge() const { return _skip_length != 0; }
 51   size_t skip_length() const { return _skip_length; }
 52   void set_skip_length(size_t length) { _skip_length = length; }
 53 
 54   void set_parent(const Edge* edge) { this->_parent = edge; }
 55 
 56   StoredEdge* parent() const {
 57     return const_cast<StoredEdge*>(static_cast<const StoredEdge*>(Edge::parent()));
 58   }
 59 };
 60 
 61 class EdgeStore : public CHeapObj<mtTracing> {
 62   typedef HashTableHost<StoredEdge, traceid, JfrHashtableEntry, EdgeStore> EdgeHashTable;
 63   typedef EdgeHashTable::HashEntry EdgeEntry;
 64   template <typename,
 65             typename,
 66             template<typename, typename> class,
 67             typename,
 68             size_t>
 69   friend class HashTableHost;
 70   friend class EventEmitter;
 71   friend class ObjectSampleWriter;
 72   friend class ObjectSampleCheckpoint;
 73  private:
 74   static traceid _edge_id_counter;
 75   EdgeHashTable* _edges;
 76 
 77   // Hash table callbacks
 78   void on_link(EdgeEntry* entry);
 79   bool on_equals(uintptr_t hash, const EdgeEntry* entry);
 80   void on_unlink(EdgeEntry* entry);
 81 
 82   StoredEdge* get(UnifiedOopRef reference) const;
 83   const StoredEdge* get(const ObjectSample* sample) const;
 84   StoredEdge* put(UnifiedOopRef reference);
 85   traceid gc_root_id(const Edge* edge) const;
 86 
 87   bool put_edges(StoredEdge** previous, const Edge** current, size_t length);
 88   bool put_skip_edge(StoredEdge** previous, const Edge** current, size_t distance_to_root);
 89   void put_chain_epilogue(StoredEdge* leak_context_edge, const Edge* root) const;
 90 
 91   StoredEdge* associate_leak_context_with_candidate(const Edge* edge);
 92   void store_gc_root_id_in_leak_context_edge(StoredEdge* leak_context_edge, const Edge* root) const;
 93   StoredEdge* link_new_edge(StoredEdge** previous, const Edge** current);
 94   void link_with_existing_chain(const StoredEdge* current_stored, StoredEdge** previous, size_t previous_length);
 95   bool has_leak_context(const ObjectSample* sample) const;
 96 
 97   template <typename T>
 98   void iterate(T& functor) const { _edges->iterate_value<T>(functor); }
 99 
100   DEBUG_ONLY(bool contains(UnifiedOopRef reference) const;)
101 
102  public:
103   EdgeStore();
104   ~EdgeStore();
105 
106   bool is_empty() const;
107   traceid get_id(const Edge* edge) const;
108   void put_chain(const Edge* chain, size_t length);
109 };
110 
111 #endif // SHARE_JFR_LEAKPROFILER_CHAINS_EDGESTORE_HPP