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