src/share/vm/services/classLoadingService.cpp
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File classload.08 Sdiff src/share/vm/services

src/share/vm/services/classLoadingService.cpp

Print this page


   1 /*
   2  * Copyright (c) 2003, 2015, 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 #include "precompiled.hpp"
  26 #include "classfile/systemDictionary.hpp"
  27 #include "memory/allocation.hpp"
  28 #include "memory/universe.hpp"
  29 #include "oops/oop.inline.hpp"
  30 #include "runtime/mutexLocker.hpp"
  31 #include "services/classLoadingService.hpp"
  32 #include "services/memoryService.hpp"
  33 #include "utilities/dtrace.hpp"
  34 #include "utilities/macros.hpp"
  35 #include "utilities/defaultStream.hpp"


  36 
  37 #ifdef DTRACE_ENABLED
  38 
  39 // Only bother with this argument setup if dtrace is available
  40 
  41 #define HOTSPOT_CLASS_unloaded HOTSPOT_CLASS_UNLOADED
  42 #define HOTSPOT_CLASS_loaded HOTSPOT_CLASS_LOADED
  43 #define DTRACE_CLASSLOAD_PROBE(type, clss, shared)  \
  44   {                                                 \
  45     char* data = NULL;                              \
  46     int len = 0;                                    \
  47     Symbol* name = (clss)->name();                  \
  48     if (name != NULL) {                             \
  49       data = (char*)name->bytes();                  \
  50       len = name->utf8_length();                    \
  51     }                                               \
  52     HOTSPOT_CLASS_##type( /* type = unloaded, loaded */ \
  53       data, len, (clss)->class_loader(), (shared)); \
  54   }
  55 


 118 
 119 void ClassLoadingService::notify_class_unloaded(InstanceKlass* k) {
 120   DTRACE_CLASSLOAD_PROBE(unloaded, k, false);
 121   // Classes that can be unloaded must be non-shared
 122   _classes_unloaded_count->inc();
 123 
 124   if (UsePerfData) {
 125     // add the class size
 126     size_t size = compute_class_size(k);
 127     _classbytes_unloaded->inc(size);
 128 
 129     // Compute method size & subtract from running total.
 130     // We are called during phase 1 of mark sweep, so it's
 131     // still ok to iterate through Method*s here.
 132     Array<Method*>* methods = k->methods();
 133     for (int i = 0; i < methods->length(); i++) {
 134       _class_methods_size->inc(-methods->at(i)->size());
 135     }
 136   }
 137 
 138   if (TraceClassUnloading) {
 139     ResourceMark rm;
 140     tty->print_cr("[Unloading class %s " INTPTR_FORMAT "]", k->external_name(), p2i(k));
 141   }
 142 }
 143 
 144 void ClassLoadingService::notify_class_loaded(InstanceKlass* k, bool shared_class) {
 145   DTRACE_CLASSLOAD_PROBE(loaded, k, shared_class);
 146   PerfCounter* classes_counter = (shared_class ? _shared_classes_loaded_count
 147                                                : _classes_loaded_count);
 148   // increment the count
 149   classes_counter->inc();
 150 
 151   if (UsePerfData) {
 152     PerfCounter* classbytes_counter = (shared_class ? _shared_classbytes_loaded
 153                                                     : _classbytes_loaded);
 154     // add the class size
 155     size_t size = compute_class_size(k);
 156     classbytes_counter->inc(size);
 157   }
 158 }
 159 
 160 size_t ClassLoadingService::compute_class_size(InstanceKlass* k) {


 162 
 163   size_t class_size = 0;
 164 
 165   class_size += k->size();
 166 
 167   if (k->is_instance_klass()) {
 168     class_size += k->methods()->size();
 169     // FIXME: Need to count the contents of methods
 170     class_size += k->constants()->size();
 171     class_size += k->local_interfaces()->size();
 172     class_size += k->transitive_interfaces()->size();
 173     // We do not have to count implementors, since we only store one!
 174     // FIXME: How should these be accounted for, now when they have moved.
 175     //class_size += k->fields()->size();
 176   }
 177   return class_size * oopSize;
 178 }
 179 
 180 bool ClassLoadingService::set_verbose(bool verbose) {
 181   MutexLocker m(Management_lock);
 182 
 183   // verbose will be set to the previous value
 184   Flag::Error error = CommandLineFlags::boolAtPut("TraceClassLoading", &verbose, Flag::MANAGEMENT);
 185   assert(error==Flag::SUCCESS, "Setting TraceClassLoading flag failed with error %s", Flag::flag_error_str(error));



 186   reset_trace_class_unloading();
 187 
 188   return verbose;
 189 }
 190 
 191 // Caller to this function must own Management_lock
 192 void ClassLoadingService::reset_trace_class_unloading() {
 193   assert(Management_lock->owned_by_self(), "Must own the Management_lock");
 194   bool value = MemoryService::get_verbose() || ClassLoadingService::get_verbose();
 195   Flag::Error error = CommandLineFlags::boolAtPut("TraceClassUnloading", &value, Flag::MANAGEMENT);
 196   assert(error==Flag::SUCCESS, "Setting TraceClassUnLoading flag failed with error %s", Flag::flag_error_str(error));



 197 }
 198 
 199 GrowableArray<KlassHandle>* LoadedClassesEnumerator::_loaded_classes = NULL;
 200 Thread* LoadedClassesEnumerator::_current_thread = NULL;
 201 
 202 LoadedClassesEnumerator::LoadedClassesEnumerator(Thread* cur_thread) {
 203   assert(cur_thread == Thread::current(), "Check current thread");
 204 
 205   int init_size = ClassLoadingService::loaded_class_count();
 206   _klass_handle_array = new GrowableArray<KlassHandle>(init_size);
 207 
 208   // For consistency of the loaded classes, grab the SystemDictionary lock
 209   MutexLocker sd_mutex(SystemDictionary_lock);
 210 
 211   // Set _loaded_classes and _current_thread and begin enumerating all classes.
 212   // Only one thread will do the enumeration at a time.
 213   // These static variables are needed and they are used by the static method
 214   // add_loaded_class called from classes_do().
 215   _loaded_classes = _klass_handle_array;
 216   _current_thread = cur_thread;
   1 /*
   2  * Copyright (c) 2003, 2016, 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 #include "precompiled.hpp"
  26 #include "classfile/systemDictionary.hpp"
  27 #include "memory/allocation.hpp"
  28 #include "memory/universe.hpp"
  29 #include "oops/oop.inline.hpp"
  30 #include "runtime/mutexLocker.hpp"
  31 #include "services/classLoadingService.hpp"
  32 #include "services/memoryService.hpp"
  33 #include "utilities/dtrace.hpp"
  34 #include "utilities/macros.hpp"
  35 #include "utilities/defaultStream.hpp"
  36 #include "logging/log.hpp"
  37 #include "logging/logConfiguration.hpp"
  38 
  39 #ifdef DTRACE_ENABLED
  40 
  41 // Only bother with this argument setup if dtrace is available
  42 
  43 #define HOTSPOT_CLASS_unloaded HOTSPOT_CLASS_UNLOADED
  44 #define HOTSPOT_CLASS_loaded HOTSPOT_CLASS_LOADED
  45 #define DTRACE_CLASSLOAD_PROBE(type, clss, shared)  \
  46   {                                                 \
  47     char* data = NULL;                              \
  48     int len = 0;                                    \
  49     Symbol* name = (clss)->name();                  \
  50     if (name != NULL) {                             \
  51       data = (char*)name->bytes();                  \
  52       len = name->utf8_length();                    \
  53     }                                               \
  54     HOTSPOT_CLASS_##type( /* type = unloaded, loaded */ \
  55       data, len, (clss)->class_loader(), (shared)); \
  56   }
  57 


 120 
 121 void ClassLoadingService::notify_class_unloaded(InstanceKlass* k) {
 122   DTRACE_CLASSLOAD_PROBE(unloaded, k, false);
 123   // Classes that can be unloaded must be non-shared
 124   _classes_unloaded_count->inc();
 125 
 126   if (UsePerfData) {
 127     // add the class size
 128     size_t size = compute_class_size(k);
 129     _classbytes_unloaded->inc(size);
 130 
 131     // Compute method size & subtract from running total.
 132     // We are called during phase 1 of mark sweep, so it's
 133     // still ok to iterate through Method*s here.
 134     Array<Method*>* methods = k->methods();
 135     for (int i = 0; i < methods->length(); i++) {
 136       _class_methods_size->inc(-methods->at(i)->size());
 137     }
 138   }
 139 
 140   if (log_is_enabled(Info, classunload)) {
 141     ResourceMark rm;
 142     log_info(classunload)("unloading class %s " INTPTR_FORMAT , k->external_name(), p2i(k));
 143   }
 144 }
 145 
 146 void ClassLoadingService::notify_class_loaded(InstanceKlass* k, bool shared_class) {
 147   DTRACE_CLASSLOAD_PROBE(loaded, k, shared_class);
 148   PerfCounter* classes_counter = (shared_class ? _shared_classes_loaded_count
 149                                                : _classes_loaded_count);
 150   // increment the count
 151   classes_counter->inc();
 152 
 153   if (UsePerfData) {
 154     PerfCounter* classbytes_counter = (shared_class ? _shared_classbytes_loaded
 155                                                     : _classbytes_loaded);
 156     // add the class size
 157     size_t size = compute_class_size(k);
 158     classbytes_counter->inc(size);
 159   }
 160 }
 161 
 162 size_t ClassLoadingService::compute_class_size(InstanceKlass* k) {


 164 
 165   size_t class_size = 0;
 166 
 167   class_size += k->size();
 168 
 169   if (k->is_instance_klass()) {
 170     class_size += k->methods()->size();
 171     // FIXME: Need to count the contents of methods
 172     class_size += k->constants()->size();
 173     class_size += k->local_interfaces()->size();
 174     class_size += k->transitive_interfaces()->size();
 175     // We do not have to count implementors, since we only store one!
 176     // FIXME: How should these be accounted for, now when they have moved.
 177     //class_size += k->fields()->size();
 178   }
 179   return class_size * oopSize;
 180 }
 181 
 182 bool ClassLoadingService::set_verbose(bool verbose) {
 183   MutexLocker m(Management_lock);

 184   // verbose will be set to the previous value
 185   if (verbose) {
 186     LogConfiguration::parse_log_arguments("stdout", "classload=info", NULL, NULL, NULL);
 187   } else {
 188     LogConfiguration::parse_log_arguments("stdout", "classload=off", NULL, NULL, NULL);
 189   }
 190   reset_trace_class_unloading();

 191   return verbose;
 192 }
 193 
 194 // Caller to this function must own Management_lock
 195 void ClassLoadingService::reset_trace_class_unloading() {
 196   assert(Management_lock->owned_by_self(), "Must own the Management_lock");
 197   bool value = MemoryService::get_verbose() || ClassLoadingService::get_verbose();
 198   if (value) {
 199     LogConfiguration::parse_log_arguments("stdout", "classunload=info", NULL, NULL, NULL);
 200   } else {
 201     LogConfiguration::parse_log_arguments("stdout", "classunload=off", NULL, NULL, NULL);
 202   }
 203 }
 204 
 205 GrowableArray<KlassHandle>* LoadedClassesEnumerator::_loaded_classes = NULL;
 206 Thread* LoadedClassesEnumerator::_current_thread = NULL;
 207 
 208 LoadedClassesEnumerator::LoadedClassesEnumerator(Thread* cur_thread) {
 209   assert(cur_thread == Thread::current(), "Check current thread");
 210 
 211   int init_size = ClassLoadingService::loaded_class_count();
 212   _klass_handle_array = new GrowableArray<KlassHandle>(init_size);
 213 
 214   // For consistency of the loaded classes, grab the SystemDictionary lock
 215   MutexLocker sd_mutex(SystemDictionary_lock);
 216 
 217   // Set _loaded_classes and _current_thread and begin enumerating all classes.
 218   // Only one thread will do the enumeration at a time.
 219   // These static variables are needed and they are used by the static method
 220   // add_loaded_class called from classes_do().
 221   _loaded_classes = _klass_handle_array;
 222   _current_thread = cur_thread;
src/share/vm/services/classLoadingService.cpp
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File