src/share/vm/classfile/symbolTable.hpp

Mon, 15 May 2017 12:20:15 +0200

author
tschatzl
date
Mon, 15 May 2017 12:20:15 +0200
changeset 8766
ce9a710b0f63
parent 7207
152cf4afc11f
child 8856
ac27a9c85bea
permissions
-rw-r--r--

8180048: Interned string and symbol table leak memory during parallel unlinking
Summary: Make appending found dead BasicHashtableEntrys to the free list atomic.
Reviewed-by: ehelin, shade

duke@435 1 /*
tschatzl@8766 2 * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
duke@435 22 *
duke@435 23 */
duke@435 24
stefank@2314 25 #ifndef SHARE_VM_CLASSFILE_SYMBOLTABLE_HPP
stefank@2314 26 #define SHARE_VM_CLASSFILE_SYMBOLTABLE_HPP
stefank@2314 27
stefank@2314 28 #include "memory/allocation.inline.hpp"
coleenp@2497 29 #include "oops/symbol.hpp"
stefank@2314 30 #include "utilities/hashtable.hpp"
stefank@2314 31
coleenp@2497 32 // The symbol table holds all Symbol*s and corresponding interned strings.
coleenp@2497 33 // Symbol*s and literal strings should be canonicalized.
duke@435 34 //
duke@435 35 // The interned strings are created lazily.
duke@435 36 //
duke@435 37 // It is implemented as an open hash table with a fixed number of buckets.
duke@435 38 //
duke@435 39 // %note:
duke@435 40 // - symbolTableEntrys are allocated in blocks to reduce the space overhead.
duke@435 41
duke@435 42 class BoolObjectClosure;
coleenp@3865 43 class outputStream;
duke@435 44
duke@435 45
coleenp@2497 46 // Class to hold a newly created or referenced Symbol* temporarily in scope.
coleenp@2497 47 // new_symbol() and lookup() will create a Symbol* if not already in the
coleenp@2497 48 // symbol table and add to the symbol's reference count.
coleenp@2497 49 // probe() and lookup_only() will increment the refcount if symbol is found.
coleenp@2497 50 class TempNewSymbol : public StackObj {
coleenp@2497 51 Symbol* _temp;
coleenp@2497 52
coleenp@2497 53 public:
coleenp@2497 54 TempNewSymbol() : _temp(NULL) {}
coleenp@2497 55 // Creating or looking up a symbol increments the symbol's reference count
coleenp@2497 56 TempNewSymbol(Symbol *s) : _temp(s) {}
coleenp@2497 57
coleenp@2497 58 // Operator= increments reference count.
coleenp@2497 59 void operator=(const TempNewSymbol &s) {
twisti@3969 60 //clear(); //FIXME
coleenp@2497 61 _temp = s._temp;
coleenp@2497 62 if (_temp !=NULL) _temp->increment_refcount();
coleenp@2497 63 }
coleenp@2497 64
coleenp@2497 65 // Decrement reference counter so it can go away if it's unique
twisti@3969 66 void clear() { if (_temp != NULL) _temp->decrement_refcount(); _temp = NULL; }
twisti@3969 67
twisti@3969 68 ~TempNewSymbol() { clear(); }
coleenp@2497 69
coleenp@2497 70 // Operators so they can be used like Symbols
coleenp@2497 71 Symbol* operator -> () const { return _temp; }
coleenp@2497 72 bool operator == (Symbol* o) const { return _temp == o; }
coleenp@2497 73 // Sneaky conversion function
coleenp@2497 74 operator Symbol*() { return _temp; }
coleenp@2497 75 };
coleenp@2497 76
mgerdin@7207 77 class SymbolTable : public RehashableHashtable<Symbol*, mtSymbol> {
duke@435 78 friend class VMStructs;
coleenp@2497 79 friend class ClassFileParser;
duke@435 80
duke@435 81 private:
duke@435 82 // The symbol table
duke@435 83 static SymbolTable* _the_table;
duke@435 84
coleenp@3865 85 // Set if one bucket is out of balance due to hash algorithm deficiency
coleenp@3865 86 static bool _needs_rehashing;
coleenp@3865 87
coleenp@2497 88 // For statistics
tschatzl@6229 89 static int _symbols_removed;
tschatzl@6229 90 static int _symbols_counted;
coleenp@2497 91
coleenp@3682 92 Symbol* allocate_symbol(const u1* name, int len, bool c_heap, TRAPS); // Assumes no characters larger than 0x7F
coleenp@2497 93
duke@435 94 // Adding elements
coleenp@3682 95 Symbol* basic_add(int index, u1* name, int len, unsigned int hashValue,
coleenp@3682 96 bool c_heap, TRAPS);
coleenp@4037 97 bool basic_add(ClassLoaderData* loader_data,
coleenp@4037 98 constantPoolHandle cp, int names_count,
duke@435 99 const char** names, int* lengths, int* cp_indices,
duke@435 100 unsigned int* hashValues, TRAPS);
duke@435 101
coleenp@4037 102 static void new_symbols(ClassLoaderData* loader_data,
coleenp@4037 103 constantPoolHandle cp, int names_count,
coleenp@2497 104 const char** name, int* lengths,
coleenp@2497 105 int* cp_indices, unsigned int* hashValues,
coleenp@2497 106 TRAPS) {
coleenp@4037 107 add(loader_data, cp, names_count, name, lengths, cp_indices, hashValues, THREAD);
coleenp@2497 108 }
coleenp@2497 109
coleenp@2497 110 Symbol* lookup(int index, const char* name, int len, unsigned int hash);
duke@435 111
duke@435 112 SymbolTable()
mgerdin@7207 113 : RehashableHashtable<Symbol*, mtSymbol>(SymbolTableSize, sizeof (HashtableEntry<Symbol*, mtSymbol>)) {}
duke@435 114
zgu@3900 115 SymbolTable(HashtableBucket<mtSymbol>* t, int number_of_entries)
mgerdin@7207 116 : RehashableHashtable<Symbol*, mtSymbol>(SymbolTableSize, sizeof (HashtableEntry<Symbol*, mtSymbol>), t,
duke@435 117 number_of_entries) {}
duke@435 118
coleenp@3682 119 // Arena for permanent symbols (null class loader) that are never unloaded
coleenp@3682 120 static Arena* _arena;
coleenp@3682 121 static Arena* arena() { return _arena; } // called for statistics
duke@435 122
coleenp@3682 123 static void initialize_symbols(int arena_alloc_size = 0);
tschatzl@6229 124
tschatzl@6229 125 static volatile int _parallel_claimed_idx;
tschatzl@6229 126
tschatzl@8766 127 typedef SymbolTable::BucketUnlinkContext BucketUnlinkContext;
tschatzl@8766 128 // Release any dead symbols. Unlinked bucket entries are collected in the given
tschatzl@8766 129 // context to be freed later.
tschatzl@8766 130 // This allows multiple threads to work on the table at once.
tschatzl@8766 131 static void buckets_unlink(int start_idx, int end_idx, BucketUnlinkContext* context, size_t* memory_total);
duke@435 132 public:
duke@435 133 enum {
coleenp@3682 134 symbol_alloc_batch_size = 8,
coleenp@3682 135 // Pick initial size based on java -version size measurements
coleenp@3682 136 symbol_alloc_arena_size = 360*K
duke@435 137 };
duke@435 138
duke@435 139 // The symbol table
duke@435 140 static SymbolTable* the_table() { return _the_table; }
duke@435 141
kevinw@5850 142 // Size of one bucket in the string table. Used when checking for rollover.
kevinw@5850 143 static uint bucket_size() { return sizeof(HashtableBucket<mtSymbol>); }
kevinw@5850 144
duke@435 145 static void create_table() {
duke@435 146 assert(_the_table == NULL, "One symbol table allowed.");
duke@435 147 _the_table = new SymbolTable();
coleenp@3682 148 initialize_symbols(symbol_alloc_arena_size);
duke@435 149 }
duke@435 150
zgu@3900 151 static void create_table(HashtableBucket<mtSymbol>* t, int length,
duke@435 152 int number_of_entries) {
duke@435 153 assert(_the_table == NULL, "One symbol table allowed.");
kevinw@5850 154
kevinw@5850 155 // If CDS archive used a different symbol table size, use that size instead
kevinw@5850 156 // which is better than giving an error.
kevinw@5850 157 SymbolTableSize = length/bucket_size();
kevinw@5850 158
duke@435 159 _the_table = new SymbolTable(t, number_of_entries);
coleenp@3682 160 // if CDS give symbol table a default arena size since most symbols
coleenp@3682 161 // are already allocated in the shared misc section.
coleenp@3682 162 initialize_symbols();
duke@435 163 }
duke@435 164
coleenp@3875 165 static unsigned int hash_symbol(const char* s, int len);
coleenp@3865 166
coleenp@2497 167 static Symbol* lookup(const char* name, int len, TRAPS);
duke@435 168 // lookup only, won't add. Also calculate hash.
coleenp@2497 169 static Symbol* lookup_only(const char* name, int len, unsigned int& hash);
duke@435 170 // Only copy to C string to be added if lookup failed.
coleenp@2497 171 static Symbol* lookup(const Symbol* sym, int begin, int end, TRAPS);
coleenp@2497 172
coleenp@2497 173 static void release(Symbol* sym);
duke@435 174
phh@3427 175 // Look up the address of the literal in the SymbolTable for this Symbol*
phh@3427 176 static Symbol** lookup_symbol_addr(Symbol* sym);
phh@3427 177
jrose@1100 178 // jchar (utf16) version of lookups
coleenp@2497 179 static Symbol* lookup_unicode(const jchar* name, int len, TRAPS);
coleenp@2497 180 static Symbol* lookup_only_unicode(const jchar* name, int len, unsigned int& hash);
jrose@1100 181
coleenp@4037 182 static void add(ClassLoaderData* loader_data,
coleenp@4037 183 constantPoolHandle cp, int names_count,
duke@435 184 const char** names, int* lengths, int* cp_indices,
duke@435 185 unsigned int* hashValues, TRAPS);
duke@435 186
coleenp@2497 187 // Release any dead symbols
tschatzl@6229 188 static void unlink() {
tschatzl@6229 189 int processed = 0;
tschatzl@6229 190 int removed = 0;
tschatzl@6229 191 unlink(&processed, &removed);
tschatzl@6229 192 }
tschatzl@6229 193 static void unlink(int* processed, int* removed);
tschatzl@6229 194 // Release any dead symbols, possibly parallel version
tschatzl@6229 195 static void possibly_parallel_unlink(int* processed, int* removed);
coleenp@2497 196
coleenp@2497 197 // iterate over symbols
coleenp@2497 198 static void symbols_do(SymbolClosure *cl);
coleenp@2497 199
coleenp@2497 200 // Symbol creation
coleenp@2497 201 static Symbol* new_symbol(const char* utf8_buffer, int length, TRAPS) {
coleenp@2497 202 assert(utf8_buffer != NULL, "just checking");
coleenp@2497 203 return lookup(utf8_buffer, length, THREAD);
duke@435 204 }
coleenp@2497 205 static Symbol* new_symbol(const char* name, TRAPS) {
coleenp@2497 206 return new_symbol(name, (int)strlen(name), THREAD);
coleenp@2497 207 }
coleenp@2497 208 static Symbol* new_symbol(const Symbol* sym, int begin, int end, TRAPS) {
coleenp@2497 209 assert(begin <= end && end <= sym->utf8_length(), "just checking");
coleenp@2497 210 return lookup(sym, begin, end, THREAD);
duke@435 211 }
duke@435 212
coleenp@3682 213 // Create a symbol in the arena for symbols that are not deleted
coleenp@3682 214 static Symbol* new_permanent_symbol(const char* name, TRAPS);
coleenp@3682 215
duke@435 216 // Symbol lookup
coleenp@2497 217 static Symbol* lookup(int index, const char* name, int len, TRAPS);
duke@435 218
duke@435 219 // Needed for preloading classes in signatures when compiling.
duke@435 220 // Returns the symbol is already present in symbol table, otherwise
duke@435 221 // NULL. NO ALLOCATION IS GUARANTEED!
coleenp@2497 222 static Symbol* probe(const char* name, int len) {
jrose@1100 223 unsigned int ignore_hash;
jrose@1100 224 return lookup_only(name, len, ignore_hash);
jrose@1100 225 }
coleenp@2497 226 static Symbol* probe_unicode(const jchar* name, int len) {
jrose@1100 227 unsigned int ignore_hash;
jrose@1100 228 return lookup_only_unicode(name, len, ignore_hash);
jrose@1100 229 }
duke@435 230
duke@435 231 // Histogram
duke@435 232 static void print_histogram() PRODUCT_RETURN;
coleenp@2497 233 static void print() PRODUCT_RETURN;
duke@435 234
duke@435 235 // Debugging
duke@435 236 static void verify();
coleenp@3865 237 static void dump(outputStream* st);
duke@435 238
duke@435 239 // Sharing
duke@435 240 static void copy_buckets(char** top, char*end) {
zgu@3900 241 the_table()->Hashtable<Symbol*, mtSymbol>::copy_buckets(top, end);
duke@435 242 }
duke@435 243 static void copy_table(char** top, char*end) {
zgu@3900 244 the_table()->Hashtable<Symbol*, mtSymbol>::copy_table(top, end);
duke@435 245 }
duke@435 246 static void reverse(void* boundary = NULL) {
zgu@3900 247 the_table()->Hashtable<Symbol*, mtSymbol>::reverse(boundary);
duke@435 248 }
coleenp@3865 249
coleenp@3865 250 // Rehash the symbol table if it gets out of balance
coleenp@3865 251 static void rehash_table();
coleenp@3865 252 static bool needs_rehashing() { return _needs_rehashing; }
tschatzl@6229 253 // Parallel chunked scanning
tschatzl@6229 254 static void clear_parallel_claimed_index() { _parallel_claimed_idx = 0; }
tschatzl@6229 255 static int parallel_claimed_index() { return _parallel_claimed_idx; }
duke@435 256 };
duke@435 257
mgerdin@7207 258 class StringTable : public RehashableHashtable<oop, mtSymbol> {
duke@435 259 friend class VMStructs;
duke@435 260
duke@435 261 private:
duke@435 262 // The string table
duke@435 263 static StringTable* _the_table;
duke@435 264
coleenp@3865 265 // Set if one bucket is out of balance due to hash algorithm deficiency
coleenp@3865 266 static bool _needs_rehashing;
coleenp@3865 267
johnc@5277 268 // Claimed high water mark for parallel chunked scanning
johnc@5277 269 static volatile int _parallel_claimed_idx;
johnc@5277 270
duke@435 271 static oop intern(Handle string_or_null, jchar* chars, int length, TRAPS);
duke@435 272 oop basic_add(int index, Handle string_or_null, jchar* name, int len,
duke@435 273 unsigned int hashValue, TRAPS);
duke@435 274
duke@435 275 oop lookup(int index, jchar* chars, int length, unsigned int hashValue);
duke@435 276
johnc@5277 277 // Apply the give oop closure to the entries to the buckets
johnc@5277 278 // in the range [start_idx, end_idx).
tschatzl@6229 279 static void buckets_oops_do(OopClosure* f, int start_idx, int end_idx);
tschatzl@8766 280
tschatzl@8766 281 typedef StringTable::BucketUnlinkContext BucketUnlinkContext;
tschatzl@6229 282 // Unlink or apply the give oop closure to the entries to the buckets
tschatzl@8766 283 // in the range [start_idx, end_idx). Unlinked bucket entries are collected in the given
tschatzl@8766 284 // context to be freed later.
tschatzl@8766 285 // This allows multiple threads to work on the table at once.
tschatzl@8766 286 static void buckets_unlink_or_oops_do(BoolObjectClosure* is_alive, OopClosure* f, int start_idx, int end_idx, BucketUnlinkContext* context);
johnc@5277 287
mgerdin@7207 288 StringTable() : RehashableHashtable<oop, mtSymbol>((int)StringTableSize,
zgu@3900 289 sizeof (HashtableEntry<oop, mtSymbol>)) {}
duke@435 290
zgu@3900 291 StringTable(HashtableBucket<mtSymbol>* t, int number_of_entries)
mgerdin@7207 292 : RehashableHashtable<oop, mtSymbol>((int)StringTableSize, sizeof (HashtableEntry<oop, mtSymbol>), t,
jcoomes@2660 293 number_of_entries) {}
duke@435 294 public:
duke@435 295 // The string table
duke@435 296 static StringTable* the_table() { return _the_table; }
duke@435 297
hseigel@4277 298 // Size of one bucket in the string table. Used when checking for rollover.
hseigel@4277 299 static uint bucket_size() { return sizeof(HashtableBucket<mtSymbol>); }
hseigel@4277 300
duke@435 301 static void create_table() {
duke@435 302 assert(_the_table == NULL, "One string table allowed.");
duke@435 303 _the_table = new StringTable();
duke@435 304 }
duke@435 305
duke@435 306 // GC support
duke@435 307 // Delete pointers to otherwise-unreachable objects.
tschatzl@6229 308 static void unlink_or_oops_do(BoolObjectClosure* cl, OopClosure* f) {
tschatzl@6229 309 int processed = 0;
tschatzl@6229 310 int removed = 0;
tschatzl@6229 311 unlink_or_oops_do(cl, f, &processed, &removed);
tschatzl@6229 312 }
stefank@5196 313 static void unlink(BoolObjectClosure* cl) {
tschatzl@6229 314 int processed = 0;
tschatzl@6229 315 int removed = 0;
tschatzl@6229 316 unlink_or_oops_do(cl, NULL, &processed, &removed);
stefank@5196 317 }
tschatzl@6229 318 static void unlink_or_oops_do(BoolObjectClosure* cl, OopClosure* f, int* processed, int* removed);
tschatzl@6229 319 static void unlink(BoolObjectClosure* cl, int* processed, int* removed) {
tschatzl@6229 320 unlink_or_oops_do(cl, NULL, processed, removed);
tschatzl@6229 321 }
johnc@5277 322 // Serially invoke "f->do_oop" on the locations of all oops in the table.
coleenp@2497 323 static void oops_do(OopClosure* f);
duke@435 324
tschatzl@6229 325 // Possibly parallel versions of the above
tschatzl@6229 326 static void possibly_parallel_unlink_or_oops_do(BoolObjectClosure* cl, OopClosure* f, int* processed, int* removed);
tschatzl@6229 327 static void possibly_parallel_unlink(BoolObjectClosure* cl, int* processed, int* removed) {
tschatzl@6229 328 possibly_parallel_unlink_or_oops_do(cl, NULL, processed, removed);
tschatzl@6229 329 }
johnc@5277 330 static void possibly_parallel_oops_do(OopClosure* f);
johnc@5277 331
coleenp@3865 332 // Hashing algorithm, used as the hash value used by the
coleenp@3865 333 // StringTable for bucket selection and comparison (stored in the
coleenp@3865 334 // HashtableEntry structures). This is used in the String.intern() method.
coleenp@3875 335 static unsigned int hash_string(const jchar* s, int len);
coleenp@3865 336
coleenp@3865 337 // Internal test.
coleenp@3865 338 static void test_alt_hash() PRODUCT_RETURN;
coleenp@3865 339
duke@435 340 // Probing
coleenp@2497 341 static oop lookup(Symbol* symbol);
mgerdin@4850 342 static oop lookup(jchar* chars, int length);
duke@435 343
duke@435 344 // Interning
coleenp@2497 345 static oop intern(Symbol* symbol, TRAPS);
duke@435 346 static oop intern(oop string, TRAPS);
duke@435 347 static oop intern(const char *utf8_string, TRAPS);
duke@435 348
duke@435 349 // Debugging
duke@435 350 static void verify();
coleenp@3865 351 static void dump(outputStream* st);
duke@435 352
dcubed@5743 353 enum VerifyMesgModes {
dcubed@5743 354 _verify_quietly = 0,
dcubed@5743 355 _verify_with_mesgs = 1
dcubed@5743 356 };
dcubed@5743 357
dcubed@5743 358 enum VerifyRetTypes {
dcubed@5743 359 _verify_pass = 0,
dcubed@5743 360 _verify_fail_continue = 1,
dcubed@5743 361 _verify_fail_done = 2
dcubed@5743 362 };
dcubed@5743 363
dcubed@5743 364 static VerifyRetTypes compare_entries(int bkt1, int e_cnt1,
dcubed@5743 365 HashtableEntry<oop, mtSymbol>* e_ptr1,
dcubed@5743 366 int bkt2, int e_cnt2,
dcubed@5743 367 HashtableEntry<oop, mtSymbol>* e_ptr2);
dcubed@5743 368 static VerifyRetTypes verify_entry(int bkt, int e_cnt,
dcubed@5743 369 HashtableEntry<oop, mtSymbol>* e_ptr,
dcubed@5743 370 VerifyMesgModes mesg_mode);
dcubed@5743 371 static int verify_and_compare_entries();
dcubed@5743 372
duke@435 373 // Sharing
duke@435 374 static void copy_buckets(char** top, char*end) {
zgu@3900 375 the_table()->Hashtable<oop, mtSymbol>::copy_buckets(top, end);
duke@435 376 }
duke@435 377 static void copy_table(char** top, char*end) {
zgu@3900 378 the_table()->Hashtable<oop, mtSymbol>::copy_table(top, end);
duke@435 379 }
duke@435 380 static void reverse() {
zgu@3900 381 the_table()->Hashtable<oop, mtSymbol>::reverse();
duke@435 382 }
coleenp@3865 383
coleenp@3865 384 // Rehash the symbol table if it gets out of balance
coleenp@3865 385 static void rehash_table();
coleenp@3865 386 static bool needs_rehashing() { return _needs_rehashing; }
johnc@5277 387
johnc@5277 388 // Parallel chunked scanning
johnc@5277 389 static void clear_parallel_claimed_index() { _parallel_claimed_idx = 0; }
tschatzl@6229 390 static int parallel_claimed_index() { return _parallel_claimed_idx; }
duke@435 391 };
stefank@2314 392 #endif // SHARE_VM_CLASSFILE_SYMBOLTABLE_HPP

mercurial