/* * Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. *
*/
template < typename K, typename V,
V (*DECODE)(address base_address, u4 offset), bool (*EQUALS)(V value, K key, int len)
> class CompactHashtable; class NumberSeq; class SimpleCompactHashtable; class SerializeClosure;
// Stats for symbol tables in the CDS archive class CompactHashtableStats { public: int hashentry_count; int hashentry_bytes; int bucket_count; int bucket_bytes;
#if INCLUDE_CDS ///////////////////////////////////////////////////////////////////////// // // The compact hash table writer. Used at dump time for writing out // the compact table to the shared archive. // // At dump time, the CompactHashtableWriter obtains all entries from the // symbol/string table and adds them to a new temporary hash table. The hash // table size (number of buckets) is calculated using // '(num_entries + bucket_size - 1) / bucket_size'. The default bucket // size is 4 and can be changed by -XX:SharedSymbolTableBucketSize option. // 4 is chosen because it produces smaller sized bucket on average for // faster lookup. It also has relatively small number of empty buckets and // good distribution of the entries. // // We use a simple hash function (hash % num_bucket) for the table. // The new table is compacted when written out. Please see comments // above the CompactHashtable class for the table layout detail. The bucket // offsets are written to the archive as part of the compact table. The // bucket offset is encoded in the low 30-bit (0-29) and the bucket type // (regular or compact) are encoded in bit[31, 30]. For buckets with more // than one entry, both hash and entry offset are written to the // table. For buckets with only one entry, only the entry offset is written // to the table and the buckets are tagged as compact in their type bits. // Buckets without entry are skipped from the table. Their offsets are // still written out for faster lookup. // class CompactHashtableWriter: public StackObj { public: class Entry { unsignedint _hash;
u4 _value;
private: int _num_entries_written; int _num_buckets; int _num_empty_buckets; int _num_value_only_buckets; int _num_other_buckets;
GrowableArray<Entry>** _buckets;
CompactHashtableStats* _stats;
Array<u4>* _compact_buckets;
Array<u4>* _compact_entries;
public: // This is called at dump-time only
CompactHashtableWriter(int num_entries, CompactHashtableStats* stats);
~CompactHashtableWriter();
void add(unsignedint hash, u4 value);
private: void allocate_table(); void dump_table(NumberSeq* summary); staticint calculate_num_buckets(int num_entries) { int num_buckets = num_entries / SharedSymbolTableBucketSize; // calculation of num_buckets can result in zero buckets, we need at least one return (num_buckets < 1) ? 1 : num_buckets;
}
///////////////////////////////////////////////////////////////////////////// // // CompactHashtable is used to store the CDS archive's symbol/string tables. // // Because these tables are read-only (no entries can be added/deleted) at run-time // and tend to have large number of entries, we try to minimize the footprint // cost per entry. // // The CompactHashtable is split into two arrays // // u4 buckets[num_buckets+1]; // bit[31,30]: type; bit[29-0]: offset // u4 entries[<variable size>] // // The size of buckets[] is 'num_buckets + 1'. Each entry of // buckets[] is a 32-bit encoding of the bucket type and bucket offset, // with the type in the left-most 2-bit and offset in the remaining 30-bit. // The last entry is a special type. It contains the end of the last // bucket. // // There are two types of buckets, regular buckets and value_only buckets. The // value_only buckets have '01' in their highest 2-bit, and regular buckets have // '00' in their highest 2-bit. // // For normal buckets, each entry is 8 bytes in the entries[]: // u4 hash; /* symbol/string hash */ // union { // u4 offset; /* Symbol* sym = (Symbol*)(base_address + offset) */ // narrowOop str; /* String narrowOop encoding */ // } // // // For value_only buckets, each entry has only the 4-byte 'offset' in the entries[]. // // Example -- note that the second bucket is a VALUE_ONLY_BUCKET_TYPE so the hash code // is skipped. // buckets[0, 4, 5, ....] // | | | // | | +---+ // | | | // | +----+ | // v v v // entries[H,O,H,O,O,H,O,H,O.....] // // See CompactHashtable::lookup() for how the table is searched at runtime. // See CompactHashtableWriter::dump() for how the table is written at CDS // dump time. // class SimpleCompactHashtable { protected:
address _base_address;
u4 _bucket_count;
u4 _entry_count;
u4* _buckets;
u4* _entries;
template < typename K, typename V,
V (*DECODE)(address base_address, u4 offset), bool (*EQUALS)(V value, K key, int len)
> class CompactHashtable : public SimpleCompactHashtable { friendclass VMStructs;
V decode(u4 offset) const { return DECODE(_base_address, offset);
}
public: // Lookup a value V from the compact table using key K inline V lookup(K key, unsignedint hash, int len) const { if (_entry_count > 0) { int index = hash % _bucket_count;
u4 bucket_info = _buckets[index];
u4 bucket_offset = BUCKET_OFFSET(bucket_info); int bucket_type = BUCKET_TYPE(bucket_info);
u4* entry = _entries + bucket_offset;
if (bucket_type == VALUE_ONLY_BUCKET_TYPE) {
V value = decode(entry[0]); if (EQUALS(value, key, len)) { return value;
}
} else { // This is a regular bucket, which has more than one // entries. Each entry is a pair of entry (hash, offset). // Seek until the end of the bucket.
u4* entry_max = _entries + BUCKET_OFFSET(_buckets[index + 1]); while (entry < entry_max) { unsignedint h = (unsignedint)(entry[0]); if (h == hash) {
V value = decode(entry[1]); if (EQUALS(value, key, len)) { return value;
}
}
entry += 2;
}
}
} return NULL;
}
template <class ITER> inlinevoid iterate(ITER* iter) const { for (u4 i = 0; i < _bucket_count; i++) {
u4 bucket_info = _buckets[i];
u4 bucket_offset = BUCKET_OFFSET(bucket_info); int bucket_type = BUCKET_TYPE(bucket_info);
u4* entry = _entries + bucket_offset;
//////////////////////////////////////////////////////////////////////// // // OffsetCompactHashtable -- This is used to store many types of objects // in the CDS archive. On 64-bit platforms, we save space by using a 32-bit // offset from the CDS base address.
template < typename K, typename V, bool (*EQUALS)(V value, K key, int len)
> class OffsetCompactHashtable : public CompactHashtable<
K, V, read_value_from_compact_hashtable<V>, EQUALS> {
};
//////////////////////////////////////////////////////////////////////// // // Read/Write the contents of a hashtable textual dump (created by // SymbolTable::dump and StringTable::dump). // Because the dump file may be big (hundred of MB in extreme cases), // we use mmap for fast access when reading it. // class HashtableTextDump { int _fd; constchar* _base; constchar* _p; constchar* _end; constchar* _filename;
size_t _size; int _prefix_type; int _line_no; public:
HashtableTextDump(constchar* filename);
~HashtableTextDump();
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.