author | mgronlun |
Mon, 02 Sep 2019 19:42:46 +0200 | |
branch | JEP-349-branch |
changeset 57983 | a57907813a83 |
parent 57870 | 00860d9caf4d |
child 58154 | 060d9d139109 |
permissions | -rw-r--r-- |
50113 | 1 |
/* |
53244
9807daeb47c4
8216167: Update include guards to reflect correct directories
coleenp
parents:
50429
diff
changeset
|
2 |
* Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved. |
50113 | 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 |
||
53244
9807daeb47c4
8216167: Update include guards to reflect correct directories
coleenp
parents:
50429
diff
changeset
|
25 |
#ifndef SHARE_JFR_UTILITIES_JFRHASHTABLE_HPP |
9807daeb47c4
8216167: Update include guards to reflect correct directories
coleenp
parents:
50429
diff
changeset
|
26 |
#define SHARE_JFR_UTILITIES_JFRHASHTABLE_HPP |
50113 | 27 |
|
57983
a57907813a83
old object sample blob:ification and serialization support
mgronlun
parents:
57870
diff
changeset
|
28 |
#include "jfr/utilities/jfrAllocation.hpp" |
50429
83aec1d357d4
8204301: Make OrderAccess functions available to hpp rather than inline.hpp files
coleenp
parents:
50113
diff
changeset
|
29 |
#include "runtime/orderAccess.hpp" |
50113 | 30 |
#include "utilities/debug.hpp" |
31 |
#include "utilities/macros.hpp" |
|
32 |
||
33 |
template <typename T> |
|
57983
a57907813a83
old object sample blob:ification and serialization support
mgronlun
parents:
57870
diff
changeset
|
34 |
class JfrBasicHashtableEntry : public JfrCHeapObj { |
50113 | 35 |
private: |
36 |
typedef JfrBasicHashtableEntry<T> Entry; |
|
37 |
Entry* _next; |
|
38 |
T _literal; // ref to item in table. |
|
39 |
uintptr_t _hash; |
|
40 |
||
41 |
public: |
|
57983
a57907813a83
old object sample blob:ification and serialization support
mgronlun
parents:
57870
diff
changeset
|
42 |
JfrBasicHashtableEntry(uintptr_t hash, const T& data) : _next(NULL), _literal(data), _hash(hash) {} |
50113 | 43 |
uintptr_t hash() const { return _hash; } |
44 |
T literal() const { return _literal; } |
|
45 |
T* literal_addr() { return &_literal; } |
|
46 |
void set_literal(T s) { _literal = s; } |
|
47 |
void set_next(Entry* next) { _next = next; } |
|
48 |
Entry* next() const { return _next; } |
|
49 |
Entry** next_addr() { return &_next; } |
|
50 |
}; |
|
51 |
||
52 |
template <typename T> |
|
53 |
class JfrHashtableBucket : public CHeapObj<mtTracing> { |
|
54 |
template <typename> |
|
55 |
friend class JfrBasicHashtable; |
|
56 |
private: |
|
57 |
typedef JfrBasicHashtableEntry<T> TableEntry; |
|
58 |
TableEntry* _entry; |
|
59 |
||
60 |
TableEntry* get_entry() const { |
|
61 |
return (TableEntry*)OrderAccess::load_acquire(&_entry); |
|
62 |
} |
|
63 |
void set_entry(TableEntry* entry) { OrderAccess::release_store(&_entry, entry);} |
|
64 |
TableEntry** entry_addr() { return &_entry; } |
|
65 |
}; |
|
66 |
||
67 |
template <typename T> |
|
68 |
class JfrBasicHashtable : public CHeapObj<mtTracing> { |
|
69 |
private: |
|
70 |
typedef JfrHashtableBucket<T> Bucket; |
|
71 |
typedef JfrBasicHashtableEntry<T> TableEntry; |
|
72 |
Bucket* _buckets; |
|
73 |
uintptr_t _table_size; |
|
74 |
const size_t _entry_size; |
|
75 |
size_t _number_of_entries; |
|
76 |
||
77 |
protected: |
|
78 |
JfrBasicHashtable(uintptr_t table_size, size_t entry_size) : |
|
79 |
_buckets(NULL), _table_size(table_size), _entry_size(entry_size), _number_of_entries(0) { |
|
80 |
_buckets = NEW_C_HEAP_ARRAY2(Bucket, table_size, mtTracing, CURRENT_PC); |
|
81 |
memset((void*)_buckets, 0, table_size * sizeof(Bucket)); |
|
82 |
} |
|
83 |
||
84 |
size_t hash_to_index(uintptr_t full_hash) const { |
|
85 |
const uintptr_t h = full_hash % _table_size; |
|
86 |
assert(h >= 0 && h < _table_size, "Illegal hash value"); |
|
87 |
return (size_t)h; |
|
88 |
} |
|
89 |
size_t entry_size() const { return _entry_size; } |
|
90 |
void unlink_entry(TableEntry* entry) { |
|
91 |
entry->set_next(NULL); |
|
92 |
--_number_of_entries; |
|
93 |
} |
|
94 |
void free_buckets() { |
|
95 |
if (NULL != _buckets) { |
|
96 |
FREE_C_HEAP_ARRAY(Bucket, _buckets); |
|
97 |
_buckets = NULL; |
|
98 |
} |
|
99 |
} |
|
100 |
TableEntry* bucket(size_t i) { return _buckets[i].get_entry();} |
|
101 |
TableEntry** bucket_addr(size_t i) { return _buckets[i].entry_addr(); } |
|
102 |
uintptr_t table_size() const { return _table_size; } |
|
103 |
size_t number_of_entries() const { return _number_of_entries; } |
|
104 |
void add_entry(size_t index, TableEntry* entry) { |
|
105 |
assert(entry != NULL, "invariant"); |
|
106 |
entry->set_next(bucket(index)); |
|
107 |
_buckets[index].set_entry(entry); |
|
108 |
++_number_of_entries; |
|
109 |
} |
|
110 |
}; |
|
111 |
||
112 |
template <typename IdType, typename Entry, typename T> |
|
57983
a57907813a83
old object sample blob:ification and serialization support
mgronlun
parents:
57870
diff
changeset
|
113 |
class AscendingId : public JfrCHeapObj { |
50113 | 114 |
private: |
115 |
IdType _id; |
|
116 |
public: |
|
117 |
AscendingId() : _id(0) {} |
|
118 |
// callbacks |
|
57983
a57907813a83
old object sample blob:ification and serialization support
mgronlun
parents:
57870
diff
changeset
|
119 |
void link(Entry* entry) { |
50113 | 120 |
assert(entry != NULL, "invariant"); |
121 |
assert(entry->id() == 0, "invariant"); |
|
122 |
entry->set_id(++_id); |
|
123 |
} |
|
57983
a57907813a83
old object sample blob:ification and serialization support
mgronlun
parents:
57870
diff
changeset
|
124 |
bool equals(uintptr_t hash, const Entry* entry) { |
50113 | 125 |
assert(entry->hash() == hash, "invariant"); |
126 |
return true; |
|
127 |
} |
|
128 |
}; |
|
129 |
||
130 |
// IdType must be scalar |
|
131 |
template <typename T, typename IdType> |
|
57983
a57907813a83
old object sample blob:ification and serialization support
mgronlun
parents:
57870
diff
changeset
|
132 |
class JfrHashtableEntry : public JfrBasicHashtableEntry<T> { |
50113 | 133 |
public: |
57983
a57907813a83
old object sample blob:ification and serialization support
mgronlun
parents:
57870
diff
changeset
|
134 |
JfrHashtableEntry(uintptr_t hash, const T& data) : JfrBasicHashtableEntry<T>(hash, data), _id(0) {} |
50113 | 135 |
typedef IdType ID; |
136 |
ID id() const { return _id; } |
|
57870
00860d9caf4d
New metadata system for oldobjects built on top of simplified tagging model. Caching and serialization improvements. Flushpoint checkpoint with chunkheader contents.
mgronlun
parents:
53244
diff
changeset
|
137 |
void set_id(ID id) const { _id = id; } |
57983
a57907813a83
old object sample blob:ification and serialization support
mgronlun
parents:
57870
diff
changeset
|
138 |
T& value() const { return *const_cast<JfrHashtableEntry*>(this)->literal_addr();} |
a57907813a83
old object sample blob:ification and serialization support
mgronlun
parents:
57870
diff
changeset
|
139 |
const T* value_addr() const { return const_cast<JfrHashtableEntry*>(this)->literal_addr(); } |
50113 | 140 |
private: |
57870
00860d9caf4d
New metadata system for oldobjects built on top of simplified tagging model. Caching and serialization improvements. Flushpoint checkpoint with chunkheader contents.
mgronlun
parents:
53244
diff
changeset
|
141 |
mutable ID _id; |
00860d9caf4d
New metadata system for oldobjects built on top of simplified tagging model. Caching and serialization improvements. Flushpoint checkpoint with chunkheader contents.
mgronlun
parents:
53244
diff
changeset
|
142 |
}; |
00860d9caf4d
New metadata system for oldobjects built on top of simplified tagging model. Caching and serialization improvements. Flushpoint checkpoint with chunkheader contents.
mgronlun
parents:
53244
diff
changeset
|
143 |
|
50113 | 144 |
template <typename T, typename IdType, template <typename, typename> class Entry, |
145 |
typename Callback = AscendingId<IdType, Entry<T, IdType>, T> , |
|
146 |
size_t TABLE_SIZE = 1009> |
|
147 |
class HashTableHost : public JfrBasicHashtable<T> { |
|
148 |
public: |
|
149 |
typedef Entry<T, IdType> HashEntry; |
|
57983
a57907813a83
old object sample blob:ification and serialization support
mgronlun
parents:
57870
diff
changeset
|
150 |
HashTableHost(size_t size = 0) : JfrBasicHashtable<T>(size == 0 ? TABLE_SIZE : size, sizeof(HashEntry)), _callback(new Callback()) {} |
a57907813a83
old object sample blob:ification and serialization support
mgronlun
parents:
57870
diff
changeset
|
151 |
HashTableHost(Callback* cb, size_t size = 0) : JfrBasicHashtable<T>(size == 0 ? TABLE_SIZE : size, sizeof(HashEntry)), _callback(cb) {} |
50113 | 152 |
~HashTableHost() { |
153 |
this->clear_entries(); |
|
154 |
this->free_buckets(); |
|
155 |
} |
|
156 |
||
157 |
// direct insert assumes non-existing entry |
|
57983
a57907813a83
old object sample blob:ification and serialization support
mgronlun
parents:
57870
diff
changeset
|
158 |
HashEntry& put(uintptr_t hash, const T& data); |
50113 | 159 |
|
160 |
// lookup entry, will put if not found |
|
57983
a57907813a83
old object sample blob:ification and serialization support
mgronlun
parents:
57870
diff
changeset
|
161 |
HashEntry& lookup_put(uintptr_t hash, const T& data) { |
a57907813a83
old object sample blob:ification and serialization support
mgronlun
parents:
57870
diff
changeset
|
162 |
HashEntry* entry = lookup_only(hash); |
a57907813a83
old object sample blob:ification and serialization support
mgronlun
parents:
57870
diff
changeset
|
163 |
return entry == NULL ? put(hash, data) : *entry; |
50113 | 164 |
} |
165 |
||
57983
a57907813a83
old object sample blob:ification and serialization support
mgronlun
parents:
57870
diff
changeset
|
166 |
HashEntry* lookup_only(uintptr_t hash); |
50113 | 167 |
|
168 |
// id retrieval |
|
57983
a57907813a83
old object sample blob:ification and serialization support
mgronlun
parents:
57870
diff
changeset
|
169 |
IdType id(uintptr_t hash, const T& data) { |
50113 | 170 |
assert(data != NULL, "invariant"); |
57983
a57907813a83
old object sample blob:ification and serialization support
mgronlun
parents:
57870
diff
changeset
|
171 |
const HashEntry& entry = lookup_put(hash, data); |
50113 | 172 |
assert(entry.id() > 0, "invariant"); |
173 |
return entry.id(); |
|
174 |
} |
|
175 |
||
176 |
template <typename Functor> |
|
177 |
void iterate_value(Functor& f); |
|
178 |
||
179 |
template <typename Functor> |
|
180 |
void iterate_entry(Functor& f); |
|
181 |
||
182 |
size_t cardinality() const { return this->number_of_entries(); } |
|
183 |
bool has_entries() const { return this->cardinality() > 0; } |
|
184 |
void clear_entries(); |
|
185 |
||
186 |
// removal and deallocation |
|
187 |
void free_entry(HashEntry* entry) { |
|
188 |
assert(entry != NULL, "invariant"); |
|
189 |
JfrBasicHashtable<T>::unlink_entry(entry); |
|
57870
00860d9caf4d
New metadata system for oldobjects built on top of simplified tagging model. Caching and serialization improvements. Flushpoint checkpoint with chunkheader contents.
mgronlun
parents:
53244
diff
changeset
|
190 |
_callback->unlink(entry); |
57983
a57907813a83
old object sample blob:ification and serialization support
mgronlun
parents:
57870
diff
changeset
|
191 |
delete entry; |
50113 | 192 |
} |
193 |
||
194 |
private: |
|
195 |
Callback* _callback; |
|
196 |
size_t index_for(uintptr_t hash) { return this->hash_to_index(hash); } |
|
57983
a57907813a83
old object sample blob:ification and serialization support
mgronlun
parents:
57870
diff
changeset
|
197 |
HashEntry* new_entry(uintptr_t hash, const T& data); |
50113 | 198 |
void add_entry(size_t index, HashEntry* new_entry) { |
199 |
assert(new_entry != NULL, "invariant"); |
|
57983
a57907813a83
old object sample blob:ification and serialization support
mgronlun
parents:
57870
diff
changeset
|
200 |
_callback->link(new_entry); |
50113 | 201 |
assert(new_entry->id() > 0, "invariant"); |
202 |
JfrBasicHashtable<T>::add_entry(index, new_entry); |
|
203 |
} |
|
204 |
}; |
|
205 |
||
206 |
template <typename T, typename IdType, template <typename, typename> class Entry, typename Callback, size_t TABLE_SIZE> |
|
57983
a57907813a83
old object sample blob:ification and serialization support
mgronlun
parents:
57870
diff
changeset
|
207 |
Entry<T, IdType>& HashTableHost<T, IdType, Entry, Callback, TABLE_SIZE>::put(uintptr_t hash, const T& data) { |
a57907813a83
old object sample blob:ification and serialization support
mgronlun
parents:
57870
diff
changeset
|
208 |
assert(lookup_only(hash) == NULL, "use lookup_put()"); |
a57907813a83
old object sample blob:ification and serialization support
mgronlun
parents:
57870
diff
changeset
|
209 |
HashEntry* const entry = new_entry(hash, data); |
50113 | 210 |
add_entry(index_for(hash), entry); |
211 |
return *entry; |
|
212 |
} |
|
213 |
||
214 |
template <typename T, typename IdType, template <typename, typename> class Entry, typename Callback, size_t TABLE_SIZE> |
|
57983
a57907813a83
old object sample blob:ification and serialization support
mgronlun
parents:
57870
diff
changeset
|
215 |
Entry<T, IdType>* HashTableHost<T, IdType, Entry, Callback, TABLE_SIZE>::lookup_only(uintptr_t hash) { |
50113 | 216 |
HashEntry* entry = (HashEntry*)this->bucket(index_for(hash)); |
217 |
while (entry != NULL) { |
|
57983
a57907813a83
old object sample blob:ification and serialization support
mgronlun
parents:
57870
diff
changeset
|
218 |
if (entry->hash() == hash && _callback->equals(hash, entry)) { |
50113 | 219 |
return entry; |
220 |
} |
|
221 |
entry = (HashEntry*)entry->next(); |
|
222 |
} |
|
223 |
return NULL; |
|
224 |
} |
|
225 |
||
226 |
template <typename T, typename IdType, template <typename, typename> class Entry, typename Callback, size_t TABLE_SIZE> |
|
227 |
template <typename Functor> |
|
228 |
void HashTableHost<T, IdType, Entry, Callback, TABLE_SIZE>::iterate_value(Functor& f) { |
|
229 |
for (size_t i = 0; i < this->table_size(); ++i) { |
|
230 |
const HashEntry* entry = (const HashEntry*)this->bucket(i); |
|
231 |
while (entry != NULL) { |
|
232 |
if (!f(entry->value())) { |
|
233 |
break; |
|
234 |
} |
|
235 |
entry = (HashEntry*)entry->next(); |
|
236 |
} |
|
237 |
} |
|
238 |
} |
|
239 |
||
240 |
template <typename T, typename IdType, template <typename, typename> class Entry, typename Callback, size_t TABLE_SIZE> |
|
241 |
template <typename Functor> |
|
242 |
void HashTableHost<T, IdType, Entry, Callback, TABLE_SIZE>::iterate_entry(Functor& f) { |
|
243 |
for (size_t i = 0; i < this->table_size(); ++i) { |
|
244 |
const HashEntry* entry = (const HashEntry*)this->bucket(i); |
|
245 |
while (entry != NULL) { |
|
246 |
if (!f(entry)) { |
|
247 |
break; |
|
248 |
} |
|
249 |
entry = (const HashEntry*)entry->next(); |
|
250 |
} |
|
251 |
} |
|
252 |
} |
|
253 |
||
254 |
template <typename T, typename IdType, template <typename, typename> class Entry, typename Callback, size_t TABLE_SIZE> |
|
255 |
void HashTableHost<T, IdType, Entry, Callback, TABLE_SIZE>::clear_entries() { |
|
256 |
for (size_t i = 0; i < this->table_size(); ++i) { |
|
257 |
HashEntry** bucket = (HashEntry**)this->bucket_addr(i); |
|
258 |
HashEntry* entry = *bucket; |
|
259 |
while (entry != NULL) { |
|
260 |
HashEntry* entry_to_remove = entry; |
|
261 |
entry = (HashEntry*)entry->next(); |
|
262 |
this->free_entry(entry_to_remove); |
|
263 |
} |
|
264 |
*bucket = NULL; |
|
265 |
} |
|
266 |
assert(this->number_of_entries() == 0, "should have removed all entries"); |
|
267 |
} |
|
268 |
||
269 |
template <typename T, typename IdType, template <typename, typename> class Entry, typename Callback, size_t TABLE_SIZE> |
|
57983
a57907813a83
old object sample blob:ification and serialization support
mgronlun
parents:
57870
diff
changeset
|
270 |
Entry<T, IdType>* HashTableHost<T, IdType, Entry, Callback, TABLE_SIZE>::new_entry(uintptr_t hash, const T& data) { |
50113 | 271 |
assert(sizeof(HashEntry) == this->entry_size(), "invariant"); |
57983
a57907813a83
old object sample blob:ification and serialization support
mgronlun
parents:
57870
diff
changeset
|
272 |
HashEntry* const entry = new HashEntry(hash, data); |
a57907813a83
old object sample blob:ification and serialization support
mgronlun
parents:
57870
diff
changeset
|
273 |
assert(entry != NULL, "invariant"); |
50113 | 274 |
assert(0 == entry->id(), "invariant"); |
275 |
return entry; |
|
276 |
} |
|
277 |
||
53244
9807daeb47c4
8216167: Update include guards to reflect correct directories
coleenp
parents:
50429
diff
changeset
|
278 |
#endif // SHARE_JFR_UTILITIES_JFRHASHTABLE_HPP |