Mon, 25 Jun 2012 21:33:35 -0400
7178670: runtime/7158800/BadUtf8.java fails in SymbolTable::rehash_table
Summary: Cannot delete _buckets and HashtableEntries in shared space (CDS)
Reviewed-by: acorn, kvn, dlong, dcubed, kamg
1 /*
2 * Copyright (c) 2003, 2012, 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 */
25 #include "precompiled.hpp"
26 #include "memory/allocation.inline.hpp"
27 #include "memory/filemap.hpp"
28 #include "memory/resourceArea.hpp"
29 #include "oops/oop.inline.hpp"
30 #include "runtime/safepoint.hpp"
31 #include "utilities/dtrace.hpp"
32 #include "utilities/hashtable.hpp"
33 #include "utilities/hashtable.inline.hpp"
36 #ifndef USDT2
37 HS_DTRACE_PROBE_DECL4(hs_private, hashtable__new_entry,
38 void*, unsigned int, void*, void*);
39 #endif /* !USDT2 */
41 // This is a generic hashtable, designed to be used for the symbol
42 // and string tables.
43 //
44 // It is implemented as an open hash table with a fixed number of buckets.
45 //
46 // %note:
47 // - HashtableEntrys are allocated in blocks to reduce the space overhead.
49 BasicHashtableEntry* BasicHashtable::new_entry(unsigned int hashValue) {
50 BasicHashtableEntry* entry;
52 if (_free_list) {
53 entry = _free_list;
54 _free_list = _free_list->next();
55 } else {
56 if (_first_free_entry + _entry_size >= _end_block) {
57 int block_size = MIN2(512, MAX2((int)_table_size / 2, (int)_number_of_entries));
58 int len = _entry_size * block_size;
59 len = 1 << log2_intptr(len); // round down to power of 2
60 assert(len >= _entry_size, "");
61 _first_free_entry = NEW_C_HEAP_ARRAY(char, len);
62 _end_block = _first_free_entry + len;
63 }
64 entry = (BasicHashtableEntry*)_first_free_entry;
65 _first_free_entry += _entry_size;
66 }
68 assert(_entry_size % HeapWordSize == 0, "");
69 entry->set_hash(hashValue);
70 return entry;
71 }
74 template <class T> HashtableEntry<T>* Hashtable<T>::new_entry(unsigned int hashValue, T obj) {
75 HashtableEntry<T>* entry;
77 entry = (HashtableEntry<T>*)BasicHashtable::new_entry(hashValue);
78 entry->set_literal(obj);
79 #ifndef USDT2
80 HS_DTRACE_PROBE4(hs_private, hashtable__new_entry,
81 this, hashValue, obj, entry);
82 #else /* USDT2 */
83 HS_PRIVATE_HASHTABLE_NEW_ENTRY(
84 this, hashValue, (uintptr_t) obj, entry);
85 #endif /* USDT2 */
86 return entry;
87 }
90 // Check to see if the hashtable is unbalanced. The caller set a flag to
91 // rehash at the next safepoint. If this bucket is 60 times greater than the
92 // expected average bucket length, it's an unbalanced hashtable.
93 // This is somewhat an arbitrary heuristic but if one bucket gets to
94 // rehash_count which is currently 100, there's probably something wrong.
96 bool BasicHashtable::check_rehash_table(int count) {
97 assert(table_size() != 0, "underflow");
98 if (count > (((double)number_of_entries()/(double)table_size())*rehash_multiple)) {
99 // Set a flag for the next safepoint, which should be at some guaranteed
100 // safepoint interval.
101 return true;
102 }
103 return false;
104 }
106 // Create a new table and using alternate hash code, populate the new table
107 // with the existing elements. This can be used to change the hash code
108 // and could in the future change the size of the table.
110 template <class T> void Hashtable<T>::move_to(Hashtable<T>* new_table) {
111 int saved_entry_count = number_of_entries();
113 // Iterate through the table and create a new entry for the new table
114 for (int i = 0; i < new_table->table_size(); ++i) {
115 for (HashtableEntry<T>* p = bucket(i); p != NULL; ) {
116 HashtableEntry<T>* next = p->next();
117 T string = p->literal();
118 // Use alternate hashing algorithm on the symbol in the first table
119 unsigned int hashValue = new_hash(string);
120 // Get a new index relative to the new table (can also change size)
121 int index = new_table->hash_to_index(hashValue);
122 p->set_hash(hashValue);
123 // Keep the shared bit in the Hashtable entry to indicate that this entry
124 // can't be deleted. The shared bit is the LSB in the _next field so
125 // walking the hashtable past these entries requires
126 // BasicHashtableEntry::make_ptr() call.
127 bool keep_shared = p->is_shared();
128 unlink_entry(p);
129 new_table->add_entry(index, p);
130 if (keep_shared) {
131 p->set_shared();
132 }
133 p = next;
134 }
135 }
136 // give the new table the free list as well
137 new_table->copy_freelist(this);
138 assert(new_table->number_of_entries() == saved_entry_count, "lost entry on dictionary copy?");
140 // Destroy memory used by the buckets in the hashtable. The memory
141 // for the elements has been used in a new table and is not
142 // destroyed. The memory reuse will benefit resizing the SystemDictionary
143 // to avoid a memory allocation spike at safepoint.
144 free_buckets();
145 }
147 void BasicHashtable::free_buckets() {
148 if (NULL != _buckets) {
149 // Don't delete the buckets in the shared space. They aren't
150 // allocated by os::malloc
151 if (!UseSharedSpaces ||
152 !FileMapInfo::current_info()->is_in_shared_space(_buckets)) {
153 FREE_C_HEAP_ARRAY(HashtableBucket, _buckets);
154 }
155 _buckets = NULL;
156 }
157 }
160 // Reverse the order of elements in the hash buckets.
162 void BasicHashtable::reverse() {
164 for (int i = 0; i < _table_size; ++i) {
165 BasicHashtableEntry* new_list = NULL;
166 BasicHashtableEntry* p = bucket(i);
167 while (p != NULL) {
168 BasicHashtableEntry* next = p->next();
169 p->set_next(new_list);
170 new_list = p;
171 p = next;
172 }
173 *bucket_addr(i) = new_list;
174 }
175 }
178 // Copy the table to the shared space.
180 void BasicHashtable::copy_table(char** top, char* end) {
182 // Dump the hash table entries.
184 intptr_t *plen = (intptr_t*)(*top);
185 *top += sizeof(*plen);
187 int i;
188 for (i = 0; i < _table_size; ++i) {
189 for (BasicHashtableEntry** p = _buckets[i].entry_addr();
190 *p != NULL;
191 p = (*p)->next_addr()) {
192 if (*top + entry_size() > end) {
193 report_out_of_shared_space(SharedMiscData);
194 }
195 *p = (BasicHashtableEntry*)memcpy(*top, *p, entry_size());
196 *top += entry_size();
197 }
198 }
199 *plen = (char*)(*top) - (char*)plen - sizeof(*plen);
201 // Set the shared bit.
203 for (i = 0; i < _table_size; ++i) {
204 for (BasicHashtableEntry* p = bucket(i); p != NULL; p = p->next()) {
205 p->set_shared();
206 }
207 }
208 }
212 // Reverse the order of elements in the hash buckets.
214 template <class T> void Hashtable<T>::reverse(void* boundary) {
216 for (int i = 0; i < table_size(); ++i) {
217 HashtableEntry<T>* high_list = NULL;
218 HashtableEntry<T>* low_list = NULL;
219 HashtableEntry<T>* last_low_entry = NULL;
220 HashtableEntry<T>* p = bucket(i);
221 while (p != NULL) {
222 HashtableEntry<T>* next = p->next();
223 if ((void*)p->literal() >= boundary) {
224 p->set_next(high_list);
225 high_list = p;
226 } else {
227 p->set_next(low_list);
228 low_list = p;
229 if (last_low_entry == NULL) {
230 last_low_entry = p;
231 }
232 }
233 p = next;
234 }
235 if (low_list != NULL) {
236 *bucket_addr(i) = low_list;
237 last_low_entry->set_next(high_list);
238 } else {
239 *bucket_addr(i) = high_list;
240 }
241 }
242 }
245 // Dump the hash table buckets.
247 void BasicHashtable::copy_buckets(char** top, char* end) {
248 intptr_t len = _table_size * sizeof(HashtableBucket);
249 *(intptr_t*)(*top) = len;
250 *top += sizeof(intptr_t);
252 *(intptr_t*)(*top) = _number_of_entries;
253 *top += sizeof(intptr_t);
255 if (*top + len > end) {
256 report_out_of_shared_space(SharedMiscData);
257 }
258 _buckets = (HashtableBucket*)memcpy(*top, _buckets, len);
259 *top += len;
260 }
263 #ifndef PRODUCT
265 template <class T> void Hashtable<T>::print() {
266 ResourceMark rm;
268 for (int i = 0; i < table_size(); i++) {
269 HashtableEntry<T>* entry = bucket(i);
270 while(entry != NULL) {
271 tty->print("%d : ", i);
272 entry->literal()->print();
273 tty->cr();
274 entry = entry->next();
275 }
276 }
277 }
280 void BasicHashtable::verify() {
281 int count = 0;
282 for (int i = 0; i < table_size(); i++) {
283 for (BasicHashtableEntry* p = bucket(i); p != NULL; p = p->next()) {
284 ++count;
285 }
286 }
287 assert(count == number_of_entries(), "number of hashtable entries incorrect");
288 }
291 #endif // PRODUCT
294 #ifdef ASSERT
296 void BasicHashtable::verify_lookup_length(double load) {
297 if ((double)_lookup_length / (double)_lookup_count > load * 2.0) {
298 warning("Performance bug: SystemDictionary lookup_count=%d "
299 "lookup_length=%d average=%lf load=%f",
300 _lookup_count, _lookup_length,
301 (double) _lookup_length / _lookup_count, load);
302 }
303 }
305 #endif
307 // Explicitly instantiate these types
308 template class Hashtable<constantPoolOop>;
309 template class Hashtable<Symbol*>;
310 template class Hashtable<klassOop>;
311 template class Hashtable<oop>;