--- old/agent/src/share/classes/sun/jvm/hotspot/memory/ProtectionDomainEntry.java 2013-09-25 14:45:10.748642577 +0200 +++ new/agent/src/share/classes/sun/jvm/hotspot/memory/ProtectionDomainEntry.java 2013-09-25 14:45:10.680642578 +0200 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2001, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2001, 2013, 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 @@ -32,7 +32,7 @@ public class ProtectionDomainEntry extends VMObject { private static AddressField nextField; - private static sun.jvm.hotspot.types.OopField protectionDomainField; + private static AddressField pdCacheField; static { VM.registerVMInitializedObserver(new Observer() { @@ -46,7 +46,7 @@ Type type = db.lookupType("ProtectionDomainEntry"); nextField = type.getAddressField("_next"); - protectionDomainField = type.getOopField("_protection_domain"); + pdCacheField = type.getAddressField("_pd_cache"); } public ProtectionDomainEntry(Address addr) { @@ -54,10 +54,12 @@ } public ProtectionDomainEntry next() { - return (ProtectionDomainEntry) VMObjectFactory.newObject(ProtectionDomainEntry.class, addr); + return (ProtectionDomainEntry) VMObjectFactory.newObject(ProtectionDomainEntry.class, nextField.getValue(addr)); } public Oop protectionDomain() { - return VM.getVM().getObjectHeap().newOop(protectionDomainField.getValue(addr)); + ProtectionDomainCacheEntry pd_cache = (ProtectionDomainCacheEntry) + VMObjectFactory.newObject(ProtectionDomainCacheEntry.class, pdCacheField.getValue(addr)); + return pd_cache.protectionDomain(); } } --- old/src/share/vm/classfile/dictionary.cpp 2013-09-25 14:45:11.144642571 +0200 +++ new/src/share/vm/classfile/dictionary.cpp 2013-09-25 14:45:11.080642572 +0200 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2013, 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 @@ -38,17 +38,21 @@ : TwoOopHashtable(table_size, sizeof(DictionaryEntry)) { _current_class_index = 0; _current_class_entry = NULL; + _pd_cache_table = new ProtectionDomainCacheTable(ProtectionDomainCacheSize); }; - Dictionary::Dictionary(int table_size, HashtableBucket* t, int number_of_entries) : TwoOopHashtable(table_size, sizeof(DictionaryEntry), t, number_of_entries) { _current_class_index = 0; _current_class_entry = NULL; + _pd_cache_table = new ProtectionDomainCacheTable(ProtectionDomainCacheSize); }; +ProtectionDomainCacheEntry* Dictionary::cache_get(oop protection_domain) { + return _pd_cache_table->get(protection_domain); +} DictionaryEntry* Dictionary::new_entry(unsigned int hash, Klass* klass, ClassLoaderData* loader_data) { @@ -65,6 +69,10 @@ while (entry->pd_set() != NULL) { ProtectionDomainEntry* to_delete = entry->pd_set(); entry->set_pd_set(to_delete->next()); + ProtectionDomainCacheEntry* cache = to_delete->release_pd_cache(); + if (cache != NULL) { + _pd_cache_table->free(cache); + } delete to_delete; } Hashtable::free_entry(entry); @@ -105,11 +113,12 @@ } -void DictionaryEntry::add_protection_domain(oop protection_domain) { +void DictionaryEntry::add_protection_domain(Dictionary* dict, oop protection_domain) { assert_locked_or_safepoint(SystemDictionary_lock); if (!contains_protection_domain(protection_domain)) { + ProtectionDomainCacheEntry* entry = dict->cache_get(protection_domain); ProtectionDomainEntry* new_head = - new ProtectionDomainEntry(protection_domain, _pd_set); + new ProtectionDomainEntry(entry, _pd_set); // Warning: Preserve store ordering. The SystemDictionary is read // without locks. The new ProtectionDomainEntry must be // complete before other threads can be allowed to see it @@ -193,7 +202,10 @@ void Dictionary::always_strong_oops_do(OopClosure* blk) { - // Follow all system classes and temporary placeholders in dictionary + // Follow all system classes and temporary placeholders in dictionary; only + // protection domain oops contain references into the heap. In a first + // pass over the system dictionary determine which need to be treated as + // strongly reachable and mark them as such. for (int index = 0; index < table_size(); index++) { for (DictionaryEntry *probe = bucket(index); probe != NULL; @@ -201,10 +213,13 @@ Klass* e = probe->klass(); ClassLoaderData* loader_data = probe->loader_data(); if (is_strongly_reachable(loader_data, e)) { - probe->protection_domain_set_oops_do(blk); + probe->set_strongly_reachable(); } } } + // then iterate over the protection domain cache to apply the closure on the + // previously marked ones. + _pd_cache_table->always_strong_oops_do(blk); } @@ -266,18 +281,12 @@ } } - void Dictionary::oops_do(OopClosure* f) { - for (int index = 0; index < table_size(); index++) { - for (DictionaryEntry* probe = bucket(index); - probe != NULL; - probe = probe->next()) { - probe->protection_domain_set_oops_do(f); - } - } + // only the protection domain oops contain references into the heap. Iterate + // over all of them. + _pd_cache_table->oops_do(f); } - void Dictionary::methods_do(void f(Method*)) { for (int index = 0; index < table_size(); index++) { for (DictionaryEntry* probe = bucket(index); @@ -292,7 +301,6 @@ } } - Klass* Dictionary::try_get_next_class() { while (true) { if (_current_class_entry != NULL) { @@ -306,7 +314,6 @@ // never reached } - // Add a loaded class to the system dictionary. // Readers of the SystemDictionary aren't always locked, so _buckets // is volatile. The store of the next field in the constructor is @@ -396,7 +403,7 @@ assert(protection_domain() != NULL, "real protection domain should be present"); - entry->add_protection_domain(protection_domain()); + entry->add_protection_domain(this, protection_domain()); assert(entry->contains_protection_domain(protection_domain()), "now protection domain should be present"); @@ -446,6 +453,130 @@ } } +ProtectionDomainCacheTable::ProtectionDomainCacheTable(int table_size) + : Hashtable(table_size, sizeof(ProtectionDomainCacheEntry)) +{ +} + +void ProtectionDomainCacheTable::oops_do(OopClosure* f) { + for (int index = 0; index < table_size(); index++) { + for (ProtectionDomainCacheEntry* probe = bucket(index); + probe != NULL; + probe = probe->next()) { + probe->oops_do(f); + } + } +} + +uint ProtectionDomainCacheTable::bucket_size() { + return sizeof(ProtectionDomainCacheEntry); +} + +#ifndef PRODUCT +void ProtectionDomainCacheTable::print() { + tty->print_cr("Protection domain cache table (table_size=%d, classes=%d)", + table_size(), number_of_entries()); + for (int index = 0; index < table_size(); index++) { + for (ProtectionDomainCacheEntry* probe = bucket(index); + probe != NULL; + probe = probe->next()) { + probe->print(); + } + } +} + +void ProtectionDomainCacheEntry::print() { + tty->print_cr("entry "PTR_FORMAT" value "PTR_FORMAT" refcount %d strongly_reachable %d next "PTR_FORMAT, + this, literal(), refcount(), _strongly_reachable, next()); +} +#endif + +void ProtectionDomainCacheTable::verify() { + int element_count = 0; + for (int index = 0; index < table_size(); index++) { + for (ProtectionDomainCacheEntry* probe = bucket(index); + probe != NULL; + probe = probe->next()) { + probe->verify(); + element_count++; + } + } + guarantee(number_of_entries() == element_count, + "Verify of protection domain cache table failed"); + debug_only(verify_lookup_length((double)number_of_entries() / table_size())); +} + +void ProtectionDomainCacheEntry::verify() { + guarantee(literal()->is_oop(), "must be an oop"); + guarantee(_refcount >= 0, "must be"); +} + +void ProtectionDomainCacheTable::always_strong_oops_do(OopClosure* f) { + // the caller marked the protection domain cache entries that we need to apply + // the closure on. Only process them. + for (int index = 0; index < table_size(); index++) { + for (ProtectionDomainCacheEntry* probe = bucket(index); + probe != NULL; + probe = probe->next()) { + if (probe->is_strongly_reachable()) { + probe->reset_strongly_reachable(); + probe->oops_do(f); + } + } + } +} + +ProtectionDomainCacheEntry* ProtectionDomainCacheTable::get(oop protection_domain) { + unsigned int hash = compute_hash(protection_domain); + int index = hash_to_index(hash); + + ProtectionDomainCacheEntry* entry = find_entry(index, protection_domain); + if (entry == NULL) { + entry = add_entry(index, hash, protection_domain); + } + return entry; +} + +ProtectionDomainCacheEntry* ProtectionDomainCacheTable::find_entry(int index, oop protection_domain) { + for (ProtectionDomainCacheEntry* e = bucket(index); e != NULL; e = e->next()) { + if (e->protection_domain() == protection_domain) { + return e; + } + } + + return NULL; +} + +ProtectionDomainCacheEntry* ProtectionDomainCacheTable::add_entry(int index, unsigned int hash, oop protection_domain) { + assert_locked_or_safepoint(SystemDictionary_lock); + assert(index == index_for(protection_domain), "incorrect index?"); + assert(find_entry(index, protection_domain) == NULL, "no double entry"); + + ProtectionDomainCacheEntry* p = new_entry(hash, protection_domain); + Hashtable::add_entry(index, p); + return p; +} + +void ProtectionDomainCacheTable::free(ProtectionDomainCacheEntry* to_delete) { + unsigned int hash = compute_hash(to_delete->protection_domain()); + int index = hash_to_index(hash); + + ProtectionDomainCacheEntry** p = bucket_addr(index); + ProtectionDomainCacheEntry* entry = bucket(index); + while (true) { + assert(entry != NULL, "sanity"); + + if (entry == to_delete) { + *p = entry->next(); + Hashtable::free_entry(entry); + break; + } else { + p = entry->next_addr(); + entry = *p; + } + } +} + SymbolPropertyTable::SymbolPropertyTable(int table_size) : Hashtable(table_size, sizeof(SymbolPropertyEntry)) { @@ -532,11 +663,12 @@ tty->cr(); } } + tty->cr(); + _pd_cache_table->print(); } #endif - void Dictionary::verify() { guarantee(number_of_entries() >= 0, "Verify of system dictionary failed"); @@ -563,5 +695,7 @@ guarantee(number_of_entries() == element_count, "Verify of system dictionary failed"); debug_only(verify_lookup_length((double)number_of_entries() / table_size())); + + _pd_cache_table->verify(); } --- old/src/share/vm/classfile/dictionary.hpp 2013-09-25 14:45:11.564642564 +0200 +++ new/src/share/vm/classfile/dictionary.hpp 2013-09-25 14:45:11.500642565 +0200 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2013, 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 @@ -27,11 +27,13 @@ #include "classfile/systemDictionary.hpp" #include "oops/instanceKlass.hpp" -#include "oops/oop.hpp" +#include "oops/oop.inline.hpp" #include "utilities/hashtable.hpp" class DictionaryEntry; class PSPromotionManager; +class ProtectionDomainCacheTable; +class ProtectionDomainCacheEntry; //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // The data structure for the system dictionary (and the shared system @@ -45,6 +47,8 @@ // pointer to the current hash table entry. static DictionaryEntry* _current_class_entry; + ProtectionDomainCacheTable* _pd_cache_table; + DictionaryEntry* get_entry(int index, unsigned int hash, Symbol* name, ClassLoaderData* loader_data); @@ -118,6 +122,7 @@ // Sharing support void reorder_dictionary(); + ProtectionDomainCacheEntry* cache_get(oop protection_domain); #ifndef PRODUCT void print(); @@ -126,21 +131,136 @@ }; // The following classes can be in dictionary.cpp, but we need these -// to be in header file so that SA's vmStructs can access. +// to be in header file so that SA's vmStructs can access them. +class ProtectionDomainCacheEntry : public HashtableEntry { + friend class VMStructs; + private: + // the number of system dictionary entries referring to this entry + int _refcount; + // flag indicating whether this protection domain entry is strongly reachable. + // Used during iterating over the system dictionary to remember oops that need + // to be updated. + bool _strongly_reachable; + public: + oop protection_domain() { return literal(); } + + void increment_refcount() { + assert(_refcount >= 0, "sanity"); + _refcount++; + } + + int refcount() { return _refcount; } + int decrement_refcount() { + assert(_refcount > 0, "sanity"); + _refcount--; + return _refcount; + } + + void init() { + _refcount = 0; + _strongly_reachable = false; + } + + ProtectionDomainCacheEntry* next() { + return (ProtectionDomainCacheEntry*)HashtableEntry::next(); + } + + ProtectionDomainCacheEntry** next_addr() { + return (ProtectionDomainCacheEntry**)HashtableEntry::next_addr(); + } + + void oops_do(OopClosure* f) { + f->do_oop(literal_addr()); + } + + void set_strongly_reachable() { _strongly_reachable = true; } + + bool is_strongly_reachable() { return _strongly_reachable; } + + void reset_strongly_reachable() { _strongly_reachable = false; } + + void print() PRODUCT_RETURN; + void verify(); +}; + +// the ProtectionDomainCacheTable contains all protection domain oops. The system +// dictionary entries reference its entries instead of having references to oops +// directly. +// This is used to speed up system dictionary iteration: the oops in the +// protection domain are the only ones referring the Java heap. So when there is +// need to update these, instead of going over every entry of the system dictionary, +// we only need to iterate over this set. +// The amount of different protection domains used is typically magnitudes smaller +// than the number of system dictionary entries (loaded classes). +class ProtectionDomainCacheTable : public Hashtable { + friend class VMStructs; +private: + ProtectionDomainCacheEntry* bucket(int i) { + return (ProtectionDomainCacheEntry*) Hashtable::bucket(i); + } + + // The following method is not MT-safe and must be done under lock. + ProtectionDomainCacheEntry** bucket_addr(int i) { + return (ProtectionDomainCacheEntry**) Hashtable::bucket_addr(i); + } + + ProtectionDomainCacheEntry* new_entry(unsigned int hash, oop protection_domain) { + ProtectionDomainCacheEntry* entry = (ProtectionDomainCacheEntry*) Hashtable::new_entry(hash, protection_domain); + entry->init(); + return entry; + } + + static unsigned int compute_hash(oop protection_domain) { + return (unsigned int)(protection_domain->identity_hash()); + } + + int index_for(oop protection_domain) { + return hash_to_index(compute_hash(protection_domain)); + } + + ProtectionDomainCacheEntry* add_entry(int index, unsigned int hash, oop protection_domain); + ProtectionDomainCacheEntry* find_entry(int index, oop protection_domain); + +public: + + ProtectionDomainCacheTable(int table_size); + + ProtectionDomainCacheEntry* get(oop protection_domain); + void free(ProtectionDomainCacheEntry* entry); + + // GC support + void oops_do(OopClosure* f); + void always_strong_oops_do(OopClosure* f); + + static uint bucket_size(); + + void print() PRODUCT_RETURN; + void verify(); +}; + class ProtectionDomainEntry :public CHeapObj { friend class VMStructs; public: ProtectionDomainEntry* _next; - oop _protection_domain; + ProtectionDomainCacheEntry* _pd_cache; - ProtectionDomainEntry(oop protection_domain, ProtectionDomainEntry* next) { - _protection_domain = protection_domain; - _next = next; + ProtectionDomainEntry(ProtectionDomainCacheEntry* pd_cache, ProtectionDomainEntry* next) { + _pd_cache = pd_cache; + _next = next; + _pd_cache->increment_refcount(); } ProtectionDomainEntry* next() { return _next; } - oop protection_domain() { return _protection_domain; } + oop protection_domain() { return _pd_cache->protection_domain(); } + + ProtectionDomainCacheEntry * release_pd_cache() { + if (_pd_cache->decrement_refcount() == 0) { + return _pd_cache; + } else { + return NULL; + } + } }; // An entry in the system dictionary, this describes a class as @@ -158,7 +278,7 @@ // Tells whether a protection is in the approved set. bool contains_protection_domain(oop protection_domain) const; // Adds a protection domain to the approved set. - void add_protection_domain(oop protection_domain); + void add_protection_domain(Dictionary* dict, oop protection_domain); Klass* klass() const { return (Klass*)literal(); } Klass** klass_addr() { return (Klass**)literal_addr(); } @@ -189,12 +309,11 @@ : contains_protection_domain(protection_domain()); } - - void protection_domain_set_oops_do(OopClosure* f) { + void set_strongly_reachable() { for (ProtectionDomainEntry* current = _pd_set; current != NULL; current = current->_next) { - f->do_oop(&(current->_protection_domain)); + current->_pd_cache->set_strongly_reachable(); } } @@ -202,7 +321,7 @@ for (ProtectionDomainEntry* current = _pd_set; current != NULL; current = current->_next) { - current->_protection_domain->verify(); + current->_pd_cache->protection_domain()->verify(); } } --- old/src/share/vm/runtime/arguments.cpp 2013-09-25 14:45:11.984642558 +0200 +++ new/src/share/vm/runtime/arguments.cpp 2013-09-25 14:45:11.916642559 +0200 @@ -23,6 +23,7 @@ */ #include "precompiled.hpp" +#include "classfile/dictionary.hpp" #include "classfile/javaAssertions.hpp" #include "classfile/symbolTable.hpp" #include "compiler/compilerOracle.hpp" @@ -2038,6 +2039,9 @@ status = status && verify_interval(StringTableSize, minimumStringTableSize, (max_uintx / StringTable::bucket_size()), "StringTable size"); + status = status && verify_interval(ProtectionDomainCacheSize, minimumProtectionDomainCacheSize, + (max_uintx / ProtectionDomainCacheTable::bucket_size()), "ProtectionDomainCache size"); + if (MinHeapFreeRatio > MaxHeapFreeRatio) { jio_fprintf(defaultStream::error_stream(), "MinHeapFreeRatio (" UINTX_FORMAT ") must be less than or " --- old/src/share/vm/runtime/globals.hpp 2013-09-25 14:45:12.404642551 +0200 +++ new/src/share/vm/runtime/globals.hpp 2013-09-25 14:45:12.336642552 +0200 @@ -3685,6 +3685,9 @@ diagnostic(bool, PrintDTraceDOF, false, \ "Print the DTrace DOF passed to the system for JSDT probes") \ \ + product(uintx, ProtectionDomainCacheSize, defaultProtectionDomainCacheSize,\ + "Number of buckets in the protection domain cache table") \ + \ product(uintx, StringTableSize, defaultStringTableSize, \ "Number of buckets in the interned String table") \ \ --- old/src/share/vm/runtime/vmStructs.cpp 2013-09-25 14:45:12.844642544 +0200 +++ new/src/share/vm/runtime/vmStructs.cpp 2013-09-25 14:45:12.776642545 +0200 @@ -718,7 +718,13 @@ /**************************/ \ \ nonstatic_field(ProtectionDomainEntry, _next, ProtectionDomainEntry*) \ - nonstatic_field(ProtectionDomainEntry, _protection_domain, oop) \ + nonstatic_field(ProtectionDomainEntry, _pd_cache, ProtectionDomainCacheEntry*) \ + \ + /*******************************/ \ + /* ProctectionDomainCacheEntry */ \ + /*******************************/ \ + \ + nonstatic_field(ProtectionDomainCacheEntry, _literal, oop) \ \ /*************************/ \ /* LoaderConstraintEntry */ \ @@ -1560,6 +1566,7 @@ declare_toplevel_type(SystemDictionary) \ declare_toplevel_type(vmSymbols) \ declare_toplevel_type(ProtectionDomainEntry) \ + declare_toplevel_type(ProtectionDomainCacheEntry) \ \ declare_toplevel_type(GenericGrowableArray) \ declare_toplevel_type(GrowableArray) \ --- old/src/share/vm/utilities/globalDefinitions.hpp 2013-09-25 14:45:13.308642537 +0200 +++ new/src/share/vm/utilities/globalDefinitions.hpp 2013-09-25 14:45:13.240642538 +0200 @@ -326,12 +326,16 @@ const int max_method_code_size = 64*K - 1; // JVM spec, 2nd ed. section 4.8.1 (p.134) +// Default ProtectionDomainCacheSize values + +const int defaultProtectionDomainCacheSize = NOT_LP64(137) LP64_ONLY(2017); +const int minimumProtectionDomainCacheSize = 137; //---------------------------------------------------------------------------------------------------- // Default and minimum StringTableSize values const int defaultStringTableSize = NOT_LP64(1009) LP64_ONLY(60013); -const int minimumStringTableSize=1009; +const int minimumStringTableSize = 1009; //----------------------------------------------------------------------------------------------------