src/share/vm/services/classLoadingService.cpp

changeset 435
a61af66fc99e
child 1580
e018e6884bd8
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/vm/services/classLoadingService.cpp	Sat Dec 01 00:00:00 2007 +0000
     1.3 @@ -0,0 +1,212 @@
     1.4 +/*
     1.5 + * Copyright 2003-2005 Sun Microsystems, Inc.  All Rights Reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or
    1.24 + * have any questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +# include "incls/_precompiled.incl"
    1.29 +# include "incls/_classLoadingService.cpp.incl"
    1.30 +
    1.31 +#ifdef DTRACE_ENABLED
    1.32 +
    1.33 +// Only bother with this argument setup if dtrace is available
    1.34 +
    1.35 +HS_DTRACE_PROBE_DECL4(hotspot, class__loaded, char*, int, oop, bool);
    1.36 +HS_DTRACE_PROBE_DECL4(hotspot, class__unloaded, char*, int, oop, bool);
    1.37 +
    1.38 +#define DTRACE_CLASSLOAD_PROBE(type, clss, shared)  \
    1.39 +  {                                                 \
    1.40 +    char* data = NULL;                              \
    1.41 +    int len = 0;                                    \
    1.42 +    symbolOop name = (clss)->name();                \
    1.43 +    if (name != NULL) {                             \
    1.44 +      data = (char*)name->bytes();                  \
    1.45 +      len = name->utf8_length();                    \
    1.46 +    }                                               \
    1.47 +    HS_DTRACE_PROBE4(hotspot, class__##type,        \
    1.48 +      data, len, (clss)->class_loader(), (shared)); \
    1.49 +  }
    1.50 +
    1.51 +#else //  ndef DTRACE_ENABLED
    1.52 +
    1.53 +#define DTRACE_CLASSLOAD_PROBE(type, clss, shared)
    1.54 +
    1.55 +#endif
    1.56 +
    1.57 +// counters for classes loaded from class files
    1.58 +PerfCounter*    ClassLoadingService::_classes_loaded_count = NULL;
    1.59 +PerfCounter*    ClassLoadingService::_classes_unloaded_count = NULL;
    1.60 +PerfCounter*    ClassLoadingService::_classbytes_loaded = NULL;
    1.61 +PerfCounter*    ClassLoadingService::_classbytes_unloaded = NULL;
    1.62 +
    1.63 +// counters for classes loaded from shared archive
    1.64 +PerfCounter*    ClassLoadingService::_shared_classes_loaded_count = NULL;
    1.65 +PerfCounter*    ClassLoadingService::_shared_classes_unloaded_count = NULL;
    1.66 +PerfCounter*    ClassLoadingService::_shared_classbytes_loaded = NULL;
    1.67 +PerfCounter*    ClassLoadingService::_shared_classbytes_unloaded = NULL;
    1.68 +PerfVariable*   ClassLoadingService::_class_methods_size = NULL;
    1.69 +
    1.70 +void ClassLoadingService::init() {
    1.71 +  EXCEPTION_MARK;
    1.72 +
    1.73 +  // These counters are for java.lang.management API support.
    1.74 +  // They are created even if -XX:-UsePerfData is set and in
    1.75 +  // that case, they will be allocated on C heap.
    1.76 +  _classes_loaded_count =
    1.77 +                 PerfDataManager::create_counter(JAVA_CLS, "loadedClasses",
    1.78 +                                                 PerfData::U_Events, CHECK);
    1.79 +
    1.80 +  _classes_unloaded_count =
    1.81 +                 PerfDataManager::create_counter(JAVA_CLS, "unloadedClasses",
    1.82 +                                                 PerfData::U_Events, CHECK);
    1.83 +
    1.84 +  _shared_classes_loaded_count =
    1.85 +                 PerfDataManager::create_counter(JAVA_CLS, "sharedLoadedClasses",
    1.86 +                                                 PerfData::U_Events, CHECK);
    1.87 +
    1.88 +  _shared_classes_unloaded_count =
    1.89 +                 PerfDataManager::create_counter(JAVA_CLS, "sharedUnloadedClasses",
    1.90 +                                                 PerfData::U_Events, CHECK);
    1.91 +
    1.92 +  if (UsePerfData) {
    1.93 +    _classbytes_loaded =
    1.94 +                 PerfDataManager::create_counter(SUN_CLS, "loadedBytes",
    1.95 +                                                 PerfData::U_Bytes, CHECK);
    1.96 +
    1.97 +    _classbytes_unloaded =
    1.98 +                 PerfDataManager::create_counter(SUN_CLS, "unloadedBytes",
    1.99 +                                                 PerfData::U_Bytes, CHECK);
   1.100 +    _shared_classbytes_loaded =
   1.101 +                 PerfDataManager::create_counter(SUN_CLS, "sharedLoadedBytes",
   1.102 +                                                 PerfData::U_Bytes, CHECK);
   1.103 +
   1.104 +    _shared_classbytes_unloaded =
   1.105 +                 PerfDataManager::create_counter(SUN_CLS, "sharedUnloadedBytes",
   1.106 +                                                 PerfData::U_Bytes, CHECK);
   1.107 +    _class_methods_size =
   1.108 +                 PerfDataManager::create_variable(SUN_CLS, "methodBytes",
   1.109 +                                                  PerfData::U_Bytes, CHECK);
   1.110 +  }
   1.111 +}
   1.112 +
   1.113 +void ClassLoadingService::notify_class_unloaded(instanceKlass* k) {
   1.114 +  DTRACE_CLASSLOAD_PROBE(unloaded, k, false);
   1.115 +  // Classes that can be unloaded must be non-shared
   1.116 +  _classes_unloaded_count->inc();
   1.117 +
   1.118 +  if (UsePerfData) {
   1.119 +    // add the class size
   1.120 +    size_t size = compute_class_size(k);
   1.121 +    _classbytes_unloaded->inc(size);
   1.122 +
   1.123 +    // Compute method size & subtract from running total.
   1.124 +    // We are called during phase 1 of mark sweep, so it's
   1.125 +    // still ok to iterate through methodOops here.
   1.126 +    objArrayOop methods = k->methods();
   1.127 +    for (int i = 0; i < methods->length(); i++) {
   1.128 +      _class_methods_size->inc(-methods->obj_at(i)->size());
   1.129 +    }
   1.130 +  }
   1.131 +
   1.132 +  if (TraceClassUnloading) {
   1.133 +    ResourceMark rm;
   1.134 +    tty->print_cr("[Unloading class %s]", k->external_name());
   1.135 +  }
   1.136 +}
   1.137 +
   1.138 +void ClassLoadingService::notify_class_loaded(instanceKlass* k, bool shared_class) {
   1.139 +  DTRACE_CLASSLOAD_PROBE(loaded, k, shared_class);
   1.140 +  PerfCounter* classes_counter = (shared_class ? _shared_classes_loaded_count
   1.141 +                                               : _classes_loaded_count);
   1.142 +  // increment the count
   1.143 +  classes_counter->inc();
   1.144 +
   1.145 +  if (UsePerfData) {
   1.146 +    PerfCounter* classbytes_counter = (shared_class ? _shared_classbytes_loaded
   1.147 +                                                    : _classbytes_loaded);
   1.148 +    // add the class size
   1.149 +    size_t size = compute_class_size(k);
   1.150 +    classbytes_counter->inc(size);
   1.151 +  }
   1.152 +}
   1.153 +
   1.154 +size_t ClassLoadingService::compute_class_size(instanceKlass* k) {
   1.155 +  // lifted from ClassStatistics.do_class(klassOop k)
   1.156 +
   1.157 +  size_t class_size = 0;
   1.158 +
   1.159 +  class_size += k->as_klassOop()->size();
   1.160 +
   1.161 +  if (k->oop_is_instance()) {
   1.162 +    class_size += k->methods()->size();
   1.163 +    class_size += k->constants()->size();
   1.164 +    class_size += k->local_interfaces()->size();
   1.165 +    class_size += k->transitive_interfaces()->size();
   1.166 +    // We do not have to count implementors, since we only store one!
   1.167 +    class_size += k->fields()->size();
   1.168 +  }
   1.169 +  return class_size * oopSize;
   1.170 +}
   1.171 +
   1.172 +
   1.173 +bool ClassLoadingService::set_verbose(bool verbose) {
   1.174 +  MutexLocker m(Management_lock);
   1.175 +
   1.176 +  // verbose will be set to the previous value
   1.177 +  bool succeed = CommandLineFlags::boolAtPut((char*)"TraceClassLoading", &verbose, MANAGEMENT);
   1.178 +  assert(succeed, "Setting TraceClassLoading flag fails");
   1.179 +  reset_trace_class_unloading();
   1.180 +
   1.181 +  return verbose;
   1.182 +}
   1.183 +
   1.184 +// Caller to this function must own Management_lock
   1.185 +void ClassLoadingService::reset_trace_class_unloading() {
   1.186 +  assert(Management_lock->owned_by_self(), "Must own the Management_lock");
   1.187 +  bool value = MemoryService::get_verbose() || ClassLoadingService::get_verbose();
   1.188 +  bool succeed = CommandLineFlags::boolAtPut((char*)"TraceClassUnloading", &value, MANAGEMENT);
   1.189 +  assert(succeed, "Setting TraceClassUnLoading flag fails");
   1.190 +}
   1.191 +
   1.192 +GrowableArray<KlassHandle>* LoadedClassesEnumerator::_loaded_classes = NULL;
   1.193 +Thread* LoadedClassesEnumerator::_current_thread = NULL;
   1.194 +
   1.195 +LoadedClassesEnumerator::LoadedClassesEnumerator(Thread* cur_thread) {
   1.196 +  assert(cur_thread == Thread::current(), "Check current thread");
   1.197 +
   1.198 +  int init_size = ClassLoadingService::loaded_class_count();
   1.199 +  _klass_handle_array = new GrowableArray<KlassHandle>(init_size);
   1.200 +
   1.201 +  // For consistency of the loaded classes, grab the SystemDictionary lock
   1.202 +  MutexLocker sd_mutex(SystemDictionary_lock);
   1.203 +
   1.204 +  // Set _loaded_classes and _current_thread and begin enumerating all classes.
   1.205 +  // Only one thread will do the enumeration at a time.
   1.206 +  // These static variables are needed and they are used by the static method
   1.207 +  // add_loaded_class called from classes_do().
   1.208 +  _loaded_classes = _klass_handle_array;
   1.209 +  _current_thread = cur_thread;
   1.210 +
   1.211 +  SystemDictionary::classes_do(&add_loaded_class);
   1.212 +
   1.213 +  // FIXME: Exclude array klasses for now
   1.214 +  // Universe::basic_type_classes_do(&add_loaded_class);
   1.215 +}

mercurial