1 /*
   2  * Copyright (c) 2014, 2018, 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_VM_LEAKPROFILER_CHAINS_EDGESTORE_HPP
  26 #define SHARE_VM_LEAKPROFILER_CHAINS_EDGESTORE_HPP
  27 
  28 #include "jfr/leakprofiler/chains/edge.hpp"
  29 #include "jfr/utilities/jfrHashtable.hpp"
  30 #include "memory/allocation.hpp"
  31 
  32 typedef u8 traceid;
  33 
  34 class StoredEdge : public Edge {
  35  private:
  36   mutable traceid _gc_root_id;
  37   size_t _skip_length;
  38 
  39  public:
  40   StoredEdge();
  41   StoredEdge(const Edge* parent, const oop* reference);
  42   StoredEdge(const Edge& edge);
  43   StoredEdge(const StoredEdge& edge);
  44   void operator=(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, Entry, 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 assign_id(EdgeEntry* entry);
  78   bool equals(const Edge& query, uintptr_t hash, const EdgeEntry* entry);
  79 
  80   StoredEdge* get(const oop* reference) const;
  81   StoredEdge* put(const oop* reference);
  82   traceid gc_root_id(const Edge* edge) const;
  83 
  84   bool put_edges(StoredEdge** previous, const Edge** current, size_t length);
  85   bool put_skip_edge(StoredEdge** previous, const Edge** current, size_t distance_to_root);
  86   void put_chain_epilogue(StoredEdge* leak_context_edge, const Edge* root) const;
  87 
  88   StoredEdge* associate_leak_context_with_candidate(const Edge* edge);
  89   void store_gc_root_id_in_leak_context_edge(StoredEdge* leak_context_edge, const Edge* root) const;
  90   StoredEdge* link_new_edge(StoredEdge** previous, const Edge** current);
  91   void link_with_existing_chain(const StoredEdge* current_stored, StoredEdge** previous, size_t previous_length);
  92 
  93   template <typename T>
  94   void iterate(T& functor) const { _edges->iterate_value<T>(functor); }
  95 
  96   DEBUG_ONLY(bool contains(const oop* reference) const;)
  97 
  98  public:
  99   EdgeStore();
 100   ~EdgeStore();
 101 
 102   bool is_empty() const;
 103   traceid get_id(const Edge* edge) const;
 104   void put_chain(const Edge* chain, size_t length);
 105 };
 106 
 107 #endif // SHARE_VM_LEAKPROFILER_CHAINS_EDGESTORE_HPP