1 /* 2 * Copyright (c) 1997, 2018, 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 */ 24 25 #ifndef SHARE_VM_CLASSFILE_COMPACTHASHTABLE_HPP 26 #define SHARE_VM_CLASSFILE_COMPACTHASHTABLE_HPP 27 28 #include "oops/array.hpp" 29 #include "oops/symbol.hpp" 30 #include "utilities/hashtable.hpp" 31 32 33 template < 34 typename K, 35 typename V, 36 V (*DECODE)(address base_address, u4 offset), 37 bool (*EQUALS)(V value, K key, int len) 38 > 39 class CompactHashtable; 40 class NumberSeq; 41 class SimpleCompactHashtable; 42 class SerializeClosure; 43 44 // Stats for symbol tables in the CDS archive 45 class CompactHashtableStats { 46 public: 47 int hashentry_count; 48 int hashentry_bytes; 49 int bucket_count; 50 int bucket_bytes; 51 }; 52 53 #if INCLUDE_CDS 54 ///////////////////////////////////////////////////////////////////////// 55 // 56 // The compact hash table writer. Used at dump time for writing out 57 // the compact table to the shared archive. 58 // 59 // At dump time, the CompactHashtableWriter obtains all entries from the 60 // symbol/string table and adds them to a new temporary hash table. The hash 61 // table size (number of buckets) is calculated using 62 // '(num_entries + bucket_size - 1) / bucket_size'. The default bucket 63 // size is 4 and can be changed by -XX:SharedSymbolTableBucketSize option. 64 // 4 is chosen because it produces smaller sized bucket on average for 65 // faster lookup. It also has relatively small number of empty buckets and 66 // good distribution of the entries. 67 // 68 // We use a simple hash function (hash % num_bucket) for the table. 69 // The new table is compacted when written out. Please see comments 70 // above the CompactHashtable class for the table layout detail. The bucket 71 // offsets are written to the archive as part of the compact table. The 72 // bucket offset is encoded in the low 30-bit (0-29) and the bucket type 73 // (regular or compact) are encoded in bit[31, 30]. For buckets with more 74 // than one entry, both hash and entry offset are written to the 75 // table. For buckets with only one entry, only the entry offset is written 76 // to the table and the buckets are tagged as compact in their type bits. 77 // Buckets without entry are skipped from the table. Their offsets are 78 // still written out for faster lookup. 79 // 80 class CompactHashtableWriter: public StackObj { 81 public: 82 class Entry { 83 unsigned int _hash; 84 u4 _value; 85 86 public: 87 Entry() {} 88 Entry(unsigned int hash, u4 val) : _hash(hash), _value(val) {} 89 90 u4 value() { 91 return _value; 92 } 93 unsigned int hash() { 94 return _hash; 95 } 96 97 bool operator==(const CompactHashtableWriter::Entry& other) { 98 return (_value == other._value && _hash == other._hash); 99 } 100 }; // class CompactHashtableWriter::Entry 101 102 private: 103 int _num_entries; 104 int _num_buckets; 105 int _num_empty_buckets; 106 int _num_value_only_buckets; 107 int _num_other_buckets; 108 GrowableArray<Entry>** _buckets; 109 CompactHashtableStats* _stats; 110 Array<u4>* _compact_buckets; 111 Array<u4>* _compact_entries; 112 113 public: 114 // This is called at dump-time only 115 CompactHashtableWriter(int num_buckets, CompactHashtableStats* stats); 116 ~CompactHashtableWriter(); 117 118 void add(unsigned int hash, u4 value); 119 120 private: 121 void allocate_table(); 122 void dump_table(NumberSeq* summary); 123 124 public: 125 void dump(SimpleCompactHashtable *cht, const char* table_name); 126 127 static int default_num_buckets(size_t num_entries) { 128 return default_num_buckets((int)num_entries); 129 } 130 static int default_num_buckets(int num_entries) { 131 int num_buckets = num_entries / SharedSymbolTableBucketSize; 132 // calculation of num_buckets can result in zero buckets, we need at least one 133 return (num_buckets < 1) ? 1 : num_buckets; 134 } 135 }; 136 #endif // INCLUDE_CDS 137 138 #define REGULAR_BUCKET_TYPE 0 139 #define VALUE_ONLY_BUCKET_TYPE 1 140 #define TABLEEND_BUCKET_TYPE 3 141 #define BUCKET_OFFSET_MASK 0x3FFFFFFF 142 #define BUCKET_OFFSET(info) ((info) & BUCKET_OFFSET_MASK) 143 #define BUCKET_TYPE_SHIFT 30 144 #define BUCKET_TYPE(info) (((info) & ~BUCKET_OFFSET_MASK) >> BUCKET_TYPE_SHIFT) 145 #define BUCKET_INFO(offset, type) (((type) << BUCKET_TYPE_SHIFT) | ((offset) & BUCKET_OFFSET_MASK)) 146 147 ///////////////////////////////////////////////////////////////////////////// 148 // 149 // CompactHashtable is used to store the CDS archive's symbol/string tables. 150 // 151 // Because these tables are read-only (no entries can be added/deleted) at run-time 152 // and tend to have large number of entries, we try to minimize the footprint 153 // cost per entry. 154 // 155 // The CompactHashtable is split into two arrays 156 // 157 // u4 buckets[num_buckets+1]; // bit[31,30]: type; bit[29-0]: offset 158 // u4 entries[<variable size>] 159 // 160 // The size of buckets[] is 'num_buckets + 1'. Each entry of 161 // buckets[] is a 32-bit encoding of the bucket type and bucket offset, 162 // with the type in the left-most 2-bit and offset in the remaining 30-bit. 163 // The last entry is a special type. It contains the end of the last 164 // bucket. 165 // 166 // There are two types of buckets, regular buckets and value_only buckets. The 167 // value_only buckets have '01' in their highest 2-bit, and regular buckets have 168 // '00' in their highest 2-bit. 169 // 170 // For normal buckets, each entry is 8 bytes in the entries[]: 171 // u4 hash; /* symbol/string hash */ 172 // union { 173 // u4 offset; /* Symbol* sym = (Symbol*)(base_address + offset) */ 174 // narrowOop str; /* String narrowOop encoding */ 175 // } 176 // 177 // 178 // For value_only buckets, each entry has only the 4-byte 'offset' in the entries[]. 179 // 180 // Example -- note that the second bucket is a VALUE_ONLY_BUCKET_TYPE so the hash code 181 // is skipped. 182 // buckets[0, 4, 5, ....] 183 // | | | 184 // | | +---+ 185 // | | | 186 // | +----+ | 187 // v v v 188 // entries[H,O,H,O,O,H,O,H,O.....] 189 // 190 // See CompactHashtable::lookup() for how the table is searched at runtime. 191 // See CompactHashtableWriter::dump() for how the table is written at CDS 192 // dump time. 193 // 194 class SimpleCompactHashtable { 195 protected: 196 address _base_address; 197 u4 _bucket_count; 198 u4 _entry_count; 199 u4* _buckets; 200 u4* _entries; 201 202 public: 203 SimpleCompactHashtable() { 204 _entry_count = 0; 205 _bucket_count = 0; 206 _buckets = 0; 207 _entries = 0; 208 } 209 210 void reset() { 211 _bucket_count = 0; 212 _entry_count = 0; 213 _buckets = 0; 214 _entries = 0; 215 } 216 217 void init(address base_address, u4 entry_count, u4 bucket_count, u4* buckets, u4* entries) { 218 _base_address = base_address; 219 _bucket_count = bucket_count; 220 _entry_count = entry_count; 221 _buckets = buckets; 222 _entries = entries; 223 } 224 225 // For reading from/writing to the CDS archive 226 void serialize(SerializeClosure* soc) NOT_CDS_RETURN; 227 228 inline bool empty() { 229 return (_entry_count == 0); 230 } 231 }; 232 233 template < 234 typename K, 235 typename V, 236 V (*DECODE)(address base_address, u4 offset), 237 bool (*EQUALS)(V value, K key, int len) 238 > 239 class CompactHashtable : public SimpleCompactHashtable { 240 friend class VMStructs; 241 242 V decode(u4 offset) const { 243 return DECODE(_base_address, offset); 244 } 245 246 public: 247 CompactHashtable() : SimpleCompactHashtable() {} 248 249 // Lookup a value V from the compact table using key K 250 inline V lookup(K key, unsigned int hash, int len) const { 251 if (_entry_count > 0) { 252 int index = hash % _bucket_count; 253 u4 bucket_info = _buckets[index]; 254 u4 bucket_offset = BUCKET_OFFSET(bucket_info); 255 int bucket_type = BUCKET_TYPE(bucket_info); 256 u4* entry = _entries + bucket_offset; 257 258 if (bucket_type == VALUE_ONLY_BUCKET_TYPE) { 259 V value = decode(entry[0]); 260 if (EQUALS(value, key, len)) { 261 return value; 262 } 263 } else { 264 // This is a regular bucket, which has more than one 265 // entries. Each entry is a pair of entry (hash, offset). 266 // Seek until the end of the bucket. 267 u4* entry_max = _entries + BUCKET_OFFSET(_buckets[index + 1]); 268 while (entry < entry_max) { 269 unsigned int h = (unsigned int)(entry[0]); 270 if (h == hash) { 271 V value = decode(entry[1]); 272 if (EQUALS(value, key, len)) { 273 return value; 274 } 275 } 276 entry += 2; 277 } 278 } 279 } 280 return NULL; 281 } 282 283 template <class ITER> 284 inline void iterate(ITER* iter) const { 285 for (u4 i = 0; i < _bucket_count; i++) { 286 u4 bucket_info = _buckets[i]; 287 u4 bucket_offset = BUCKET_OFFSET(bucket_info); 288 int bucket_type = BUCKET_TYPE(bucket_info); 289 u4* entry = _entries + bucket_offset; 290 291 if (bucket_type == VALUE_ONLY_BUCKET_TYPE) { 292 iter->do_value(decode(entry[0])); 293 } else { 294 u4*entry_max = _entries + BUCKET_OFFSET(_buckets[i + 1]); 295 while (entry < entry_max) { 296 iter->do_value(decode(entry[1])); 297 entry += 2; 298 } 299 } 300 } 301 } 302 }; 303 304 //////////////////////////////////////////////////////////////////////// 305 // 306 // Read/Write the contents of a hashtable textual dump (created by 307 // SymbolTable::dump and StringTable::dump). 308 // Because the dump file may be big (hundred of MB in extreme cases), 309 // we use mmap for fast access when reading it. 310 // 311 class HashtableTextDump { 312 int _fd; 313 const char* _base; 314 const char* _p; 315 const char* _end; 316 const char* _filename; 317 size_t _size; 318 int _prefix_type; 319 int _line_no; 320 public: 321 HashtableTextDump(const char* filename); 322 ~HashtableTextDump(); 323 324 enum { 325 SymbolPrefix = 1 << 0, 326 StringPrefix = 1 << 1, 327 Unknown = 1 << 2 328 }; 329 330 void quit(const char* err, const char* msg); 331 332 inline int remain() { 333 return (int)(_end - _p); 334 } 335 336 void corrupted(const char *p, const char *msg); 337 338 inline void corrupted_if(bool cond, const char *msg) { 339 if (cond) { 340 corrupted(_p, msg); 341 } 342 } 343 344 bool skip_newline(); 345 int skip(char must_be_char); 346 void skip_past(char c); 347 void check_version(const char* ver); 348 349 inline void get_num(char delim, int *num) { 350 const char* p = _p; 351 const char* end = _end; 352 u8 n = 0; 353 354 while (p < end) { 355 char c = *p++; 356 if ('0' <= c && c <= '9') { 357 n = n * 10 + (c - '0'); 358 if (n > (u8)INT_MAX) { 359 corrupted(_p, "Num overflow"); 360 } 361 } else if (c == delim) { 362 _p = p; 363 *num = (int)n; 364 return; 365 } else { 366 // Not [0-9], not 'delim' 367 corrupted(_p, "Unrecognized format");; 368 } 369 } 370 371 corrupted(_end, "Incorrect format"); 372 ShouldNotReachHere(); 373 } 374 375 void scan_prefix_type(); 376 int scan_prefix(int* utf8_length); 377 int scan_string_prefix(); 378 int scan_symbol_prefix(); 379 380 jchar unescape(const char* from, const char* end, int count); 381 void get_utf8(char* utf8_buffer, int utf8_length); 382 static void put_utf8(outputStream* st, const char* utf8_string, int utf8_length); 383 }; 384 385 #endif // SHARE_VM_CLASSFILE_COMPACTHASHTABLE_HPP