1 /*
  2  * Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 
 25 #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 
 35 class StoredEdge : public Edge {
 36  private:
 37   mutable traceid _gc_root_id;
 38   size_t _skip_length;
 39 
 40  public:
 41   StoredEdge();
 42   StoredEdge(const Edge* parent, UnifiedOopRef reference);
 43   StoredEdge(const Edge& edge);
 44   StoredEdge(const StoredEdge& edge);
 45 
 46   traceid gc_root_id() const { return _gc_root_id; }
 47   void set_gc_root_id(traceid root_id) const { _gc_root_id = root_id; }
 48 
 49   bool is_skip_edge() const { return _skip_length != 0; }
 50   size_t skip_length() const { return _skip_length; }
 51   void set_skip_length(size_t length) { _skip_length = length; }
 52 
 53   void set_parent(const Edge* edge) { this->_parent = edge; }
 54 
 55   StoredEdge* parent() const {
 56     return const_cast<StoredEdge*>(static_cast<const StoredEdge*>(Edge::parent()));
 57   }
 58 };
 59 
 60 class EdgeStore : public CHeapObj<mtTracing> {
 61   typedef HashTableHost<StoredEdge, traceid, JfrHashtableEntry, EdgeStore> EdgeHashTable;
 62   typedef EdgeHashTable::HashEntry EdgeEntry;
 63   template <typename,
 64             typename,
 65             template<typename, typename> class,
 66             typename,
 67             size_t>
 68   friend class HashTableHost;
 69   friend class EventEmitter;
 70   friend class ObjectSampleWriter;
 71   friend class ObjectSampleCheckpoint;
 72  private:
 73   static traceid _edge_id_counter;
 74   EdgeHashTable* _edges;
 75 
 76   // Hash table callbacks
 77   void on_link(EdgeEntry* entry);
 78   bool on_equals(uintptr_t hash, const EdgeEntry* entry);
 79   void on_unlink(EdgeEntry* entry);
 80 
 81   StoredEdge* get(UnifiedOopRef reference) const;
 82   StoredEdge* put(UnifiedOopRef reference);
 83   traceid gc_root_id(const Edge* edge) const;
 84 
 85   bool put_edges(StoredEdge** previous, const Edge** current, size_t length);
 86   bool put_skip_edge(StoredEdge** previous, const Edge** current, size_t distance_to_root);
 87   void put_chain_epilogue(StoredEdge* leak_context_edge, const Edge* root) const;
 88 
 89   StoredEdge* associate_leak_context_with_candidate(const Edge* edge);
 90   void store_gc_root_id_in_leak_context_edge(StoredEdge* leak_context_edge, const Edge* root) const;
 91   StoredEdge* link_new_edge(StoredEdge** previous, const Edge** current);
 92   void link_with_existing_chain(const StoredEdge* current_stored, StoredEdge** previous, size_t previous_length);
 93 
 94   template <typename T>
 95   void iterate(T& functor) const { _edges->iterate_value<T>(functor); }
 96 
 97   DEBUG_ONLY(bool contains(UnifiedOopRef reference) const;)
 98 
 99  public:
100   EdgeStore();
101   ~EdgeStore();
102 
103   bool is_empty() const;
104   traceid get_id(const Edge* edge) const;
105   void put_chain(const Edge* chain, size_t length);
106 };
107 
108 #endif // SHARE_JFR_LEAKPROFILER_CHAINS_EDGESTORE_HPP