src/hotspot/share/classfile/stringTable.hpp
changeset 50445 bd6b78feb6a3
parent 49621 5ef28d560b6f
child 50556 e5a40146791b
equal deleted inserted replaced
50444:db65921e9a9b 50445:bd6b78feb6a3
    23  */
    23  */
    24 
    24 
    25 #ifndef SHARE_VM_CLASSFILE_STRINGTABLE_HPP
    25 #ifndef SHARE_VM_CLASSFILE_STRINGTABLE_HPP
    26 #define SHARE_VM_CLASSFILE_STRINGTABLE_HPP
    26 #define SHARE_VM_CLASSFILE_STRINGTABLE_HPP
    27 
    27 
    28 #include "utilities/hashtable.hpp"
    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"
    29 
    35 
    30 template <class T, class N> class CompactHashtable;
    36 template <class T, class N> class CompactHashtable;
    31 class CompactStringTableWriter;
    37 class CompactStringTableWriter;
    32 class FileMapInfo;
       
    33 class SerializeClosure;
    38 class SerializeClosure;
    34 
    39 
    35 class StringTable : public RehashableHashtable<oop, mtSymbol> {
    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>{
    36   friend class VMStructs;
    48   friend class VMStructs;
    37   friend class Symbol;
    49   friend class Symbol;
       
    50   friend class StringTableConfig;
       
    51   friend class StringTableCreateEntry;
    38 
    52 
    39 private:
    53 private:
       
    54   void grow(JavaThread* jt);
       
    55   void clean_dead_entries(JavaThread* jt);
       
    56 
    40   // The string table
    57   // The string table
    41   static StringTable* _the_table;
    58   static StringTable* _the_table;
    42 
       
    43   // Shared string table
    59   // Shared string table
    44   static CompactHashtable<oop, char> _shared_table;
    60   static CompactHashtable<oop, char> _shared_table;
    45   static bool _shared_string_mapped;
    61   static bool _shared_string_mapped;
       
    62   static bool _alt_hash;
       
    63 private:
    46 
    64 
    47   // Set if one bucket is out of balance due to hash algorithm deficiency
    65    // Set if one bucket is out of balance due to hash algorithm deficiency
    48   static bool _needs_rehashing;
    66   StringTableHash* _local_table;
       
    67   size_t _current_size;
       
    68   volatile bool _has_work;
       
    69   volatile bool _needs_rehashing;
    49 
    70 
    50   // Claimed high water mark for parallel chunked scanning
    71   OopStorage* _weak_handles;
    51   static volatile int _parallel_claimed_idx;
       
    52 
    72 
    53   static oop intern(Handle string_or_null, jchar* chars, int length, TRAPS);
    73   volatile size_t _items;
    54   oop basic_add(int index, Handle string_or_null, jchar* name, int len,
    74   DEFINE_PAD_MINUS_SIZE(1, DEFAULT_CACHE_LINE_SIZE, sizeof(volatile size_t));
    55                 unsigned int hashValue, TRAPS);
    75   volatile size_t _uncleaned_items;
       
    76   DEFINE_PAD_MINUS_SIZE(2, DEFAULT_CACHE_LINE_SIZE, sizeof(volatile size_t));
    56 
    77 
    57   oop lookup_in_main_table(int index, jchar* chars, int length, unsigned int hashValue);
    78   double get_load_factor();
    58   static oop lookup_shared(jchar* name, int len, unsigned int hash);
    79   double get_dead_factor();
    59 
    80 
    60   // Apply the give oop closure to the entries to the buckets
    81   void check_concurrent_work();
    61   // in the range [start_idx, end_idx).
    82   void trigger_concurrent_work();
    62   static void buckets_oops_do(OopClosure* f, int start_idx, int end_idx);
       
    63 
    83 
    64   typedef StringTable::BucketUnlinkContext BucketUnlinkContext;
    84   static uintx item_added();
    65   // Unlink or apply the give oop closure to the entries to the buckets
    85   static void item_removed();
    66   // in the range [start_idx, end_idx). Unlinked bucket entries are collected in the given
    86   static size_t items_to_clean(size_t ncl);
    67   // context to be freed later.
       
    68   // This allows multiple threads to work on the table at once.
       
    69   static void buckets_unlink_or_oops_do(BoolObjectClosure* is_alive, OopClosure* f, int start_idx, int end_idx, BucketUnlinkContext* context);
       
    70 
    87 
    71   // Hashing algorithm, used as the hash value used by the
    88   StringTable();
    72   //     StringTable for bucket selection and comparison (stored in the
       
    73   //     HashtableEntry structures).  This is used in the String.intern() method.
       
    74   static unsigned int hash_string(const jchar* s, int len);
       
    75   static unsigned int hash_string(oop string);
       
    76   static unsigned int alt_hash_string(const jchar* s, int len);
       
    77 
    89 
    78   // Accessors for the string roots in the hashtable entries.
    90   static oop intern(Handle string_or_null_h, jchar* name, int len, TRAPS);
    79   // Use string_object_no_keepalive() only when the value is not returned
    91   oop do_intern(Handle string_or_null, jchar* name, int len, uintx hash, TRAPS);
    80   // outside of a scope where a thread transition is possible.
    92   oop do_lookup(jchar* name, int len, uintx hash);
    81   static oop string_object(HashtableEntry<oop, mtSymbol>* entry);
       
    82   static oop string_object_no_keepalive(HashtableEntry<oop, mtSymbol>* entry);
       
    83   static void set_string_object(HashtableEntry<oop, mtSymbol>* entry, oop string);
       
    84 
    93 
    85   StringTable() : RehashableHashtable<oop, mtSymbol>((int)StringTableSize,
    94   void concurrent_work(JavaThread* jt);
    86                               sizeof (HashtableEntry<oop, mtSymbol>)) {}
    95   void print_table_statistics(outputStream* st, const char* table_name);
    87 
    96 
    88   StringTable(HashtableBucket<mtSymbol>* t, int number_of_entries)
    97   void try_rehash_table();
    89     : RehashableHashtable<oop, mtSymbol>((int)StringTableSize, sizeof (HashtableEntry<oop, mtSymbol>), t,
    98   bool do_rehash();
    90                      number_of_entries) {}
    99 
    91 public:
   100  public:
    92   // The string table
   101   // The string table
    93   static StringTable* the_table() { return _the_table; }
   102   static StringTable* the_table() { return _the_table; }
       
   103   size_t table_size(Thread* thread = NULL);
    94 
   104 
    95   // Size of one bucket in the string table.  Used when checking for rollover.
   105   static OopStorage* weak_storage() { return the_table()->_weak_handles; }
    96   static uint bucket_size() { return sizeof(HashtableBucket<mtSymbol>); }
       
    97 
   106 
    98   static void create_table() {
   107   static void create_table() {
    99     assert(_the_table == NULL, "One string table allowed.");
   108     assert(_the_table == NULL, "One string table allowed.");
   100     _the_table = new StringTable();
   109     _the_table = new StringTable();
   101   }
   110   }
   102 
   111 
       
   112   static void do_concurrent_work(JavaThread* jt);
       
   113   static bool has_work() { return the_table()->_has_work; }
       
   114 
   103   // GC support
   115   // GC support
   104   //   Delete pointers to otherwise-unreachable objects.
   116   //   Delete pointers to otherwise-unreachable objects.
   105   static void unlink_or_oops_do(BoolObjectClosure* cl, OopClosure* f) {
   117   static void unlink(BoolObjectClosure* cl) {
   106     int processed = 0;
   118     unlink_or_oops_do(cl);
   107     int removed = 0;
       
   108     unlink_or_oops_do(cl, f, &processed, &removed);
       
   109   }
   119   }
   110   static void unlink(BoolObjectClosure* cl) {
   120   static void unlink_or_oops_do(BoolObjectClosure* is_alive, OopClosure* f = NULL,
   111     int processed = 0;
   121                                 int* processed = NULL, int* removed = NULL);
   112     int removed = 0;
   122 
   113     unlink_or_oops_do(cl, NULL, &processed, &removed);
       
   114   }
       
   115   static void unlink_or_oops_do(BoolObjectClosure* cl, OopClosure* f, int* processed, int* removed);
       
   116   static void unlink(BoolObjectClosure* cl, int* processed, int* removed) {
       
   117     unlink_or_oops_do(cl, NULL, processed, removed);
       
   118   }
       
   119   // Serially invoke "f->do_oop" on the locations of all oops in the table.
   123   // Serially invoke "f->do_oop" on the locations of all oops in the table.
   120   static void oops_do(OopClosure* f);
   124   static void oops_do(OopClosure* f);
   121 
   125 
   122   // Possibly parallel versions of the above
   126   // Possibly parallel versions of the above
   123   static void possibly_parallel_unlink_or_oops_do(BoolObjectClosure* cl, OopClosure* f, int* processed, int* removed);
   127   static void possibly_parallel_unlink(
   124   static void possibly_parallel_unlink(BoolObjectClosure* cl, int* processed, int* removed) {
   128      OopStorage::ParState<false /* concurrent */, false /* const*/>* par_state_string,
   125     possibly_parallel_unlink_or_oops_do(cl, NULL, processed, removed);
   129      BoolObjectClosure* cl, int* processed, int* removed);
   126   }
   130   static void possibly_parallel_oops_do(
   127   static void possibly_parallel_oops_do(OopClosure* f);
   131      OopStorage::ParState<false /* concurrent */, false /* const*/>* par_state_string,
   128 
   132      OopClosure* f);
   129   // Internal test.
       
   130   static void test_alt_hash() PRODUCT_RETURN;
       
   131 
   133 
   132   // Probing
   134   // Probing
   133   static oop lookup(Symbol* symbol);
   135   static oop lookup(Symbol* symbol);
   134   static oop lookup(jchar* chars, int length);
   136   static oop lookup(jchar* chars, int length);
   135 
   137 
   136   // Interning
   138   // Interning
   137   static oop intern(Symbol* symbol, TRAPS);
   139   static oop intern(Symbol* symbol, TRAPS);
   138   static oop intern(oop string, TRAPS);
   140   static oop intern(oop string, TRAPS);
   139   static oop intern(const char *utf8_string, TRAPS);
   141   static oop intern(const char *utf8_string, TRAPS);
   140 
   142 
   141   // Debugging
   143   // Rehash the string table if it gets out of balance
   142   static void verify();
   144   static void rehash_table();
   143   static void dump(outputStream* st, bool verbose=false);
   145   static bool needs_rehashing()
   144 
   146     { return StringTable::the_table()->_needs_rehashing; }
   145   enum VerifyMesgModes {
       
   146     _verify_quietly    = 0,
       
   147     _verify_with_mesgs = 1
       
   148   };
       
   149 
       
   150   enum VerifyRetTypes {
       
   151     _verify_pass          = 0,
       
   152     _verify_fail_continue = 1,
       
   153     _verify_fail_done     = 2
       
   154   };
       
   155 
       
   156   static VerifyRetTypes compare_entries(int bkt1, int e_cnt1,
       
   157                                         HashtableEntry<oop, mtSymbol>* e_ptr1,
       
   158                                         int bkt2, int e_cnt2,
       
   159                                         HashtableEntry<oop, mtSymbol>* e_ptr2);
       
   160   static VerifyRetTypes verify_entry(int bkt, int e_cnt,
       
   161                                      HashtableEntry<oop, mtSymbol>* e_ptr,
       
   162                                      VerifyMesgModes mesg_mode);
       
   163   static int verify_and_compare_entries();
       
   164 
   147 
   165   // Sharing
   148   // Sharing
       
   149  private:
       
   150   oop lookup_shared(jchar* name, int len, unsigned int hash) NOT_CDS_JAVA_HEAP_RETURN_(NULL);
       
   151   static void copy_shared_string_table(CompactStringTableWriter* ch_table) NOT_CDS_JAVA_HEAP_RETURN;
       
   152  public:
       
   153   static oop create_archived_string(oop s, Thread* THREAD);
   166   static void set_shared_string_mapped() { _shared_string_mapped = true; }
   154   static void set_shared_string_mapped() { _shared_string_mapped = true; }
   167   static bool shared_string_mapped()       { return _shared_string_mapped; }
   155   static bool shared_string_mapped()       { return _shared_string_mapped; }
   168   static void shared_oops_do(OopClosure* f) NOT_CDS_JAVA_HEAP_RETURN;
   156   static void shared_oops_do(OopClosure* f) NOT_CDS_JAVA_HEAP_RETURN;
   169   static bool copy_shared_string(GrowableArray<MemRegion> *string_space,
   157   static void write_to_archive() NOT_CDS_JAVA_HEAP_RETURN;
   170                                  CompactStringTableWriter* ch_table) NOT_CDS_JAVA_HEAP_RETURN_(false);
       
   171   static oop  create_archived_string(oop s, Thread* THREAD) NOT_CDS_JAVA_HEAP_RETURN_(NULL);
       
   172   static void write_to_archive(GrowableArray<MemRegion> *string_space) NOT_CDS_JAVA_HEAP_RETURN;
       
   173   static void serialize(SerializeClosure* soc) NOT_CDS_JAVA_HEAP_RETURN;
   158   static void serialize(SerializeClosure* soc) NOT_CDS_JAVA_HEAP_RETURN;
   174 
   159 
   175   // Rehash the symbol table if it gets out of balance
   160   // Jcmd
   176   static void rehash_table();
   161   static void dump(outputStream* st, bool verbose=false);
   177   static bool needs_rehashing() { return _needs_rehashing; }
   162   // Debugging
       
   163   static size_t verify_and_compare_entries();
       
   164   static void verify();
       
   165 };
   178 
   166 
   179   // Parallel chunked scanning
       
   180   static void clear_parallel_claimed_index() { _parallel_claimed_idx = 0; }
       
   181   static int parallel_claimed_index() { return _parallel_claimed_idx; }
       
   182 };
       
   183 #endif // SHARE_VM_CLASSFILE_STRINGTABLE_HPP
   167 #endif // SHARE_VM_CLASSFILE_STRINGTABLE_HPP