1 /*
   2  * Copyright (c) 1997, 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_CLASSFILE_STRINGTABLE_HPP
  26 #define SHARE_VM_CLASSFILE_STRINGTABLE_HPP
  27 
  28 #include "gc/shared/oopStorage.hpp"
  29 #include "gc/shared/oopStorageParState.hpp"
  30 #include "memory/allocation.hpp"
  31 #include "memory/padded.hpp"
  32 #include "oops/oop.hpp"
  33 #include "oops/weakHandle.hpp"
  34 #include "utilities/concurrentHashTable.hpp"
  35 
  36 template <class T, class N> class CompactHashtable;
  37 class CompactStringTableWriter;
  38 class SerializeClosure;
  39 
  40 class StringTable;
  41 class StringTableConfig;
  42 typedef ConcurrentHashTable<WeakHandle<vm_string_table_data>,
  43                             StringTableConfig, mtSymbol> StringTableHash;
  44 
  45 class StringTableCreateEntry;
  46 
  47 class StringTable : public CHeapObj<mtSymbol>{
  48   friend class VMStructs;
  49   friend class Symbol;
  50   friend class StringTableConfig;
  51   friend class StringTableCreateEntry;
  52 
  53 private:
  54   void grow(JavaThread* jt);
  55   void clean_dead_entries(JavaThread* jt);
  56 
  57   // The string table
  58   static StringTable* _the_table;
  59   // Shared string table
  60   static CompactHashtable<oop, char> _shared_table;
  61   static bool _shared_string_mapped;
  62   static bool _alt_hash;
  63 private:
  64 
  65    // Set if one bucket is out of balance due to hash algorithm deficiency
  66   StringTableHash* _local_table;
  67   size_t _current_size;
  68   volatile bool _has_work;
  69   volatile bool _needs_rehashing;
  70 
  71   OopStorage* _weak_handles;
  72 
  73   volatile size_t _items;
  74   DEFINE_PAD_MINUS_SIZE(1, 64, sizeof(volatile size_t));
  75   volatile size_t _uncleaned_items;
  76   DEFINE_PAD_MINUS_SIZE(2, 64, sizeof(volatile size_t));
  77 
  78   double get_load_factor();
  79   double get_dead_factor();
  80 
  81   void check_concurrent_work();
  82   void trigger_concurrent_work();
  83 
  84   static uintx item_added();
  85   static void item_removed();
  86   static size_t items_to_clean(size_t ncl);
  87 
  88   StringTable();
  89 
  90   static oop intern(Handle string_or_null_h, jchar* name, int len, TRAPS);
  91   oop do_intern(Handle string_or_null, jchar* name, int len, uintx hash, TRAPS);
  92   oop do_lookup(jchar* name, int len, uintx hash);
  93 
  94   void concurrent_work(JavaThread* jt);
  95   void print_table_statistics(outputStream* st, const char* table_name);
  96 
  97   void try_rehash_table();
  98   bool do_rehash();
  99 
 100  public:
 101   // The string table
 102   static StringTable* the_table() { return _the_table; }
 103   size_t table_size(Thread* thread = NULL);
 104 
 105   static OopStorage* weak_storage() { return the_table()->_weak_handles; }
 106 
 107   static void create_table() {
 108     assert(_the_table == NULL, "One string table allowed.");
 109     _the_table = new StringTable();
 110   }
 111 
 112   static void do_concurrent_work(JavaThread* jt);
 113   static bool has_work() { return the_table()->_has_work; }
 114 
 115   // GC support
 116   //   Delete pointers to otherwise-unreachable objects.
 117   static void unlink(BoolObjectClosure* cl) {
 118     unlink_or_oops_do(cl);
 119   }
 120   static void unlink_or_oops_do(BoolObjectClosure* is_alive, OopClosure* f = NULL,
 121                                 int* processed = NULL, int* removed = NULL);
 122 
 123   // Serially invoke "f->do_oop" on the locations of all oops in the table.
 124   static void oops_do(OopClosure* f);
 125 
 126   // Possibly parallel versions of the above
 127   static void possibly_parallel_unlink(
 128      OopStorage::ParState<false /* concurrent */, false /* const*/>* par_state_string,
 129      BoolObjectClosure* cl, int* processed, int* removed);
 130   static void possibly_parallel_oops_do(
 131      OopStorage::ParState<false /* concurrent */, false /* const*/>* par_state_string,
 132      OopClosure* f);
 133 
 134   // Probing
 135   static oop lookup(Symbol* symbol);
 136   static oop lookup(jchar* chars, int length);
 137 
 138   // Interning
 139   static oop intern(Symbol* symbol, TRAPS);
 140   static oop intern(oop string, TRAPS);
 141   static oop intern(const char *utf8_string, TRAPS);
 142 
 143   // Rehash the string table if it gets out of balance
 144   static void rehash_table();
 145   static bool needs_rehashing()
 146     { return StringTable::the_table()->_needs_rehashing; }
 147 
 148   // Sharing
 149   oop lookup_shared(jchar* name, int len, unsigned int hash);
 150   static oop create_archived_string(oop s, Thread* THREAD);
 151   static void set_shared_string_mapped() { _shared_string_mapped = true; }
 152   static bool shared_string_mapped()       { return _shared_string_mapped; }
 153   static void shared_oops_do(OopClosure* f) NOT_CDS_JAVA_HEAP_RETURN;
 154   static void copy_shared_string(CompactStringTableWriter* ch_table)
 155     NOT_CDS_JAVA_HEAP_RETURN;
 156   static void write_to_archive() NOT_CDS_JAVA_HEAP_RETURN;
 157   static void serialize(SerializeClosure* soc) NOT_CDS_JAVA_HEAP_RETURN;
 158 
 159   // Jcmd
 160   static void dump(outputStream* st, bool verbose=false);
 161   // Debugging
 162   static void verify();
 163 };
 164 
 165 #endif // SHARE_VM_CLASSFILE_STRINGTABLE_HPP