src/share/vm/classfile/placeholders.cpp

Sat, 01 Sep 2012 13:25:18 -0400

author
coleenp
date
Sat, 01 Sep 2012 13:25:18 -0400
changeset 4037
da91efe96a93
parent 3900
d2a62e0f25eb
child 4278
070d523b96a7
permissions
-rw-r--r--

6964458: Reimplement class meta-data storage to use native memory
Summary: Remove PermGen, allocate meta-data in metaspace linked to class loaders, rewrite GC walking, rewrite and rename metadata to be C++ classes
Reviewed-by: jmasa, stefank, never, coleenp, kvn, brutisso, mgerdin, dholmes, jrose, twisti, roland
Contributed-by: jmasa <jon.masamitsu@oracle.com>, stefank <stefan.karlsson@oracle.com>, mgerdin <mikael.gerdin@oracle.com>, never <tom.rodriguez@oracle.com>

     1 /*
     2  * Copyright (c) 2003, 2012, 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  */
    25 #include "precompiled.hpp"
    26 #include "classfile/placeholders.hpp"
    27 #include "classfile/systemDictionary.hpp"
    28 #include "oops/oop.inline.hpp"
    29 #include "runtime/fieldType.hpp"
    30 #include "utilities/hashtable.inline.hpp"
    32 // Placeholder methods
    34 PlaceholderEntry* PlaceholderTable::new_entry(int hash, Symbol* name,
    35                                               ClassLoaderData* loader_data,
    36                                               bool havesupername,
    37                                               Symbol* supername) {
    38   PlaceholderEntry* entry = (PlaceholderEntry*)Hashtable<Symbol*, mtClass>::new_entry(hash, name);
    39   // Hashtable with Symbol* literal must increment and decrement refcount.
    40   name->increment_refcount();
    41   entry->set_loader_data(loader_data);
    42   entry->set_havesupername(havesupername);
    43   entry->set_supername(supername);
    44   entry->set_superThreadQ(NULL);
    45   entry->set_loadInstanceThreadQ(NULL);
    46   entry->set_defineThreadQ(NULL);
    47   entry->set_definer(NULL);
    48   entry->set_instanceKlass(NULL);
    49   return entry;
    50 }
    52 void PlaceholderTable::free_entry(PlaceholderEntry* entry) {
    53   // decrement Symbol refcount here because Hashtable doesn't.
    54   entry->literal()->decrement_refcount();
    55   if (entry->supername() != NULL) entry->supername()->decrement_refcount();
    56   Hashtable<Symbol*, mtClass>::free_entry(entry);
    57 }
    60 // Placeholder objects represent classes currently being loaded.
    61 // All threads examining the placeholder table must hold the
    62 // SystemDictionary_lock, so we don't need special precautions
    63 // on store ordering here.
    64 void PlaceholderTable::add_entry(int index, unsigned int hash,
    65                                  Symbol* class_name, ClassLoaderData* loader_data,
    66                                  bool havesupername, Symbol* supername){
    67   assert_locked_or_safepoint(SystemDictionary_lock);
    68   assert(class_name != NULL, "adding NULL obj");
    70   // Both readers and writers are locked so it's safe to just
    71   // create the placeholder and insert it in the list without a membar.
    72   PlaceholderEntry* entry = new_entry(hash, class_name, loader_data, havesupername, supername);
    73   add_entry(index, entry);
    74 }
    77 // Remove a placeholder object.
    78 void PlaceholderTable::remove_entry(int index, unsigned int hash,
    79                                     Symbol* class_name,
    80                                     ClassLoaderData* loader_data) {
    81   assert_locked_or_safepoint(SystemDictionary_lock);
    82   PlaceholderEntry** p = bucket_addr(index);
    83   while (*p) {
    84     PlaceholderEntry *probe = *p;
    85     if (probe->hash() == hash && probe->equals(class_name, loader_data)) {
    86       // Delete entry
    87       *p = probe->next();
    88       free_entry(probe);
    89       return;
    90     }
    91     p = probe->next_addr();
    92   }
    93 }
    95 PlaceholderEntry* PlaceholderTable::get_entry(int index, unsigned int hash,
    96                                        Symbol* class_name,
    97                                        ClassLoaderData* loader_data) {
    98   assert_locked_or_safepoint(SystemDictionary_lock);
   100   for (PlaceholderEntry *place_probe = bucket(index);
   101                          place_probe != NULL;
   102                          place_probe = place_probe->next()) {
   103     if (place_probe->hash() == hash &&
   104         place_probe->equals(class_name, loader_data)) {
   105       return place_probe;
   106     }
   107   }
   108   return NULL;
   109 }
   111 Symbol* PlaceholderTable::find_entry(int index, unsigned int hash,
   112                                        Symbol* class_name,
   113                                        ClassLoaderData* loader_data) {
   114   PlaceholderEntry* probe = get_entry(index, hash, class_name, loader_data);
   115   return (probe? probe->klassname(): (Symbol*)NULL);
   116 }
   118   // find_and_add returns probe pointer - old or new
   119   // If no entry exists, add a placeholder entry
   120   // If entry exists, reuse entry
   121   // For both, push SeenThread for classloadAction
   122   // if havesupername: this is used for circularity for instanceklass loading
   123 PlaceholderEntry* PlaceholderTable::find_and_add(int index, unsigned int hash,
   124                                                  Symbol* name,
   125                                                  ClassLoaderData* loader_data,
   126                                                  classloadAction action,
   127                                                  Symbol* supername,
   128                                                  Thread* thread) {
   129   PlaceholderEntry* probe = get_entry(index, hash, name, loader_data);
   130   if (probe == NULL) {
   131     // Nothing found, add place holder
   132     add_entry(index, hash, name, loader_data, (action == LOAD_SUPER), supername);
   133     probe = get_entry(index, hash, name, loader_data);
   134   } else {
   135     if (action == LOAD_SUPER) {
   136       probe->set_havesupername(true);
   137       probe->set_supername(supername);
   138     }
   139   }
   140   if (probe) probe->add_seen_thread(thread, action);
   141   return probe;
   142 }
   145 // placeholder used to track class loading internal states
   146 // placeholder existence now for loading superclass/superinterface
   147 // superthreadQ tracks class circularity, while loading superclass/superinterface
   148 // loadInstanceThreadQ tracks load_instance_class calls
   149 // definer() tracks the single thread that owns define token
   150 // defineThreadQ tracks waiters on defining thread's results
   151 // 1st claimant creates placeholder
   152 // find_and_add adds SeenThread entry for appropriate queue
   153 // All claimants remove SeenThread after completing action
   154 // On removal: if definer and all queues empty, remove entry
   155 // Note: you can be in both placeholders and systemDictionary
   156 // see parse_stream for redefine classes
   157 // Therefore - must always check SD first
   158 // Ignores the case where entry is not found
   159 void PlaceholderTable::find_and_remove(int index, unsigned int hash,
   160                        Symbol* name, ClassLoaderData* loader_data, Thread* thread) {
   161     assert_locked_or_safepoint(SystemDictionary_lock);
   162     PlaceholderEntry *probe = get_entry(index, hash, name, loader_data);
   163     if (probe != NULL) {
   164        // No other threads using this entry
   165        if ((probe->superThreadQ() == NULL) && (probe->loadInstanceThreadQ() == NULL)
   166           && (probe->defineThreadQ() == NULL) && (probe->definer() == NULL)) {
   167          remove_entry(index, hash, name, loader_data);
   168        }
   169     }
   170   }
   172 PlaceholderTable::PlaceholderTable(int table_size)
   173     : TwoOopHashtable<Symbol*, mtClass>(table_size, sizeof(PlaceholderEntry)) {
   174 }
   177 void PlaceholderTable::classes_do(KlassClosure* f) {
   178   for (int index = 0; index < table_size(); index++) {
   179     for (PlaceholderEntry* probe = bucket(index);
   180                            probe != NULL;
   181                            probe = probe->next()) {
   182       probe->classes_do(f);
   183     }
   184   }
   185 }
   188 void PlaceholderEntry::classes_do(KlassClosure* closure) {
   189   assert(klassname() != NULL, "should have a non-null klass");
   190   if (_instanceKlass != NULL) {
   191     closure->do_klass(InstanceKlass());
   192   }
   193 }
   195 // do all entries in the placeholder table
   196 void PlaceholderTable::entries_do(void f(Symbol*)) {
   197   for (int index = 0; index < table_size(); index++) {
   198     for (PlaceholderEntry* probe = bucket(index);
   199                            probe != NULL;
   200                            probe = probe->next()) {
   201       f(probe->klassname());
   202     }
   203   }
   204 }
   207 #ifndef PRODUCT
   208 // Note, doesn't append a cr
   209 void PlaceholderEntry::print() const {
   210   klassname()->print_value();
   211   if (loader_data() != NULL) {
   212     tty->print(", loader ");
   213     loader_data()->print_value();
   214   }
   215   if (supername() != NULL) {
   216     tty->print(", supername ");
   217     supername()->print_value();
   218   }
   219   if (definer() != NULL) {
   220     tty->print(", definer ");
   221     definer()->print_value();
   222   }
   223   if (InstanceKlass() != NULL) {
   224     tty->print(", InstanceKlass ");
   225     InstanceKlass()->print_value();
   226   }
   227   tty->print("\n");
   228   tty->print("loadInstanceThreadQ threads:");
   229   loadInstanceThreadQ()->printActionQ();
   230   tty->print("\n");
   231   tty->print("superThreadQ threads:");
   232   superThreadQ()->printActionQ();
   233   tty->print("\n");
   234   tty->print("defineThreadQ threads:");
   235   defineThreadQ()->printActionQ();
   236   tty->print("\n");
   237 }
   238 #endif
   240 void PlaceholderEntry::verify() const {
   241   guarantee(loader_data() != NULL, "Must have been setup.");
   242   guarantee(loader_data()->class_loader() == NULL || loader_data()->class_loader()->is_instance(),
   243             "checking type of _loader");
   244   guarantee(InstanceKlass() == NULL
   245             || Klass::cast(InstanceKlass())->oop_is_instance(),
   246             "checking type of InstanceKlass result");
   247 }
   249 void PlaceholderTable::verify() {
   250   int element_count = 0;
   251   for (int pindex = 0; pindex < table_size(); pindex++) {
   252     for (PlaceholderEntry* probe = bucket(pindex);
   253                            probe != NULL;
   254                            probe = probe->next()) {
   255       probe->verify();
   256       element_count++;  // both klasses and place holders count
   257     }
   258   }
   259   guarantee(number_of_entries() == element_count,
   260             "Verify of system dictionary failed");
   261 }
   264 #ifndef PRODUCT
   265 void PlaceholderTable::print() {
   266   for (int pindex = 0; pindex < table_size(); pindex++) {
   267     for (PlaceholderEntry* probe = bucket(pindex);
   268                            probe != NULL;
   269                            probe = probe->next()) {
   270       if (Verbose) tty->print("%4d: ", pindex);
   271       tty->print(" place holder ");
   273       probe->print();
   274       tty->cr();
   275     }
   276   }
   277 }
   278 #endif

mercurial