1 /*
   2  * Copyright (c) 1997, 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_CLASSFILE_STRINGTABLE_HPP
  26 #define SHARE_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 class CompactHashtableWriter;
  37 class SerializeClosure;
  38 
  39 class StringTable;
  40 class StringTableConfig;
  41 typedef ConcurrentHashTable<WeakHandle<vm_string_table_data>,
  42                             StringTableConfig, mtSymbol> StringTableHash;
  43 
  44 class StringTableCreateEntry;
  45 
  46 class StringTable : public CHeapObj<mtSymbol>{
  47   friend class VMStructs;
  48   friend class Symbol;
  49   friend class StringTableConfig;
  50   friend class StringTableCreateEntry;
  51 
  52 private:
  53   void grow(JavaThread* jt);
  54   void clean_dead_entries(JavaThread* jt);
  55 
  56   // The string table
  57   static StringTable* _the_table;
  58   static volatile bool _alt_hash;
  59 
  60 private:
  61 
  62   StringTableHash* _local_table;
  63   size_t _current_size;
  64   volatile bool _has_work;
  65   // Set if one bucket is out of balance due to hash algorithm deficiency
  66   volatile bool _needs_rehashing;
  67 
  68   OopStorage* _weak_handles;
  69 
  70   volatile size_t _items_count;
  71   DEFINE_PAD_MINUS_SIZE(1, DEFAULT_CACHE_LINE_SIZE, sizeof(volatile size_t));
  72   volatile size_t _uncleaned_items_count;
  73   DEFINE_PAD_MINUS_SIZE(2, DEFAULT_CACHE_LINE_SIZE, sizeof(volatile size_t));
  74 
  75   double get_load_factor() const;
  76   double get_dead_factor() const;
  77 
  78   void check_concurrent_work();
  79   void trigger_concurrent_work();
  80 
  81   static size_t item_added();
  82   static void item_removed();
  83   size_t add_items_to_clean(size_t ndead);
  84 
  85   StringTable();
  86 
  87   static oop intern(Handle string_or_null_h, const jchar* name, int len, TRAPS);
  88   oop do_intern(Handle string_or_null, const jchar* name, int len, uintx hash, TRAPS);
  89   oop do_lookup(const jchar* name, int len, uintx hash);
  90 
  91   void concurrent_work(JavaThread* jt);
  92   void print_table_statistics(outputStream* st, const char* table_name);
  93 
  94   void try_rehash_table();
  95   bool do_rehash();
  96   inline void update_needs_rehash(bool rehash);
  97 
  98  public:
  99   // The string table
 100   static StringTable* the_table() { return _the_table; }
 101   size_t table_size();
 102   TableStatistics get_table_statistics();
 103 
 104   static OopStorage* weak_storage() { return the_table()->_weak_handles; }
 105 
 106   static void create_table() {
 107     assert(_the_table == NULL, "One string table allowed.");
 108     _the_table = new StringTable();
 109   }
 110 
 111   static void do_concurrent_work(JavaThread* jt);
 112   static bool has_work() { return the_table()->_has_work; }
 113 
 114   // GC support
 115 
 116   // Must be called before a parallel walk where strings might die.
 117   static void reset_dead_counter() {
 118     the_table()->_uncleaned_items_count = 0;
 119   }
 120   // After the parallel walk this method must be called to trigger
 121   // cleaning. Note it might trigger a resize instead.
 122   static void finish_dead_counter() {
 123     the_table()->check_concurrent_work();
 124   }
 125 
 126   // If GC uses ParState directly it should add the number of cleared
 127   // strings to this method.
 128   static void inc_dead_counter(size_t ndead) {
 129     the_table()->add_items_to_clean(ndead);
 130   }
 131 
 132   // Serially invoke "f->do_oop" on the locations of all oops in the table.
 133   static void oops_do(OopClosure* f);
 134 
 135   // Possibly parallel versions of the above
 136   static void possibly_parallel_oops_do(
 137      OopStorage::ParState<false /* concurrent */, false /* const*/>* par_state_string,
 138      OopClosure* f);
 139 
 140   // Probing
 141   static oop lookup(Symbol* symbol);
 142   static oop lookup(const jchar* chars, int length);
 143 
 144   // Interning
 145   static oop intern(Symbol* symbol, TRAPS);
 146   static oop intern(oop string, TRAPS);
 147   static oop intern(const char *utf8_string, TRAPS);
 148 
 149   // Rehash the string table if it gets out of balance
 150   static void rehash_table();
 151   static bool needs_rehashing()
 152     { return StringTable::the_table()->_needs_rehashing; }
 153 
 154   // Sharing
 155  private:
 156   oop lookup_shared(const jchar* name, int len, unsigned int hash) NOT_CDS_JAVA_HEAP_RETURN_(NULL);
 157   static void copy_shared_string_table(CompactHashtableWriter* ch_table) NOT_CDS_JAVA_HEAP_RETURN;
 158  public:
 159   static oop create_archived_string(oop s, Thread* THREAD) NOT_CDS_JAVA_HEAP_RETURN_(NULL);
 160   static void shared_oops_do(OopClosure* f) NOT_CDS_JAVA_HEAP_RETURN;
 161   static void write_to_archive() NOT_CDS_JAVA_HEAP_RETURN;
 162   static void serialize_shared_table_header(SerializeClosure* soc) NOT_CDS_JAVA_HEAP_RETURN;
 163 
 164   // Jcmd
 165   static void dump(outputStream* st, bool verbose=false);
 166   // Debugging
 167   static size_t verify_and_compare_entries();
 168   static void verify();
 169 };
 170 
 171 #endif // SHARE_CLASSFILE_STRINGTABLE_HPP