author | coleenp |
Mon, 25 Jun 2012 21:33:35 -0400 | |
changeset 13097 | c146b608d91f |
parent 13087 | 673ea6efaf18 |
child 13195 | be27e1b6a4b9 |
permissions | -rw-r--r-- |
1 | 1 |
/* |
13087 | 2 |
* Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved. |
1 | 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 |
* |
|
5547
f4b087cbb361
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
1623
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
f4b087cbb361
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
1623
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
f4b087cbb361
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
1623
diff
changeset
|
21 |
* questions. |
1 | 22 |
* |
23 |
*/ |
|
24 |
||
7397 | 25 |
#include "precompiled.hpp" |
26 |
#include "memory/allocation.inline.hpp" |
|
13097
c146b608d91f
7178670: runtime/7158800/BadUtf8.java fails in SymbolTable::rehash_table
coleenp
parents:
13087
diff
changeset
|
27 |
#include "memory/filemap.hpp" |
7397 | 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" |
|
1 | 34 |
|
8076
96d498ec7ae1
6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents:
7397
diff
changeset
|
35 |
|
10739 | 36 |
#ifndef USDT2 |
1 | 37 |
HS_DTRACE_PROBE_DECL4(hs_private, hashtable__new_entry, |
8076
96d498ec7ae1
6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents:
7397
diff
changeset
|
38 |
void*, unsigned int, void*, void*); |
10739 | 39 |
#endif /* !USDT2 */ |
1 | 40 |
|
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. |
|
48 |
||
49 |
BasicHashtableEntry* BasicHashtable::new_entry(unsigned int hashValue) { |
|
50 |
BasicHashtableEntry* entry; |
|
51 |
||
52 |
if (_free_list) { |
|
53 |
entry = _free_list; |
|
54 |
_free_list = _free_list->next(); |
|
55 |
} else { |
|
1551 | 56 |
if (_first_free_entry + _entry_size >= _end_block) { |
57 |
int block_size = MIN2(512, MAX2((int)_table_size / 2, (int)_number_of_entries)); |
|
1 | 58 |
int len = _entry_size * block_size; |
1551 | 59 |
len = 1 << log2_intptr(len); // round down to power of 2 |
60 |
assert(len >= _entry_size, ""); |
|
1 | 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 |
} |
|
67 |
||
1551 | 68 |
assert(_entry_size % HeapWordSize == 0, ""); |
1 | 69 |
entry->set_hash(hashValue); |
70 |
return entry; |
|
71 |
} |
|
72 |
||
73 |
||
8076
96d498ec7ae1
6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents:
7397
diff
changeset
|
74 |
template <class T> HashtableEntry<T>* Hashtable<T>::new_entry(unsigned int hashValue, T obj) { |
96d498ec7ae1
6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents:
7397
diff
changeset
|
75 |
HashtableEntry<T>* entry; |
1 | 76 |
|
8076
96d498ec7ae1
6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents:
7397
diff
changeset
|
77 |
entry = (HashtableEntry<T>*)BasicHashtable::new_entry(hashValue); |
96d498ec7ae1
6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents:
7397
diff
changeset
|
78 |
entry->set_literal(obj); |
10739 | 79 |
#ifndef USDT2 |
1 | 80 |
HS_DTRACE_PROBE4(hs_private, hashtable__new_entry, |
81 |
this, hashValue, obj, entry); |
|
10739 | 82 |
#else /* USDT2 */ |
83 |
HS_PRIVATE_HASHTABLE_NEW_ENTRY( |
|
84 |
this, hashValue, (uintptr_t) obj, entry); |
|
85 |
#endif /* USDT2 */ |
|
1 | 86 |
return entry; |
87 |
} |
|
88 |
||
89 |
||
13087 | 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. |
|
95 |
||
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 |
} |
|
105 |
||
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. |
|
109 |
||
110 |
template <class T> void Hashtable<T>::move_to(Hashtable<T>* new_table) { |
|
111 |
int saved_entry_count = number_of_entries(); |
|
112 |
||
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); |
|
13097
c146b608d91f
7178670: runtime/7158800/BadUtf8.java fails in SymbolTable::rehash_table
coleenp
parents:
13087
diff
changeset
|
123 |
// Keep the shared bit in the Hashtable entry to indicate that this entry |
c146b608d91f
7178670: runtime/7158800/BadUtf8.java fails in SymbolTable::rehash_table
coleenp
parents:
13087
diff
changeset
|
124 |
// can't be deleted. The shared bit is the LSB in the _next field so |
c146b608d91f
7178670: runtime/7158800/BadUtf8.java fails in SymbolTable::rehash_table
coleenp
parents:
13087
diff
changeset
|
125 |
// walking the hashtable past these entries requires |
c146b608d91f
7178670: runtime/7158800/BadUtf8.java fails in SymbolTable::rehash_table
coleenp
parents:
13087
diff
changeset
|
126 |
// BasicHashtableEntry::make_ptr() call. |
c146b608d91f
7178670: runtime/7158800/BadUtf8.java fails in SymbolTable::rehash_table
coleenp
parents:
13087
diff
changeset
|
127 |
bool keep_shared = p->is_shared(); |
13087 | 128 |
unlink_entry(p); |
129 |
new_table->add_entry(index, p); |
|
13097
c146b608d91f
7178670: runtime/7158800/BadUtf8.java fails in SymbolTable::rehash_table
coleenp
parents:
13087
diff
changeset
|
130 |
if (keep_shared) { |
c146b608d91f
7178670: runtime/7158800/BadUtf8.java fails in SymbolTable::rehash_table
coleenp
parents:
13087
diff
changeset
|
131 |
p->set_shared(); |
c146b608d91f
7178670: runtime/7158800/BadUtf8.java fails in SymbolTable::rehash_table
coleenp
parents:
13087
diff
changeset
|
132 |
} |
13087 | 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?"); |
|
139 |
||
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 |
} |
|
146 |
||
13097
c146b608d91f
7178670: runtime/7158800/BadUtf8.java fails in SymbolTable::rehash_table
coleenp
parents:
13087
diff
changeset
|
147 |
void BasicHashtable::free_buckets() { |
c146b608d91f
7178670: runtime/7158800/BadUtf8.java fails in SymbolTable::rehash_table
coleenp
parents:
13087
diff
changeset
|
148 |
if (NULL != _buckets) { |
c146b608d91f
7178670: runtime/7158800/BadUtf8.java fails in SymbolTable::rehash_table
coleenp
parents:
13087
diff
changeset
|
149 |
// Don't delete the buckets in the shared space. They aren't |
c146b608d91f
7178670: runtime/7158800/BadUtf8.java fails in SymbolTable::rehash_table
coleenp
parents:
13087
diff
changeset
|
150 |
// allocated by os::malloc |
c146b608d91f
7178670: runtime/7158800/BadUtf8.java fails in SymbolTable::rehash_table
coleenp
parents:
13087
diff
changeset
|
151 |
if (!UseSharedSpaces || |
c146b608d91f
7178670: runtime/7158800/BadUtf8.java fails in SymbolTable::rehash_table
coleenp
parents:
13087
diff
changeset
|
152 |
!FileMapInfo::current_info()->is_in_shared_space(_buckets)) { |
c146b608d91f
7178670: runtime/7158800/BadUtf8.java fails in SymbolTable::rehash_table
coleenp
parents:
13087
diff
changeset
|
153 |
FREE_C_HEAP_ARRAY(HashtableBucket, _buckets); |
c146b608d91f
7178670: runtime/7158800/BadUtf8.java fails in SymbolTable::rehash_table
coleenp
parents:
13087
diff
changeset
|
154 |
} |
c146b608d91f
7178670: runtime/7158800/BadUtf8.java fails in SymbolTable::rehash_table
coleenp
parents:
13087
diff
changeset
|
155 |
_buckets = NULL; |
c146b608d91f
7178670: runtime/7158800/BadUtf8.java fails in SymbolTable::rehash_table
coleenp
parents:
13087
diff
changeset
|
156 |
} |
c146b608d91f
7178670: runtime/7158800/BadUtf8.java fails in SymbolTable::rehash_table
coleenp
parents:
13087
diff
changeset
|
157 |
} |
c146b608d91f
7178670: runtime/7158800/BadUtf8.java fails in SymbolTable::rehash_table
coleenp
parents:
13087
diff
changeset
|
158 |
|
c146b608d91f
7178670: runtime/7158800/BadUtf8.java fails in SymbolTable::rehash_table
coleenp
parents:
13087
diff
changeset
|
159 |
|
1 | 160 |
// Reverse the order of elements in the hash buckets. |
161 |
||
162 |
void BasicHashtable::reverse() { |
|
163 |
||
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 |
} |
|
176 |
||
177 |
||
178 |
// Copy the table to the shared space. |
|
179 |
||
180 |
void BasicHashtable::copy_table(char** top, char* end) { |
|
181 |
||
182 |
// Dump the hash table entries. |
|
183 |
||
184 |
intptr_t *plen = (intptr_t*)(*top); |
|
185 |
*top += sizeof(*plen); |
|
186 |
||
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) { |
|
8076
96d498ec7ae1
6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents:
7397
diff
changeset
|
193 |
report_out_of_shared_space(SharedMiscData); |
1 | 194 |
} |
195 |
*p = (BasicHashtableEntry*)memcpy(*top, *p, entry_size()); |
|
196 |
*top += entry_size(); |
|
197 |
} |
|
198 |
} |
|
199 |
*plen = (char*)(*top) - (char*)plen - sizeof(*plen); |
|
200 |
||
201 |
// Set the shared bit. |
|
202 |
||
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 |
} |
|
209 |
||
210 |
||
211 |
||
212 |
// Reverse the order of elements in the hash buckets. |
|
213 |
||
8076
96d498ec7ae1
6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents:
7397
diff
changeset
|
214 |
template <class T> void Hashtable<T>::reverse(void* boundary) { |
1 | 215 |
|
216 |
for (int i = 0; i < table_size(); ++i) { |
|
8076
96d498ec7ae1
6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents:
7397
diff
changeset
|
217 |
HashtableEntry<T>* high_list = NULL; |
96d498ec7ae1
6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents:
7397
diff
changeset
|
218 |
HashtableEntry<T>* low_list = NULL; |
96d498ec7ae1
6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents:
7397
diff
changeset
|
219 |
HashtableEntry<T>* last_low_entry = NULL; |
96d498ec7ae1
6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents:
7397
diff
changeset
|
220 |
HashtableEntry<T>* p = bucket(i); |
1 | 221 |
while (p != NULL) { |
8076
96d498ec7ae1
6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents:
7397
diff
changeset
|
222 |
HashtableEntry<T>* next = p->next(); |
1 | 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 |
} |
|
243 |
||
244 |
||
245 |
// Dump the hash table buckets. |
|
246 |
||
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); |
|
251 |
||
252 |
*(intptr_t*)(*top) = _number_of_entries; |
|
253 |
*top += sizeof(intptr_t); |
|
254 |
||
255 |
if (*top + len > end) { |
|
8076
96d498ec7ae1
6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents:
7397
diff
changeset
|
256 |
report_out_of_shared_space(SharedMiscData); |
1 | 257 |
} |
258 |
_buckets = (HashtableBucket*)memcpy(*top, _buckets, len); |
|
259 |
*top += len; |
|
260 |
} |
|
261 |
||
262 |
||
263 |
#ifndef PRODUCT |
|
264 |
||
8076
96d498ec7ae1
6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents:
7397
diff
changeset
|
265 |
template <class T> void Hashtable<T>::print() { |
1 | 266 |
ResourceMark rm; |
267 |
||
268 |
for (int i = 0; i < table_size(); i++) { |
|
8076
96d498ec7ae1
6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents:
7397
diff
changeset
|
269 |
HashtableEntry<T>* entry = bucket(i); |
1 | 270 |
while(entry != NULL) { |
271 |
tty->print("%d : ", i); |
|
272 |
entry->literal()->print(); |
|
273 |
tty->cr(); |
|
274 |
entry = entry->next(); |
|
275 |
} |
|
276 |
} |
|
277 |
} |
|
278 |
||
279 |
||
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 |
} |
|
289 |
||
290 |
||
291 |
#endif // PRODUCT |
|
292 |
||
293 |
||
294 |
#ifdef ASSERT |
|
295 |
||
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 |
} |
|
304 |
||
305 |
#endif |
|
8076
96d498ec7ae1
6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents:
7397
diff
changeset
|
306 |
|
96d498ec7ae1
6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents:
7397
diff
changeset
|
307 |
// Explicitly instantiate these types |
96d498ec7ae1
6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents:
7397
diff
changeset
|
308 |
template class Hashtable<constantPoolOop>; |
96d498ec7ae1
6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents:
7397
diff
changeset
|
309 |
template class Hashtable<Symbol*>; |
96d498ec7ae1
6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents:
7397
diff
changeset
|
310 |
template class Hashtable<klassOop>; |
96d498ec7ae1
6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents:
7397
diff
changeset
|
311 |
template class Hashtable<oop>; |
96d498ec7ae1
6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents:
7397
diff
changeset
|
312 |