classLoader.hpp revision 2062:3582bf76420e
1/*
2 * Copyright (c) 1997, 2010, 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_CLASSFILE_CLASSLOADER_HPP
26#define SHARE_VM_CLASSFILE_CLASSLOADER_HPP
27
28#include "classfile/classFileParser.hpp"
29#include "runtime/perfData.hpp"
30
31// The VM class loader.
32#include <sys/stat.h>
33
34
35// Meta-index (optional, to be able to skip opening boot classpath jar files)
36class MetaIndex: public CHeapObj {
37 private:
38  char** _meta_package_names;
39  int    _num_meta_package_names;
40 public:
41  MetaIndex(char** meta_package_names, int num_meta_package_names);
42  ~MetaIndex();
43  bool may_contain(const char* class_name);
44};
45
46
47// Class path entry (directory or zip file)
48
49class ClassPathEntry: public CHeapObj {
50 private:
51  ClassPathEntry* _next;
52 public:
53  // Next entry in class path
54  ClassPathEntry* next()              { return _next; }
55  void set_next(ClassPathEntry* next) {
56    // may have unlocked readers, so write atomically.
57    OrderAccess::release_store_ptr(&_next, next);
58  }
59  virtual bool is_jar_file() = 0;
60  virtual const char* name() = 0;
61  virtual bool is_lazy();
62  // Constructor
63  ClassPathEntry();
64  // Attempt to locate file_name through this class path entry.
65  // Returns a class file parsing stream if successfull.
66  virtual ClassFileStream* open_stream(const char* name) = 0;
67  // Debugging
68  NOT_PRODUCT(virtual void compile_the_world(Handle loader, TRAPS) = 0;)
69  NOT_PRODUCT(virtual bool is_rt_jar() = 0;)
70};
71
72
73class ClassPathDirEntry: public ClassPathEntry {
74 private:
75  char* _dir;           // Name of directory
76 public:
77  bool is_jar_file()  { return false;  }
78  const char* name()  { return _dir; }
79  ClassPathDirEntry(char* dir);
80  ClassFileStream* open_stream(const char* name);
81  // Debugging
82  NOT_PRODUCT(void compile_the_world(Handle loader, TRAPS);)
83  NOT_PRODUCT(bool is_rt_jar();)
84};
85
86
87// Type definitions for zip file and zip file entry
88typedef void* jzfile;
89typedef struct {
90  char *name;                   /* entry name */
91  jlong time;                   /* modification time */
92  jlong size;                   /* size of uncompressed data */
93  jlong csize;                  /* size of compressed data (zero if uncompressed) */
94  jint crc;                     /* crc of uncompressed data */
95  char *comment;                /* optional zip file comment */
96  jbyte *extra;                 /* optional extra data */
97  jlong pos;                    /* position of LOC header (if negative) or data */
98} jzentry;
99
100
101class ClassPathZipEntry: public ClassPathEntry {
102 private:
103  jzfile* _zip;        // The zip archive
104  char*   _zip_name;   // Name of zip archive
105 public:
106  bool is_jar_file()  { return true;  }
107  const char* name()  { return _zip_name; }
108  ClassPathZipEntry(jzfile* zip, const char* zip_name);
109  ~ClassPathZipEntry();
110  ClassFileStream* open_stream(const char* name);
111  void contents_do(void f(const char* name, void* context), void* context);
112  // Debugging
113  NOT_PRODUCT(void compile_the_world(Handle loader, TRAPS);)
114  NOT_PRODUCT(void compile_the_world12(Handle loader, TRAPS);) // JDK 1.2 version
115  NOT_PRODUCT(void compile_the_world13(Handle loader, TRAPS);) // JDK 1.3 version
116  NOT_PRODUCT(bool is_rt_jar();)
117  NOT_PRODUCT(bool is_rt_jar12();)
118  NOT_PRODUCT(bool is_rt_jar13();)
119};
120
121
122// For lazier loading of boot class path entries
123class LazyClassPathEntry: public ClassPathEntry {
124 private:
125  char* _path; // dir or file
126  struct stat _st;
127  MetaIndex* _meta_index;
128  volatile ClassPathEntry* _resolved_entry;
129  ClassPathEntry* resolve_entry();
130 public:
131  bool is_jar_file();
132  const char* name()  { return _path; }
133  LazyClassPathEntry(char* path, struct stat st);
134  ClassFileStream* open_stream(const char* name);
135  void set_meta_index(MetaIndex* meta_index) { _meta_index = meta_index; }
136  virtual bool is_lazy();
137  // Debugging
138  NOT_PRODUCT(void compile_the_world(Handle loader, TRAPS);)
139  NOT_PRODUCT(bool is_rt_jar();)
140};
141
142class PackageHashtable;
143class PackageInfo;
144class HashtableBucket;
145
146class ClassLoader: AllStatic {
147 public:
148  enum SomeConstants {
149    package_hash_table_size = 31  // Number of buckets
150  };
151 private:
152  friend class LazyClassPathEntry;
153
154  // Performance counters
155  static PerfCounter* _perf_accumulated_time;
156  static PerfCounter* _perf_classes_inited;
157  static PerfCounter* _perf_class_init_time;
158  static PerfCounter* _perf_class_init_selftime;
159  static PerfCounter* _perf_classes_verified;
160  static PerfCounter* _perf_class_verify_time;
161  static PerfCounter* _perf_class_verify_selftime;
162  static PerfCounter* _perf_classes_linked;
163  static PerfCounter* _perf_class_link_time;
164  static PerfCounter* _perf_class_link_selftime;
165  static PerfCounter* _perf_class_parse_time;
166  static PerfCounter* _perf_class_parse_selftime;
167  static PerfCounter* _perf_sys_class_lookup_time;
168  static PerfCounter* _perf_shared_classload_time;
169  static PerfCounter* _perf_sys_classload_time;
170  static PerfCounter* _perf_app_classload_time;
171  static PerfCounter* _perf_app_classload_selftime;
172  static PerfCounter* _perf_app_classload_count;
173  static PerfCounter* _perf_define_appclasses;
174  static PerfCounter* _perf_define_appclass_time;
175  static PerfCounter* _perf_define_appclass_selftime;
176  static PerfCounter* _perf_app_classfile_bytes_read;
177  static PerfCounter* _perf_sys_classfile_bytes_read;
178
179  static PerfCounter* _sync_systemLoaderLockContentionRate;
180  static PerfCounter* _sync_nonSystemLoaderLockContentionRate;
181  static PerfCounter* _sync_JVMFindLoadedClassLockFreeCounter;
182  static PerfCounter* _sync_JVMDefineClassLockFreeCounter;
183  static PerfCounter* _sync_JNIDefineClassLockFreeCounter;
184
185  static PerfCounter* _unsafe_defineClassCallCounter;
186  static PerfCounter* _isUnsyncloadClass;
187  static PerfCounter* _load_instance_class_failCounter;
188
189  // First entry in linked list of ClassPathEntry instances
190  static ClassPathEntry* _first_entry;
191  // Last entry in linked list of ClassPathEntry instances
192  static ClassPathEntry* _last_entry;
193  // Hash table used to keep track of loaded packages
194  static PackageHashtable* _package_hash_table;
195  static const char* _shared_archive;
196
197  // Hash function
198  static unsigned int hash(const char *s, int n);
199  // Returns the package file name corresponding to the specified package
200  // or class name, or null if not found.
201  static PackageInfo* lookup_package(const char *pkgname);
202  // Adds a new package entry for the specified class or package name and
203  // corresponding directory or jar file name.
204  static bool add_package(const char *pkgname, int classpath_index, TRAPS);
205
206  // Initialization
207  static void setup_meta_index();
208  static void setup_bootstrap_search_path();
209  static void load_zip_library();
210  static void create_class_path_entry(char *path, struct stat st, ClassPathEntry **new_entry, bool lazy);
211
212  // Canonicalizes path names, so strcmp will work properly. This is mainly
213  // to avoid confusing the zip library
214  static bool get_canonical_path(char* orig, char* out, int len);
215 public:
216  // Used by the kernel jvm.
217  static void update_class_path_entry_list(const char *path,
218                                           bool check_for_duplicates);
219  static void print_bootclasspath();
220
221  // Timing
222  static PerfCounter* perf_accumulated_time()         { return _perf_accumulated_time; }
223  static PerfCounter* perf_classes_inited()           { return _perf_classes_inited; }
224  static PerfCounter* perf_class_init_time()          { return _perf_class_init_time; }
225  static PerfCounter* perf_class_init_selftime()      { return _perf_class_init_selftime; }
226  static PerfCounter* perf_classes_verified()         { return _perf_classes_verified; }
227  static PerfCounter* perf_class_verify_time()        { return _perf_class_verify_time; }
228  static PerfCounter* perf_class_verify_selftime()    { return _perf_class_verify_selftime; }
229  static PerfCounter* perf_classes_linked()           { return _perf_classes_linked; }
230  static PerfCounter* perf_class_link_time()          { return _perf_class_link_time; }
231  static PerfCounter* perf_class_link_selftime()      { return _perf_class_link_selftime; }
232  static PerfCounter* perf_class_parse_time()         { return _perf_class_parse_time; }
233  static PerfCounter* perf_class_parse_selftime()     { return _perf_class_parse_selftime; }
234  static PerfCounter* perf_sys_class_lookup_time()    { return _perf_sys_class_lookup_time; }
235  static PerfCounter* perf_shared_classload_time()    { return _perf_shared_classload_time; }
236  static PerfCounter* perf_sys_classload_time()       { return _perf_sys_classload_time; }
237  static PerfCounter* perf_app_classload_time()       { return _perf_app_classload_time; }
238  static PerfCounter* perf_app_classload_selftime()   { return _perf_app_classload_selftime; }
239  static PerfCounter* perf_app_classload_count()      { return _perf_app_classload_count; }
240  static PerfCounter* perf_define_appclasses()        { return _perf_define_appclasses; }
241  static PerfCounter* perf_define_appclass_time()     { return _perf_define_appclass_time; }
242  static PerfCounter* perf_define_appclass_selftime() { return _perf_define_appclass_selftime; }
243  static PerfCounter* perf_app_classfile_bytes_read() { return _perf_app_classfile_bytes_read; }
244  static PerfCounter* perf_sys_classfile_bytes_read() { return _perf_sys_classfile_bytes_read; }
245
246  // Record how often system loader lock object is contended
247  static PerfCounter* sync_systemLoaderLockContentionRate() {
248    return _sync_systemLoaderLockContentionRate;
249  }
250
251  // Record how often non system loader lock object is contended
252  static PerfCounter* sync_nonSystemLoaderLockContentionRate() {
253    return _sync_nonSystemLoaderLockContentionRate;
254  }
255
256  // Record how many calls to JVM_FindLoadedClass w/o holding a lock
257  static PerfCounter* sync_JVMFindLoadedClassLockFreeCounter() {
258    return _sync_JVMFindLoadedClassLockFreeCounter;
259  }
260
261  // Record how many calls to JVM_DefineClass w/o holding a lock
262  static PerfCounter* sync_JVMDefineClassLockFreeCounter() {
263    return _sync_JVMDefineClassLockFreeCounter;
264  }
265
266  // Record how many calls to jni_DefineClass w/o holding a lock
267  static PerfCounter* sync_JNIDefineClassLockFreeCounter() {
268    return _sync_JNIDefineClassLockFreeCounter;
269  }
270
271  // Record how many calls to Unsafe_DefineClass
272  static PerfCounter* unsafe_defineClassCallCounter() {
273    return _unsafe_defineClassCallCounter;
274  }
275
276  // Record how many times SystemDictionary::load_instance_class call
277  // fails with linkageError when Unsyncloadclass flag is set.
278  static PerfCounter* load_instance_class_failCounter() {
279    return _load_instance_class_failCounter;
280  }
281
282  // Load individual .class file
283  static instanceKlassHandle load_classfile(Symbol* h_name, TRAPS);
284
285  // If the specified package has been loaded by the system, then returns
286  // the name of the directory or ZIP file that the package was loaded from.
287  // Returns null if the package was not loaded.
288  // Note: The specified name can either be the name of a class or package.
289  // If a package name is specified, then it must be "/"-separator and also
290  // end with a trailing "/".
291  static oop get_system_package(const char* name, TRAPS);
292
293  // Returns an array of Java strings representing all of the currently
294  // loaded system packages.
295  // Note: The package names returned are "/"-separated and end with a
296  // trailing "/".
297  static objArrayOop get_system_packages(TRAPS);
298
299  // Initialization
300  static void initialize();
301  static void create_package_info_table();
302  static void create_package_info_table(HashtableBucket *t, int length,
303                                        int number_of_entries);
304  static int compute_Object_vtable();
305
306  static ClassPathEntry* classpath_entry(int n) {
307    ClassPathEntry* e = ClassLoader::_first_entry;
308    while (--n >= 0) {
309      assert(e != NULL, "Not that many classpath entries.");
310      e = e->next();
311    }
312    return e;
313  }
314
315  // Sharing dump and restore
316  static void copy_package_info_buckets(char** top, char* end);
317  static void copy_package_info_table(char** top, char* end);
318
319  // VM monitoring and management support
320  static jlong classloader_time_ms();
321  static jlong class_method_total_size();
322  static jlong class_init_count();
323  static jlong class_init_time_ms();
324  static jlong class_verify_time_ms();
325  static jlong class_link_count();
326  static jlong class_link_time_ms();
327
328  // indicates if class path already contains a entry (exact match by name)
329  static bool contains_entry(ClassPathEntry* entry);
330
331  // adds a class path list
332  static void add_to_list(ClassPathEntry* new_entry);
333
334  // creates a class path zip entry (returns NULL if JAR file cannot be opened)
335  static ClassPathZipEntry* create_class_path_zip_entry(const char *apath);
336
337  // Debugging
338  static void verify()              PRODUCT_RETURN;
339
340  // Force compilation of all methods in all classes in bootstrap class path (stress test)
341#ifndef PRODUCT
342 private:
343  static int _compile_the_world_counter;
344 public:
345  static void compile_the_world();
346  static void compile_the_world_in(char* name, Handle loader, TRAPS);
347  static int  compile_the_world_counter() { return _compile_the_world_counter; }
348#endif //PRODUCT
349};
350
351// PerfClassTraceTime is used to measure time for class loading related events.
352// This class tracks cumulative time and exclusive time for specific event types.
353// During the execution of one event, other event types (e.g. class loading and
354// resolution) as well as recursive calls of the same event type could happen.
355// Only one elapsed timer (cumulative) and one thread-local self timer (exclusive)
356// (i.e. only one event type) are active at a time even multiple PerfClassTraceTime
357// instances have been created as multiple events are happening.
358class PerfClassTraceTime {
359 public:
360  enum {
361    CLASS_LOAD   = 0,
362    PARSE_CLASS  = 1,
363    CLASS_LINK   = 2,
364    CLASS_VERIFY = 3,
365    CLASS_CLINIT = 4,
366    DEFINE_CLASS = 5,
367    EVENT_TYPE_COUNT = 6
368  };
369 protected:
370  // _t tracks time from initialization to destruction of this timer instance
371  // including time for all other event types, and recursive calls of this type.
372  // When a timer is called recursively, the elapsedTimer _t would not be used.
373  elapsedTimer     _t;
374  PerfLongCounter* _timep;
375  PerfLongCounter* _selftimep;
376  PerfLongCounter* _eventp;
377  // pointer to thread-local recursion counter and timer array
378  // The thread_local timers track cumulative time for specific event types
379  // exclusive of time for other event types, but including recursive calls
380  // of the same type.
381  int*             _recursion_counters;
382  elapsedTimer*    _timers;
383  int              _event_type;
384  int              _prev_active_event;
385
386 public:
387
388  inline PerfClassTraceTime(PerfLongCounter* timep,     /* counter incremented with inclusive time */
389                            PerfLongCounter* selftimep, /* counter incremented with exclusive time */
390                            PerfLongCounter* eventp,    /* event counter */
391                            int* recursion_counters,    /* thread-local recursion counter array */
392                            elapsedTimer* timers,       /* thread-local timer array */
393                            int type                    /* event type */ ) :
394      _timep(timep), _selftimep(selftimep), _eventp(eventp), _recursion_counters(recursion_counters), _timers(timers), _event_type(type) {
395    initialize();
396  }
397
398  inline PerfClassTraceTime(PerfLongCounter* timep,     /* counter incremented with inclusive time */
399                            elapsedTimer* timers,       /* thread-local timer array */
400                            int type                    /* event type */ ) :
401      _timep(timep), _selftimep(NULL), _eventp(NULL), _recursion_counters(NULL), _timers(timers), _event_type(type) {
402    initialize();
403  }
404
405  inline void suspend() { _t.stop(); _timers[_event_type].stop(); }
406  inline void resume()  { _t.start(); _timers[_event_type].start(); }
407
408  ~PerfClassTraceTime();
409  void initialize();
410};
411
412#endif // SHARE_VM_CLASSFILE_CLASSLOADER_HPP
413