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 #ifndef SHARE_VM_SERVICES_CLASSLOADINGSERVICE_HPP
  26 #define SHARE_VM_SERVICES_CLASSLOADINGSERVICE_HPP
  27 
  28 #include "logging/log.hpp"
  29 #include "runtime/handles.hpp"
  30 #include "runtime/perfData.hpp"
  31 #include "utilities/growableArray.hpp"
  32 #include "utilities/macros.hpp"
  33 
  34 class InstanceKlass;
  35 
  36 // VM monitoring and management support for the Class Loading subsystem
  37 class ClassLoadingService : public AllStatic {
  38 private:
  39   // Counters for classes loaded from class files
  40   static PerfCounter*  _classes_loaded_count;
  41   static PerfCounter*  _classes_unloaded_count;
  42   static PerfCounter*  _classbytes_loaded;
  43   static PerfCounter*  _classbytes_unloaded;
  44 
  45   // Counters for classes loaded from shared archive
  46   static PerfCounter*  _shared_classes_loaded_count;
  47   static PerfCounter*  _shared_classes_unloaded_count;
  48   static PerfCounter*  _shared_classbytes_loaded;
  49   static PerfCounter*  _shared_classbytes_unloaded;
  50 
  51   static PerfVariable* _class_methods_size;
  52 
  53   static size_t compute_class_size(InstanceKlass* k);
  54 
  55 public:
  56   static void init();
  57 
  58   static bool get_verbose() { return log_is_enabled(Info, class, load); }
  59   static bool set_verbose(bool verbose);
  60   static void reset_trace_class_unloading() NOT_MANAGEMENT_RETURN;
  61 
  62   static jlong loaded_class_count() {
  63     return _classes_loaded_count->get_value() + _shared_classes_loaded_count->get_value();
  64   }
  65   static jlong unloaded_class_count() {
  66     return _classes_unloaded_count->get_value() + _shared_classes_unloaded_count->get_value();
  67   }
  68   static jlong loaded_class_bytes() {
  69     if (UsePerfData) {
  70       return _classbytes_loaded->get_value() + _shared_classbytes_loaded->get_value();
  71     } else {
  72       return -1;
  73     }
  74   }
  75   static jlong unloaded_class_bytes() {
  76     if (UsePerfData) {
  77       return _classbytes_unloaded->get_value() + _shared_classbytes_unloaded->get_value();
  78     } else {
  79       return -1;
  80     }
  81   }
  82 
  83   static jlong loaded_shared_class_count() {
  84     return _shared_classes_loaded_count->get_value();
  85   }
  86   static jlong unloaded_shared_class_count() {
  87     return _shared_classes_unloaded_count->get_value();
  88   }
  89   static jlong loaded_shared_class_bytes() {
  90     if (UsePerfData) {
  91       return _shared_classbytes_loaded->get_value();
  92     } else {
  93       return -1;
  94     }
  95   }
  96   static jlong unloaded_shared_class_bytes() {
  97     if (UsePerfData) {
  98       return _shared_classbytes_unloaded->get_value();
  99     } else {
 100       return -1;
 101     }
 102   }
 103   static jlong class_method_data_size() {
 104     return (UsePerfData ? _class_methods_size->get_value() : -1);
 105   }
 106 
 107   static void notify_class_loaded(InstanceKlass* k, bool shared_class)
 108       NOT_MANAGEMENT_RETURN;
 109   // All unloaded classes are non-shared
 110   static void notify_class_unloaded(InstanceKlass* k) NOT_MANAGEMENT_RETURN;
 111   static void add_class_method_size(int size) {
 112 #if INCLUDE_MANAGEMENT
 113     if (UsePerfData) {
 114       _class_methods_size->inc(size);
 115     }
 116 #endif // INCLUDE_MANAGEMENT
 117   }
 118 };
 119 
 120 // FIXME: make this piece of code to be shared by M&M and JVMTI
 121 class LoadedClassesEnumerator : public StackObj {
 122 private:
 123   static GrowableArray<KlassHandle>* _loaded_classes;
 124   // _current_thread is for creating a KlassHandle with a faster version constructor
 125   static Thread*                     _current_thread;
 126 
 127   GrowableArray<KlassHandle>* _klass_handle_array;
 128 
 129 public:
 130   LoadedClassesEnumerator(Thread* cur_thread);
 131 
 132   int num_loaded_classes()         { return _klass_handle_array->length(); }
 133   KlassHandle get_klass(int index) { return _klass_handle_array->at(index); }
 134 
 135   static void add_loaded_class(Klass* k) {
 136     // FIXME: For now - don't include array klasses
 137     // The spec is unclear at this point to count array klasses or not
 138     // and also indirect creation of array of super class and secondaries
 139     //
 140     // for (Klass* l = k; l != NULL; l = l->array_klass_or_null()) {
 141     //  KlassHandle h(_current_thread, l);
 142     //  _loaded_classes->append(h);
 143     // }
 144     KlassHandle h(_current_thread, k);
 145     _loaded_classes->append(h);
 146   }
 147 };
 148 
 149 #endif // SHARE_VM_SERVICES_CLASSLOADINGSERVICE_HPP